From c6ab4fcfb91aefc680ff5761f4399694497e72cb Mon Sep 17 00:00:00 2001 From: althafvly Date: Tue, 7 Feb 2023 17:12:23 +0530 Subject: [PATCH 1/4] Updater: Utils: Move battery level check Change-Id: I94447b33e717bd05f8a93d8a7784c862a07f8c3a --- .../lineageos/updater/UpdatesListAdapter.java | 23 +------------------ src/org/lineageos/updater/misc/Utils.java | 21 +++++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/org/lineageos/updater/UpdatesListAdapter.java b/src/org/lineageos/updater/UpdatesListAdapter.java index 7ee4b3dc..b3d4e70c 100644 --- a/src/org/lineageos/updater/UpdatesListAdapter.java +++ b/src/org/lineageos/updater/UpdatesListAdapter.java @@ -18,11 +18,9 @@ package org.lineageos.updater; import android.app.Activity; import android.content.Intent; -import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.Resources; import android.net.Uri; -import android.os.BatteryManager; import android.os.Build; import android.os.PowerManager; import android.text.SpannableString; @@ -74,10 +72,6 @@ public class UpdatesListAdapter extends RecyclerView.Adapter mDownloadIds; @@ -507,7 +501,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter= required; - } } diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java index 497b635f..4e7b20f2 100644 --- a/src/org/lineageos/updater/misc/Utils.java +++ b/src/org/lineageos/updater/misc/Utils.java @@ -22,6 +22,7 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; @@ -29,6 +30,7 @@ import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkCapabilities; import android.net.Uri; +import android.os.BatteryManager; import android.os.Build; import android.os.Environment; import android.os.StatFs; @@ -66,6 +68,10 @@ import java.util.zip.ZipFile; public class Utils { + private static final int BATTERY_PLUGGED_ANY = BatteryManager.BATTERY_PLUGGED_AC + | BatteryManager.BATTERY_PLUGGED_USB + | BatteryManager.BATTERY_PLUGGED_WIRELESS; + private static final String TAG = "Utils"; private static final String CONTENT_URI_PATH = "content://custom.setting.Provider.OTA_SERVER/cte"; @@ -507,4 +513,19 @@ public class Utils { public static boolean isRecoveryUpdateExecPresent() { return new File(Constants.UPDATE_RECOVERY_EXEC).exists(); } + + public static boolean isBatteryLevelOk(Context context) { + Intent intent = context.registerReceiver(null, + new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + 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 plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); + int required = (plugged & BATTERY_PLUGGED_ANY) != 0 ? + context.getResources().getInteger(R.integer.battery_ok_percentage_charging) : + context.getResources().getInteger(R.integer.battery_ok_percentage_discharging); + return percent >= required; + } } -- GitLab From 5bf67d4fcc867600f0b3044a64d79d830f298de0 Mon Sep 17 00:00:00 2001 From: althafvly Date: Tue, 7 Feb 2023 17:13:17 +0530 Subject: [PATCH 2/4] Updater: Update A/B devices on one go Change-Id: Iac2a6888b8da6b27c0c9887f32266e8e5b73d9af --- res/values/strings.xml | 2 + .../lineageos/updater/UpdatesListAdapter.java | 9 ++-- .../updater/controller/UpdaterService.java | 43 ++++++++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index fc92d524..531dea85 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -53,6 +53,7 @@ Pause Resume Suspend + Reboot and install Installing update package Install error @@ -108,6 +109,7 @@ You are running the latest /e/OS version. To manually check for updates, use the Refresh button. Download + Apply update Pause Resume Install diff --git a/src/org/lineageos/updater/UpdatesListAdapter.java b/src/org/lineageos/updater/UpdatesListAdapter.java index b3d4e70c..ab025ffb 100644 --- a/src/org/lineageos/updater/UpdatesListAdapter.java +++ b/src/org/lineageos/updater/UpdatesListAdapter.java @@ -250,7 +250,8 @@ public class UpdatesListAdapter extends RecyclerView.Adapter { if (checkbox.isChecked()) { preferences.edit() @@ -377,7 +379,8 @@ public class UpdatesListAdapter extends RecyclerView.Adapter { AlertDialog.Builder freeSpaceDialog = getSpaceDialog( diff --git a/src/org/lineageos/updater/controller/UpdaterService.java b/src/org/lineageos/updater/controller/UpdaterService.java index f5ee697b..e23fd407 100644 --- a/src/org/lineageos/updater/controller/UpdaterService.java +++ b/src/org/lineageos/updater/controller/UpdaterService.java @@ -346,12 +346,45 @@ public class UpdaterService extends Service { mNotificationBuilder.setSmallIcon(R.drawable.ic_system_update); mNotificationBuilder.setProgress(0, 0, false); String text = getString(R.string.download_completed_notification); + boolean hasRequiredSpace = Utils.availableFreeSpace() > (update.getFileSize() * 2); + + if (!Utils.canInstall(update) || !Utils.isBatteryLevelOk(this) + || !hasRequiredSpace) { + /* Show notification if any of the below condition didn't met. */ + text = getString(R.string.blocked_update_dialog_title) + ". "; + if (!Utils.isBatteryLevelOk(this)) { + text = text + getString(R.string.dialog_battery_low_title); + } else if (!hasRequiredSpace) { + text = text + getString(R.string.dialog_free_space_low_title); + } else if (!Utils.canInstall(update)) { + text = text + getString(R.string.verification_failed_notification); + } + } else if (!Utils.isABDevice()) { + /* Add action to reboot and install for Non-A/B devices. */ + mNotificationBuilder.mActions.clear(); + mNotificationBuilder.addAction(R.drawable.ic_system_update, + getString(R.string.reboot_install), + getInstallationPendingIntent(update.getDownloadId())); + } + mNotificationBuilder.setContentText(text); mNotificationBuilder.setTicker(text); mNotificationBuilder.setOngoing(false); mNotificationBuilder.setAutoCancel(true); mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build()); - tryStopSelf(); + + /* Make sure these conditions are met before auto install + - Can install package (Is a newer build or downgrade allowed) + - Battery level (Above 30% if discharging or 20% if charging) + - Free space to install (Double the size of the ota zip) + */ + if (Utils.isABDevice() && Utils.canInstall(update) + && Utils.isBatteryLevelOk(this) && hasRequiredSpace) { + Utils.triggerUpdate(this, update.getDownloadId()); + } else { + tryStopSelf(); + } + break; } case VERIFICATION_FAILED: { @@ -528,6 +561,14 @@ public class UpdaterService extends Service { PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); } + private PendingIntent getInstallationPendingIntent(String downloadId) { + final Intent intent = new Intent(this, UpdaterService.class); + intent.setAction(ACTION_INSTALL_UPDATE); + intent.putExtra(UpdaterService.EXTRA_DOWNLOAD_ID, downloadId); + return PendingIntent.getService(this, 0, intent, + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); + } + private PendingIntent getResumeInstallationPendingIntent() { final Intent intent = new Intent(this, UpdaterService.class); intent.setAction(ACTION_INSTALL_RESUME); -- GitLab From 37c7792fa898d83a32904b082d4a44fd1e94cc3a Mon Sep 17 00:00:00 2001 From: althafvly Date: Wed, 8 Feb 2023 11:57:45 +0530 Subject: [PATCH 3/4] Updater: check for active notifications before self stop Change-Id: I869ab48a0d518bac3522aab2b8778321612855d1 --- .../updater/controller/UpdaterService.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/org/lineageos/updater/controller/UpdaterService.java b/src/org/lineageos/updater/controller/UpdaterService.java index e23fd407..ba4baafd 100644 --- a/src/org/lineageos/updater/controller/UpdaterService.java +++ b/src/org/lineageos/updater/controller/UpdaterService.java @@ -28,6 +28,7 @@ import android.content.SharedPreferences; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; +import android.service.notification.StatusBarNotification; import android.text.format.Formatter; import android.util.Log; @@ -245,12 +246,27 @@ public class UpdaterService extends Service { private void tryStopSelf() { if (!mHasClients && !mUpdaterController.hasActiveDownloads() && - !mUpdaterController.isInstallingUpdate()) { + !mUpdaterController.isInstallingUpdate() && !areNotificationsActive()) { Log.d(TAG, "Service no longer needed, stopping"); stopSelf(); } } + private boolean areNotificationsActive() { + NotificationManager notificationManager = + (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + StatusBarNotification[] notifications = notificationManager.getActiveNotifications(); + if (notifications != null && notifications.length > 0) { + for (StatusBarNotification notification : notifications) { + if (notification.getId() == NOTIFICATION_ID && + notification.getPackageName().equals(getPackageName())) { + return true; + } + } + } + return false; + } + private void handleUpdateStatusChange(UpdateInfo update) { switch (update.getStatus()) { case DELETED: { -- GitLab From 79532609cef7fb69134235327e77d6ca622f3173 Mon Sep 17 00:00:00 2001 From: althafvly Date: Thu, 9 Feb 2023 12:55:25 +0530 Subject: [PATCH 4/4] Updater: change download completed string Change-Id: Icc5409297ff01b58de4eb21dcf6012262324c099 --- res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 531dea85..d6b241cc 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -41,7 +41,7 @@ Downloading Download paused Download error - Download completed + Update is ready to be installed Starting download Update failed Installation suspended -- GitLab