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

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

Merge "Acquire the backup wakelock with a timeout" into main

parents c88a1427 bb80de0b
Loading
Loading
Loading
Loading
+14 −0
Original line number Original line Diff line number Diff line
@@ -72,6 +72,9 @@ public class BackupManagerConstants extends KeyValueSettingObserver {
    public static final String BACKUP_FINISHED_NOTIFICATION_RECEIVERS =
    public static final String BACKUP_FINISHED_NOTIFICATION_RECEIVERS =
            "backup_finished_notification_receivers";
            "backup_finished_notification_receivers";


    @VisibleForTesting
    public static final String WAKELOCK_TIMEOUT_MILLIS = "wakelock_timeout_millis";

    // Hard coded default values.
    // Hard coded default values.
    @VisibleForTesting
    @VisibleForTesting
    public static final long DEFAULT_KEY_VALUE_BACKUP_INTERVAL_MILLISECONDS =
    public static final long DEFAULT_KEY_VALUE_BACKUP_INTERVAL_MILLISECONDS =
@@ -97,6 +100,9 @@ public class BackupManagerConstants extends KeyValueSettingObserver {
    @VisibleForTesting
    @VisibleForTesting
    public static final String DEFAULT_BACKUP_FINISHED_NOTIFICATION_RECEIVERS = "";
    public static final String DEFAULT_BACKUP_FINISHED_NOTIFICATION_RECEIVERS = "";


    @VisibleForTesting
    public static final long DEFAULT_WAKELOCK_TIMEOUT_MILLIS = 30 * 60 * 1000; // 30 minutes

    // Backup manager constants.
    // Backup manager constants.
    private long mKeyValueBackupIntervalMilliseconds;
    private long mKeyValueBackupIntervalMilliseconds;
    private long mKeyValueBackupFuzzMilliseconds;
    private long mKeyValueBackupFuzzMilliseconds;
@@ -106,6 +112,7 @@ public class BackupManagerConstants extends KeyValueSettingObserver {
    private boolean mFullBackupRequireCharging;
    private boolean mFullBackupRequireCharging;
    private int mFullBackupRequiredNetworkType;
    private int mFullBackupRequiredNetworkType;
    private String[] mBackupFinishedNotificationReceivers;
    private String[] mBackupFinishedNotificationReceivers;
    private long mWakelockTimeoutMillis;


    public BackupManagerConstants(Handler handler, ContentResolver resolver) {
    public BackupManagerConstants(Handler handler, ContentResolver resolver) {
        super(handler, resolver, Settings.Secure.getUriFor(SETTING));
        super(handler, resolver, Settings.Secure.getUriFor(SETTING));
@@ -152,6 +159,8 @@ public class BackupManagerConstants extends KeyValueSettingObserver {
        } else {
        } else {
            mBackupFinishedNotificationReceivers = backupFinishedNotificationReceivers.split(":");
            mBackupFinishedNotificationReceivers = backupFinishedNotificationReceivers.split(":");
        }
        }
        mWakelockTimeoutMillis = parser.getLong(WAKELOCK_TIMEOUT_MILLIS,
                DEFAULT_WAKELOCK_TIMEOUT_MILLIS);
    }
    }


    // The following are access methods for the individual parameters.
    // The following are access methods for the individual parameters.
@@ -235,4 +244,9 @@ public class BackupManagerConstants extends KeyValueSettingObserver {
        }
        }
        return mBackupFinishedNotificationReceivers;
        return mBackupFinishedNotificationReceivers;
    }
    }

    public synchronized long getWakelockTimeoutMillis() {
        Slog.v(TAG, "wakelock timeout: " + mWakelockTimeoutMillis);
        return mWakelockTimeoutMillis;
    }
}
}
+10 −7
Original line number Original line Diff line number Diff line
@@ -181,11 +181,14 @@ public class UserBackupManagerService {
    public static class BackupWakeLock {
    public static class BackupWakeLock {
        private final PowerManager.WakeLock mPowerManagerWakeLock;
        private final PowerManager.WakeLock mPowerManagerWakeLock;
        private boolean mHasQuit = false;
        private boolean mHasQuit = false;
        private int mUserId;
        private final int mUserId;
        private final BackupManagerConstants mBackupManagerConstants;


        public BackupWakeLock(PowerManager.WakeLock powerManagerWakeLock, int userId) {
        public BackupWakeLock(PowerManager.WakeLock powerManagerWakeLock, int userId,
                BackupManagerConstants backupManagerConstants) {
            mPowerManagerWakeLock = powerManagerWakeLock;
            mPowerManagerWakeLock = powerManagerWakeLock;
            mUserId = userId;
            mUserId = userId;
            mBackupManagerConstants = backupManagerConstants;
        }
        }


        /** Acquires the {@link PowerManager.WakeLock} if hasn't been quit. */
        /** Acquires the {@link PowerManager.WakeLock} if hasn't been quit. */
@@ -199,7 +202,9 @@ public class UserBackupManagerService {
                                        + mPowerManagerWakeLock.getTag()));
                                        + mPowerManagerWakeLock.getTag()));
                return;
                return;
            }
            }
            mPowerManagerWakeLock.acquire();
            // 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.v(
            Slog.v(
                    TAG,
                    TAG,
                    addUserIdToLogMessage(
                    addUserIdToLogMessage(
@@ -674,10 +679,8 @@ public class UserBackupManagerService {
        mBackupPreferences = new UserBackupPreferences(mContext, mBaseStateDir);
        mBackupPreferences = new UserBackupPreferences(mContext, mBaseStateDir);


        // Power management
        // Power management
        mWakelock = new BackupWakeLock(
        mWakelock = new BackupWakeLock(mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                mPowerManager.newWakeLock(
                "*backup*-" + userId + "-" + userBackupThread.getThreadId()), userId, mConstants);
                        PowerManager.PARTIAL_WAKE_LOCK,
                        "*backup*-" + userId + "-" + userBackupThread.getThreadId()), userId);


        // Set up the various sorts of package tracking we do
        // Set up the various sorts of package tracking we do
        mFullBackupScheduleFile = new File(mBaseStateDir, "fb-schedule");
        mFullBackupScheduleFile = new File(mBaseStateDir, "fb-schedule");
+4 −3
Original line number Original line Diff line number Diff line
@@ -37,6 +37,7 @@ import android.os.Process;
import android.util.Log;
import android.util.Log;


import com.android.server.backup.BackupAgentTimeoutParameters;
import com.android.server.backup.BackupAgentTimeoutParameters;
import com.android.server.backup.BackupManagerConstants;
import com.android.server.backup.BackupManagerService;
import com.android.server.backup.BackupManagerService;
import com.android.server.backup.TransportManager;
import com.android.server.backup.TransportManager;
import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.UserBackupManagerService;
@@ -162,10 +163,10 @@ public class BackupManagerServiceTestUtils {


    public static UserBackupManagerService.BackupWakeLock createBackupWakeLock(
    public static UserBackupManagerService.BackupWakeLock createBackupWakeLock(
            Application application) {
            Application application) {
        PowerManager powerManager =
        PowerManager powerManager = application.getSystemService(PowerManager.class);
                (PowerManager) application.getSystemService(Context.POWER_SERVICE);
        return new UserBackupManagerService.BackupWakeLock(
        return new UserBackupManagerService.BackupWakeLock(
                powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*"), 0);
                powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*"), 0,
                new BackupManagerConstants(Handler.getMain(), application.getContentResolver()));
    }
    }


    /**
    /**