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

Commit 8704fc3d authored by Matthew Williams's avatar Matthew Williams
Browse files

Remove blocking from SM before boot completed.

Bug: 10916655
Add a stash where the SyncHandler can copy and place
msgs rather than run them. After boot is complete
we iterate through the stash in order and send the
messages off.

Change-Id: I9c175ee79fe60952346003a29225b8687979b44e
parent 723ad75e
Loading
Loading
Loading
Loading
+30 −20
Original line number Original line Diff line number Diff line
@@ -1785,16 +1785,20 @@ public class SyncManager {
        public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker();
        public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker();
        private final HashMap<Pair<Account, String>, PowerManager.WakeLock> mWakeLocks =
        private final HashMap<Pair<Account, String>, PowerManager.WakeLock> mWakeLocks =
                Maps.newHashMap();
                Maps.newHashMap();

        private List<Message> mBootQueue = new ArrayList<Message>();
        private volatile CountDownLatch mReadyToRunLatch = new CountDownLatch(1);


        public void onBootCompleted() {
        public void onBootCompleted() {
            mBootCompleted = true;
            if (Log.isLoggable(TAG, Log.VERBOSE)) {

                Log.v(TAG, "Boot completed, clearing boot queue.");
            }
            doDatabaseCleanup();
            doDatabaseCleanup();

            synchronized(this) {
            if (mReadyToRunLatch != null) {
                // Dispatch any stashed messages.
                mReadyToRunLatch.countDown();
                for (Message message : mBootQueue) {
                    sendMessage(message);
                }
                mBootQueue = null;
                mBootCompleted = true;
            }
            }
        }
        }


@@ -1811,20 +1815,24 @@ public class SyncManager {
            return wakeLock;
            return wakeLock;
        }
        }


        private void waitUntilReadyToRun() {
        /**
            CountDownLatch latch = mReadyToRunLatch;
         * Stash any messages that come to the handler before boot is complete.
            if (latch != null) {
         * {@link #onBootCompleted()} will disable this and dispatch all the messages collected.
                while (true) {
         * @param msg Message to dispatch at a later point.
                    try {
         * @return true if a message was enqueued, false otherwise. This is to avoid losing the
                        latch.await();
         * message if we manage to acquire the lock but by the time we do boot has completed.
                        mReadyToRunLatch = null;
         */
                        return;
        private boolean tryEnqueueMessageUntilReadyToRun(Message msg) {
                    } catch (InterruptedException e) {
            synchronized (this) {
                        Thread.currentThread().interrupt();
                if (!mBootCompleted) {
                    }
                    // Need to copy the message bc looper will recycle it.
                    mBootQueue.add(Message.obtain(msg));
                    return true;
                }
                }
                return false;
            }
            }
        }
        }

        /**
        /**
         * Used to keep track of whether a sync notification is active and who it is for.
         * Used to keep track of whether a sync notification is active and who it is for.
         */
         */
@@ -1854,13 +1862,15 @@ public class SyncManager {


        @Override
        @Override
        public void handleMessage(Message msg) {
        public void handleMessage(Message msg) {
            if (tryEnqueueMessageUntilReadyToRun(msg)) {
                return;
            }

            long earliestFuturePollTime = Long.MAX_VALUE;
            long earliestFuturePollTime = Long.MAX_VALUE;
            long nextPendingSyncTime = Long.MAX_VALUE;
            long nextPendingSyncTime = Long.MAX_VALUE;

            // Setting the value here instead of a method because we want the dumpsys logs
            // Setting the value here instead of a method because we want the dumpsys logs
            // to have the most recent value used.
            // to have the most recent value used.
            try {
            try {
                waitUntilReadyToRun();
                mDataConnectionIsConnected = readDataConnectionState();
                mDataConnectionIsConnected = readDataConnectionState();
                mSyncManagerWakeLock.acquire();
                mSyncManagerWakeLock.acquire();
                // Always do this first so that we be sure that any periodic syncs that
                // Always do this first so that we be sure that any periodic syncs that