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

Commit 6e430b6c authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

Fix keyguard wallpaper issues



- separate file observers for proper bookkeeping
- cache keyguard wallpaper in PhoneStatusBar so we don't query it and
  generate unnecessary binder calls every time updateMetaData() gets
  called (which happens a lot)
- more consistency with regular wallpaper logic

REF: NIGHTLIES-1429, CYNGNOS-386
Change-Id: I0bf7d5ffdf8972cf10ba1a90ae9c5d3f9a6a9aab
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent be2106da
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -341,6 +341,10 @@ public class WallpaperManager {
        }

        private Bitmap getCurrentKeyguardWallpaperLocked(Context context) {
            if (mService == null) {
                Log.w(TAG, "WallpaperService not running");
                return null;
            }
            try {
                Bundle params = new Bundle();
                ParcelFileDescriptor fd = mService.getKeyguardWallpaper(this, params);
+12 −1
Original line number Diff line number Diff line
@@ -360,6 +360,8 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
    private long mKeyguardFadingAwayDelay;
    private long mKeyguardFadingAwayDuration;

    private Bitmap mKeyguardWallpaper;

    int mKeyguardMaxNotificationCount;

    // carrier/wifi label
@@ -868,6 +870,11 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                    Settings.Global.getUriFor(SETTING_HEADS_UP_TICKER), true,
                    mHeadsUpObserver);
        }

        WallpaperManager wm = (WallpaperManager) mContext.getSystemService(
                Context.WALLPAPER_SERVICE);
        mKeyguardWallpaper = wm.getKeyguardBitmap();

        mUnlockMethodCache = UnlockMethodCache.getInstance(mContext);
        mUnlockMethodCache.addListener(this);
        startKeyguard();
@@ -2293,7 +2300,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
            WallpaperManager wm = (WallpaperManager)
                    mContext.getSystemService(Context.WALLPAPER_SERVICE);
            if (wm != null) {
                backdropBitmap = wm.getKeyguardBitmap();
                backdropBitmap = mKeyguardWallpaper;
            }
        }

@@ -3722,6 +3729,9 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                    updateMediaMetaData(true);
                }
            } else if (Intent.ACTION_KEYGUARD_WALLPAPER_CHANGED.equals(action)) {
                WallpaperManager wm = (WallpaperManager) mContext.getSystemService(
                        Context.WALLPAPER_SERVICE);
                mKeyguardWallpaper = wm.getKeyguardBitmap();
                updateMediaMetaData(true);
            }
        }
@@ -3792,6 +3802,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
        WallpaperManager wm = (WallpaperManager)
                mContext.getSystemService(Context.WALLPAPER_SERVICE);
        wm.forgetLoadedKeyguardWallpaper();
        mKeyguardWallpaper = wm.getKeyguardBitmap();
        updateMediaMetaData(true);

        if (mNavigationBarView != null) {
+72 −37
Original line number Diff line number Diff line
@@ -116,21 +116,17 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
    private class WallpaperObserver extends FileObserver {

        final WallpaperData mWallpaper;
        final KeyguardWallpaperData mKeyguardWallpaper;
        final File mWallpaperDir;
        final File mWallpaperFile;
        final File mWallpaperInfoFile;
        final File mKeyguardWallpaperFile;

        public WallpaperObserver(WallpaperData wallpaper, KeyguardWallpaperData keyguardWallpaper) {
        public WallpaperObserver(WallpaperData wallpaper) {
            super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
                    CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF);
            mWallpaperDir = getWallpaperDir(wallpaper.userId);
            mWallpaper = wallpaper;
            mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
            mWallpaperInfoFile = new File(mWallpaperDir, WALLPAPER_INFO);
            mKeyguardWallpaper = keyguardWallpaper;
            mKeyguardWallpaperFile = new File(mWallpaperDir, KEYGUARD_WALLPAPER);
        }

        @Override
@@ -149,12 +145,6 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
                    Binder.restoreCallingIdentity(origId);
                }
                if (mWallpaperFile.equals(changedFile)) {
                    // changing the wallpaper means we'll need to back up the new one
                    long origId = Binder.clearCallingIdentity();
                    BackupManager bm = new BackupManager(mContext);
                    bm.dataChanged();
                    Binder.restoreCallingIdentity(origId);

                    notifyCallbacksLocked(mWallpaper);
                    final boolean written = (event == CLOSE_WRITE || event == MOVED_TO);
                    if (mWallpaper.wallpaperComponent == null
@@ -167,11 +157,40 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
                                false, mWallpaper, null);
                        saveSettingsLocked(mWallpaper);
                    }
                } else if (mKeyguardWallpaperFile.equals(changedFile)) {
                }
            }
        }
    }

    private class KeyguardWallpaperObserver extends FileObserver {

        final KeyguardWallpaperData mKeyguardWallpaper;
        final File mWallpaperDir;
        final File mKeyguardWallpaperFile;

        public KeyguardWallpaperObserver(KeyguardWallpaperData keyguardWallpaper) {
            super(getWallpaperDir(keyguardWallpaper.userId).getAbsolutePath(),
                    CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF);
            mWallpaperDir = getWallpaperDir(keyguardWallpaper.userId);
            mKeyguardWallpaper = keyguardWallpaper;
            mKeyguardWallpaperFile = new File(mWallpaperDir, KEYGUARD_WALLPAPER);
        }

        @Override
        public void onEvent(int event, String path) {
            if (path == null) {
                return;
            }
            synchronized (mLock) {
                File changedFile = new File(mWallpaperDir, path);
                final boolean written = (event == CLOSE_WRITE || event == MOVED_TO);
                if (mKeyguardWallpaperFile.equals(changedFile)) {
                    notifyCallbacksLocked(mKeyguardWallpaper);
                    if (event == CLOSE_WRITE
                    if (written
                            || mKeyguardWallpaper.imageWallpaperPending) {
                        if (written) {
                            mKeyguardWallpaper.imageWallpaperPending = false;
                        }
                        saveSettingsLocked(mKeyguardWallpaper);
                    }
                }
@@ -261,6 +280,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
         */
        String name = "";

        KeyguardWallpaperObserver keyguardWallpaperObserver;

        /**
         * List of callbacks registered they should each be notified when the wallpaper is changed.
         */
@@ -548,6 +569,10 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
            WallpaperData wallpaper = mWallpaperMap.valueAt(i);
            wallpaper.wallpaperObserver.stopWatching();
        }
        for (int i = 0; i < mKeyguardWallpaperMap.size(); i++) {
            KeyguardWallpaperData wallpaper = mKeyguardWallpaperMap.valueAt(i);
            wallpaper.keyguardWallpaperObserver.stopWatching();
        }
    }

    public void systemRunning() {
@@ -555,9 +580,13 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
        WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER);
        KeyguardWallpaperData keyguardWallpaper = mKeyguardWallpaperMap.get(UserHandle.USER_OWNER);
        switchWallpaper(wallpaper, null);
        wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper, keyguardWallpaper);
        wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
        wallpaper.wallpaperObserver.startWatching();

        keyguardWallpaper.keyguardWallpaperObserver
                = new KeyguardWallpaperObserver(keyguardWallpaper);
        keyguardWallpaper.keyguardWallpaperObserver.startWatching();

        IntentFilter userFilter = new IntentFilter();
        userFilter.addAction(Intent.ACTION_USER_REMOVED);
        userFilter.addAction(Intent.ACTION_USER_STOPPING);
@@ -617,6 +646,13 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
                    wallpaper.wallpaperObserver = null;
                }
                mWallpaperMap.remove(userId);
            }
            KeyguardWallpaperData kgwallpaper = mKeyguardWallpaperMap.get(userId);
            if (kgwallpaper != null) {
                if (kgwallpaper.keyguardWallpaperObserver != null) {
                    kgwallpaper.keyguardWallpaperObserver.stopWatching();
                    kgwallpaper.keyguardWallpaperObserver = null;
                }
                mKeyguardWallpaperMap.remove(userId);
            }
        }
