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

Commit 8eb8d824 authored by Aurélien Pomini's avatar Aurélien Pomini Committed by Android (Google) Code Review
Browse files

Merge changes I466acd7e,Iba71f250 into main

* changes:
  Update wallpaper packageMonitor & tryToRebind for LWP
  Reland "use new clearWallpaperLocked method everywhere"
parents faa4e5cb c65d91dd
Loading
Loading
Loading
Loading
+67 −51
Original line number Diff line number Diff line
@@ -1005,11 +1005,11 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                    return;
                }

                if (!mWallpaper.wallpaperUpdating
                        && mWallpaper.userId == mCurrentUserId) {
                if (!mWallpaper.wallpaperUpdating && mWallpaper.userId == mCurrentUserId) {
                    Slog.w(TAG, "Wallpaper reconnect timed out for " + mWallpaper.wallpaperComponent
                            + ", reverting to built-in wallpaper!");
                    clearWallpaperLocked(FLAG_SYSTEM, mWallpaper.userId, null);
                    int which = mIsLockscreenLiveWallpaperEnabled ? mWallpaper.mWhich : FLAG_SYSTEM;
                    clearWallpaperLocked(which, mWallpaper.userId, null);
                }
            }
        };
@@ -1189,7 +1189,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                } else {
                    // Timeout
                    Slog.w(TAG, "Reverting to built-in wallpaper!");
                    clearWallpaperLocked(FLAG_SYSTEM, mWallpaper.userId, null);
                    clearWallpaperLocked(mWallpaper.mWhich, mWallpaper.userId, null);
                    final String flattened = wpService.flattenToString();
                    EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED,
                            flattened.substring(0, Math.min(flattened.length(),
@@ -1228,7 +1228,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                            } else {
                                if (mLmkLimitRebindRetries <= 0) {
                                    Slog.w(TAG, "Reverting to built-in wallpaper due to lmk!");
                                    clearWallpaperLocked(FLAG_SYSTEM, mWallpaper.userId, null);
                                    clearWallpaperLocked(
                                            mWallpaper.mWhich, mWallpaper.userId, null);
                                    mLmkLimitRebindRetries = LMK_RECONNECT_REBIND_RETRIES;
                                    return;
                                }
@@ -1470,8 +1471,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (mCurrentUserId != getChangingUserId()) {
                    return;
                }
                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
                if (wallpaper != null) {
                for (WallpaperData wallpaper: getWallpapers()) {
                    final ComponentName wpService = wallpaper.wallpaperComponent;
                    if (wpService != null && wpService.getPackageName().equals(packageName)) {
                        if (DEBUG_LIVE) {
@@ -1483,7 +1483,9 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                                wallpaper, null)) {
                            Slog.w(TAG, "Wallpaper " + wpService
                                    + " no longer available; reverting to default");
                            clearWallpaperLocked(FLAG_SYSTEM, wallpaper.userId, null);
                            int which = mIsLockscreenLiveWallpaperEnabled
                                    ? wallpaper.mWhich : FLAG_SYSTEM;
                            clearWallpaperLocked(which, wallpaper.userId, null);
                        }
                    }
                }
@@ -1496,16 +1498,14 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (mCurrentUserId != getChangingUserId()) {
                    return;
                }
                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
                if (wallpaper != null) {
                    if (wallpaper.wallpaperComponent == null
                            || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
                        return;
                    }
                for (WallpaperData wallpaper: getWallpapers()) {
                    if (wallpaper.wallpaperComponent != null
                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
                        doPackagesChangedLocked(true, wallpaper);
                    }
                }
            }
        }

        @Override
        public void onPackageUpdateStarted(String packageName, int uid) {
@@ -1513,8 +1513,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (mCurrentUserId != getChangingUserId()) {
                    return;
                }
                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
                if (wallpaper != null) {
                for (WallpaperData wallpaper: getWallpapers()) {
                    if (wallpaper.wallpaperComponent != null
                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
                        if (DEBUG_LIVE) {
@@ -1538,8 +1537,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (mCurrentUserId != getChangingUserId()) {
                    return false;
                }
                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
                if (wallpaper != null) {
                for (WallpaperData wallpaper: getWallpapers()) {
                    boolean res = doPackagesChangedLocked(doit, wallpaper);
                    changed |= res;
                }
@@ -1553,8 +1551,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (mCurrentUserId != getChangingUserId()) {
                    return;
                }
                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
                if (wallpaper != null) {
                for (WallpaperData wallpaper: getWallpapers()) {
                    doPackagesChangedLocked(true, wallpaper);
                }
            }
@@ -1562,6 +1559,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub

        boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
            boolean changed = false;
            int which = mIsLockscreenLiveWallpaperEnabled ? wallpaper.mWhich : FLAG_SYSTEM;
            if (wallpaper.wallpaperComponent != null) {
                int change = isPackageDisappearing(wallpaper.wallpaperComponent
                        .getPackageName());
@@ -1571,7 +1569,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                    if (doit) {
                        Slog.w(TAG, "Wallpaper uninstalled, removing: "
                                + wallpaper.wallpaperComponent);
                        clearWallpaperLocked(FLAG_SYSTEM, wallpaper.userId, null);
                        clearWallpaperLocked(which, wallpaper.userId, null);
                    }
                }
            }
@@ -1592,7 +1590,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                } catch (NameNotFoundException e) {
                    Slog.w(TAG, "Wallpaper component gone, removing: "
                            + wallpaper.wallpaperComponent);
                    clearWallpaperLocked(FLAG_SYSTEM, wallpaper.userId, null);
                    clearWallpaperLocked(which, wallpaper.userId, null);
                }
            }
            if (wallpaper.nextWallpaperComponent != null
@@ -1708,7 +1706,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                if (DEBUG) {
                    Slog.i(TAG, "Unable to regenerate crop; resetting");
                }
                clearWallpaperLocked(FLAG_SYSTEM, UserHandle.USER_SYSTEM, null);
                int which = isLockscreenLiveWallpaperEnabled() ? wallpaper.mWhich : FLAG_SYSTEM;
                clearWallpaperLocked(which, UserHandle.USER_SYSTEM, null);
            }
        } else {
            if (DEBUG) {
@@ -1988,12 +1987,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            WallpaperData wallpaper, IRemoteCallback reply, ServiceInfo serviceInfo) {

        if (serviceInfo == null) {
            if (wallpaper.mWhich == (FLAG_LOCK | FLAG_SYSTEM)) {
                clearWallpaperLocked(FLAG_SYSTEM, wallpaper.userId, null);
                clearWallpaperLocked(FLAG_LOCK, wallpaper.userId, reply);
            } else {
            clearWallpaperLocked(wallpaper.mWhich, wallpaper.userId, reply);
            }
            return;
        }
        Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked");
@@ -2014,7 +2008,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub

    @Override
    public void clearWallpaper(String callingPackage, int which, int userId) {
        if (DEBUG) Slog.v(TAG, "clearWallpaper");
        if (DEBUG) Slog.v(TAG, "clearWallpaper: " + which);
        checkPermission(android.Manifest.permission.SET_WALLPAPER);
        if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
            return;
@@ -2025,7 +2019,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        WallpaperData data = null;
        synchronized (mLock) {
            if (mIsLockscreenLiveWallpaperEnabled) {
                clearWallpaperLocked(callingPackage, which, userId);
                boolean fromForeground = isFromForegroundApp(callingPackage);
                clearWallpaperLocked(which, userId, fromForeground, null);
            } else {
                clearWallpaperLocked(which, userId, null);
            }
@@ -2045,7 +2040,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        }
    }

    private void clearWallpaperLocked(String callingPackage, int which, int userId) {
    private void clearWallpaperLocked(int which, int userId, boolean fromForeground,
            IRemoteCallback reply) {

        // Might need to bring it in the first time to establish our rewrite
        if (!mWallpaperMap.contains(userId)) {
@@ -2084,8 +2080,10 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                finalWhich = which;
            }

            boolean success = withCleanCallingIdentity(() -> setWallpaperComponent(
                    component, callingPackage, finalWhich, userId));
            // except for the lock case (for which we keep the system wallpaper as-is), force rebind
            boolean force = which != FLAG_LOCK;
            boolean success = withCleanCallingIdentity(() -> setWallpaperComponentInternal(
                    component, finalWhich, userId, force, fromForeground, reply));
            if (success) return;
        } catch (IllegalArgumentException e1) {
            e = e1;
@@ -2097,10 +2095,23 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        // wallpaper.
        Slog.e(TAG, "Default wallpaper component not found!", e);
        withCleanCallingIdentity(() -> clearWallpaperComponentLocked(wallpaper));
        if (reply != null) {
            try {
                reply.sendResult(null);
            } catch (RemoteException e1) {
                Slog.w(TAG, "Failed to notify callback after wallpaper clear", e1);
            }
        }
    }

    // TODO(b/266818039) remove this version of the method
    // TODO(b/266818039) remove
    private void clearWallpaperLocked(int which, int userId, IRemoteCallback reply) {

        if (mIsLockscreenLiveWallpaperEnabled) {
            clearWallpaperLocked(which, userId, false, reply);
            return;
        }

        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to clear");
        }
@@ -2815,6 +2826,18 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                : new WallpaperData[0];
    }

    // TODO(b/266818039) remove
    private WallpaperData[] getWallpapers() {
        WallpaperData systemWallpaper = mWallpaperMap.get(mCurrentUserId);
        WallpaperData lockWallpaper = mLockWallpaperMap.get(mCurrentUserId);
        boolean systemValid = systemWallpaper != null;
        boolean lockValid = lockWallpaper != null && !isLockscreenLiveWallpaperEnabled();
        return systemValid && lockValid ? new WallpaperData[]{systemWallpaper, lockWallpaper}
                : systemValid ? new WallpaperData[]{systemWallpaper}
                : lockValid ? new WallpaperData[]{lockWallpaper}
                : new WallpaperData[0];
    }

    private IWallpaperEngine getEngine(int which, int userId, int displayId) {
        WallpaperData wallpaperData = findWallpaperAtDisplay(userId, displayId);
        if (wallpaperData == null) return null;
@@ -3272,15 +3295,16 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
    boolean setWallpaperComponent(ComponentName name, String callingPackage,
            @SetWallpaperFlags int which, int userId) {
        if (mIsLockscreenLiveWallpaperEnabled) {
            return setWallpaperComponentInternal(name, callingPackage, which, userId);
            boolean fromForeground = isFromForegroundApp(callingPackage);
            return setWallpaperComponentInternal(name, which, userId, false, fromForeground, null);
        } else {
            setWallpaperComponentInternalLegacy(name, callingPackage, which, userId);
            return true;
        }
    }

    private boolean setWallpaperComponentInternal(ComponentName name, String callingPackage,
            @SetWallpaperFlags int which, int userIdIn) {
    private boolean setWallpaperComponentInternal(ComponentName name,  @SetWallpaperFlags int which,
            int userIdIn, boolean force, boolean fromForeground, IRemoteCallback reply) {
        if (DEBUG) {
            Slog.v(TAG, "Setting new live wallpaper: which=" + which + ", component: " + name);
        }
@@ -3294,7 +3318,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        final WallpaperData newWallpaper;

        synchronized (mLock) {
            Slog.v(TAG, "setWallpaperComponent name=" + name);
            Slog.v(TAG, "setWallpaperComponent name=" + name + ", which = " + which);
            final WallpaperData originalSystemWallpaper = mWallpaperMap.get(userId);
            if (originalSystemWallpaper == null) {
                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
@@ -3317,29 +3341,21 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                newWallpaper.imageWallpaperPending = false;
                newWallpaper.mWhich = which;
                newWallpaper.mSystemWasBoth = systemIsBoth;
                newWallpaper.fromForegroundApp = isFromForegroundApp(callingPackage);
                newWallpaper.fromForegroundApp = fromForeground;
                final WallpaperDestinationChangeHandler
                        liveSync = new WallpaperDestinationChangeHandler(
                        newWallpaper);
                boolean same = changingToSame(name, newWallpaper);
                IRemoteCallback.Stub callback = new IRemoteCallback.Stub() {
                    @Override
                    public void sendResult(Bundle data) throws RemoteException {
                        if (DEBUG) {
                            Slog.d(TAG, "publish system wallpaper changed!");
                        }
                    }
                };

                /*
                 * If we have a shared system+lock wallpaper, and we reapply the same wallpaper
                 * to system only, force rebind: the current wallpaper will be migrated to lock
                 * and a new engine with the same wallpaper will be applied to system.
                 */
                boolean forceRebind = same && systemIsBoth && which == FLAG_SYSTEM;
                boolean forceRebind = force || (same && systemIsBoth && which == FLAG_SYSTEM);

                bindSuccess = bindWallpaperComponentLocked(name, /* force */
                        forceRebind, /* fromUser */ true, newWallpaper, callback);
                        forceRebind, /* fromUser */ true, newWallpaper, reply);
                if (bindSuccess) {
                    if (!same) {
                        newWallpaper.primaryColors = null;
@@ -3409,7 +3425,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        WallpaperData wallpaper;

        synchronized (mLock) {
            Slog.v(TAG, "setWallpaperComponent name=" + name + ", which=" + which);
            Slog.v(TAG, "setWallpaperComponentLegacy name=" + name + ", which=" + which);
            wallpaper = mWallpaperMap.get(userId);
            if (wallpaper == null) {
                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);