Updater: Improve battery check

* Lower default value by 10%
* Decrease the level by another 10% if charging
* Always allow installation in case device doesn't have a battery
* Show the requirements in the alert dialog

Change-Id: Iebb10220612006fbd096eb474cf3034ef52144b3
This commit is contained in:
Luca Stefani
2018-07-10 21:32:07 +02:00
committed by Gabriele M
parent 74d7a3e78b
commit 4ec02fd780
3 changed files with 21 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<integer name="battery_ok_percentage">40</integer> <integer name="battery_ok_percentage_charging">20</integer>
<integer name="battery_ok_percentage_discharging">30</integer>
</resources> </resources>

View File

@@ -57,7 +57,7 @@
<string name="preparing_ota_first_boot">Preparing for first boot</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_prepare_zip_message">Preliminary update preparation</string>
<string name="dialog_battery_low_title">Low battery</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="dialog_battery_low_message_pct">The battery level is too low, you need at least <xliff:g id="percent_discharging">%1$d</xliff:g>%% of the battery to continue, <xliff:g id="percent_charging">%2$d</xliff:g>%% if charging.</string>
<string name="reboot">Reboot</string> <string name="reboot">Reboot</string>

View File

@@ -16,7 +16,9 @@
package org.lineageos.updater; package org.lineageos.updater;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
@@ -424,9 +426,13 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
private AlertDialog.Builder getInstallDialog(final String downloadId) { private AlertDialog.Builder getInstallDialog(final String downloadId) {
if (!isBatteryLevelOk()) { if (!isBatteryLevelOk()) {
Resources resources = mActivity.getResources();
String message = resources.getString(R.string.dialog_battery_low_message_pct,
resources.getInteger(R.integer.battery_ok_percentage_discharging),
resources.getInteger(R.integer.battery_ok_percentage_charging));
return new AlertDialog.Builder(mActivity) return new AlertDialog.Builder(mActivity)
.setTitle(R.string.dialog_battery_low_title) .setTitle(R.string.dialog_battery_low_title)
.setMessage(R.string.dialog_battery_low_message) .setMessage(message)
.setPositiveButton(android.R.string.ok, null); .setPositiveButton(android.R.string.ok, null);
} }
UpdateInfo update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
@@ -538,8 +544,16 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
} }
private boolean isBatteryLevelOk() { private boolean isBatteryLevelOk() {
BatteryManager bm = mActivity.getSystemService(BatteryManager.class); Intent intent = mActivity.registerReceiver(null,
int percent = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return percent >= mActivity.getResources().getInteger(R.integer.battery_ok_percentage); if (!intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)) {
return true;
}
int percent = Math.round(100.f * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100) /
intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
int required = intent.getBooleanExtra(BatteryManager.EXTRA_PLUGGED, false) ?
mActivity.getResources().getInteger(R.integer.battery_ok_percentage_charging) :
mActivity.getResources().getInteger(R.integer.battery_ok_percentage_discharging);
return percent >= required;
} }
} }