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

Commit f589b973 authored by Jeongsik Mun's avatar Jeongsik Mun Committed by Todd Kennedy
Browse files

Fix the shorcuts deletion issue in ShortcutService

Since it cleans up directories and rewrite the ShortcutPackageItems
during saveToXml(), it could lead to shortcuts missing when shutdown.

This CL receives ACTION_SHUTDOWN so that it can finish up saving it
before shutdown if the task is scheduled.

Test: compile & verify basic functions working
Bug: 178191974
Change-Id: If7dece01d6ca5280e6e596317292273246a816e4
parent 569bfb35
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -362,6 +362,7 @@ public class ShortcutService extends IShortcutService.Stub {
    private List<Integer> mDirtyUserIds = new ArrayList<>();

    private final AtomicBoolean mBootCompleted = new AtomicBoolean();
    private final AtomicBoolean mShutdown = new AtomicBoolean();

    /**
     * Note we use a fine-grained lock for {@link #mUnlockedUsers} due to b/64303666.
@@ -498,6 +499,12 @@ public class ShortcutService extends IShortcutService.Stub {
        mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL,
                localeFilter, null, mHandler);

        IntentFilter shutdownFilter = new IntentFilter();
        shutdownFilter.addAction(Intent.ACTION_SHUTDOWN);
        shutdownFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        mContext.registerReceiverAsUser(mShutdownReceiver, UserHandle.SYSTEM,
                shutdownFilter, null, mHandler);

        injectRegisterUidObserver(mUidObserver, ActivityManager.UID_OBSERVER_PROCSTATE
                | ActivityManager.UID_OBSERVER_GONE);

@@ -1162,6 +1169,9 @@ public class ShortcutService extends IShortcutService.Stub {
        if (DEBUG) {
            Slog.d(TAG, "saveDirtyInfo");
        }
        if (mShutdown.get()) {
            return;
        }
        try {
            synchronized (mLock) {
                for (int i = mDirtyUserIds.size() - 1; i >= 0; i--) {
@@ -3494,6 +3504,22 @@ public class ShortcutService extends IShortcutService.Stub {
        }
    };

    private final BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Since it cleans up the shortcut directory and rewrite the ShortcutPackageItems
            // in odrder during saveToXml(), it could lead to shortcuts missing when shutdown.
            // We need it so that it can finish up saving before shutdown.
            synchronized (mLock) {
                if (mHandler.hasCallbacks(mSaveDirtyInfoRunner)) {
                    mHandler.removeCallbacks(mSaveDirtyInfoRunner);
                    saveDirtyInfo();
                }
                mShutdown.set(true);
            }
        }
    };

    /**
     * Called when a user is unlocked.
     * - Check all known packages still exist, and otherwise perform cleanup.