From 0bb56c3189798a48d67fa5e4d515dcf64050eddd Mon Sep 17 00:00:00 2001 From: Muntashir Al-Islam Date: Thu, 14 Sep 2023 16:10:46 +0600 Subject: [PATCH] [Refactor] Check if WakeLock is held before releasing it Signed-off-by: Muntashir Al-Islam --- .../installer/PackageInstallerService.java | 4 +--- .../dialog/BackupRestoreDialogViewModel.java | 2 +- .../AppManager/batchops/BatchOpsService.java | 4 +--- .../AppManager/logcat/LogViewerActivity.java | 4 +--- .../logcat/LogcatRecordingService.java | 4 +--- .../BackupTasksDialogFragment.java | 21 ++++++------------- .../oneclickops/OneClickOpsActivity.java | 16 +++++++------- .../RestoreTasksDialogFragment.java | 13 ++++-------- .../profiles/ProfileApplierService.java | 4 +--- .../RulesTypeSelectionDialogFragment.java | 4 ++-- .../AppManager/runner/TermActivity.java | 4 +--- .../AppManager/scanner/vt/VirusTotal.java | 2 +- .../settings/MainPreferencesViewModel.java | 2 +- .../AppManager/utils/CpuUtils.java | 8 +++++++ .../AppManager/utils/PackageUtils.java | 4 ++-- .../audio/AudioPlayerDialogFragment.java | 4 +--- 16 files changed, 39 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/apk/installer/PackageInstallerService.java b/app/src/main/java/io/github/muntashirakon/AppManager/apk/installer/PackageInstallerService.java index 8c62d2242b0..8e1927f930d 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/apk/installer/PackageInstallerService.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/apk/installer/PackageInstallerService.java @@ -232,9 +232,7 @@ public void onDestroy() { if (mProgressHandler != null) { mProgressHandler.onDetach(this); } - if (mWakeLock != null) { - mWakeLock.release(); - } + CpuUtils.releaseWakeLock(mWakeLock); super.onDestroy(); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/backup/dialog/BackupRestoreDialogViewModel.java b/app/src/main/java/io/github/muntashirakon/AppManager/backup/dialog/BackupRestoreDialogViewModel.java index e8b9c55a395..33c2116d540 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/backup/dialog/BackupRestoreDialogViewModel.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/backup/dialog/BackupRestoreDialogViewModel.java @@ -133,7 +133,7 @@ public void processPackages(@Nullable List userPackagePairs) { try { processPackagesInternal(userPackagePairs); } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } }); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/batchops/BatchOpsService.java b/app/src/main/java/io/github/muntashirakon/AppManager/batchops/BatchOpsService.java index b92f8d1b18c..e12406fc375 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/batchops/BatchOpsService.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/batchops/BatchOpsService.java @@ -202,9 +202,7 @@ public void onDestroy() { if (mProgressHandler != null) { mProgressHandler.onDetach(this); } - if (mWakeLock != null) { - mWakeLock.release(); - } + CpuUtils.releaseWakeLock(mWakeLock); super.onDestroy(); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogViewerActivity.java b/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogViewerActivity.java index 0f65a3b2a98..8137d93c3cf 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogViewerActivity.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogViewerActivity.java @@ -323,9 +323,7 @@ protected void onNewIntent(Intent intent) { @Override public void onDestroy() { - if (mWakeLock != null) { - mWakeLock.release(); - } + CpuUtils.releaseWakeLock(mWakeLock); super.onDestroy(); mExecutor.shutdownNow(); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogcatRecordingService.java b/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogcatRecordingService.java index 9125efcaa7f..15fece70e4a 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogcatRecordingService.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/logcat/LogcatRecordingService.java @@ -108,9 +108,7 @@ private void initializeReader(@NonNull LogcatReaderLoader loader) { @Override public void onDestroy() { - if (mWakeLock != null) { - mWakeLock.release(); - } + CpuUtils.releaseWakeLock(mWakeLock); super.onDestroy(); killProcess(); unregisterReceiver(mReceiver); diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/BackupTasksDialogFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/BackupTasksDialogFragment.java index 78e2e7b605b..46fc5a8183f 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/BackupTasksDialogFragment.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/BackupTasksDialogFragment.java @@ -34,6 +34,7 @@ import io.github.muntashirakon.AppManager.types.UserPackagePair; import io.github.muntashirakon.AppManager.usage.AppUsageStatsManager; import io.github.muntashirakon.AppManager.usage.UsageUtils; +import io.github.muntashirakon.AppManager.utils.CpuUtils; import io.github.muntashirakon.AppManager.utils.DigestUtils; import io.github.muntashirakon.AppManager.utils.LangUtils; import io.github.muntashirakon.AppManager.utils.PackageUtils; @@ -81,9 +82,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); @@ -112,9 +111,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); @@ -143,9 +140,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); @@ -186,9 +181,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); @@ -250,9 +243,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/OneClickOpsActivity.java b/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/OneClickOpsActivity.java index 3d8cfd1feda..2a3f5a90596 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/OneClickOpsActivity.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/OneClickOpsActivity.java @@ -94,12 +94,12 @@ protected void onAuthenticated(Bundle savedInstanceState) { setAppOps(listPairPair.first, listPairPair.second.first, listPairPair.second.second)); mViewModel.getClearDataCandidates().observe(this, this::clearData); mViewModel.watchTrimCachesResult().observe(this, isSuccessful -> { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); progressIndicator.hide(); UIUtils.displayShortToast(isSuccessful ? R.string.done : R.string.failed); }); mViewModel.getAppsInstalledByAmForDexOpt().observe(this, packages -> { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); progressIndicator.hide(); DexOptDialog dialog = DexOptDialog.getInstance(packages); dialog.show(getSupportFragmentManager(), DexOptDialog.TAG); @@ -226,7 +226,7 @@ protected void onPause() { } private void blockTrackers(@Nullable List trackerCounts) { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); progressIndicator.hide(); if (trackerCounts == null) { UIUtils.displayShortToast(R.string.failed_to_fetch_package_info); @@ -268,7 +268,7 @@ private void blockTrackers(@Nullable List trackerCounts) { } private void blockComponents(@Nullable List componentCounts, @NonNull String[] signatures) { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); progressIndicator.hide(); if (componentCounts == null) { UIUtils.displayShortToast(R.string.failed_to_fetch_package_info); @@ -361,7 +361,7 @@ private void showAppOpsSelectionDialog() { } private void setAppOps(@Nullable List appOpCounts, @NonNull int[] appOpList, int mode) { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); progressIndicator.hide(); if (appOpCounts == null) { UIUtils.displayShortToast(R.string.failed_to_fetch_package_info); @@ -401,7 +401,7 @@ private void setAppOps(@Nullable List appOpCounts, @NonNull int[] ap } private void clearData(@NonNull List candidatePackages) { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); String[] packages = candidatePackages.toArray(new String[0]); new SearchableMultiChoiceDialogBuilder<>(this, packages, packages) .setTitle(R.string.filtered_packages) @@ -433,9 +433,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { @Override protected void onDestroy() { - if (wakeLock != null && wakeLock.isHeld()) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); super.onDestroy(); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/RestoreTasksDialogFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/RestoreTasksDialogFragment.java index 2957fcc4309..1d3144f3ee0 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/RestoreTasksDialogFragment.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/oneclickops/RestoreTasksDialogFragment.java @@ -22,6 +22,7 @@ import io.github.muntashirakon.AppManager.R; import io.github.muntashirakon.AppManager.backup.dialog.BackupRestoreDialogFragment; import io.github.muntashirakon.AppManager.main.ApplicationItem; +import io.github.muntashirakon.AppManager.utils.CpuUtils; import io.github.muntashirakon.AppManager.utils.PackageUtils; import io.github.muntashirakon.AppManager.utils.ThreadUtils; import io.github.muntashirakon.dialog.SearchableMultiChoiceDialogBuilder; @@ -62,9 +63,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); @@ -93,9 +92,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); @@ -125,9 +122,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { if (ThreadUtils.isInterrupted()) return; ThreadUtils.postOnMainThread(() -> runMultiChoiceDialog(applicationItems, applicationLabels)); } finally { - if (wakeLock != null) { - wakeLock.release(); - } + CpuUtils.releaseWakeLock(wakeLock); } }); }); diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileApplierService.java b/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileApplierService.java index 2d571893478..ca34bc1a2be 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileApplierService.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileApplierService.java @@ -113,9 +113,7 @@ public void onDestroy() { if (mProgressHandler != null) { mProgressHandler.onDetach(this); } - if (mWakeLock != null) { - mWakeLock.release(); - } + CpuUtils.releaseWakeLock(mWakeLock); super.onDestroy(); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/rules/RulesTypeSelectionDialogFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/rules/RulesTypeSelectionDialogFragment.java index f9a6863265d..962049eebd9 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/rules/RulesTypeSelectionDialogFragment.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/rules/RulesTypeSelectionDialogFragment.java @@ -115,7 +115,7 @@ private void handleExport() { } catch (IOException e) { ThreadUtils.postOnMainThread(() -> UIUtils.displayLongToast(R.string.export_failed)); } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } hideProgressBar(activityRef); }); @@ -137,7 +137,7 @@ private void handleImport() { } catch (IOException e) { ThreadUtils.postOnMainThread(() -> UIUtils.displayLongToast(R.string.import_failed)); } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } hideProgressBar(activityRef); }); diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/runner/TermActivity.java b/app/src/main/java/io/github/muntashirakon/AppManager/runner/TermActivity.java index 0b825fb6334..d2a3b9b157e 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/runner/TermActivity.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/runner/TermActivity.java @@ -65,9 +65,7 @@ protected void onAuthenticated(@Nullable Bundle savedInstanceState) { @Override protected void onDestroy() { - if (mWakeLock != null) { - mWakeLock.release(); - } + CpuUtils.releaseWakeLock(mWakeLock); mExecutor.shutdownNow(); super.onDestroy(); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/scanner/vt/VirusTotal.java b/app/src/main/java/io/github/muntashirakon/AppManager/scanner/vt/VirusTotal.java index 848fcd2cae8..06c33883e15 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/scanner/vt/VirusTotal.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/scanner/vt/VirusTotal.java @@ -108,7 +108,7 @@ public void fetchReportsOrScan(@NonNull Path file, response.onScanCompleted(scanMeta); responseCode = VirusTotal.RESPONSE_QUEUED; } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } } else { // Item is queued diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/settings/MainPreferencesViewModel.java b/app/src/main/java/io/github/muntashirakon/AppManager/settings/MainPreferencesViewModel.java index d0dd8b5e87e..00428376186 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/settings/MainPreferencesViewModel.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/settings/MainPreferencesViewModel.java @@ -112,7 +112,7 @@ public void reloadApps() { appDb.deleteAllBackups(); appDb.loadInstalledOrBackedUpApplications(getApplication()); } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } }); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/utils/CpuUtils.java b/app/src/main/java/io/github/muntashirakon/AppManager/utils/CpuUtils.java index 2338de55ae7..b3d45b7cef9 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/utils/CpuUtils.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/utils/CpuUtils.java @@ -6,6 +6,8 @@ import android.os.PowerManager; import androidx.annotation.Keep; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; public class CpuUtils { static { @@ -19,4 +21,10 @@ public static PowerManager.WakeLock getPartialWakeLock(String tagPostfix) { PowerManager pm = (PowerManager) ContextUtils.getContext().getSystemService(Context.POWER_SERVICE); return pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AppManager::" + tagPostfix); } + + public static void releaseWakeLock(@Nullable PowerManager.WakeLock wakeLock) { + if (wakeLock != null && wakeLock.isHeld()) { + wakeLock.release(); + } + } } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/utils/PackageUtils.java b/app/src/main/java/io/github/muntashirakon/AppManager/utils/PackageUtils.java index 41eac530e55..a270a38adbe 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/utils/PackageUtils.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/utils/PackageUtils.java @@ -151,7 +151,7 @@ public static List getInstalledOrBackedUpApplicationsFromDb(@No appDb.loadInstalledOrBackedUpApplications(context); apps = appDb.getAllApplications(); } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } } Map backups = appDb.getBackups(false); @@ -253,7 +253,7 @@ public static List getInstalledOrBackedUpApplicationsFromDb(@No appDb.loadInstalledOrBackedUpApplications(context); } else appDb.updateApplications(context); } finally { - wakeLock.release(); + CpuUtils.releaseWakeLock(wakeLock); } }); } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/viewer/audio/AudioPlayerDialogFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/viewer/audio/AudioPlayerDialogFragment.java index ef8ea5c7b2d..5d18df09f88 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/viewer/audio/AudioPlayerDialogFragment.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/viewer/audio/AudioPlayerDialogFragment.java @@ -131,10 +131,8 @@ public void onDismiss(@NonNull DialogInterface dialog) { @Override public void onDestroy() { + CpuUtils.releaseWakeLock(mWakeLock); super.onDestroy(); - if (mWakeLock != null) { - mWakeLock.release(); - } if (mCloseActivity) { requireActivity().finish(); }