summaryrefslogtreecommitdiff
path: root/services/backup
diff options
context:
space:
mode:
author Sarp Misoglu <sarpm@google.com> 2025-01-08 19:15:19 +0000
committer Sarp Misoglu <sarpm@google.com> 2025-01-09 22:02:33 +0000
commit3682f501fb72835ec6a580377c2f610570573bd4 (patch)
treea6a83ac352f7ae2c5801e1fa3772bb8409af344e /services/backup
parent8a7102c166fe464256b4a1daa9684ca8b1212931 (diff)
Move BackupWakeLock to its own file
There's no reason for it to be under UserBackupManagerService and bloat it up. Test: n/a Flag: EXEMPT no-op refactor Change-Id: I595fe26e97e804e59033a5dc95e8fa8e16de895f
Diffstat (limited to 'services/backup')
-rw-r--r--services/backup/java/com/android/server/backup/BackupWakeLock.java95
-rw-r--r--services/backup/java/com/android/server/backup/UserBackupManagerService.java95
-rw-r--r--services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java2
-rw-r--r--services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java2
-rw-r--r--services/backup/java/com/android/server/backup/internal/BackupHandler.java8
-rw-r--r--services/backup/java/com/android/server/backup/internal/PerformClearTask.java2
-rw-r--r--services/backup/java/com/android/server/backup/keyvalue/KeyValueBackupTask.java7
-rw-r--r--services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java5
-rw-r--r--services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java2
9 files changed, 111 insertions, 107 deletions
diff --git a/services/backup/java/com/android/server/backup/BackupWakeLock.java b/services/backup/java/com/android/server/backup/BackupWakeLock.java
new file mode 100644
index 000000000000..d839e7a1583b
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/BackupWakeLock.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.backup;
+
+import static com.android.server.backup.BackupManagerService.TAG;
+
+import android.annotation.Nullable;
+import android.os.PowerManager;
+import android.os.WorkSource;
+import android.util.Slog;
+
+/**
+ * Wrapper over {@link PowerManager.WakeLock} to prevent double-free exceptions on release()
+ * after quit().
+ *
+ * <p>There should be a single instance of this class per {@link UserBackupManagerService}.
+ */
+public class BackupWakeLock {
+ private final PowerManager.WakeLock mPowerManagerWakeLock;
+ private boolean mHasQuit = false;
+ private final String mUserIdMessage;
+ private final BackupManagerConstants mBackupManagerConstants;
+
+ public BackupWakeLock(PowerManager.WakeLock powerManagerWakeLock, int userId,
+ BackupManagerConstants backupManagerConstants) {
+ mPowerManagerWakeLock = powerManagerWakeLock;
+ mUserIdMessage = "[UserID:" + userId + "] ";
+ mBackupManagerConstants = backupManagerConstants;
+ }
+
+ /** Acquires the {@link PowerManager.WakeLock} if hasn't been quit. */
+ public synchronized void acquire() {
+ if (mHasQuit) {
+ Slog.d(TAG, mUserIdMessage + "Ignore wakelock acquire after quit: "
+ + mPowerManagerWakeLock.getTag());
+ return;
+ }
+ // Set a timeout for the wakelock. Otherwise if we fail internally and never call
+ // release(), the device might stay awake and drain battery indefinitely.
+ mPowerManagerWakeLock.acquire(mBackupManagerConstants.getWakelockTimeoutMillis());
+ Slog.d(TAG, mUserIdMessage + "Acquired wakelock:" + mPowerManagerWakeLock.getTag());
+ }
+
+ /** Releases the {@link PowerManager.WakeLock} if hasn't been quit. */
+ public synchronized void release() {
+ if (mHasQuit) {
+ Slog.d(TAG, mUserIdMessage + "Ignore wakelock release after quit: "
+ + mPowerManagerWakeLock.getTag());
+ return;
+ }
+
+ if (!mPowerManagerWakeLock.isHeld()) {
+ Slog.w(TAG, mUserIdMessage + "Wakelock not held: " + mPowerManagerWakeLock.getTag());
+ return;
+ }
+
+ mPowerManagerWakeLock.release();
+ Slog.d(TAG, mUserIdMessage + "Released wakelock:" + mPowerManagerWakeLock.getTag());
+ }
+
+ /**
+ * Returns true if the {@link PowerManager.WakeLock} has been acquired but not yet released.
+ */
+ public synchronized boolean isHeld() {
+ return mPowerManagerWakeLock.isHeld();
+ }
+
+ /** Release the {@link PowerManager.WakeLock} till it isn't held. */
+ public synchronized void quit() {
+ while (mPowerManagerWakeLock.isHeld()) {
+ Slog.d(TAG, mUserIdMessage + "Releasing wakelock: " + mPowerManagerWakeLock.getTag());
+ mPowerManagerWakeLock.release();
+ }
+ mHasQuit = true;
+ }
+
+ /** Calls {@link PowerManager.WakeLock#setWorkSource} on the underlying wake lock. */
+ public void setWorkSource(@Nullable WorkSource workSource) {
+ mPowerManagerWakeLock.setWorkSource(workSource);
+ }
+}
diff --git a/services/backup/java/com/android/server/backup/UserBackupManagerService.java b/services/backup/java/com/android/server/backup/UserBackupManagerService.java
index ef1d09381237..5af2346650ed 100644
--- a/services/backup/java/com/android/server/backup/UserBackupManagerService.java
+++ b/services/backup/java/com/android/server/backup/UserBackupManagerService.java
@@ -86,7 +86,6 @@ import android.os.RemoteException;
import android.os.SELinux;
import android.os.SystemClock;
import android.os.UserHandle;
-import android.os.WorkSource;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArraySet;
@@ -172,88 +171,6 @@ import java.util.concurrent.atomic.AtomicInteger;
/** System service that performs backup/restore operations. */
public class UserBackupManagerService {
- /**
- * Wrapper over {@link PowerManager.WakeLock} to prevent double-free exceptions on release()
- * after quit().
- */
- public static class BackupWakeLock {
- private final PowerManager.WakeLock mPowerManagerWakeLock;
- private boolean mHasQuit = false;
- private final int mUserId;
- private final BackupManagerConstants mBackupManagerConstants;
-
- public BackupWakeLock(PowerManager.WakeLock powerManagerWakeLock, int userId,
- BackupManagerConstants backupManagerConstants) {
- mPowerManagerWakeLock = powerManagerWakeLock;
- mUserId = userId;
- mBackupManagerConstants = backupManagerConstants;
- }
-
- /** Acquires the {@link PowerManager.WakeLock} if hasn't been quit. */
- public synchronized void acquire() {
- if (mHasQuit) {
- Slog.d(
- TAG,
- addUserIdToLogMessage(
- mUserId,
- "Ignore wakelock acquire after quit: "
- + mPowerManagerWakeLock.getTag()));
- return;
- }
- // Set a timeout for the wakelock. Otherwise if we fail internally and never call
- // release(), the device might stay awake and drain battery indefinitely.
- mPowerManagerWakeLock.acquire(mBackupManagerConstants.getWakelockTimeoutMillis());
- Slog.d(
- TAG,
- addUserIdToLogMessage(
- mUserId, "Acquired wakelock:" + mPowerManagerWakeLock.getTag()));
- }
-
- /** Releases the {@link PowerManager.WakeLock} if hasn't been quit. */
- public synchronized void release() {
- if (mHasQuit) {
- Slog.d(
- TAG,
- addUserIdToLogMessage(
- mUserId,
- "Ignore wakelock release after quit: "
- + mPowerManagerWakeLock.getTag()));
- return;
- }
-
- if (!mPowerManagerWakeLock.isHeld()) {
- Slog.w(TAG, addUserIdToLogMessage(mUserId,
- "Wakelock not held: " + mPowerManagerWakeLock.getTag()));
- return;
- }
-
- mPowerManagerWakeLock.release();
- Slog.d(
- TAG,
- addUserIdToLogMessage(
- mUserId, "Released wakelock:" + mPowerManagerWakeLock.getTag()));
- }
-
- /**
- * Returns true if the {@link PowerManager.WakeLock} has been acquired but not yet released.
- */
- public synchronized boolean isHeld() {
- return mPowerManagerWakeLock.isHeld();
- }
-
- /** Release the {@link PowerManager.WakeLock} till it isn't held. */
- public synchronized void quit() {
- while (mPowerManagerWakeLock.isHeld()) {
- Slog.d(
- TAG,
- addUserIdToLogMessage(
- mUserId, "Releasing wakelock: " + mPowerManagerWakeLock.getTag()));
- mPowerManagerWakeLock.release();
- }
- mHasQuit = true;
- }
- }
-
// Persistently track the need to do a full init.
private static final String INIT_SENTINEL_FILE_NAME = "_need_init_";
@@ -756,20 +673,10 @@ public class UserBackupManagerService {
mSetupComplete = setupComplete;
}
- public BackupWakeLock getWakelock() {
+ public BackupWakeLock getWakeLock() {
return mWakelock;
}
- /**
- * Sets the {@link WorkSource} of the {@link PowerManager.WakeLock} returned by {@link
- * #getWakelock()}.
- */
- @VisibleForTesting
- public void setWorkSource(@Nullable WorkSource workSource) {
- // TODO: This is for testing, unfortunately WakeLock is final and WorkSource is not exposed
- mWakelock.mPowerManagerWakeLock.setWorkSource(workSource);
- }
-
public Handler getBackupHandler() {
return mBackupHandler;
}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java b/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
index 9d680c0cf1fa..0d4364e14e03 100644
--- a/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
+++ b/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
@@ -468,7 +468,7 @@ public class PerformAdbBackupTask extends FullBackupTask implements BackupRestor
sendEndBackup();
obbConnection.tearDown();
Slog.d(TAG, "Full backup pass complete.");
- mUserBackupManagerService.getWakelock().release();
+ mUserBackupManagerService.getWakeLock().release();
}
}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java b/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java
index 88abc439a566..bd34f33226a1 100644
--- a/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java
+++ b/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java
@@ -688,7 +688,7 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba
.getBackupAgentConnectionManager().clearNoRestrictedModePackages();
Slog.i(TAG, "Full data backup pass finished.");
- mUserBackupManagerService.getWakelock().release();
+ mUserBackupManagerService.getWakeLock().release();
}
}
diff --git a/services/backup/java/com/android/server/backup/internal/BackupHandler.java b/services/backup/java/com/android/server/backup/internal/BackupHandler.java
index 18c983585490..87cf8a313651 100644
--- a/services/backup/java/com/android/server/backup/internal/BackupHandler.java
+++ b/services/backup/java/com/android/server/backup/internal/BackupHandler.java
@@ -136,7 +136,7 @@ public class BackupHandler extends Handler {
public void handleMessage(Message msg) {
if (msg.what == MSG_STOP) {
Slog.d(TAG, "Stopping backup handler");
- backupManagerService.getWakelock().quit();
+ backupManagerService.getWakeLock().quit();
mBackupThread.quitSafely();
}
@@ -181,7 +181,7 @@ public class BackupHandler extends Handler {
// Acquire the wakelock and pass it to the backup thread. It will be released
// once backup concludes.
backupManagerService.setBackupRunning(true);
- backupManagerService.getWakelock().acquire();
+ backupManagerService.getWakeLock().acquire();
// Do we have any work to do? Construct the work queue
// then release the synchronization lock to actually run
@@ -254,7 +254,7 @@ public class BackupHandler extends Handler {
synchronized (backupManagerService.getQueueLock()) {
backupManagerService.setBackupRunning(false);
}
- backupManagerService.getWakelock().release();
+ backupManagerService.getWakeLock().release();
}
break;
}
@@ -466,7 +466,7 @@ public class BackupHandler extends Handler {
Slog.d(TAG, "MSG_REQUEST_BACKUP observer=" + params.observer);
}
backupManagerService.setBackupRunning(true);
- backupManagerService.getWakelock().acquire();
+ backupManagerService.getWakeLock().acquire();
KeyValueBackupTask.start(
backupManagerService,
diff --git a/services/backup/java/com/android/server/backup/internal/PerformClearTask.java b/services/backup/java/com/android/server/backup/internal/PerformClearTask.java
index de0177c1b62c..b9ac5648a5f4 100644
--- a/services/backup/java/com/android/server/backup/internal/PerformClearTask.java
+++ b/services/backup/java/com/android/server/backup/internal/PerformClearTask.java
@@ -75,7 +75,7 @@ public class PerformClearTask implements Runnable {
}
mListener.onFinished(callerLogString);
// Last but not least, release the cpu
- mBackupManagerService.getWakelock().release();
+ mBackupManagerService.getWakeLock().release();
}
}
}
diff --git a/services/backup/java/com/android/server/backup/keyvalue/KeyValueBackupTask.java b/services/backup/java/com/android/server/backup/keyvalue/KeyValueBackupTask.java
index 689c43a1cc96..494b9d59a238 100644
--- a/services/backup/java/com/android/server/backup/keyvalue/KeyValueBackupTask.java
+++ b/services/backup/java/com/android/server/backup/keyvalue/KeyValueBackupTask.java
@@ -826,7 +826,7 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
}
mTaskFinishedListener.onFinished(callerLogString);
mReporter.onBackupFinished(getBackupFinishedStatus(mCancelled, status));
- mBackupManagerService.getWakelock().release();
+ mBackupManagerService.getWakeLock().release();
}
private int getBackupFinishedStatus(boolean cancelled, int transportStatus) {
@@ -878,12 +878,13 @@ public class KeyValueBackupTask implements BackupRestoreTask, Runnable {
* the transport or not. It's the caller responsibility to do the clean-up or delegate it.
*/
private void extractAgentData(PackageInfo packageInfo) throws AgentException, TaskException {
- mBackupManagerService.setWorkSource(new WorkSource(packageInfo.applicationInfo.uid));
+ mBackupManagerService.getWakeLock().setWorkSource(
+ new WorkSource(packageInfo.applicationInfo.uid));
try {
mAgent = bindAgent(packageInfo);
extractAgentData(packageInfo, mAgent);
} finally {
- mBackupManagerService.setWorkSource(null);
+ mBackupManagerService.getWakeLock().setWorkSource(null);
}
}
diff --git a/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java b/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java
index 3ed0b835c634..41134d68916a 100644
--- a/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java
+++ b/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java
@@ -40,6 +40,7 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.LocalServices;
+import com.android.server.backup.BackupWakeLock;
import com.android.server.backup.Flags;
import com.android.server.backup.TransportManager;
import com.android.server.backup.UserBackupManagerService;
@@ -120,7 +121,7 @@ public class ActiveRestoreSession extends IRestoreSession.Stub {
// comes in.
mBackupManagerService.getBackupHandler().removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
- UserBackupManagerService.BackupWakeLock wakelock = mBackupManagerService.getWakelock();
+ BackupWakeLock wakelock = mBackupManagerService.getWakeLock();
wakelock.acquire();
// Prevent lambda from leaking 'this'
@@ -420,7 +421,7 @@ public class ActiveRestoreSession extends IRestoreSession.Stub {
Handler backupHandler = mBackupManagerService.getBackupHandler();
backupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
- UserBackupManagerService.BackupWakeLock wakelock = mBackupManagerService.getWakelock();
+ BackupWakeLock wakelock = mBackupManagerService.getWakeLock();
wakelock.acquire();
if (DEBUG) {
Slog.d(TAG, callerLogString);
diff --git a/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java b/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java
index 211628412449..5a3494c2bc6e 100644
--- a/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java
+++ b/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java
@@ -141,7 +141,7 @@ public class PerformAdbRestoreTask implements Runnable {
mObbConnection.tearDown();
mObserver = FullBackupRestoreObserverUtils.sendEndRestore(mObserver);
Slog.d(TAG, "Full restore pass complete.");
- mBackupManagerService.getWakelock().release();
+ mBackupManagerService.getWakeLock().release();
}
}