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

Commit 2b9feda3 authored by Pinyao Ting's avatar Pinyao Ting Committed by Android (Google) Code Review
Browse files

Merge "Rename locks used in shortcut service for clarity" into main

parents 2edb4057 f4d59329
Loading
Loading
Loading
Loading
+12 −12
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ import java.util.List;
/**
 * Launcher information used by {@link ShortcutService}.
 *
 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
 * All methods should be guarded by {@code ShortcutPackageItem#mPackageItemLock}.
 */
class ShortcutLauncher extends ShortcutPackageItem {
    private static final String TAG = ShortcutService.TAG;
@@ -66,7 +66,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
    /**
     * Package name -> IDs.
     */
    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private final ArrayMap<UserPackage, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>();

    private ShortcutLauncher(@NonNull ShortcutUser shortcutUser,
@@ -99,7 +99,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
     */
    private void onRestoreBlocked() {
        final ArrayList<UserPackage> pinnedPackages;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            pinnedPackages = new ArrayList<>(mPinnedShortcuts.keySet());
            mPinnedShortcuts.clear();
        }
@@ -138,7 +138,7 @@ class ShortcutLauncher extends ShortcutPackageItem {

        final int idSize = ids.size();
        if (idSize == 0) {
            synchronized (mLock) {
            synchronized (mPackageItemLock) {
                mPinnedShortcuts.remove(up);
            }
        } else {
@@ -165,7 +165,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
                    floatingSet.add(id);
                }
            }
            synchronized (mLock) {
            synchronized (mPackageItemLock) {
                final ArraySet<String> prevSet = mPinnedShortcuts.get(up);
                if (prevSet != null) {
                    for (String id : floatingSet) {
@@ -187,7 +187,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
    @Nullable
    public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName,
            @UserIdInt int packageUserId) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final ArraySet<String> pinnedShortcuts = mPinnedShortcuts.get(
                    UserPackage.of(packageUserId, packageName));
            return pinnedShortcuts == null ? null : new ArraySet<>(pinnedShortcuts);
@@ -198,7 +198,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
     * Return true if the given shortcut is pinned by this launcher.<code></code>
     */
    public boolean hasPinned(ShortcutInfo shortcut) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final ArraySet<String> pinned = mPinnedShortcuts.get(
                    UserPackage.of(shortcut.getUserId(), shortcut.getPackage()));
            return (pinned != null) && pinned.contains(shortcut.getId());
@@ -211,7 +211,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
    public void addPinnedShortcut(@NonNull String packageName, @UserIdInt int packageUserId,
            String id, boolean forPinRequest) {
        final ArrayList<String> pinnedList;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final ArraySet<String> pinnedSet = mPinnedShortcuts.get(
                    UserPackage.of(packageUserId, packageName));
            if (pinnedSet != null) {
@@ -227,7 +227,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
    }

    boolean cleanUpPackage(String packageName, @UserIdInt int packageUserId) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return mPinnedShortcuts.remove(UserPackage.of(packageUserId, packageName)) != null;
        }
    }
@@ -253,7 +253,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
            return;
        }
        final ArrayMap<UserPackage, ArraySet<String>> pinnedShortcuts;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            pinnedShortcuts = new ArrayMap<>(mPinnedShortcuts);
        }
        final int size = pinnedShortcuts.size();
@@ -366,7 +366,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
                                : ShortcutService.parseIntAttribute(parser,
                                ATTR_PACKAGE_USER_ID, ownerUserId);
                        ids = new ArraySet<>();
                        synchronized (ret.mLock) {
                        synchronized (ret.mPackageItemLock) {
                            ret.mPinnedShortcuts.put(
                                    UserPackage.of(packageUserId, packageName), ids);
                        }
@@ -407,7 +407,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
        pw.println();

        final ArrayMap<UserPackage, ArraySet<String>> pinnedShortcuts;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            pinnedShortcuts = new ArrayMap<>(mPinnedShortcuts);
        }
        final int size = pinnedShortcuts.size();
+29 −29
Original line number Diff line number Diff line
@@ -163,20 +163,20 @@ class ShortcutPackage extends ShortcutPackageItem {
    /**
     * An in-memory copy of shortcuts for this package that was loaded from xml, keyed on IDs.
     */
    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private final ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();

    /**
     * A temporary copy of shortcuts that are to be cleared once persisted into AppSearch, keyed on
     * IDs.
     */
    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private final ArrayMap<String, ShortcutInfo> mTransientShortcuts = new ArrayMap<>(0);

    /**
     * All the share targets from the package
     */
    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private final ArrayList<ShareTargetInfo> mShareTargets = new ArrayList<>(0);

    /**
@@ -193,10 +193,10 @@ class ShortcutPackage extends ShortcutPackageItem {

    private long mLastKnownForegroundElapsedTime;

    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private long mLastReportedTime;

    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private boolean mIsAppSearchSchemaUpToDate;

    private ShortcutPackage(ShortcutUser shortcutUser,
@@ -233,7 +233,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    }

    public int getShortcutCount() {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return mShortcuts.size();
        }
    }
@@ -276,7 +276,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    @Nullable
    public ShortcutInfo findShortcutById(@Nullable final String id) {
        if (id == null) return null;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return mShortcuts.get(id);
        }
    }
@@ -354,7 +354,7 @@ class ShortcutPackage extends ShortcutPackageItem {
     */
    private ShortcutInfo forceDeleteShortcutInner(@NonNull String id) {
        final ShortcutInfo shortcut;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            shortcut = mShortcuts.remove(id);
            if (shortcut != null) {
                removeIcon(shortcut);
@@ -409,7 +409,7 @@ class ShortcutPackage extends ShortcutPackageItem {

        if (newShortcut.isExcludedFromSurfaces(ShortcutInfo.SURFACE_LAUNCHER)) {
            if (isAppSearchEnabled()) {
                synchronized (mLock) {
                synchronized (mPackageItemLock) {
                    mTransientShortcuts.put(newShortcut.getId(), newShortcut);
                }
            }
@@ -482,7 +482,7 @@ class ShortcutPackage extends ShortcutPackageItem {

        if (newShortcut.isExcludedFromSurfaces(ShortcutInfo.SURFACE_LAUNCHER)) {
            if (isAppSearchEnabled()) {
                synchronized (mLock) {
                synchronized (mPackageItemLock) {
                    mTransientShortcuts.put(newShortcut.getId(), newShortcut);
                }
            }
@@ -506,7 +506,7 @@ class ShortcutPackage extends ShortcutPackageItem {
        final ShortcutService service = mShortcutUser.mService;
        // Ensure the total number of shortcuts doesn't exceed the hard limit per app.
        final int maxShortcutPerApp = service.getMaxAppShortcuts();
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final List<ShortcutInfo> appShortcuts = mShortcuts.values().stream().filter(si ->
                    !si.isPinned()).collect(Collectors.toList());
            if (appShortcuts.size() >= maxShortcutPerApp) {
@@ -555,7 +555,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    public List<ShortcutInfo> deleteAllDynamicShortcuts() {
        final long now = mShortcutUser.mService.injectCurrentTimeMillis();
        boolean changed = false;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            for (int i = mShortcuts.size() - 1; i >= 0; i--) {
                ShortcutInfo si = mShortcuts.valueAt(i);
                if (si.isDynamic() && si.isVisibleToPublisher()) {
@@ -914,7 +914,7 @@ class ShortcutPackage extends ShortcutPackageItem {

    List<ShortcutManager.ShareShortcutInfo> getMatchingShareTargets(
            @NonNull final IntentFilter filter, @Nullable final String pkgName) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final List<ShareTargetInfo> matchedTargets = new ArrayList<>();
            for (int i = 0; i < mShareTargets.size(); i++) {
                final ShareTargetInfo target = mShareTargets.get(i);
@@ -967,7 +967,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    }

    public boolean hasShareTargets() {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return !mShareTargets.isEmpty();
        }
    }
@@ -978,7 +978,7 @@ class ShortcutPackage extends ShortcutPackageItem {
     * the app's Xml resource.
     */
    int getSharingShortcutCount() {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            if (mShareTargets.isEmpty()) {
                return 0;
            }
@@ -1017,7 +1017,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    /**
     * Return the filenames (excluding path names) of icon bitmap files from this package.
     */
    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    private ArraySet<String> getUsedBitmapFilesLocked() {
        final ArraySet<String> usedFiles = new ArraySet<>(1);
        forEachShortcut(si -> {
@@ -1029,7 +1029,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    }

    public void cleanupDanglingBitmapFiles(@NonNull File path) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            mShortcutBitmapSaver.waitForAllSavesLocked();
            final ArraySet<String> usedFiles = getUsedBitmapFilesLocked();

@@ -1136,7 +1136,7 @@ class ShortcutPackage extends ShortcutPackageItem {
        // Now prepare to publish manifest shortcuts.
        List<ShortcutInfo> newManifestShortcutList = null;
        int shareTargetSize = 0;
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            try {
                shareTargetSize = mShareTargets.size();
                newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
@@ -1680,7 +1680,7 @@ class ShortcutPackage extends ShortcutPackageItem {

    void reportShortcutUsed(@NonNull final UsageStatsManagerInternal usageStatsManagerInternal,
            @NonNull final String shortcutId) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final long currentTS = SystemClock.elapsedRealtime();
            final ShortcutService s = mShortcutUser.mService;
            if (currentTS - mLastReportedTime > s.mSaveDelayMillis) {
@@ -1757,7 +1757,7 @@ class ShortcutPackage extends ShortcutPackageItem {
        pw.println(")");

        pw.println();
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            mShortcutBitmapSaver.dumpLocked(pw, "  ");
        }
    }
@@ -1827,7 +1827,7 @@ class ShortcutPackage extends ShortcutPackageItem {
    @Override
    public void saveToXml(@NonNull TypedXmlSerializer out, boolean forBackup)
            throws IOException, XmlPullParserException {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            final int size = mShortcuts.size();
            final int shareTargetSize = mShareTargets.size();

@@ -2037,7 +2037,7 @@ class ShortcutPackage extends ShortcutPackageItem {

        final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
                shortcutUser.getUserId(), packageName);
        synchronized (ret.mLock) {
        synchronized (ret.mPackageItemLock) {
            ret.mIsAppSearchSchemaUpToDate = ShortcutService.parseIntAttribute(
                    parser, ATTR_SCHEMA_VERSON, 0) == AppSearchShortcutInfo.SCHEMA_VERSION;

@@ -2283,7 +2283,7 @@ class ShortcutPackage extends ShortcutPackageItem {

    @VisibleForTesting
    List<ShareTargetInfo> getAllShareTargetsForTest() {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return new ArrayList<>(mShareTargets);
        }
    }
@@ -2404,7 +2404,7 @@ class ShortcutPackage extends ShortcutPackageItem {
            @NonNull final Consumer<ShortcutInfo> transform) {
        Objects.requireNonNull(id);
        Objects.requireNonNull(transform);
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            if (shortcut != null) {
                transform.accept(shortcut);
            }
@@ -2424,7 +2424,7 @@ class ShortcutPackage extends ShortcutPackageItem {

    private void saveShortcut(@NonNull final Collection<ShortcutInfo> shortcuts) {
        Objects.requireNonNull(shortcuts);
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            for (ShortcutInfo si : shortcuts) {
                mShortcuts.put(si.getId(), si);
            }
@@ -2433,7 +2433,7 @@ class ShortcutPackage extends ShortcutPackageItem {

    @Nullable
    List<ShortcutInfo> findAll(@NonNull final Collection<String> ids) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return ids.stream().map(mShortcuts::get)
                    .filter(Objects::nonNull).collect(Collectors.toList());
        }
@@ -2455,7 +2455,7 @@ class ShortcutPackage extends ShortcutPackageItem {

    private void forEachShortcutStopWhen(
            @NonNull final Function<ShortcutInfo, Boolean> cb) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            for (int i = mShortcuts.size() - 1; i >= 0; i--) {
                final ShortcutInfo si = mShortcuts.valueAt(i);
                if (cb.apply(si)) {
@@ -2600,7 +2600,7 @@ class ShortcutPackage extends ShortcutPackageItem {
                        })));
    }

    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    @Override
    void scheduleSaveToAppSearchLocked() {
        final Map<String, ShortcutInfo> copy = new ArrayMap<>(mShortcuts);
@@ -2684,7 +2684,7 @@ class ShortcutPackage extends ShortcutPackageItem {
                    .penaltyLog() // TODO: change this to penaltyDeath to fix the call-site
                    .build());
            future = mShortcutUser.getAppSearch(searchContext);
            synchronized (mLock) {
            synchronized (mPackageItemLock) {
                if (!mIsAppSearchSchemaUpToDate) {
                    future = future.thenCompose(this::setupSchema);
                }
+11 −11
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * All methods should be either guarded by {@code #mShortcutUser.mService.mLock} or {@code #mLock}.
 * All methods should be either guarded by {@code #mPackageItemLock}.
 */
abstract class ShortcutPackageItem {
    private static final String TAG = ShortcutService.TAG;
@@ -52,10 +52,10 @@ abstract class ShortcutPackageItem {

    protected ShortcutUser mShortcutUser;

    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    protected final ShortcutBitmapSaver mShortcutBitmapSaver;

    protected final Object mLock = new Object();
    protected final Object mPackageItemLock = new Object();

    protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser,
            int packageUserId, @NonNull String packageName,
@@ -157,7 +157,7 @@ abstract class ShortcutPackageItem {
    public abstract void saveToXml(@NonNull TypedXmlSerializer out, boolean forBackup)
            throws IOException, XmlPullParserException;

    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    public void saveToFileLocked(File path, boolean forBackup) {
        try (ResilientAtomicFile file = getResilientFile(path)) {
            FileOutputStream os = null;
@@ -187,7 +187,7 @@ abstract class ShortcutPackageItem {
        }
    }

    @GuardedBy("mLock")
    @GuardedBy("mPackageItemLock")
    void scheduleSaveToAppSearchLocked() {

    }
@@ -219,7 +219,7 @@ abstract class ShortcutPackageItem {
        if (ShortcutService.DEBUG || ShortcutService.DEBUG_REBOOT) {
            Slog.d(TAG, "Saving package item " + getPackageName() + " to " + path);
        }
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            path.getParentFile().mkdirs();
            // TODO: Since we are persisting shortcuts into AppSearch, we should read from/write to
            //  AppSearch as opposed to maintaining a separate XML file.
@@ -229,14 +229,14 @@ abstract class ShortcutPackageItem {
    }

    public boolean waitForBitmapSaves() {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return mShortcutBitmapSaver.waitForAllSavesLocked();
        }
    }

    public void saveBitmap(ShortcutInfo shortcut,
            int maxDimension, Bitmap.CompressFormat format, int quality) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            mShortcutBitmapSaver.saveBitmapLocked(shortcut, maxDimension, format, quality);
        }
    }
@@ -246,19 +246,19 @@ abstract class ShortcutPackageItem {
     */
    @Nullable
    public String getBitmapPathMayWait(ShortcutInfo shortcut) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            return mShortcutBitmapSaver.getBitmapPathMayWaitLocked(shortcut);
        }
    }

    public void removeIcon(ShortcutInfo shortcut) {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            mShortcutBitmapSaver.removeIcon(shortcut);
        }
    }

    void removeShortcutPackageItem() {
        synchronized (mLock) {
        synchronized (mPackageItemLock) {
            getResilientFile(getShortcutPackageItemFile()).delete();
        }
    }
+106 −106

File changed.

Preview size limit exceeded, changes collapsed.