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

Commit 77e2fa2a authored by Kweku Adams's avatar Kweku Adams Committed by Android (Google) Code Review
Browse files

Merge "Revert "Put syncs in a dedicated job namespace.""

parents 20aa5c48 11ea4997
Loading
Loading
Loading
Loading
+27 −49
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.PeriodicSync;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SyncActivityTooManyDeletes;
import android.content.SyncAdapterType;
import android.content.SyncAdaptersCache;
@@ -206,6 +205,13 @@ public class SyncManager {
     */
    private static final long SYNC_DELAY_ON_CONFLICT = 10*1000; // 10 seconds

    /**
     * Generate job ids in the range [MIN_SYNC_JOB_ID, MAX_SYNC_JOB_ID) to avoid conflicts with
     * other jobs scheduled by the system process.
     */
    private static final int MIN_SYNC_JOB_ID = 100000;
    private static final int MAX_SYNC_JOB_ID = 110000;

    private static final String SYNC_WAKE_LOCK_PREFIX = "*sync*/";
    private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm";
    private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock";
@@ -223,9 +229,6 @@ public class SyncManager {
    private static final int SYNC_ADAPTER_CONNECTION_FLAGS = Context.BIND_AUTO_CREATE
            | Context.BIND_NOT_FOREGROUND | Context.BIND_ALLOW_OOM_MANAGEMENT;

    private static final String PREF_KEY_SYNC_JOB_NAMESPACE_MIGRATED =
            "sync_job_namespace_migrated";

    /** Singleton instance. */
    @GuardedBy("SyncManager.class")
    private static SyncManager sInstance;
@@ -239,11 +242,12 @@ public class SyncManager {

    volatile private PowerManager.WakeLock mSyncManagerWakeLock;
    volatile private boolean mDataConnectionIsConnected = false;
    private volatile int mNextJobId = 0;
    private volatile int mNextJobIdOffset = 0;

    private final NotificationManager mNotificationMgr;
    private final IBatteryStats mBatteryStats;
    private JobScheduler mJobScheduler;
    private JobSchedulerInternal mJobSchedulerInternal;

    private SyncStorageEngine mSyncStorageEngine;

@@ -277,19 +281,24 @@ public class SyncManager {
    }

    private int getUnusedJobIdH() {
        final List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs();
        while (isJobIdInUseLockedH(mNextJobId, pendingJobs)) {
            // SyncManager jobs are placed in their own namespace. Since there's no chance of
            // conflicting with other parts of the system, we can just keep incrementing until
            // we find an unused ID.
            mNextJobId++;
        final int maxNumSyncJobIds = MAX_SYNC_JOB_ID - MIN_SYNC_JOB_ID;
        final List<JobInfo> pendingJobs = mJobSchedulerInternal.getSystemScheduledPendingJobs();
        for (int i = 0; i < maxNumSyncJobIds; ++i) {
            int newJobId = MIN_SYNC_JOB_ID + ((mNextJobIdOffset + i) % maxNumSyncJobIds);
            if (!isJobIdInUseLockedH(newJobId, pendingJobs)) {
                mNextJobIdOffset = (mNextJobIdOffset + i + 1) % maxNumSyncJobIds;
                return newJobId;
            }
        }
        return mNextJobId;
        // We've used all 10,000 intended job IDs.... We're probably in a world of pain right now :/
        Slog.wtf(TAG, "All " + maxNumSyncJobIds + " possible sync job IDs are taken :/");
        mNextJobIdOffset = (mNextJobIdOffset + 1) % maxNumSyncJobIds;
        return MIN_SYNC_JOB_ID + mNextJobIdOffset;
    }

    private List<SyncOperation> getAllPendingSyncs() {
        verifyJobScheduler();
        List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs();
        List<JobInfo> pendingJobs = mJobSchedulerInternal.getSystemScheduledPendingJobs();
        final int numJobs = pendingJobs.size();
        final List<SyncOperation> pendingSyncs = new ArrayList<>(numJobs);
        for (int i = 0; i < numJobs; ++i) {
@@ -297,8 +306,6 @@ public class SyncManager {
            SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras());
            if (op != null) {
                pendingSyncs.add(op);
            } else {
                Slog.wtf(TAG, "Non-sync job inside of SyncManager's namespace");
            }
        }
        return pendingSyncs;
@@ -484,31 +491,6 @@ public class SyncManager {
        });
    }

    /**
     * Migrate syncs from the default job namespace to SyncManager's namespace if they haven't been
     * migrated already.
     */
    private void migrateSyncJobNamespaceIfNeeded() {
        final SharedPreferences prefs = mContext.getSharedPreferences(
                mSyncStorageEngine.getSyncDir(), Context.MODE_PRIVATE);
        if (prefs.getBoolean(PREF_KEY_SYNC_JOB_NAMESPACE_MIGRATED, false)) {
            return;
        }
        final List<JobInfo> pendingJobs = getJobSchedulerInternal().getSystemScheduledPendingJobs();
        final JobScheduler jobSchedulerDefaultNamespace =
                mContext.getSystemService(JobScheduler.class);
        for (int i = pendingJobs.size() - 1; i >= 0; --i) {
            final JobInfo job = pendingJobs.get(i);
            final SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras());
            if (op != null) {
                // This is a sync. Move it over to SyncManager's namespace.
                mJobScheduler.schedule(job);
                jobSchedulerDefaultNamespace.cancel(job.getId());
            }
        }
        prefs.edit().putBoolean(PREF_KEY_SYNC_JOB_NAMESPACE_MIGRATED, true).apply();
    }

    private synchronized void verifyJobScheduler() {
        if (mJobScheduler != null) {
            return;
@@ -518,12 +500,10 @@ public class SyncManager {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.d(TAG, "initializing JobScheduler object.");
            }
            // Use a dedicated namespace to avoid conflicts with other jobs
            // scheduled by the system process.
            mJobScheduler = mContext.getSystemService(JobScheduler.class)
                    .forNamespace("SyncManager");
            migrateSyncJobNamespaceIfNeeded();
            // Get all persisted syncs from JobScheduler in the SyncManager namespace.
            mJobScheduler = (JobScheduler) mContext.getSystemService(
                    Context.JOB_SCHEDULER_SERVICE);
            mJobSchedulerInternal = getJobSchedulerInternal();
            // Get all persisted syncs from JobScheduler
            List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs();

            int numPersistedPeriodicSyncs = 0;
@@ -539,8 +519,6 @@ public class SyncManager {
                        // shown on the settings activity.
                        mSyncStorageEngine.markPending(op.target, true);
                    }
                } else {
                    Slog.wtf(TAG, "Non-sync job inside of SyncManager namespace");
                }
            }
            final String summary = "Loaded persisted syncs: "
+0 −6
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static com.android.server.content.SyncLogger.logSafe;
import android.accounts.Account;
import android.accounts.AccountAndUser;
import android.accounts.AccountManager;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.backup.BackupManager;
import android.content.ComponentName;
@@ -575,11 +574,6 @@ public class SyncStorageEngine {
        return sSyncStorageEngine;
    }

    @NonNull
    File getSyncDir() {
        return mSyncDir;
    }

    protected void setOnSyncRequestListener(OnSyncRequestListener listener) {
        if (mSyncRequestListener == null) {
            mSyncRequestListener = listener;