@@ -655,9 +691,14 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
            }
            // Not started watching yet, in case wallpaper data was loaded for other reasons.
            if (wallpaper.wallpaperObserver == null) {
                wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper, keygaurdWallpaper);
                wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
                wallpaper.wallpaperObserver.startWatching();
            }
            if (keygaurdWallpaper.keyguardWallpaperObserver == null) {
                keygaurdWallpaper.keyguardWallpaperObserver
                        = new KeyguardWallpaperObserver(keygaurdWallpaper);
                keygaurdWallpaper.keyguardWallpaperObserver.startWatching();
            }
            switchWallpaper(wallpaper, reply);
        }
    }
@@ -730,13 +771,14 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
    public void clearKeyguardWallpaper() {
        if (DEBUG) Slog.v(TAG, "clearWallpaper");
        synchronized (mLock) {
            clearKeyguardWallpaperLocked(UserHandle.getCallingUserId(), null);
            clearKeyguardWallpaperLocked(UserHandle.getCallingUserId());
        }
    }

    void clearKeyguardWallpaperLocked(int userId, IRemoteCallback reply) {
        KeyguardWallpaperData wallpaper = mKeyguardWallpaperMap.get(userId);
    void clearKeyguardWallpaperLocked(int userId) {
        final long ident = Binder.clearCallingIdentity();
        try {
            KeyguardWallpaperData wallpaper = mKeyguardWallpaperMap.get(userId);
            wallpaper.imageWallpaperPending = false;
            wallpaper.height = -1;
            wallpaper.width = -1;
@@ -746,15 +788,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
            if (f.exists()) {
                f.delete();
            }
        if (userId != mCurrentUserId)
            return;
        } finally {
            Binder.restoreCallingIdentity(ident);

        if (reply != null) {
            try {
                reply.sendResult(null);
            } catch (RemoteException e1) {
            }
        }
    }

@@ -921,15 +956,15 @@ public class WallpaperManagerService extends IWallpaperManager.Stub {
                    outParams.putInt("width", wallpaper.width);
                    outParams.putInt("height", wallpaper.height);
                }
                wallpaper.callbacks.register(cb);
                File f = new File(getWallpaperDir(wallpaperUserId), KEYGUARD_WALLPAPER);
                if (!f.exists()) {
                    return null;
                }
                wallpaper.callbacks.register(cb, new UserHandle(wallpaperUserId));
                return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
            } catch (FileNotFoundException e) {
                /* Shouldn't happen as we check to see if the file exists */
                Slog.w(TAG, "Error getting wallpaper", e);
                Slog.w(TAG, "Error getting keyguard wallpaper", e);
            }
            return null;
        }