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
This commit is contained in:
Gabriele M
2018-04-28 15:34:03 +02:00
parent b9c9037c34
commit 219c6d38f8
3 changed files with 24 additions and 0 deletions

4
res/values/integers.xml Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="battery_ok_percentage">40</integer>
</resources>

View File

@@ -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>

View File

@@ -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);