Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0e8cc6f authored by Sarp Misoglu's avatar Sarp Misoglu Committed by Android (Google) Code Review
Browse files

Merge changes I595fe26e,I117f7fe0 into main

* changes:
  Move BackupWakeLock to its own file
  Fix debug logging in the backup service
parents 7615f9b2 3682f501
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package com.android.server.backup;

import static com.android.server.backup.BackupManagerService.MORE_DEBUG;
import static com.android.server.backup.BackupManagerService.DEBUG;
import static com.android.server.backup.BackupManagerService.TAG;

import android.annotation.Nullable;
@@ -302,7 +302,7 @@ public class BackupAgentConnectionManager {
                // that the package being backed up doesn't get stuck in restricted mode until the
                // backup time-out elapses.
                for (int token : mOperationStorage.operationTokensForPackage(packageName)) {
                    if (MORE_DEBUG) {
                    if (DEBUG) {
                        Slog.d(TAG,
                                mUserIdMsg + "agentDisconnected: will handleCancel(all) for token:"
                                        + Integer.toHexString(token));
+16 −48
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.server.backup;

import static com.android.server.backup.BackupManagerService.DEBUG_SCHEDULING;

import android.app.AlarmManager;
import android.app.job.JobInfo;
import android.content.ContentResolver;
@@ -168,85 +166,55 @@ public class BackupManagerConstants extends KeyValueSettingObserver {
    // group the calls of these methods in a block syncrhonized on
    // a reference of this object.
    public synchronized long getKeyValueBackupIntervalMilliseconds() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getKeyValueBackupIntervalMilliseconds(...) returns "
        Slog.d(TAG, "getKeyValueBackupIntervalMilliseconds(...) returns "
                + mKeyValueBackupIntervalMilliseconds);
        }
        return mKeyValueBackupIntervalMilliseconds;
    }

    public synchronized long getKeyValueBackupFuzzMilliseconds() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getKeyValueBackupFuzzMilliseconds(...) returns "
        Slog.d(TAG, "getKeyValueBackupFuzzMilliseconds(...) returns "
                + mKeyValueBackupFuzzMilliseconds);
        }
        return mKeyValueBackupFuzzMilliseconds;
    }

    public synchronized boolean getKeyValueBackupRequireCharging() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getKeyValueBackupRequireCharging(...) returns "
                            + mKeyValueBackupRequireCharging);
        }
        Slog.d(TAG,
                "getKeyValueBackupRequireCharging(...) returns " + mKeyValueBackupRequireCharging);
        return mKeyValueBackupRequireCharging;
    }

    public synchronized int getKeyValueBackupRequiredNetworkType() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getKeyValueBackupRequiredNetworkType(...) returns "
        Slog.d(TAG, "getKeyValueBackupRequiredNetworkType(...) returns "
                + mKeyValueBackupRequiredNetworkType);
        }
        return mKeyValueBackupRequiredNetworkType;
    }

    public synchronized long getFullBackupIntervalMilliseconds() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getFullBackupIntervalMilliseconds(...) returns "
        Slog.d(TAG, "getFullBackupIntervalMilliseconds(...) returns "
                + mFullBackupIntervalMilliseconds);
        }
        return mFullBackupIntervalMilliseconds;
    }

    public synchronized boolean getFullBackupRequireCharging() {
        if (DEBUG_SCHEDULING) {
            Slog.v(TAG, "getFullBackupRequireCharging(...) returns " + mFullBackupRequireCharging);
        }
        Slog.d(TAG, "getFullBackupRequireCharging(...) returns " + mFullBackupRequireCharging);
        return mFullBackupRequireCharging;
    }

    public synchronized int getFullBackupRequiredNetworkType() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getFullBackupRequiredNetworkType(...) returns "
                            + mFullBackupRequiredNetworkType);
        }
        Slog.d(TAG,
                "getFullBackupRequiredNetworkType(...) returns " + mFullBackupRequiredNetworkType);
        return mFullBackupRequiredNetworkType;
    }

    /** Returns an array of package names that should be notified whenever a backup finishes. */
    public synchronized String[] getBackupFinishedNotificationReceivers() {
        if (DEBUG_SCHEDULING) {
            Slog.v(
                    TAG,
                    "getBackupFinishedNotificationReceivers(...) returns "
                            + TextUtils.join(", ", mBackupFinishedNotificationReceivers));
        }
        Slog.d(TAG, "getBackupFinishedNotificationReceivers(...) returns " + TextUtils.join(", ",
                mBackupFinishedNotificationReceivers));
        return mBackupFinishedNotificationReceivers;
    }

    public synchronized long getWakelockTimeoutMillis() {
        Slog.v(TAG, "wakelock timeout: " + mWakelockTimeoutMillis);
        Slog.d(TAG, "wakelock timeout: " + mWakelockTimeoutMillis);
        return mWakelockTimeoutMillis;
    }
}
+2 −6
Original line number Diff line number Diff line
@@ -94,9 +94,7 @@ import java.util.Set;
 */
