Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9463c37 authored by Gabriele M's avatar Gabriele M
Browse files

Requre a minimum remaining battery capacity to install updates

The recovery doesn't install the update if the remaining battery
capacity isn't at least 20% (or 15% if charging). Require at least
40%, just to be safe.

Change-Id: I5cd7c40f029141cde2b0922b25fece2b55989710
parent 0332be5e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="battery_ok_percentage">40</integer>
</resources>
+2 −0
Original line number Diff line number Diff line
@@ -56,6 +56,8 @@
    <string name="finalizing_package">Finalizing package installation</string>
    <string name="preparing_ota_first_boot">Preparing for first boot</string>
    <string name="dialog_prepare_zip_message">Preliminary update preparation</string>
    <string name="dialog_battery_low_title">Low battery</string>
    <string name="dialog_battery_low_message">The battery level is too low, please charge your device to continue.</string>

    <string name="reboot">Reboot</string>

+18 −0
Original line number Diff line number Diff line
@@ -17,11 +17,14 @@ package org.lineageos.updater.controller;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.support.v7.app.AlertDialog;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;

import org.lineageos.updater.R;
import org.lineageos.updater.misc.Constants;
import org.lineageos.updater.misc.FileUtils;
import org.lineageos.updater.misc.Utils;
@@ -65,12 +68,27 @@ class UpdateInstaller {
        return sInstallingUpdate != null && sInstallingUpdate.equals(downloadId);
    }

    private static boolean isBatteryLevelOk(Context context) {
        BatteryManager bm = context.getSystemService(BatteryManager.class);
        int percent = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        return percent >= context.getResources().getInteger(R.integer.battery_ok_percentage);
    }

    void install(String downloadId) {
        if (isInstalling()) {
            Log.e(TAG, "Already installing an update");
            return;
        }

        if (!isBatteryLevelOk(mContext)) {
            new AlertDialog.Builder(mContext)
                    .setTitle(R.string.dialog_battery_low_title)
                    .setMessage(R.string.dialog_battery_low_message)
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
            return;
        }

        UpdateInfo update = mUpdaterController.getUpdate(downloadId);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        long buildTimestamp = SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0);