blob: ce7c4306d45e218bd7e5c4855af6cbd83bff87a3 [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
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070018import static android.view.View.MeasureSpec.EXACTLY;
19import static android.view.View.MeasureSpec.makeMeasureSpec;
20
21import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY;
22import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT;
23import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION;
24
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070025import android.animation.Animator;
26import android.animation.AnimatorListenerAdapter;
27import android.app.Activity;
28import android.content.Context;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070029import android.graphics.Bitmap;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070030import android.graphics.Color;
31import android.graphics.Point;
32import android.graphics.PointF;
33import android.graphics.Rect;
34import android.os.Bundle;
Tracy Zhouc5a6bad2020-05-20 23:28:22 -070035import android.os.Handler;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070036import android.view.Display;
37import android.view.LayoutInflater;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070038import android.view.Surface;
39import android.view.SurfaceControlViewHost;
40import android.view.SurfaceHolder;
41import android.view.SurfaceView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070042import android.view.View;
43import android.view.ViewGroup;
44import android.widget.ImageView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070045
Chuck Liaofc1b8c92020-05-21 20:54:15 +080046import androidx.annotation.NonNull;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070047import androidx.annotation.Nullable;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070048import androidx.cardview.widget.CardView;
Tracy Zhou3eb234c2020-05-05 15:17:33 -070049import androidx.constraintlayout.widget.ConstraintLayout;
50import androidx.constraintlayout.widget.ConstraintSet;
51import androidx.core.content.ContextCompat;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070052import androidx.fragment.app.FragmentActivity;
53
54import com.android.wallpaper.R;
55import com.android.wallpaper.asset.Asset;
“Chuckffd832c2020-03-22 02:15:58 +080056import com.android.wallpaper.model.WallpaperInfo;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070057import com.android.wallpaper.module.WallpaperPersister.Destination;
58import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
59import com.android.wallpaper.util.ScreenSizeCalculator;
Santiago Etchebehere53c63432020-05-07 18:55:35 -070060import com.android.wallpaper.util.SizeCalculator;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070061import com.android.wallpaper.util.WallpaperCropUtils;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070062import com.android.wallpaper.widget.BottomActionBar;
Chuck Liaofc1b8c92020-05-21 20:54:15 +080063import com.android.wallpaper.widget.LockScreenOverlayUpdater;
64import com.android.wallpaper.widget.WallpaperColorsLoader;
Tracy Zhou35997422020-05-01 13:42:06 -070065import com.android.wallpaper.widget.WallpaperInfoView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070066
67import com.bumptech.glide.Glide;
68import com.bumptech.glide.MemoryCategory;
69import com.davemorrissey.labs.subscaleview.ImageSource;
70import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
71import com.google.android.material.bottomsheet.BottomSheetBehavior;
72
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070073/**
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -080074 * Fragment which displays the UI for previewing an individual static wallpaper and its attribution
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070075 * information.
76 */
77public class ImagePreviewFragment extends PreviewFragment {
78
79 private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f;
80
Tracy Zhouc5a6bad2020-05-20 23:28:22 -070081 private final Handler mHandler = new Handler();
82
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070083 private SubsamplingScaleImageView mFullResImageView;
84 private Asset mWallpaperAsset;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070085 private Point mDefaultCropSurfaceSize;
86 private Point mScreenSize;
87 private Point mRawWallpaperSize; // Native size of wallpaper image.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070088 private ImageView mLowResImageView;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070089 private TouchForwardingLayout mTouchForwardingLayout;
Tracy Zhou3eb234c2020-05-05 15:17:33 -070090 private ConstraintLayout mContainer;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070091 private SurfaceView mWorkspaceSurface;
Tracy Zhou3eb234c2020-05-05 15:17:33 -070092 private WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070093 private SurfaceView mWallpaperSurface;
Chuck Liaofc1b8c92020-05-21 20:54:15 +080094 private View mLockOverlay;
95 private LockScreenOverlayUpdater mLockScreenOverlayUpdater;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070096 private View mTabs;
Tracy Zhou35997422020-05-01 13:42:06 -070097 private WallpaperInfoView mWallpaperInfoView;
Santiago Etchebeheredc55b192019-11-08 13:56:52 -080098 private InfoPageController mInfoPageController;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070099
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700100 @Override
101 public void onCreate(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
103 mWallpaperAsset = mWallpaper.getAsset(requireContext().getApplicationContext());
104 }
105
106 @Override
107 protected int getLayoutResId() {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700108 return R.layout.fragment_image_preview_v2;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700109 }
110
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800111
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700112 protected int getBottomSheetResId() {
113 return R.id.bottom_sheet;
114 }
115
116 @Override
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800117 protected int getLoadingIndicatorResId() {
118 return R.id.loading_indicator;
119 }
120
121 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700122 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800123 Bundle savedInstanceState) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700124 View view = super.onCreateView(inflater, container, savedInstanceState);
125
126 Activity activity = requireActivity();
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700127 mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize(
128 activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700129
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700130 // TODO: Consider moving some part of this to the base class when live preview is ready.
131 view.findViewById(R.id.low_res_image).setVisibility(View.GONE);
132 view.findViewById(R.id.full_res_image).setVisibility(View.GONE);
133 mLoadingProgressBar.hide();
134 mContainer = view.findViewById(R.id.container);
135 mTouchForwardingLayout = mContainer.findViewById(R.id.touch_forwarding_layout);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700136
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700137 // Set aspect ratio on the preview card dynamically.
138 ConstraintSet set = new ConstraintSet();
139 set.clone(mContainer);
140 String ratio = String.format("%d:%d", mScreenSize.x, mScreenSize.y);
141 set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio);
142 set.applyTo(mContainer);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700143
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700144 mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface);
145 mWorkspaceSurfaceCallback = new WorkspaceSurfaceHolderCallback(mWorkspaceSurface,
146 getContext());
147 mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface);
148 mLockOverlay = mContainer.findViewById(R.id.lock_overlay);
149 mLockScreenOverlayUpdater = new LockScreenOverlayUpdater(
150 getContext(), mLockOverlay, getLifecycle());
151 mLockScreenOverlayUpdater.adjustOverlayLayout(true);
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800152
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700153 mTabs = view.findViewById(R.id.tabs_container);
154 View lock = mTabs.findViewById(R.id.lock);
155 View home = mTabs.findViewById(R.id.home);
156 lock.setOnClickListener(v -> showLockscreenPreview());
157 home.setOnClickListener(v -> showHomescreenPreview());
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700158
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700159 onBottomActionBarReady(mBottomActionBar);
160 view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY),
161 makeMeasureSpec(mScreenSize.y, EXACTLY));
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700162
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700163 ((CardView) mWorkspaceSurface.getParent())
164 .setRadius(SizeCalculator.getPreviewCornerRadius(
165 activity, mContainer.getMeasuredWidth()));
166 renderImageWallpaper();
167 renderWorkspaceSurface();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700168
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800169 mInfoPageController = new InfoPageController(view.findViewById(R.id.page_info),
170 mPreviewMode);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700171
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700172 // Trim some memory from Glide to make room for the full-size image in this fragment.
173 Glide.get(activity).setMemoryCategory(MemoryCategory.LOW);
174
175 mDefaultCropSurfaceSize = WallpaperCropUtils.getDefaultCropSurfaceSize(
176 getResources(), activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700177
178 // Load a low-res placeholder image if there's a thumbnail available from the asset that can
179 // be shown to the user more quickly than the full-sized image.
180 if (mWallpaperAsset.hasLowResDataSource()) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700181 mHandler.post(() ->
182 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK,
183 new WallpaperPreviewBitmapTransformation(
184 activity.getApplicationContext(), isRtl())));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700185 }
186
187 mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
188 // Don't continue loading the wallpaper if the Fragment is detached.
189 if (getActivity() == null) {
190 return;
191 }
192
193 // Return early and show a dialog if dimensions are null (signaling a decoding error).
194 if (dimensions == null) {
195 showLoadWallpaperErrorDialog();
196 return;
197 }
198
199 mRawWallpaperSize = dimensions;
chihhangchuangc687d912020-05-04 14:33:05 +0800200
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700201 setUpExploreIntentAndLabel(ImagePreviewFragment.this::initFullResView);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700202 });
203
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800204 setUpLoadingIndicator();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700205
206 return view;
207 }
208
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800209 @Override
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800210 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
211 super.onViewCreated(view, savedInstanceState);
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800212
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700213 WallpaperColorsLoader.getWallpaperColors(
214 getContext(),
215 mWallpaperAsset,
216 mWallpaperSurface.getMeasuredWidth(),
217 mWallpaperSurface.getMeasuredHeight(),
218 mLockScreenOverlayUpdater::setColor);
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800219 }
220
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800221 @Override
222 protected boolean isLoaded() {
223 return mFullResImageView != null && mFullResImageView.hasImage();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700224 }
225
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700226 @Override
227 public void onClickOk() {
228 FragmentActivity activity = getActivity();
229 if (activity != null) {
230 activity.finish();
231 }
232 }
233
234 @Override
235 public void onDestroy() {
236 super.onDestroy();
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800237 if (mLoadingProgressBar != null) {
238 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700239 }
240 mFullResImageView.recycle();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700241
242 mWorkspaceSurfaceCallback.cleanUp();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700243 }
244
245 @Override
246 public void onSaveInstanceState(Bundle outState) {
247 super.onSaveInstanceState(outState);
248
249 final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(mBottomSheet);
250 outState.putInt(KEY_BOTTOM_SHEET_STATE, bottomSheetBehavior.getState());
251 }
252
253 @Override
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700254 protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700255 super.onBottomActionBarReady(bottomActionBar);
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700256
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700257 mWallpaperInfoView = (WallpaperInfoView)
258 LayoutInflater.from(getContext()).inflate(
259 R.layout.wallpaper_info_view, /* root= */null);
260 mBottomActionBar.attachViewToBottomSheetAndBindAction(mWallpaperInfoView, INFORMATION);
261 mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY);
262 mBottomActionBar.bindBackButtonToSystemBackKey(getActivity());
263 mBottomActionBar.setActionClickListener(EDIT, v ->
Tracy Zhou16112e32020-05-27 13:13:23 -0700264 setEditingEnabled(mBottomActionBar.isActionSelected(EDIT))
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700265 );
266 mBottomActionBar.setActionSelectedListener(EDIT,
267 selected -> {
268 if (!selected) {
269 setEditingEnabled(false);
270 }
271 });
272 mBottomActionBar.setActionClickListener(APPLY, v -> onSetWallpaperClicked(v));
273 mBottomActionBar.show();
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700274 }
275
276 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700277 protected void setBottomSheetContentAlpha(float alpha) {
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800278 mInfoPageController.setContentAlpha(alpha);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700279 }
280
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800281 @Override
282 protected CharSequence getExploreButtonLabel(Context context) {
283 return context.getString(mWallpaper.getActionLabelRes(context));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700284 }
285
286 /**
287 * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
288 * initializing a zoom-scroll observer and click listener.
289 */
290 private void initFullResView() {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700291 setEditingEnabled(false);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700292 mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
293
294 // Set a solid black "page bitmap" so MosaicView draws a black background while waiting
295 // for the image to load or a transparent one if a thumbnail already loaded.
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700296 Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700297 int preColor = ContextCompat.getColor(getContext(), R.color.fullscreen_preview_background);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700298 int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT;
299 backgroundBitmap.setPixel(0, 0, color);
300 mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700301
302 // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual
303 // (lower res) version of the image to be displayed.
304 Point targetPageBitmapSize = new Point(mRawWallpaperSize);
305 mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y,
306 pageBitmap -> {
307 // Check that the activity is still around since the decoding task started.
308 if (getActivity() == null) {
309 return;
310 }
311
312 // Some of these may be null depending on if the Fragment is paused, stopped,
313 // or destroyed.
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800314 if (mLoadingProgressBar != null) {
315 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700316 }
317 // The page bitmap may be null if there was a decoding error, so show an
318 // error dialog.
319 if (pageBitmap == null) {
320 showLoadWallpaperErrorDialog();
321 return;
322 }
323 if (mFullResImageView != null) {
324 // Set page bitmap.
325 mFullResImageView.setImage(ImageSource.bitmap(pageBitmap));
326
327 setDefaultWallpaperZoomAndScroll();
328 crossFadeInMosaicView();
329 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700330 getActivity().invalidateOptionsMenu();
331
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700332 if (mWallpaperInfoView != null && mWallpaper != null) {
333 mWallpaperInfoView.populateWallpaperInfo(mWallpaper, mActionLabel,
334 mExploreIntent, this::onExploreClicked);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700335 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700336 });
337 }
338
339 /**
340 * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading
341 * indicator.
342 */
343 private void crossFadeInMosaicView() {
344 long shortAnimationDuration = getResources().getInteger(
345 android.R.integer.config_shortAnimTime);
346
347 mFullResImageView.setAlpha(0f);
348 mFullResImageView.animate()
349 .alpha(1f)
350 .setDuration(shortAnimationDuration)
351 .setListener(new AnimatorListenerAdapter() {
352 @Override
353 public void onAnimationEnd(Animator animation) {
354 // Clear the thumbnail bitmap reference to save memory since it's no longer
355 // visible.
356 if (mLowResImageView != null) {
357 mLowResImageView.setImageBitmap(null);
358 }
359 }
360 });
361
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800362 mLoadingProgressBar.animate()
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700363 .alpha(0f)
364 .setDuration(shortAnimationDuration)
365 .setListener(new AnimatorListenerAdapter() {
366 @Override
367 public void onAnimationEnd(Animator animation) {
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800368 if (mLoadingProgressBar != null) {
369 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700370 }
371 }
372 });
373 }
374
375 /**
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800376 * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra
377 * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as
378 * possible on the crop surface and align screen to crop surface such that the default preview
379 * matches what would be seen by the user in the left-most home screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700380 *
381 * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded
382 * and rendered to the layout.
383 */
384 private void setDefaultWallpaperZoomAndScroll() {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700385 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
386 int cropWidth = mWorkspaceSurface.getMeasuredWidth();
387 int cropHeight = mWorkspaceSurface.getMeasuredHeight();
388 Point crop = new Point(cropWidth, cropHeight);
389 Rect visibleRawWallpaperRect =
390 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
391 WallpaperCropUtils.adjustCurrentWallpaperCropRect(getContext(), mRawWallpaperSize,
392 visibleRawWallpaperRect);
393 Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
394 visibleRawWallpaperRect.height());
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700395
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700396 final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
397 visibleRawWallpaperSize, crop);
398 final float minWallpaperZoom = defaultWallpaperZoom;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700399
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700400 final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
401 visibleRawWallpaperRect.centerY());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700402
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700403 // Set min wallpaper zoom and max zoom on MosaicView widget.
404 mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
405 mFullResImageView.setMinScale(minWallpaperZoom);
406
407 // Set center to composite positioning between scaled wallpaper and screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700408 mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
409 }
410
411 private Rect calculateCropRect() {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700412 float wallpaperZoom = mFullResImageView.getScale();
Chuck Liao5182a312020-03-04 20:04:18 +0800413 Context context = requireContext().getApplicationContext();
414 Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay();
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700415
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700416 Rect visibleFileRect = new Rect();
417 mFullResImageView.visibleFileRect(visibleFileRect);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700418
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700419 int cropWidth = mWorkspaceSurface.getMeasuredWidth();
420 int cropHeight = mWorkspaceSurface.getMeasuredHeight();
421 Point hostViewSize = new Point(cropWidth, cropHeight);
422 Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize,
423 hostViewSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
424 WallpaperCropUtils.adjustCropRect(context, cropRect, false /* zoomIn */);
425 return cropRect;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700426 }
427
428 @Override
429 protected void setCurrentWallpaper(@Destination int destination) {
430 mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
431 destination, mFullResImageView.getScale(), calculateCropRect(),
432 new SetWallpaperCallback() {
433 @Override
“Chuckffd832c2020-03-22 02:15:58 +0800434 public void onSuccess(WallpaperInfo wallpaperInfo) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700435 finishActivityWithResultOk();
436 }
437
438 @Override
439 public void onError(@Nullable Throwable throwable) {
440 showSetWallpaperErrorDialog(destination);
441 }
442 });
443 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700444
445 private void renderWorkspaceSurface() {
446 mWorkspaceSurface.setZOrderMediaOverlay(true);
447 mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback);
448 }
449
450 private void renderImageWallpaper() {
451 mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
452 }
453
454 // TODO(tracyzhou): Refactor this into a utility class.
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700455 private final SurfaceHolder.Callback mWallpaperSurfaceCallback = new SurfaceHolder.Callback() {
456
457 private Surface mLastSurface;
458
459 @Override
460 public void surfaceCreated(SurfaceHolder holder) {
461 if (mLastSurface != holder.getSurface()) {
462 mLastSurface = holder.getSurface();
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700463 if (mFullResImageView != null) {
464 mFullResImageView.recycle();
465 }
466 View wallpaperPreviewContainer = LayoutInflater.from(getContext()).inflate(
467 R.layout.fullscreen_wallpaper_preview, null);
468 mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image);
469 mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image);
470 wallpaperPreviewContainer.measure(
471 makeMeasureSpec(mWallpaperSurface.getWidth(), EXACTLY),
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700472 makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY));
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700473 wallpaperPreviewContainer.layout(0, 0, mWallpaperSurface.getWidth(),
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700474 mWallpaperSurface.getHeight());
Ching-Sung Lib9404622020-05-18 22:38:45 +0800475 mTouchForwardingLayout.setTargetView(mFullResImageView);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700476
477 SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(),
478 getContext().getDisplay(), mWallpaperSurface.getHostToken());
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700479 host.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
480 wallpaperPreviewContainer.getHeight());
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700481 mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
482 }
483 }
484
485 @Override
486 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
487
488 @Override
489 public void surfaceDestroyed(SurfaceHolder holder) { }
490 };
491
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700492 private void setEditingEnabled(boolean enabled) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700493 mTouchForwardingLayout.setForwardingEnabled(enabled);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700494 }
495
496 private void showHomescreenPreview() {
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700497 mWorkspaceSurface.setVisibility(View.VISIBLE);
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800498 mLockOverlay.setVisibility(View.GONE);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700499 }
500
501 private void showLockscreenPreview() {
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800502 mWorkspaceSurface.setVisibility(View.GONE);
503 mLockOverlay.setVisibility(View.VISIBLE);
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700504 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700505}