public class BackupManagerService extends IBackupManager.Stub implements BackupManagerInternal {
    public static final String TAG = "BackupManagerService";
    public static final boolean DEBUG = true;
    public static final boolean MORE_DEBUG = false;
    public static final boolean DEBUG_SCHEDULING = true;
    public static final boolean DEBUG = false;

    @VisibleForTesting
    static final String DUMP_RUNNING_USERS_MESSAGE = "Backup Manager is running for users:";
@@ -187,10 +185,8 @@ public class BackupManagerService extends IBackupManager.Stub implements BackupM
                mUserRemovedReceiver, new IntentFilter(Intent.ACTION_USER_REMOVED));
        UserHandle mainUser = getUserManager().getMainUser();
        mDefaultBackupUserId = mainUser == null ? UserHandle.USER_SYSTEM : mainUser.getIdentifier();
        if (DEBUG) {
        Slog.d(TAG, "Default backup user id = " + mDefaultBackupUserId);
    }
    }

    @VisibleForTesting
    Handler getBackupHandler() {
+95 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+4 −12
Original line number Diff line number Diff line
@@ -180,9 +180,7 @@ public class KeyValueAdbBackupEngine {
                Slog.e(TAG, "Key-value backup failed on package " + packageName);
                return false;
            }
            if (DEBUG) {
            Slog.i(TAG, "Key-value backup success for package " + packageName);
            }
            return true;
        } catch (RemoteException e) {
            Slog.e(TAG, "Error invoking agent for backup on " + packageName + ". " + e);
@@ -210,9 +208,7 @@ public class KeyValueAdbBackupEngine {
                AppMetadataBackupWriter writer =
                        new AppMetadataBackupWriter(output, mPackageManager);

                if (DEBUG) {
                Slog.d(TAG, "Writing manifest for " + mPackage.packageName);
                }

                writer.backupManifest(
                        mPackage,
@@ -223,9 +219,7 @@ public class KeyValueAdbBackupEngine {
                        /* withApk */ false);
                mManifestFile.delete();

                if (DEBUG) {
                Slog.d(TAG, "Writing key-value package payload" + mPackage.packageName);
                }
                FullBackup.backupToTar(mPackage.packageName, FullBackup.KEY_VALUE_DATA_TOKEN, null,
                        mDataDir.getAbsolutePath(),
                        mBackupDataName.getAbsolutePath(),
@@ -283,10 +277,8 @@ public class KeyValueAdbBackupEngine {
            if (!mBackupManagerService.waitUntilOperationComplete(token)) {
                Slog.e(TAG, "Full backup failed on package " + mCurrentPackage.packageName);
            } else {
                if (DEBUG) {
                Slog.d(TAG, "Full package backup success: " + mCurrentPackage.packageName);
            }
            }
        } catch (IOException e) {
            Slog.e(TAG, "Error backing up " + mCurrentPackage.packageName + ": " + e);
        } finally {
Loading