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

Commit 1e68053f authored by Aurélien Pomini's avatar Aurélien Pomini
Browse files

Reland "use new clearWallpaperLocked method everywhere"

Reland of ag/23728650

The old clear method had a lot of references. Replace these references
with a call to the new clear method. Now, everything that tries to clear
the wallpaper will call setWallpaperComponent(..) and not directly
bindWallpaperComponentLocked.

We add two new arguments to setWallpaperComponentInternal: a callback,
and a boolean to forceRebind. Clearing will call
setWallpaperComponentInternal with forceRebind = true unless we're
clearing FLAG_LOCK only, in which case we just reuse the existing home
wallpaper without rebinding anything.

Flag: lockscreen live wallpaper
Bug: 298366419
Test: atest WallpaperManagerTest
Change-Id: Iba71f250b59b08814033eb9364613c96b15ea966
parent 11d6424a
Loading
Loading
Loading
Loading
+33 −28
Original line number Diff line number Diff line
@@ -1988,12 +1988,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 +2009,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 +2020,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 +2041,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 +2081,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 +2096,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");
        }
@@ -3272,15 +3284,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 +3307,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 +3330,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 +3414,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);