Skip to content

Commit

Permalink
[Refactor] Check if WakeLock is held before releasing it
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Sep 14, 2023
1 parent dddb6bd commit 0bb56c3
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ public void onDestroy() {
if (mProgressHandler != null) {
mProgressHandler.onDetach(this);
}
if (mWakeLock != null) {
mWakeLock.release();
}
CpuUtils.releaseWakeLock(mWakeLock);
super.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void processPackages(@Nullable List<UserPackagePair> userPackagePairs) {
try {
processPackagesInternal(userPackagePairs);
} finally {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ public void onDestroy() {
if (mProgressHandler != null) {
mProgressHandler.onDetach(this);
}
if (mWakeLock != null) {
mWakeLock.release();
}
CpuUtils.releaseWakeLock(mWakeLock);
super.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -226,7 +226,7 @@ protected void onPause() {
}

private void blockTrackers(@Nullable List<ItemCount> trackerCounts) {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
progressIndicator.hide();
if (trackerCounts == null) {
UIUtils.displayShortToast(R.string.failed_to_fetch_package_info);
Expand Down Expand Up @@ -268,7 +268,7 @@ private void blockTrackers(@Nullable List<ItemCount> trackerCounts) {
}

private void blockComponents(@Nullable List<ItemCount> componentCounts, @NonNull String[] signatures) {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
progressIndicator.hide();
if (componentCounts == null) {
UIUtils.displayShortToast(R.string.failed_to_fetch_package_info);
Expand Down Expand Up @@ -361,7 +361,7 @@ private void showAppOpsSelectionDialog() {
}

private void setAppOps(@Nullable List<AppOpCount> 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);
Expand Down Expand Up @@ -401,7 +401,7 @@ private void setAppOps(@Nullable List<AppOpCount> appOpCounts, @NonNull int[] ap
}

private void clearData(@NonNull List<String> candidatePackages) {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
String[] packages = candidatePackages.toArray(new String[0]);
new SearchableMultiChoiceDialogBuilder<>(this, packages, packages)
.setTitle(R.string.filtered_packages)
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ public void onDestroy() {
if (mProgressHandler != null) {
mProgressHandler.onDetach(this);
}
if (mWakeLock != null) {
mWakeLock.release();
}
CpuUtils.releaseWakeLock(mWakeLock);
super.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void reloadApps() {
appDb.deleteAllBackups();
appDb.loadInstalledOrBackedUpApplications(getApplication());
} finally {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.os.PowerManager;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CpuUtils {
static {
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static List<ApplicationItem> getInstalledOrBackedUpApplicationsFromDb(@No
appDb.loadInstalledOrBackedUpApplications(context);
apps = appDb.getAllApplications();
} finally {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
}
}
Map<String, Backup> backups = appDb.getBackups(false);
Expand Down Expand Up @@ -253,7 +253,7 @@ public static List<ApplicationItem> getInstalledOrBackedUpApplicationsFromDb(@No
appDb.loadInstalledOrBackedUpApplications(context);
} else appDb.updateApplications(context);
} finally {
wakeLock.release();
CpuUtils.releaseWakeLock(wakeLock);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 0bb56c3

Please sign in to comment.