Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.wallpaper.picker; |
| 17 | |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 18 | import android.app.WallpaperColors; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.os.Bundle; |
| 21 | import android.os.Message; |
| 22 | import android.os.RemoteException; |
Santiago Etchebehere | 616808f | 2021-05-19 16:10:07 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 24 | import android.view.Surface; |
| 25 | import android.view.SurfaceHolder; |
| 26 | import android.view.SurfaceView; |
| 27 | |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 28 | import androidx.annotation.Nullable; |
| 29 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 30 | import com.android.wallpaper.R; |
| 31 | import com.android.wallpaper.util.PreviewUtils; |
| 32 | import com.android.wallpaper.util.SurfaceViewUtils; |
| 33 | |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 34 | import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 36 | /** A surface holder callback that renders user's workspace on the passed in surface view. */ |
| 37 | public class WorkspaceSurfaceHolderCallback implements SurfaceHolder.Callback { |
| 38 | |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Listener to be called when workspace surface is updated with a new Surface Package. |
| 41 | */ |
| 42 | public interface WorkspaceRenderListener { |
| 43 | /** |
| 44 | * Called on the main thread after the workspace surface is updated from the provider |
| 45 | */ |
| 46 | void onWorkspaceRendered(); |
| 47 | } |
| 48 | |
Santiago Etchebehere | 616808f | 2021-05-19 16:10:07 -0700 | [diff] [blame] | 49 | private static final String TAG = "WsSurfaceHolderCallback"; |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 50 | private static final String KEY_WALLPAPER_COLORS = "wallpaper_colors"; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 51 | private final SurfaceView mWorkspaceSurface; |
| 52 | private final PreviewUtils mPreviewUtils; |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 53 | private final boolean mShouldUseWallpaperColors; |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 54 | private final AtomicBoolean mRequestPending = new AtomicBoolean(false); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 55 | |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 56 | private WallpaperColors mWallpaperColors; |
| 57 | private boolean mIsWallpaperColorsReady; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 58 | private Surface mLastSurface; |
| 59 | private Message mCallback; |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 60 | private WorkspaceRenderListener mListener; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 61 | |
Tracy Zhou | a63c986 | 2020-06-22 01:05:14 -0700 | [diff] [blame] | 62 | private boolean mNeedsToCleanUp; |
| 63 | |
chihhangchuang | c0e80c1 | 2020-06-05 16:48:49 +0800 | [diff] [blame] | 64 | public WorkspaceSurfaceHolderCallback(SurfaceView workspaceSurface, Context context) { |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 65 | this(workspaceSurface, context, false); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a new instance of {@link WorkspaceSurfaceHolderCallback} specifying if wallpaper |
| 70 | * colors should be used to preview the workspace. |
| 71 | * |
| 72 | * @param shouldUseWallpaperColors if true, the workspace preview won't be requested until both |
| 73 | * the surface is created and wallpaper colors are set via |
| 74 | * {@link #setWallpaperColors(WallpaperColors)} |
| 75 | */ |
| 76 | public WorkspaceSurfaceHolderCallback(SurfaceView workspaceSurface, Context context, |
| 77 | boolean shouldUseWallpaperColors) { |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 78 | mWorkspaceSurface = workspaceSurface; |
| 79 | mPreviewUtils = new PreviewUtils(context, |
| 80 | context.getString(R.string.grid_control_metadata_name)); |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 81 | mShouldUseWallpaperColors = shouldUseWallpaperColors; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void surfaceCreated(SurfaceHolder holder) { |
| 86 | if (mPreviewUtils.supportsPreview() && mLastSurface != holder.getSurface()) { |
| 87 | mLastSurface = holder.getSurface(); |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 88 | maybeRenderPreview(); |
| 89 | } |
| 90 | } |
Tracy Zhou | a63c986 | 2020-06-22 01:05:14 -0700 | [diff] [blame] | 91 | |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Set the current wallpaper's colors. This method must be called if this instance was created |
Santiago Etchebehere | 6228729 | 2021-04-30 15:53:22 -0700 | [diff] [blame] | 94 | * with shouldUseWallpaperColors = true (even with {@code null} colors), and conversely, calling |
| 95 | * this method when {@code shouldUseWallpaperColors = false} will be a no-op. |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 96 | * |
| 97 | * @param colors WallpaperColors extracted from the current wallpaper preview, or {@code null} |
| 98 | * if none are available. |
| 99 | * @see #WorkspaceSurfaceHolderCallback(SurfaceView, Context, boolean) |
| 100 | */ |
| 101 | public void setWallpaperColors(@Nullable WallpaperColors colors) { |
Santiago Etchebehere | 6228729 | 2021-04-30 15:53:22 -0700 | [diff] [blame] | 102 | if (!mShouldUseWallpaperColors) { |
| 103 | return; |
| 104 | } |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 105 | mWallpaperColors = colors; |
| 106 | mIsWallpaperColorsReady = true; |
| 107 | maybeRenderPreview(); |
| 108 | } |
| 109 | |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 110 | public void setListener(WorkspaceRenderListener listener) { |
| 111 | mListener = listener; |
| 112 | } |
| 113 | |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 114 | private void maybeRenderPreview() { |
| 115 | if ((mShouldUseWallpaperColors && !mIsWallpaperColorsReady) || mLastSurface == null) { |
| 116 | return; |
| 117 | } |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 118 | |
| 119 | mRequestPending.set(true); |
Santiago Etchebehere | f99ff20 | 2021-06-01 15:19:22 -0700 | [diff] [blame] | 120 | requestPreview(mWorkspaceSurface, (result) -> { |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 121 | mRequestPending.set(false); |
| 122 | if (result != null && mLastSurface != null) { |
Santiago Etchebehere | 3e45291 | 2021-06-07 18:10:30 -0700 | [diff] [blame] | 123 | mWorkspaceSurface.setChildSurfacePackage( |
| 124 | SurfaceViewUtils.getSurfacePackage(result)); |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 125 | |
Ching-Sung Li | 724a03c | 2021-05-27 16:05:05 +0800 | [diff] [blame] | 126 | mCallback = SurfaceViewUtils.getCallback(result); |
| 127 | |
| 128 | if (mNeedsToCleanUp) { |
| 129 | cleanUp(); |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 130 | } else if (mListener != null) { |
| 131 | mListener.onWorkspaceRendered(); |
Ching-Sung Li | 724a03c | 2021-05-27 16:05:05 +0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | }); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } |
| 139 | |
| 140 | @Override |
Santiago Etchebehere | f99ff20 | 2021-06-01 15:19:22 -0700 | [diff] [blame] | 141 | public void surfaceDestroyed(SurfaceHolder holder) { |
Santiago Etchebehere | f99ff20 | 2021-06-01 15:19:22 -0700 | [diff] [blame] | 142 | } |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 143 | |
| 144 | public void cleanUp() { |
| 145 | if (mCallback != null) { |
| 146 | try { |
| 147 | mCallback.replyTo.send(mCallback); |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 148 | mNeedsToCleanUp = false; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 149 | } catch (RemoteException e) { |
Santiago Etchebehere | 616808f | 2021-05-19 16:10:07 -0700 | [diff] [blame] | 150 | Log.w(TAG, "Couldn't call cleanup on workspace preview", e); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 151 | } finally { |
| 152 | mCallback = null; |
| 153 | } |
Tracy Zhou | a63c986 | 2020-06-22 01:05:14 -0700 | [diff] [blame] | 154 | } else { |
Santiago Etchebehere | 8b49567 | 2021-05-29 11:37:46 -0700 | [diff] [blame] | 155 | if (mRequestPending.get()) { |
| 156 | mNeedsToCleanUp = true; |
| 157 | } |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
chihhangchuang | c0e80c1 | 2020-06-05 16:48:49 +0800 | [diff] [blame] | 160 | |
| 161 | public void resetLastSurface() { |
| 162 | mLastSurface = null; |
| 163 | } |
| 164 | |
Santiago Etchebehere | f99ff20 | 2021-06-01 15:19:22 -0700 | [diff] [blame] | 165 | protected void requestPreview(SurfaceView workspaceSurface, |
| 166 | PreviewUtils.WorkspacePreviewCallback callback) { |
James O'Leary | 0c26b0e | 2021-06-21 09:45:24 -0400 | [diff] [blame] | 167 | if (workspaceSurface.getDisplay() == null) { |
| 168 | Log.w(TAG, |
| 169 | "No display ID, avoiding asking for workspace preview, lest WallpaperPicker " |
| 170 | + "crash"); |
| 171 | return; |
| 172 | } |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 173 | Bundle request = SurfaceViewUtils.createSurfaceViewRequest(workspaceSurface); |
| 174 | if (mWallpaperColors != null) { |
| 175 | request.putParcelable(KEY_WALLPAPER_COLORS, mWallpaperColors); |
| 176 | } |
Santiago Etchebehere | f99ff20 | 2021-06-01 15:19:22 -0700 | [diff] [blame] | 177 | mPreviewUtils.renderPreview(request, callback); |
chihhangchuang | c0e80c1 | 2020-06-05 16:48:49 +0800 | [diff] [blame] | 178 | } |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 179 | } |