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

Commit 94a66aac authored by Wu Ahan's avatar Wu Ahan Committed by Automerger Merge Worker
Browse files

Merge "Fix temporary black wallpaper after setting a new image wallpaper" into...

Merge "Fix temporary black wallpaper after setting a new image wallpaper" into sc-v2-dev am: 14dbc9fa

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15372157

Change-Id: Ia2eec16346b7cf15ed51e80a61b47f7031be27ba
parents a85e702b 14dbc9fa
Loading
Loading
Loading
Loading
+58 −0
Original line number Diff line number Diff line
@@ -573,6 +573,53 @@ public class WallpaperManager {
            return null;
        }

        public Rect peekWallpaperDimensions(Context context, boolean returnDefault, int userId) {
            if (mService != null) {
                try {
                    if (!mService.isWallpaperSupported(context.getOpPackageName())) {
                        return new Rect();
                    }
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }

            Rect dimensions = null;
            synchronized (this) {
                try {
                    Bundle params = new Bundle();
                    // Let's peek user wallpaper first.
                    ParcelFileDescriptor pfd = mService.getWallpaperWithFeature(
                            context.getOpPackageName(), context.getAttributionTag(), this,
                            FLAG_SYSTEM, params, userId);
                    if (pfd != null) {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, options);
                        dimensions = new Rect(0, 0, options.outWidth, options.outHeight);
                    }
                } catch (RemoteException ex) {
                    Log.w(TAG, "peek wallpaper dimensions failed", ex);
                }
            }
            // If user wallpaper is unavailable, may be the default one instead.
            if ((dimensions == null || dimensions.width() == 0 || dimensions.height() == 0)
                    && returnDefault) {
                InputStream is = openDefaultWallpaper(context, FLAG_SYSTEM);
                if (is != null) {
                    try {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeStream(is, null, options);
                        dimensions = new Rect(0, 0, options.outWidth, options.outHeight);
                    } finally {
                        IoUtils.closeQuietly(is);
                    }
                }
            }
            return dimensions;
        }

        void forgetLoadedWallpaper() {
            synchronized (this) {
                mCachedWallpaper = null;
@@ -1052,6 +1099,17 @@ public class WallpaperManager {
        return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId, hardware, cmProxy);
    }

    /**
     * Peek the dimensions of system wallpaper of the user without decoding it.
     *
     * @return the dimensions of system wallpaper
     * @hide
     */
    public Rect peekBitmapDimensions() {
        return sGlobals.peekWallpaperDimensions(
                mContext, true /* returnDefault */, mContext.getUserId());
    }

    /**
     * Get an open, readable file descriptor to the given wallpaper image file.
     * The caller is responsible for closing the file descriptor when done ingesting the file.
+37 −3
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

@@ -158,6 +159,7 @@ public abstract class WallpaperService extends Service {
    private static final int MSG_REQUEST_WALLPAPER_COLORS = 10050;
    private static final int MSG_ZOOM = 10100;
    private static final int MSG_SCALE_PREVIEW = 10110;
    private static final int MSG_REPORT_SHOWN = 10150;
    private static final List<Float> PROHIBITED_STEPS = Arrays.asList(0f, Float.POSITIVE_INFINITY,
            Float.NEGATIVE_INFINITY);

@@ -539,6 +541,35 @@ public abstract class WallpaperService extends Service {
            return false;
        }

        /**
         * This will be called in the end of {@link #updateSurface(boolean, boolean, boolean)}.
         * If true is returned, the engine will not report shown until rendering finished is
         * reported. Otherwise, the engine will report shown immediately right after redraw phase
         * in {@link #updateSurface(boolean, boolean, boolean)}.
         *
         * @hide
         */
        public boolean shouldWaitForEngineShown() {
            return false;
        }

        /**
         * Reports the rendering is finished, stops waiting, then invokes
         * {@link IWallpaperEngineWrapper#reportShown()}.
         *
         * @hide
         */
        public void reportEngineShown(boolean waitForEngineShown) {
            if (mIWallpaperEngine.mShownReported) return;
            Message message = mCaller.obtainMessage(MSG_REPORT_SHOWN);
            if (!waitForEngineShown) {
                mCaller.removeMessages(MSG_REPORT_SHOWN);
                mCaller.sendMessage(message);
            } else {
                mCaller.sendMessageDelayed(message, TimeUnit.SECONDS.toMillis(1));
            }
        }

        /**
         * Control whether this wallpaper will receive raw touch events
         * from the window manager as the user interacts with the window
@@ -943,7 +974,8 @@ public abstract class WallpaperService extends Service {

        void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) {
            if (mDestroyed) {
                Log.w(TAG, "Ignoring updateSurface: destroyed");
                Log.w(TAG, "Ignoring updateSurface due to destroyed");
                return;
            }

            boolean fixedSize = false;
@@ -1210,7 +1242,6 @@ public abstract class WallpaperService extends Service {
                                        + this);
                            onVisibilityChanged(false);
                        }

                    } finally {
                        mIsCreating = false;
                        mSurfaceCreated = true;
@@ -1220,7 +1251,7 @@ public abstract class WallpaperService extends Service {
                            processLocalColors(mPendingXOffset, mPendingXOffsetStep);
                        }
                        reposition();
                        mIWallpaperEngine.reportShown();
                        reportEngineShown(shouldWaitForEngineShown());
                    }
                } catch (RemoteException ex) {
                }
@@ -2379,6 +2410,9 @@ public abstract class WallpaperService extends Service {
                        // Connection went away, nothing to do in here.
                    }
                } break;
                case MSG_REPORT_SHOWN: {
                    reportShown();
                } break;
                default :
                    Log.w(TAG, "Unknown message type " + message.what);
            }
+14 −2
Original line number Diff line number Diff line
@@ -120,6 +120,7 @@ public class ImageWallpaper extends WallpaperService {

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            Trace.beginSection("ImageWallpaper.Engine#onCreate");
            mEglHelper = getEglHelperInstance();
            // Deferred init renderer because we need to get wallpaper by display context.
            mRenderer = getRendererInstance();
@@ -129,6 +130,7 @@ public class ImageWallpaper extends WallpaperService {
            mRenderer.setOnBitmapChanged(this::updateMiniBitmap);
            getDisplayContext().getSystemService(DisplayManager.class)
                    .registerDisplayListener(this, mWorker.getThreadHandler());
            Trace.endSection();
        }

        @Override
@@ -185,11 +187,13 @@ public class ImageWallpaper extends WallpaperService {
        }

        private void updateSurfaceSize() {
            Trace.beginSection("ImageWallpaper#updateSurfaceSize");
            SurfaceHolder holder = getSurfaceHolder();
            Size frameSize = mRenderer.reportSurfaceSize();
            int width = Math.max(MIN_SURFACE_WIDTH, frameSize.getWidth());
            int height = Math.max(MIN_SURFACE_HEIGHT, frameSize.getHeight());
            holder.setFixedSize(width, height);
            Trace.endSection();
        }

        @Override
@@ -197,6 +201,11 @@ public class ImageWallpaper extends WallpaperService {
            return true;
        }

        @Override
        public boolean shouldWaitForEngineShown() {
            return true;
        }

        @Override
        public void onDestroy() {
            getDisplayContext().getSystemService(DisplayManager.class)
@@ -340,8 +349,10 @@ public class ImageWallpaper extends WallpaperService {
        public void onSurfaceCreated(SurfaceHolder holder) {
            if (mWorker == null) return;
            mWorker.getThreadHandler().post(() -> {
                Trace.beginSection("ImageWallpaper#onSurfaceCreated");
                mEglHelper.init(holder, needSupportWideColorGamut());
                mRenderer.onSurfaceCreated();
                Trace.endSection();
            });
        }

@@ -358,9 +369,11 @@ public class ImageWallpaper extends WallpaperService {
        }

        private void drawFrame() {
            Trace.beginSection("ImageWallpaper#drawFrame");
            preRender();
            requestRender();
            postRender();
            Trace.endSection();
        }

        public void preRender() {
@@ -425,9 +438,8 @@ public class ImageWallpaper extends WallpaperService {

        public void postRender() {
            // This method should only be invoked from worker thread.
            Trace.beginSection("ImageWallpaper#postRender");
            scheduleFinishRendering();
            Trace.endSection();
            reportEngineShown(false /* waitForEngineShown */);
        }

        private void cancelFinishRenderingTask() {
+5 −1
Original line number Diff line number Diff line
@@ -102,7 +102,6 @@ public class ImageWallpaperRenderer implements GLWallpaperRenderer {

    @Override
    public Size reportSurfaceSize() {
        mTexture.use(null /* consumer */);
        mSurfaceSize.set(mTexture.getTextureDimensions());
        return new Size(mSurfaceSize.width(), mSurfaceSize.height());
    }
@@ -124,6 +123,7 @@ public class ImageWallpaperRenderer implements GLWallpaperRenderer {
        private final WallpaperManager mWallpaperManager;
        private Bitmap mBitmap;
        private boolean mWcgContent;
        private boolean mTextureUsed;

        private WallpaperTexture(WallpaperManager wallpaperManager) {
            mWallpaperManager = wallpaperManager;
@@ -141,6 +141,7 @@ public class ImageWallpaperRenderer implements GLWallpaperRenderer {
                    mWallpaperManager.forgetLoadedWallpaper();
                    if (mBitmap != null) {
                        mDimensions.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
                        mTextureUsed = true;
                    } else {
                        Log.w(TAG, "Can't get bitmap");
                    }
@@ -171,6 +172,9 @@ public class ImageWallpaperRenderer implements GLWallpaperRenderer {
        }

        private Rect getTextureDimensions() {
            if (!mTextureUsed) {
                mDimensions.set(mWallpaperManager.peekBitmapDimensions());
            }
            return mDimensions;
        }

+32 −17
Original line number Diff line number Diff line
@@ -208,7 +208,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
     * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
     * every time the wallpaper is changed.
     */
    private class WallpaperObserver extends FileObserver {
    class WallpaperObserver extends FileObserver {

        final int mUserId;
        final WallpaperData mWallpaper;
@@ -226,7 +226,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG);
        }

        private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) {
        WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) {
            WallpaperData wallpaper = null;
            synchronized (mLock) {
                if (lockChanged) {
@@ -309,9 +309,18 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                            }
                            wallpaper.imageWallpaperPending = false;
                            if (sysWallpaperChanged) {
                                IRemoteCallback.Stub callback = new IRemoteCallback.Stub() {
                                    @Override
                                    public void sendResult(Bundle data) throws RemoteException {
                                        if (DEBUG) {
                                            Slog.d(TAG, "publish system wallpaper changed!");
                                        }
                                        notifyWallpaperChanged(wallpaper);
                                    }
                                };
                                // If this was the system wallpaper, rebind...
                                bindWallpaperComponentLocked(mImageWallpaper, true,
                                        false, wallpaper, null);
                                        false, wallpaper, callback);
                                notifyColorsWhich |= FLAG_SYSTEM;
                            }
                            if (lockWallpaperChanged
@@ -331,15 +340,9 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                            }

                            saveSettingsLocked(wallpaper.userId);

                            // Publish completion *after* we've persisted the changes
                            if (wallpaper.setComplete != null) {
                                try {
                                    wallpaper.setComplete.onWallpaperChanged();
                                } catch (RemoteException e) {
                                    // if this fails we don't really care; the setting app may just
                                    // have crashed and that sort of thing is a fact of life.
                                }
                            // Notify the client immediately if only lockscreen wallpaper changed.
                            if (lockWallpaperChanged && !sysWallpaperChanged) {
                                notifyWallpaperChanged(wallpaper);
                            }
                        }
                    }
@@ -353,6 +356,18 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        }
    }

    private void notifyWallpaperChanged(WallpaperData wallpaper) {
        // Publish completion *after* we've persisted the changes
        if (wallpaper.setComplete != null) {
            try {
                wallpaper.setComplete.onWallpaperChanged();
            } catch (RemoteException e) {
                // if this fails we don't really care; the setting app may just
                // have crashed and that sort of thing is a fact of life.
            }
        }
    }

    private void notifyLockWallpaperChanged() {
        final IWallpaperManagerCallback cb = mKeyguardListener;
        if (cb != null) {
@@ -364,7 +379,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        }
    }

    private void notifyWallpaperColorsChanged(@NonNull WallpaperData wallpaper, int which) {
    void notifyWallpaperColorsChanged(@NonNull WallpaperData wallpaper, int which) {
        if (wallpaper.connection != null) {
            wallpaper.connection.forEachDisplayConnector(connector -> {
                notifyWallpaperColorsChangedOnDisplay(wallpaper, which, connector.mDisplayId);
@@ -568,7 +583,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
     * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped
     * for display.
     */
    private void generateCrop(WallpaperData wallpaper) {
    void generateCrop(WallpaperData wallpaper) {
        boolean success = false;

        // Only generate crop for default display.
@@ -2834,7 +2849,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        return false;
    }

    private boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
    boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
            boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
        if (DEBUG_LIVE) {
            Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
@@ -3121,7 +3136,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        return new JournaledFile(new File(base), new File(base + ".tmp"));
    }

    private void saveSettingsLocked(int userId) {
    void saveSettingsLocked(int userId) {
        JournaledFile journal = makeJournaledFile(userId);
        FileOutputStream fstream = null;
        try {
@@ -3270,7 +3285,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
     * Important: this method loads settings to initialize the given user's wallpaper data if
     * there is no current in-memory state.
     */
    private WallpaperData getWallpaperSafeLocked(int userId, int which) {
    WallpaperData getWallpaperSafeLocked(int userId, int which) {
        // We're setting either just system (work with the system wallpaper),
        // both (also work with the system wallpaper), or just the lock
        // wallpaper (update against the existing lock wallpaper if any).
Loading