blob: c41d3949853d390c43d58540f1d2f3a058a7de44 [file] [log] [blame]
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -07001/*
2 * Copyright (C) 2019 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 */
16package com.android.wallpaper.picker;
17
Chihhang Chuang15169282021-06-07 20:25:18 +080018import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
19import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070020import static android.view.View.MeasureSpec.EXACTLY;
21import static android.view.View.MeasureSpec.makeMeasureSpec;
22
23import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY;
24import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT;
25import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION;
26
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070027import android.animation.Animator;
28import android.animation.AnimatorListenerAdapter;
29import android.app.Activity;
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +080030import android.app.WallpaperColors;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070031import android.content.Context;
Santiago Etchebeheref172aef2020-07-04 18:48:35 -070032import android.content.res.Resources;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070033import android.graphics.Bitmap;
James O'Learyd6458392021-06-21 09:39:51 -040034import android.graphics.BitmapFactory;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070035import android.graphics.Color;
James O'Learyd6458392021-06-21 09:39:51 -040036import android.graphics.ColorSpace;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070037import android.graphics.Point;
38import android.graphics.PointF;
39import android.graphics.Rect;
40import android.os.Bundle;
Santiago Etchebehere537a0d32021-06-30 14:55:50 +020041import android.os.Handler;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080042import android.util.Log;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070043import android.view.LayoutInflater;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070044import android.view.Surface;
45import android.view.SurfaceControlViewHost;
46import android.view.SurfaceHolder;
47import android.view.SurfaceView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070048import android.view.View;
49import android.view.ViewGroup;
Santiago Etchebehere8557c462020-12-02 18:41:40 -080050import android.view.ViewGroup.LayoutParams;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070051import android.widget.ImageView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070052
Chuck Liaofc1b8c92020-05-21 20:54:15 +080053import androidx.annotation.NonNull;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070054import androidx.annotation.Nullable;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070055import androidx.cardview.widget.CardView;
Tracy Zhou3eb234c2020-05-05 15:17:33 -070056import androidx.constraintlayout.widget.ConstraintLayout;
57import androidx.constraintlayout.widget.ConstraintSet;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070058import androidx.fragment.app.FragmentActivity;
59
60import com.android.wallpaper.R;
61import com.android.wallpaper.asset.Asset;
Chuck Liaodc424e82020-09-04 00:17:44 +080062import com.android.wallpaper.asset.CurrentWallpaperAssetVN;
“Chuckffd832c2020-03-22 02:15:58 +080063import com.android.wallpaper.model.WallpaperInfo;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080064import com.android.wallpaper.module.BitmapCropper;
65import com.android.wallpaper.module.InjectorProvider;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070066import com.android.wallpaper.module.WallpaperPersister.Destination;
67import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
Tianguang Zhang893187f2021-05-07 22:14:04 +020068import com.android.wallpaper.util.FullScreenAnimation;
Tianguang Zhangd3e4f632021-04-14 00:30:15 +020069import com.android.wallpaper.util.ResourceUtils;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070070import com.android.wallpaper.util.ScreenSizeCalculator;
Santiago Etchebehere53c63432020-05-07 18:55:35 -070071import com.android.wallpaper.util.SizeCalculator;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070072import com.android.wallpaper.util.WallpaperCropUtils;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070073import com.android.wallpaper.widget.BottomActionBar;
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +080074import com.android.wallpaper.widget.BottomActionBar.AccessibilityCallback;
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +080075import com.android.wallpaper.widget.LockScreenPreviewer;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070076
77import com.bumptech.glide.Glide;
78import com.bumptech.glide.MemoryCategory;
79import com.davemorrissey.labs.subscaleview.ImageSource;
80import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070081
James O'Learyd6458392021-06-21 09:39:51 -040082import java.io.ByteArrayOutputStream;
chihhangchuang923e29b2020-07-02 19:09:36 +080083import java.util.Locale;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070084import java.util.concurrent.ExecutionException;
Santiago Etchebehere537a0d32021-06-30 14:55:50 +020085import java.util.concurrent.Executor;
86import java.util.concurrent.Executors;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070087import java.util.concurrent.Future;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080088import java.util.concurrent.atomic.AtomicInteger;
chihhangchuang923e29b2020-07-02 19:09:36 +080089
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070090/**
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -080091 * Fragment which displays the UI for previewing an individual static wallpaper and its attribution
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070092 * information.
93 */
Chihhang Chuangc9c108a2021-04-28 18:35:14 +080094public class ImagePreviewFragment extends PreviewFragment {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070095
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080096 private static final String TAG = "ImagePreviewFragment";
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070097 private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f;
Santiago Etchebehere537a0d32021-06-30 14:55:50 +020098 private static final Executor sExecutor = Executors.newCachedThreadPool();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070099
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700100 private final WallpaperSurfaceCallback mWallpaperSurfaceCallback =
101 new WallpaperSurfaceCallback();
Tracy Zhouc5a6bad2020-05-20 23:28:22 -0700102
Santiago Etchebehere537a0d32021-06-30 14:55:50 +0200103 private final AtomicInteger mImageScaleChangeCounter = new AtomicInteger(0);
104 private final AtomicInteger mRecalculateColorCounter = new AtomicInteger(0);
105
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700106 private SubsamplingScaleImageView mFullResImageView;
107 private Asset mWallpaperAsset;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700108 private Point mScreenSize;
109 private Point mRawWallpaperSize; // Native size of wallpaper image.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700110 private ImageView mLowResImageView;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700111 private TouchForwardingLayout mTouchForwardingLayout;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700112 private ConstraintLayout mContainer;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700113 private SurfaceView mWallpaperSurface;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700114
Santiago Etchebehere62287292021-04-30 15:53:22 -0700115 protected SurfaceView mWorkspaceSurface;
Santiago Etchebeheredbdfb662021-04-26 10:25:13 -0700116 protected WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback;
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800117 protected ViewGroup mLockPreviewContainer;
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800118 protected LockScreenPreviewer mLockScreenPreviewer;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700119 private Future<Integer> mPlaceholderColorFuture;
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800120
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700121 @Override
122 public void onCreate(Bundle savedInstanceState) {
123 super.onCreate(savedInstanceState);
124 mWallpaperAsset = mWallpaper.getAsset(requireContext().getApplicationContext());
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700125 mPlaceholderColorFuture = mWallpaper.computePlaceholderColor(requireContext());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700126 }
127
128 @Override
129 protected int getLayoutResId() {
Ching-Sung Lid2f46be2021-06-22 20:55:28 +0800130 return R.layout.fragment_image_preview;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700131 }
132
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700133 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700134 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800135 Bundle savedInstanceState) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700136 View view = super.onCreateView(inflater, container, savedInstanceState);
137
138 Activity activity = requireActivity();
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700139 mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize(
140 activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700141
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700142 mContainer = view.findViewById(R.id.container);
143 mTouchForwardingLayout = mContainer.findViewById(R.id.touch_forwarding_layout);
Chihhang Chuang60a5bc22021-05-14 23:00:01 +0800144 mTouchForwardingLayout.setForwardingEnabled(true);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700145
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700146 // Set aspect ratio on the preview card dynamically.
147 ConstraintSet set = new ConstraintSet();
148 set.clone(mContainer);
chihhangchuang923e29b2020-07-02 19:09:36 +0800149 String ratio = String.format(Locale.US, "%d:%d", mScreenSize.x, mScreenSize.y);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700150 set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio);
151 set.applyTo(mContainer);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700152
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700153 mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface);
Santiago Etchebeheredbdfb662021-04-26 10:25:13 -0700154 mWorkspaceSurfaceCallback = createWorkspaceSurfaceCallback(mWorkspaceSurface);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700155 mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface);
chihhangchuang287d4b72020-06-25 01:09:09 +0800156 mLockPreviewContainer = mContainer.findViewById(R.id.lock_screen_preview_container);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700157 int placeHolderColor = ResourceUtils.getColorAttr(getContext(),
158 android.R.attr.colorBackground);
159 mWorkspaceSurface.setResizeBackgroundColor(placeHolderColor);
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800160 mLockScreenPreviewer = new LockScreenPreviewer(getLifecycle(), getContext(),
chihhangchuang287d4b72020-06-25 01:09:09 +0800161 mLockPreviewContainer);
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800162 mLockScreenPreviewer.setDateViewVisibility(!mFullScreenAnimation.isFullScreen());
163 mFullScreenAnimation.setFullScreenStatusListener(
164 isFullScreen -> mLockScreenPreviewer.setDateViewVisibility(!isFullScreen));
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800165 setUpTabs(view.findViewById(R.id.separated_tabs));
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700166
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700167 view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY),
168 makeMeasureSpec(mScreenSize.y, EXACTLY));
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700169 ((CardView) mWorkspaceSurface.getParent())
170 .setRadius(SizeCalculator.getPreviewCornerRadius(
171 activity, mContainer.getMeasuredWidth()));
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800172
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800173 renderImageWallpaper();
174 renderWorkspaceSurface();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700175
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700176 // Trim some memory from Glide to make room for the full-size image in this fragment.
177 Glide.get(activity).setMemoryCategory(MemoryCategory.LOW);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200178
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700179 return view;
180 }
181
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800182 @Override
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800183 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
184 super.onViewCreated(view, savedInstanceState);
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800185 }
186
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800187 protected void onWallpaperColorsChanged(@Nullable WallpaperColors colors) {
Chihhang Chuang1e8bece2021-05-17 15:08:06 +0800188 // Make it enabled since the buttons are disabled while wallpaper is moving.
189 mBottomActionBar.enableActionButtonsWithBottomSheet(true);
190
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800191 mLockScreenPreviewer.setColor(colors);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200192
193 mFullScreenAnimation.setFullScreenTextColor(
194 colors == null || (colors.getColorHints()
195 & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0
196 ? FullScreenAnimation.FullScreenTextColor.LIGHT
197 : FullScreenAnimation.FullScreenTextColor.DARK);
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800198 }
199
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800200 @Override
201 protected boolean isLoaded() {
202 return mFullResImageView != null && mFullResImageView.hasImage();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700203 }
204
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700205 @Override
206 public void onClickOk() {
207 FragmentActivity activity = getActivity();
208 if (activity != null) {
209 activity.finish();
210 }
211 }
212
213 @Override
214 public void onDestroy() {
215 super.onDestroy();
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800216
217 if (mFullResImageView != null) {
218 mFullResImageView.recycle();
219 }
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700220
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800221 if (mLockScreenPreviewer != null) {
222 mLockScreenPreviewer.release();
223 }
224
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700225 mWallpaperSurfaceCallback.cleanUp();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700226 mWorkspaceSurfaceCallback.cleanUp();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700227 }
228
229 @Override
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700230 protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700231 super.onBottomActionBarReady(bottomActionBar);
Chihhang Chuangf97f1592021-06-24 16:51:04 +0800232 mBottomActionBar.bindBottomSheetContentWithAction(
233 new WallpaperInfoContent(getContext()), INFORMATION);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700234 mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200235
Chihhang Chuangca7c8252020-06-17 17:32:46 +0800236 mBottomActionBar.setActionClickListener(APPLY, this::onSetWallpaperClicked);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800237
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800238 View separatedTabsContainer = getView().findViewById(R.id.separated_tabs_container);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800239 // Update target view's accessibility param since it will be blocked by the bottom sheet
240 // when expanded.
241 mBottomActionBar.setAccessibilityCallback(new AccessibilityCallback() {
242 @Override
243 public void onBottomSheetCollapsed() {
Chihhang Chuang15169282021-06-07 20:25:18 +0800244 mContainer.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800245 separatedTabsContainer.setImportantForAccessibility(
246 IMPORTANT_FOR_ACCESSIBILITY_YES);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800247 }
248
249 @Override
250 public void onBottomSheetExpanded() {
Wesley.CW Wangb8e51a22020-07-02 18:40:31 +0800251 mContainer.setImportantForAccessibility(
Chihhang Chuang15169282021-06-07 20:25:18 +0800252 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800253 separatedTabsContainer.setImportantForAccessibility(
Chihhang Chuang15169282021-06-07 20:25:18 +0800254 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
255
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800256 }
257 });
258
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700259 mBottomActionBar.show();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800260 // To avoid applying the wallpaper when the wallpaper's not parsed.
chihhangchuang13cb1fe2020-06-30 16:46:59 +0800261 mBottomActionBar.disableActions();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800262 // If the wallpaper is parsed, enable the bottom action bar.
263 if (mRawWallpaperSize != null) {
264 mBottomActionBar.enableActions();
265 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700266 }
267
268 /**
269 * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
270 * initializing a zoom-scroll observer and click listener.
271 */
272 private void initFullResView() {
Chihhang Chuang9c1d4e42021-05-14 23:53:16 +0800273 if (mRawWallpaperSize == null || mFullResImageView == null) {
274 return;
275 }
276
Tracy Zhoudee07892020-06-16 20:14:30 -0700277 // Minimum scale will only be respected under this scale type.
278 mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
279 // When we set a minimum scale bigger than the scale with which the full image is shown,
280 // disallow user to pan outside the view we show the wallpaper in.
281 mFullResImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700282
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700283 // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual
284 // (lower res) version of the image to be displayed.
285 Point targetPageBitmapSize = new Point(mRawWallpaperSize);
286 mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y,
287 pageBitmap -> {
288 // Check that the activity is still around since the decoding task started.
289 if (getActivity() == null) {
290 return;
291 }
292
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700293 // The page bitmap may be null if there was a decoding error, so show an
294 // error dialog.
295 if (pageBitmap == null) {
296 showLoadWallpaperErrorDialog();
297 return;
298 }
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700299 // Some of these may be null depending on if the Fragment is paused, stopped,
300 // or destroyed.
301 mWallpaperSurface.setBackgroundColor(Color.TRANSPARENT);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700302 if (mFullResImageView != null) {
303 // Set page bitmap.
304 mFullResImageView.setImage(ImageSource.bitmap(pageBitmap));
305
Chuck Liaodc424e82020-09-04 00:17:44 +0800306 setDefaultWallpaperZoomAndScroll(
307 mWallpaperAsset instanceof CurrentWallpaperAssetVN);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700308 crossFadeInMosaicView();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800309 mFullResImageView.setOnStateChangedListener(
310 new SubsamplingScaleImageView.DefaultOnStateChangedListener() {
311 @Override
312 public void onCenterChanged(PointF newCenter, int origin) {
313 super.onCenterChanged(newCenter, origin);
Chihhang Chuang1e8bece2021-05-17 15:08:06 +0800314 // Disallow bottom sheet to popup when wallpaper is moving
315 // by user dragging.
316 mBottomActionBar.enableActionButtonsWithBottomSheet(false);
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800317 mImageScaleChangeCounter.incrementAndGet();
318 mFullResImageView.postDelayed(() -> {
319 if (mImageScaleChangeCounter.decrementAndGet() == 0) {
320 recalculateColors();
321 }
322 }, /* delayMillis= */ 100);
323 }
324 });
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800325 mFullResImageView.post(this::recalculateColors);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700326 }
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800327 });
Chihhang Chuang1e8bece2021-05-17 15:08:06 +0800328
329 mFullResImageView.setOnTouchListener((v, ev) -> {
330 // Consume the touch event for collapsing bottom sheet while it is expanded or
331 // dragging (not collapsed).
332 if (mBottomActionBar != null && !mBottomActionBar.isBottomSheetCollapsed()) {
333 mBottomActionBar.collapseBottomSheetIfExpanded();
334 return true;
335 }
336 return false;
337 });
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800338 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700339
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800340 private void recalculateColors() {
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800341 Context context = getContext();
342 if (context == null) {
343 Log.e(TAG, "Got null context, skip recalculating colors");
344 return;
345 }
346
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800347 BitmapCropper bitmapCropper = InjectorProvider.getInjector().getBitmapCropper();
348 bitmapCropper.cropAndScaleBitmap(mWallpaperAsset, mFullResImageView.getScale(),
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800349 calculateCropRect(context), /* adjustForRtl= */ false,
350 new BitmapCropper.Callback() {
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800351 @Override
352 public void onBitmapCropped(Bitmap croppedBitmap) {
Santiago Etchebehere537a0d32021-06-30 14:55:50 +0200353 mRecalculateColorCounter.incrementAndGet();
354 sExecutor.execute(() -> {
355 boolean shouldRecycle = false;
356 ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
357 Bitmap cropped = croppedBitmap;
358 if (cropped.compress(Bitmap.CompressFormat.PNG, 100, tmpOut)) {
359 byte[] outByteArray = tmpOut.toByteArray();
360 BitmapFactory.Options options = new BitmapFactory.Options();
361 options.inPreferredColorSpace =
362 ColorSpace.get(ColorSpace.Named.SRGB);
363 cropped = BitmapFactory.decodeByteArray(outByteArray, 0,
364 outByteArray.length);
365 }
366 if (cropped.getConfig() == Bitmap.Config.HARDWARE) {
367 cropped = cropped.copy(Bitmap.Config.ARGB_8888, false);
368 shouldRecycle = true;
369 }
370 WallpaperColors colors = WallpaperColors.fromBitmap(cropped);
371 if (shouldRecycle) {
372 cropped.recycle();
373 }
374 if (mRecalculateColorCounter.decrementAndGet() == 0) {
375 Handler.getMain().post(() -> onWallpaperColorsChanged(colors));
376 }
377 });
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800378 }
379
380 @Override
381 public void onError(@Nullable Throwable e) {
382 Log.w(TAG, "Recalculate colors, crop and scale bitmap failed.", e);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700383 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700384 });
385 }
386
387 /**
388 * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading
389 * indicator.
390 */
391 private void crossFadeInMosaicView() {
392 long shortAnimationDuration = getResources().getInteger(
393 android.R.integer.config_shortAnimTime);
394
395 mFullResImageView.setAlpha(0f);
396 mFullResImageView.animate()
397 .alpha(1f)
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700398 .setInterpolator(ALPHA_OUT)
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700399 .setDuration(shortAnimationDuration)
400 .setListener(new AnimatorListenerAdapter() {
401 @Override
402 public void onAnimationEnd(Animator animation) {
403 // Clear the thumbnail bitmap reference to save memory since it's no longer
404 // visible.
405 if (mLowResImageView != null) {
406 mLowResImageView.setImageBitmap(null);
407 }
408 }
409 });
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700410 }
411
412 /**
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800413 * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra
414 * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as
415 * possible on the crop surface and align screen to crop surface such that the default preview
416 * matches what would be seen by the user in the left-most home screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700417 *
418 * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded
419 * and rendered to the layout.
Chuck Liaodc424e82020-09-04 00:17:44 +0800420 *
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800421 * @param offsetToStart {@code true} if we want to offset the visible rectangle to the start
422 * side of the raw wallpaper; {@code false} otherwise.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700423 */
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800424 private void setDefaultWallpaperZoomAndScroll(boolean offsetToStart) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700425 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800426 int cropWidth = mWallpaperSurface.getMeasuredWidth();
427 int cropHeight = mWallpaperSurface.getMeasuredHeight();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700428 Point crop = new Point(cropWidth, cropHeight);
429 Rect visibleRawWallpaperRect =
430 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800431 if (offsetToStart) {
432 if (WallpaperCropUtils.isRtl(requireContext())) {
433 visibleRawWallpaperRect.offsetTo(mRawWallpaperSize.x
434 - visibleRawWallpaperRect.width(), visibleRawWallpaperRect.top);
435 } else {
436 visibleRawWallpaperRect.offsetTo(/* newLeft= */ 0, visibleRawWallpaperRect.top);
437 }
Chuck Liaodc424e82020-09-04 00:17:44 +0800438 }
Santiago Etchebeheree95b6d92020-06-08 14:19:46 -0700439
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800440 final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
441 visibleRawWallpaperRect.centerY());
Santiago Etchebeheree95b6d92020-06-08 14:19:46 -0700442
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700443 Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
444 visibleRawWallpaperRect.height());
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700445
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700446 final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
447 visibleRawWallpaperSize, crop);
448 final float minWallpaperZoom = defaultWallpaperZoom;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700449
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700450
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700451 // Set min wallpaper zoom and max zoom on MosaicView widget.
452 mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
453 mFullResImageView.setMinScale(minWallpaperZoom);
454
455 // Set center to composite positioning between scaled wallpaper and screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700456 mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
457 }
458
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800459 private Rect calculateCropRect(Context context) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700460 float wallpaperZoom = mFullResImageView.getScale();
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800461 Context appContext = context.getApplicationContext();
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700462
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700463 Rect visibleFileRect = new Rect();
464 mFullResImageView.visibleFileRect(visibleFileRect);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700465
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800466 int cropWidth = mWallpaperSurface.getMeasuredWidth();
467 int cropHeight = mWallpaperSurface.getMeasuredHeight();
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700468 int maxCrop = Math.max(cropWidth, cropHeight);
469 int minCrop = Math.min(cropWidth, cropHeight);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700470 Point hostViewSize = new Point(cropWidth, cropHeight);
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700471
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800472 Resources res = appContext.getResources();
Santiago Etchebeherebcd6bb62020-07-09 12:09:21 -0700473 Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop);
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700474
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700475 return WallpaperCropUtils.calculateCropRect(appContext, hostViewSize,
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700476 cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700477 }
478
479 @Override
480 protected void setCurrentWallpaper(@Destination int destination) {
481 mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800482 destination, mFullResImageView.getScale(), calculateCropRect(getContext()),
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700483 new SetWallpaperCallback() {
484 @Override
“Chuckffd832c2020-03-22 02:15:58 +0800485 public void onSuccess(WallpaperInfo wallpaperInfo) {
Ching-Sung Lib5e9bd72020-06-15 14:10:55 +0800486 finishActivity(/* success= */ true);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700487 }
488
489 @Override
490 public void onError(@Nullable Throwable throwable) {
491 showSetWallpaperErrorDialog(destination);
492 }
493 });
494 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700495
496 private void renderWorkspaceSurface() {
497 mWorkspaceSurface.setZOrderMediaOverlay(true);
498 mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback);
499 }
500
501 private void renderImageWallpaper() {
502 mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
503 }
504
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700505 private class WallpaperSurfaceCallback implements SurfaceHolder.Callback {
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700506 private Surface mLastSurface;
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700507 private SurfaceControlViewHost mHost;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700508
509 @Override
510 public void surfaceCreated(SurfaceHolder holder) {
511 if (mLastSurface != holder.getSurface()) {
512 mLastSurface = holder.getSurface();
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700513 if (mFullResImageView != null) {
514 mFullResImageView.recycle();
515 }
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800516 Context context = getContext();
517 View wallpaperPreviewContainer = LayoutInflater.from(context).inflate(
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700518 R.layout.fullscreen_wallpaper_preview, null);
519 mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image);
520 mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700521 mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
522 // Don't continue loading the wallpaper if the Fragment is detached.
523 if (getActivity() == null) {
524 return;
525 }
526
527 // Return early and show a dialog if dimensions are null (signaling a decoding
528 // error).
529 if (dimensions == null) {
530 showLoadWallpaperErrorDialog();
531 return;
532 }
533
534 // To avoid applying the wallpaper when it's not parsed. Now it's parsed, enable
535 // the bottom action bar to allow applying the wallpaper.
536 if (mBottomActionBar != null) {
537 mBottomActionBar.enableActions();
538 }
539
540 mRawWallpaperSize = dimensions;
541 initFullResView();
542 });
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800543 // Scale the mWallpaperSurface based on system zoom's scale so that the wallpaper is
544 // rendered in a larger surface than what preview shows, simulating the behavior of
545 // the actual wallpaper surface.
546 float scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context);
547 int origWidth = mWallpaperSurface.getWidth();
548 int width = (int) (origWidth * scale);
549 int origHeight = mWallpaperSurface.getHeight();
550 int height = (int) (origHeight * scale);
551 int left = (origWidth - width) / 2;
552 int top = (origHeight - height) / 2;
553
554 if (WallpaperCropUtils.isRtl(context)) {
555 left *= -1;
556 }
557
558 LayoutParams params = mWallpaperSurface.getLayoutParams();
559 params.width = width;
560 params.height = height;
561 mWallpaperSurface.setX(left);
562 mWallpaperSurface.setY(top);
563 mWallpaperSurface.setLayoutParams(params);
564 mWallpaperSurface.requestLayout();
565
Chuck Liaoca1d1292020-06-19 01:29:53 +0800566 // Load a low-res placeholder image if there's a thumbnail available from the asset
567 // that can be shown to the user more quickly than the full-sized image.
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700568 Activity activity = requireActivity();
569 int placeHolderColor = ResourceUtils.getColorAttr(activity,
570 android.R.attr.colorBackground);
571 if (mPlaceholderColorFuture.isDone()) {
572 try {
573 placeHolderColor = mWallpaper.computePlaceholderColor(context).get();
574 } catch (InterruptedException | ExecutionException e) {
575 // Do nothing
576 }
Chuck Liaoca1d1292020-06-19 01:29:53 +0800577 }
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700578 mWallpaperSurface.setResizeBackgroundColor(placeHolderColor);
579 mWallpaperSurface.setBackgroundColor(placeHolderColor);
580
581 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, placeHolderColor,
Ching-Sung Lid2f46be2021-06-22 20:55:28 +0800582 mPreviewBitmapTransformation);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700583
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700584 wallpaperPreviewContainer.measure(
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800585 makeMeasureSpec(width, EXACTLY),
586 makeMeasureSpec(height, EXACTLY));
587 wallpaperPreviewContainer.layout(0, 0, width, height);
Ching-Sung Lib9404622020-05-18 22:38:45 +0800588 mTouchForwardingLayout.setTargetView(mFullResImageView);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700589
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700590 cleanUp();
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800591 mHost = new SurfaceControlViewHost(context,
592 context.getDisplay(), mWallpaperSurface.getHostToken());
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700593 mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700594 wallpaperPreviewContainer.getHeight());
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700595 mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700596 }
597 }
598
599 @Override
600 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
601
602 @Override
603 public void surfaceDestroyed(SurfaceHolder holder) { }
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700604
605 public void cleanUp() {
606 if (mHost != null) {
607 mHost.release();
608 mHost = null;
609 }
610 }
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700611 }
612
Santiago Etchebehere3e452912021-06-07 18:10:30 -0700613 @Override
614 protected void updateScreenPreview(boolean isHomeSelected) {
chihhangchuang287d4b72020-06-25 01:09:09 +0800615 mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE);
Santiago Etchebehere3e452912021-06-07 18:10:30 -0700616
chihhangchuang287d4b72020-06-25 01:09:09 +0800617 mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200618
619 mFullScreenAnimation.setIsHomeSelected(isHomeSelected);
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700620 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700621}