blob: 251b0734d32d04950abf283f54b9a94f8408eb28 [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;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080041import android.util.Log;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070042import android.view.LayoutInflater;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070043import android.view.Surface;
44import android.view.SurfaceControlViewHost;
45import android.view.SurfaceHolder;
46import android.view.SurfaceView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070047import android.view.View;
48import android.view.ViewGroup;
Santiago Etchebehere8557c462020-12-02 18:41:40 -080049import android.view.ViewGroup.LayoutParams;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070050import android.widget.ImageView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070051
Chuck Liaofc1b8c92020-05-21 20:54:15 +080052import androidx.annotation.NonNull;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070053import androidx.annotation.Nullable;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070054import androidx.cardview.widget.CardView;
Tracy Zhou3eb234c2020-05-05 15:17:33 -070055import androidx.constraintlayout.widget.ConstraintLayout;
56import androidx.constraintlayout.widget.ConstraintSet;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070057import androidx.fragment.app.FragmentActivity;
58
59import com.android.wallpaper.R;
60import com.android.wallpaper.asset.Asset;
Chuck Liaodc424e82020-09-04 00:17:44 +080061import com.android.wallpaper.asset.CurrentWallpaperAssetVN;
“Chuckffd832c2020-03-22 02:15:58 +080062import com.android.wallpaper.model.WallpaperInfo;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080063import com.android.wallpaper.module.BitmapCropper;
64import com.android.wallpaper.module.InjectorProvider;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070065import com.android.wallpaper.module.WallpaperPersister.Destination;
66import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
Tianguang Zhang893187f2021-05-07 22:14:04 +020067import com.android.wallpaper.util.FullScreenAnimation;
Tianguang Zhangd3e4f632021-04-14 00:30:15 +020068import com.android.wallpaper.util.ResourceUtils;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070069import com.android.wallpaper.util.ScreenSizeCalculator;
Santiago Etchebehere53c63432020-05-07 18:55:35 -070070import com.android.wallpaper.util.SizeCalculator;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070071import com.android.wallpaper.util.WallpaperCropUtils;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070072import com.android.wallpaper.widget.BottomActionBar;
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +080073import com.android.wallpaper.widget.BottomActionBar.AccessibilityCallback;
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +080074import com.android.wallpaper.widget.LockScreenPreviewer;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070075
76import com.bumptech.glide.Glide;
77import com.bumptech.glide.MemoryCategory;
78import com.davemorrissey.labs.subscaleview.ImageSource;
79import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070080
James O'Learyd6458392021-06-21 09:39:51 -040081import java.io.ByteArrayOutputStream;
chihhangchuang923e29b2020-07-02 19:09:36 +080082import java.util.Locale;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070083import java.util.concurrent.ExecutionException;
84import java.util.concurrent.Future;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080085import java.util.concurrent.atomic.AtomicInteger;
chihhangchuang923e29b2020-07-02 19:09:36 +080086
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070087/**
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -080088 * Fragment which displays the UI for previewing an individual static wallpaper and its attribution
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070089 * information.
90 */
Chihhang Chuangc9c108a2021-04-28 18:35:14 +080091public class ImagePreviewFragment extends PreviewFragment {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070092
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080093 private static final String TAG = "ImagePreviewFragment";
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070094 private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f;
95
Tracy Zhou4421a3d2020-06-15 14:56:18 -070096 private final WallpaperSurfaceCallback mWallpaperSurfaceCallback =
97 new WallpaperSurfaceCallback();
Tracy Zhouc5a6bad2020-05-20 23:28:22 -070098
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070099 private SubsamplingScaleImageView mFullResImageView;
100 private Asset mWallpaperAsset;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700101 private Point mScreenSize;
102 private Point mRawWallpaperSize; // Native size of wallpaper image.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700103 private ImageView mLowResImageView;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700104 private TouchForwardingLayout mTouchForwardingLayout;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700105 private ConstraintLayout mContainer;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700106 private SurfaceView mWallpaperSurface;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800107 private AtomicInteger mImageScaleChangeCounter = new AtomicInteger(0);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700108
Santiago Etchebehere62287292021-04-30 15:53:22 -0700109 protected SurfaceView mWorkspaceSurface;
Santiago Etchebeheredbdfb662021-04-26 10:25:13 -0700110 protected WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback;
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800111 protected ViewGroup mLockPreviewContainer;
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800112 protected LockScreenPreviewer mLockScreenPreviewer;
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700113 private Future<Integer> mPlaceholderColorFuture;
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800114
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700115 @Override
116 public void onCreate(Bundle savedInstanceState) {
117 super.onCreate(savedInstanceState);
118 mWallpaperAsset = mWallpaper.getAsset(requireContext().getApplicationContext());
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700119 mPlaceholderColorFuture = mWallpaper.computePlaceholderColor(requireContext());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700120 }
121
122 @Override
123 protected int getLayoutResId() {
Ching-Sung Lid2f46be2021-06-22 20:55:28 +0800124 return R.layout.fragment_image_preview;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700125 }
126
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700127 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700128 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800129 Bundle savedInstanceState) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700130 View view = super.onCreateView(inflater, container, savedInstanceState);
131
132 Activity activity = requireActivity();
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700133 mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize(
134 activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700135
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700136 mContainer = view.findViewById(R.id.container);
137 mTouchForwardingLayout = mContainer.findViewById(R.id.touch_forwarding_layout);
Chihhang Chuang60a5bc22021-05-14 23:00:01 +0800138 mTouchForwardingLayout.setForwardingEnabled(true);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700139
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700140 // Set aspect ratio on the preview card dynamically.
141 ConstraintSet set = new ConstraintSet();
142 set.clone(mContainer);
chihhangchuang923e29b2020-07-02 19:09:36 +0800143 String ratio = String.format(Locale.US, "%d:%d", mScreenSize.x, mScreenSize.y);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700144 set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio);
145 set.applyTo(mContainer);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700146
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700147 mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface);
Santiago Etchebeheredbdfb662021-04-26 10:25:13 -0700148 mWorkspaceSurfaceCallback = createWorkspaceSurfaceCallback(mWorkspaceSurface);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700149 mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface);
chihhangchuang287d4b72020-06-25 01:09:09 +0800150 mLockPreviewContainer = mContainer.findViewById(R.id.lock_screen_preview_container);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700151 int placeHolderColor = ResourceUtils.getColorAttr(getContext(),
152 android.R.attr.colorBackground);
153 mWorkspaceSurface.setResizeBackgroundColor(placeHolderColor);
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800154 mLockScreenPreviewer = new LockScreenPreviewer(getLifecycle(), getContext(),
chihhangchuang287d4b72020-06-25 01:09:09 +0800155 mLockPreviewContainer);
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800156 mLockScreenPreviewer.setDateViewVisibility(!mFullScreenAnimation.isFullScreen());
157 mFullScreenAnimation.setFullScreenStatusListener(
158 isFullScreen -> mLockScreenPreviewer.setDateViewVisibility(!isFullScreen));
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800159 setUpTabs(view.findViewById(R.id.separated_tabs));
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700160
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700161 view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY),
162 makeMeasureSpec(mScreenSize.y, EXACTLY));
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700163 ((CardView) mWorkspaceSurface.getParent())
164 .setRadius(SizeCalculator.getPreviewCornerRadius(
165 activity, mContainer.getMeasuredWidth()));
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800166
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800167 renderImageWallpaper();
168 renderWorkspaceSurface();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700169
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700170 // Trim some memory from Glide to make room for the full-size image in this fragment.
171 Glide.get(activity).setMemoryCategory(MemoryCategory.LOW);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200172
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700173 return view;
174 }
175
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800176 @Override
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800177 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
178 super.onViewCreated(view, savedInstanceState);
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800179 }
180
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800181 protected void onWallpaperColorsChanged(@Nullable WallpaperColors colors) {
Chihhang Chuang1e8bece2021-05-17 15:08:06 +0800182 // Make it enabled since the buttons are disabled while wallpaper is moving.
183 mBottomActionBar.enableActionButtonsWithBottomSheet(true);
184
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800185 mLockScreenPreviewer.setColor(colors);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200186
187 mFullScreenAnimation.setFullScreenTextColor(
188 colors == null || (colors.getColorHints()
189 & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0
190 ? FullScreenAnimation.FullScreenTextColor.LIGHT
191 : FullScreenAnimation.FullScreenTextColor.DARK);
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800192 }
193
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800194 @Override
195 protected boolean isLoaded() {
196 return mFullResImageView != null && mFullResImageView.hasImage();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700197 }
198
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700199 @Override
200 public void onClickOk() {
201 FragmentActivity activity = getActivity();
202 if (activity != null) {
203 activity.finish();
204 }
205 }
206
207 @Override
208 public void onDestroy() {
209 super.onDestroy();
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800210
211 if (mFullResImageView != null) {
212 mFullResImageView.recycle();
213 }
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700214
Chihhang Chuang8a9cd7d2021-06-16 12:37:59 +0800215 if (mLockScreenPreviewer != null) {
216 mLockScreenPreviewer.release();
217 }
218
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700219 mWallpaperSurfaceCallback.cleanUp();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700220 mWorkspaceSurfaceCallback.cleanUp();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700221 }
222
223 @Override
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700224 protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700225 super.onBottomActionBarReady(bottomActionBar);
Chihhang Chuangf97f1592021-06-24 16:51:04 +0800226 mBottomActionBar.bindBottomSheetContentWithAction(
227 new WallpaperInfoContent(getContext()), INFORMATION);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700228 mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200229
Chihhang Chuangca7c8252020-06-17 17:32:46 +0800230 mBottomActionBar.setActionClickListener(APPLY, this::onSetWallpaperClicked);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800231
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800232 View separatedTabsContainer = getView().findViewById(R.id.separated_tabs_container);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800233 // Update target view's accessibility param since it will be blocked by the bottom sheet
234 // when expanded.
235 mBottomActionBar.setAccessibilityCallback(new AccessibilityCallback() {
236 @Override
237 public void onBottomSheetCollapsed() {
Chihhang Chuang15169282021-06-07 20:25:18 +0800238 mContainer.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800239 separatedTabsContainer.setImportantForAccessibility(
240 IMPORTANT_FOR_ACCESSIBILITY_YES);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800241 }
242
243 @Override
244 public void onBottomSheetExpanded() {
Wesley.CW Wangb8e51a22020-07-02 18:40:31 +0800245 mContainer.setImportantForAccessibility(
Chihhang Chuang15169282021-06-07 20:25:18 +0800246 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
Chihhang Chuang17821eb2021-06-21 15:31:07 +0800247 separatedTabsContainer.setImportantForAccessibility(
Chihhang Chuang15169282021-06-07 20:25:18 +0800248 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
249
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800250 }
251 });
252
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700253 mBottomActionBar.show();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800254 // To avoid applying the wallpaper when the wallpaper's not parsed.
chihhangchuang13cb1fe2020-06-30 16:46:59 +0800255 mBottomActionBar.disableActions();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800256 // If the wallpaper is parsed, enable the bottom action bar.
257 if (mRawWallpaperSize != null) {
258 mBottomActionBar.enableActions();
259 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700260 }
261
262 /**
263 * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
264 * initializing a zoom-scroll observer and click listener.
265 */
266 private void initFullResView() {
Chihhang Chuang9c1d4e42021-05-14 23:53:16 +0800267 if (mRawWallpaperSize == null || mFullResImageView == null) {
268 return;
269 }
270
Tracy Zhoudee07892020-06-16 20:14:30 -0700271 // Minimum scale will only be respected under this scale type.
272 mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
273 // When we set a minimum scale bigger than the scale with which the full image is shown,
274 // disallow user to pan outside the view we show the wallpaper in.
275 mFullResImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700276
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700277 // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual
278 // (lower res) version of the image to be displayed.
279 Point targetPageBitmapSize = new Point(mRawWallpaperSize);
280 mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y,
281 pageBitmap -> {
282 // Check that the activity is still around since the decoding task started.
283 if (getActivity() == null) {
284 return;
285 }
286
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700287 // The page bitmap may be null if there was a decoding error, so show an
288 // error dialog.
289 if (pageBitmap == null) {
290 showLoadWallpaperErrorDialog();
291 return;
292 }
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700293 // Some of these may be null depending on if the Fragment is paused, stopped,
294 // or destroyed.
295 mWallpaperSurface.setBackgroundColor(Color.TRANSPARENT);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700296 if (mFullResImageView != null) {
297 // Set page bitmap.
298 mFullResImageView.setImage(ImageSource.bitmap(pageBitmap));
299
Chuck Liaodc424e82020-09-04 00:17:44 +0800300 setDefaultWallpaperZoomAndScroll(
301 mWallpaperAsset instanceof CurrentWallpaperAssetVN);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700302 crossFadeInMosaicView();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800303 mFullResImageView.setOnStateChangedListener(
304 new SubsamplingScaleImageView.DefaultOnStateChangedListener() {
305 @Override
306 public void onCenterChanged(PointF newCenter, int origin) {
307 super.onCenterChanged(newCenter, origin);
Chihhang Chuang1e8bece2021-05-17 15:08:06 +0800308 // Disallow bottom sheet to popup when wallpaper is moving
309 // by user dragging.
310 mBottomActionBar.enableActionButtonsWithBottomSheet(false);
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800311 mImageScaleChangeCounter.incrementAndGet();
312 mFullResImageView.postDelayed(() -> {
313 if (mImageScaleChangeCounter.decrementAndGet() == 0) {
314 recalculateColors();
315 }
316 }, /* delayMillis= */ 100);
317 }
318 });
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800319 mFullResImageView.post(this::recalculateColors);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700320 }
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800321 });
Chihhang Chuang1e8bece2021-05-17 15:08:06 +0800322
323 mFullResImageView.setOnTouchListener((v, ev) -> {
324 // Consume the touch event for collapsing bottom sheet while it is expanded or
325 // dragging (not collapsed).
326 if (mBottomActionBar != null && !mBottomActionBar.isBottomSheetCollapsed()) {
327 mBottomActionBar.collapseBottomSheetIfExpanded();
328 return true;
329 }
330 return false;
331 });
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800332 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700333
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800334 private void recalculateColors() {
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800335 Context context = getContext();
336 if (context == null) {
337 Log.e(TAG, "Got null context, skip recalculating colors");
338 return;
339 }
340
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800341 BitmapCropper bitmapCropper = InjectorProvider.getInjector().getBitmapCropper();
342 bitmapCropper.cropAndScaleBitmap(mWallpaperAsset, mFullResImageView.getScale(),
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800343 calculateCropRect(context), /* adjustForRtl= */ false,
344 new BitmapCropper.Callback() {
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800345 @Override
346 public void onBitmapCropped(Bitmap croppedBitmap) {
Santiago Etchebehere40ce6d22021-06-08 18:41:35 -0700347 boolean shouldRecycle = false;
James O'Learyd6458392021-06-21 09:39:51 -0400348 ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
349 if (croppedBitmap.compress(Bitmap.CompressFormat.PNG, 100, tmpOut)) {
350 byte[] outByteArray = tmpOut.toByteArray();
351 BitmapFactory.Options options = new BitmapFactory.Options();
352 options.inPreferredColorSpace = ColorSpace.get(ColorSpace.Named.SRGB);
353 Bitmap decodedPng = BitmapFactory.decodeByteArray(outByteArray, 0,
354 outByteArray.length);
355 croppedBitmap = decodedPng;
356 }
Santiago Etchebehere40ce6d22021-06-08 18:41:35 -0700357 if (croppedBitmap.getConfig() == Bitmap.Config.HARDWARE) {
358 croppedBitmap = croppedBitmap.copy(Bitmap.Config.ARGB_8888, false);
359 shouldRecycle = true;
360 }
361 WallpaperColors colors = WallpaperColors.fromBitmap(croppedBitmap);
362 if (shouldRecycle) {
363 croppedBitmap.recycle();
364 }
365 onWallpaperColorsChanged(colors);
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800366 }
367
368 @Override
369 public void onError(@Nullable Throwable e) {
370 Log.w(TAG, "Recalculate colors, crop and scale bitmap failed.", e);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700371 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700372 });
373 }
374
375 /**
376 * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading
377 * indicator.
378 */
379 private void crossFadeInMosaicView() {
380 long shortAnimationDuration = getResources().getInteger(
381 android.R.integer.config_shortAnimTime);
382
383 mFullResImageView.setAlpha(0f);
384 mFullResImageView.animate()
385 .alpha(1f)
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700386 .setInterpolator(ALPHA_OUT)
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700387 .setDuration(shortAnimationDuration)
388 .setListener(new AnimatorListenerAdapter() {
389 @Override
390 public void onAnimationEnd(Animator animation) {
391 // Clear the thumbnail bitmap reference to save memory since it's no longer
392 // visible.
393 if (mLowResImageView != null) {
394 mLowResImageView.setImageBitmap(null);
395 }
396 }
397 });
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700398 }
399
400 /**
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800401 * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra
402 * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as
403 * possible on the crop surface and align screen to crop surface such that the default preview
404 * matches what would be seen by the user in the left-most home screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700405 *
406 * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded
407 * and rendered to the layout.
Chuck Liaodc424e82020-09-04 00:17:44 +0800408 *
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800409 * @param offsetToStart {@code true} if we want to offset the visible rectangle to the start
410 * side of the raw wallpaper; {@code false} otherwise.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700411 */
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800412 private void setDefaultWallpaperZoomAndScroll(boolean offsetToStart) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700413 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800414 int cropWidth = mWallpaperSurface.getMeasuredWidth();
415 int cropHeight = mWallpaperSurface.getMeasuredHeight();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700416 Point crop = new Point(cropWidth, cropHeight);
417 Rect visibleRawWallpaperRect =
418 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800419 if (offsetToStart) {
420 if (WallpaperCropUtils.isRtl(requireContext())) {
421 visibleRawWallpaperRect.offsetTo(mRawWallpaperSize.x
422 - visibleRawWallpaperRect.width(), visibleRawWallpaperRect.top);
423 } else {
424 visibleRawWallpaperRect.offsetTo(/* newLeft= */ 0, visibleRawWallpaperRect.top);
425 }
Chuck Liaodc424e82020-09-04 00:17:44 +0800426 }
Santiago Etchebeheree95b6d92020-06-08 14:19:46 -0700427
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800428 final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
429 visibleRawWallpaperRect.centerY());
Santiago Etchebeheree95b6d92020-06-08 14:19:46 -0700430
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700431 Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
432 visibleRawWallpaperRect.height());
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700433
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700434 final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
435 visibleRawWallpaperSize, crop);
436 final float minWallpaperZoom = defaultWallpaperZoom;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700437
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700438
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700439 // Set min wallpaper zoom and max zoom on MosaicView widget.
440 mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
441 mFullResImageView.setMinScale(minWallpaperZoom);
442
443 // Set center to composite positioning between scaled wallpaper and screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700444 mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
445 }
446
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800447 private Rect calculateCropRect(Context context) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700448 float wallpaperZoom = mFullResImageView.getScale();
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800449 Context appContext = context.getApplicationContext();
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700450
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700451 Rect visibleFileRect = new Rect();
452 mFullResImageView.visibleFileRect(visibleFileRect);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700453
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800454 int cropWidth = mWallpaperSurface.getMeasuredWidth();
455 int cropHeight = mWallpaperSurface.getMeasuredHeight();
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700456 int maxCrop = Math.max(cropWidth, cropHeight);
457 int minCrop = Math.min(cropWidth, cropHeight);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700458 Point hostViewSize = new Point(cropWidth, cropHeight);
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700459
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800460 Resources res = appContext.getResources();
Santiago Etchebeherebcd6bb62020-07-09 12:09:21 -0700461 Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop);
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700462
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700463 return WallpaperCropUtils.calculateCropRect(appContext, hostViewSize,
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700464 cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700465 }
466
467 @Override
468 protected void setCurrentWallpaper(@Destination int destination) {
469 mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
Chihhang Chuang5d94cb62021-05-25 18:10:22 +0800470 destination, mFullResImageView.getScale(), calculateCropRect(getContext()),
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700471 new SetWallpaperCallback() {
472 @Override
“Chuckffd832c2020-03-22 02:15:58 +0800473 public void onSuccess(WallpaperInfo wallpaperInfo) {
Ching-Sung Lib5e9bd72020-06-15 14:10:55 +0800474 finishActivity(/* success= */ true);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700475 }
476
477 @Override
478 public void onError(@Nullable Throwable throwable) {
479 showSetWallpaperErrorDialog(destination);
480 }
481 });
482 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700483
484 private void renderWorkspaceSurface() {
485 mWorkspaceSurface.setZOrderMediaOverlay(true);
486 mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback);
487 }
488
489 private void renderImageWallpaper() {
490 mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
491 }
492
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700493 private class WallpaperSurfaceCallback implements SurfaceHolder.Callback {
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700494 private Surface mLastSurface;
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700495 private SurfaceControlViewHost mHost;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700496
497 @Override
498 public void surfaceCreated(SurfaceHolder holder) {
499 if (mLastSurface != holder.getSurface()) {
500 mLastSurface = holder.getSurface();
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700501 if (mFullResImageView != null) {
502 mFullResImageView.recycle();
503 }
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800504 Context context = getContext();
505 View wallpaperPreviewContainer = LayoutInflater.from(context).inflate(
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700506 R.layout.fullscreen_wallpaper_preview, null);
507 mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image);
508 mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700509 mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
510 // Don't continue loading the wallpaper if the Fragment is detached.
511 if (getActivity() == null) {
512 return;
513 }
514
515 // Return early and show a dialog if dimensions are null (signaling a decoding
516 // error).
517 if (dimensions == null) {
518 showLoadWallpaperErrorDialog();
519 return;
520 }
521
522 // To avoid applying the wallpaper when it's not parsed. Now it's parsed, enable
523 // the bottom action bar to allow applying the wallpaper.
524 if (mBottomActionBar != null) {
525 mBottomActionBar.enableActions();
526 }
527
528 mRawWallpaperSize = dimensions;
529 initFullResView();
530 });
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800531 // Scale the mWallpaperSurface based on system zoom's scale so that the wallpaper is
532 // rendered in a larger surface than what preview shows, simulating the behavior of
533 // the actual wallpaper surface.
534 float scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context);
535 int origWidth = mWallpaperSurface.getWidth();
536 int width = (int) (origWidth * scale);
537 int origHeight = mWallpaperSurface.getHeight();
538 int height = (int) (origHeight * scale);
539 int left = (origWidth - width) / 2;
540 int top = (origHeight - height) / 2;
541
542 if (WallpaperCropUtils.isRtl(context)) {
543 left *= -1;
544 }
545
546 LayoutParams params = mWallpaperSurface.getLayoutParams();
547 params.width = width;
548 params.height = height;
549 mWallpaperSurface.setX(left);
550 mWallpaperSurface.setY(top);
551 mWallpaperSurface.setLayoutParams(params);
552 mWallpaperSurface.requestLayout();
553
Chuck Liaoca1d1292020-06-19 01:29:53 +0800554 // Load a low-res placeholder image if there's a thumbnail available from the asset
555 // that can be shown to the user more quickly than the full-sized image.
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700556 Activity activity = requireActivity();
557 int placeHolderColor = ResourceUtils.getColorAttr(activity,
558 android.R.attr.colorBackground);
559 if (mPlaceholderColorFuture.isDone()) {
560 try {
561 placeHolderColor = mWallpaper.computePlaceholderColor(context).get();
562 } catch (InterruptedException | ExecutionException e) {
563 // Do nothing
564 }
Chuck Liaoca1d1292020-06-19 01:29:53 +0800565 }
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700566 mWallpaperSurface.setResizeBackgroundColor(placeHolderColor);
567 mWallpaperSurface.setBackgroundColor(placeHolderColor);
568
569 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, placeHolderColor,
Ching-Sung Lid2f46be2021-06-22 20:55:28 +0800570 mPreviewBitmapTransformation);
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -0700571
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700572 wallpaperPreviewContainer.measure(
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800573 makeMeasureSpec(width, EXACTLY),
574 makeMeasureSpec(height, EXACTLY));
575 wallpaperPreviewContainer.layout(0, 0, width, height);
Ching-Sung Lib9404622020-05-18 22:38:45 +0800576 mTouchForwardingLayout.setTargetView(mFullResImageView);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700577
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700578 cleanUp();
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800579 mHost = new SurfaceControlViewHost(context,
580 context.getDisplay(), mWallpaperSurface.getHostToken());
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700581 mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700582 wallpaperPreviewContainer.getHeight());
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700583 mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700584 }
585 }
586
587 @Override
588 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
589
590 @Override
591 public void surfaceDestroyed(SurfaceHolder holder) { }
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700592
593 public void cleanUp() {
594 if (mHost != null) {
595 mHost.release();
596 mHost = null;
597 }
598 }
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700599 }
600
Santiago Etchebehere3e452912021-06-07 18:10:30 -0700601 @Override
602 protected void updateScreenPreview(boolean isHomeSelected) {
chihhangchuang287d4b72020-06-25 01:09:09 +0800603 mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE);
Santiago Etchebehere3e452912021-06-07 18:10:30 -0700604
chihhangchuang287d4b72020-06-25 01:09:09 +0800605 mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200606
607 mFullScreenAnimation.setIsHomeSelected(isHomeSelected);
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700608 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700609}