blob: ce61731443f65f933becca84f10da34a47155fdd [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 Zhoua19cbdd2020-04-26 12:12:11 -0700108 return USE_NEW_UI ? R.layout.fragment_image_preview_v2 : R.layout.fragment_image_preview;
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 Zhoua19cbdd2020-04-26 12:12:11 -0700130 if (USE_NEW_UI) {
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700131 // TODO: Consider moving some part of this to the base class when live preview is ready.
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700132 view.findViewById(R.id.low_res_image).setVisibility(View.GONE);
133 view.findViewById(R.id.full_res_image).setVisibility(View.GONE);
134 mLoadingProgressBar.hide();
135 mContainer = view.findViewById(R.id.container);
136 mTouchForwardingLayout = mContainer.findViewById(R.id.touch_forwarding_layout);
137
138 // Set aspect ratio on the preview card dynamically.
139 ConstraintSet set = new ConstraintSet();
140 set.clone(mContainer);
141 String ratio = String.format("%d:%d", mScreenSize.x, mScreenSize.y);
142 set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio);
143 set.applyTo(mContainer);
144
145 mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface);
146 mWorkspaceSurfaceCallback = new WorkspaceSurfaceHolderCallback(mWorkspaceSurface,
147 getContext());
148 mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface);
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800149 mLockOverlay = mContainer.findViewById(R.id.lock_overlay);
150 mLockScreenOverlayUpdater = new LockScreenOverlayUpdater(
151 getContext(), mLockOverlay, getLifecycle());
152 mLockScreenOverlayUpdater.adjustOverlayLayout(true);
153
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700154 mTabs = view.findViewById(R.id.tabs_container);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700155 View lock = mTabs.findViewById(R.id.lock);
156 View home = mTabs.findViewById(R.id.home);
157 lock.setOnClickListener(v -> showLockscreenPreview());
158 home.setOnClickListener(v -> showHomescreenPreview());
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700159
160 onBottomActionBarReady(mBottomActionBar);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700161 view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY),
162 makeMeasureSpec(mScreenSize.y, EXACTLY));
163
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700164 ((CardView) mWorkspaceSurface.getParent())
165 .setRadius(SizeCalculator.getPreviewCornerRadius(
166 activity, mContainer.getMeasuredWidth()));
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700167 renderImageWallpaper();
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700168 renderWorkspaceSurface();
169 } else {
170 mFullResImageView = view.findViewById(R.id.full_res_image);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700171 mLowResImageView = view.findViewById(R.id.low_res_image);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700172 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700173
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800174 mInfoPageController = new InfoPageController(view.findViewById(R.id.page_info),
175 mPreviewMode);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700176
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700177 // Trim some memory from Glide to make room for the full-size image in this fragment.
178 Glide.get(activity).setMemoryCategory(MemoryCategory.LOW);
179
180 mDefaultCropSurfaceSize = WallpaperCropUtils.getDefaultCropSurfaceSize(
181 getResources(), activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700182
183 // Load a low-res placeholder image if there's a thumbnail available from the asset that can
184 // be shown to the user more quickly than the full-sized image.
185 if (mWallpaperAsset.hasLowResDataSource()) {
Tracy Zhouc5a6bad2020-05-20 23:28:22 -0700186 if (!USE_NEW_UI) {
187 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK,
188 new WallpaperPreviewBitmapTransformation(
189 activity.getApplicationContext(), isRtl()));
190 } else {
191 mHandler.post(() ->
192 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK,
193 new WallpaperPreviewBitmapTransformation(
194 activity.getApplicationContext(), isRtl())));
195 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700196 }
197
198 mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
199 // Don't continue loading the wallpaper if the Fragment is detached.
200 if (getActivity() == null) {
201 return;
202 }
203
204 // Return early and show a dialog if dimensions are null (signaling a decoding error).
205 if (dimensions == null) {
206 showLoadWallpaperErrorDialog();
207 return;
208 }
209
210 mRawWallpaperSize = dimensions;
chihhangchuangc687d912020-05-04 14:33:05 +0800211
212 if (USE_NEW_UI) {
213 setUpExploreIntentAndLabel(ImagePreviewFragment.this::initFullResView);
214 } else {
215 setUpExploreIntent(ImagePreviewFragment.this::initFullResView);
216 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700217 });
218
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800219 setUpLoadingIndicator();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700220
221 return view;
222 }
223
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800224 @Override
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800225 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
226 super.onViewCreated(view, savedInstanceState);
227 if (USE_NEW_UI) {
228 WallpaperColorsLoader.getWallpaperColors(
229 getContext(),
230 mWallpaperAsset,
231 mWallpaperSurface.getMeasuredWidth(),
232 mWallpaperSurface.getMeasuredHeight(),
233 mLockScreenOverlayUpdater::setColor);
234 }
235 }
236
237 @Override
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800238 protected void setUpBottomSheetView(ViewGroup bottomSheet) {
239 // Nothing needed here.
240 }
241
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800242 @Override
243 protected boolean isLoaded() {
244 return mFullResImageView != null && mFullResImageView.hasImage();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700245 }
246
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700247 @Override
248 public void onClickOk() {
249 FragmentActivity activity = getActivity();
250 if (activity != null) {
251 activity.finish();
252 }
253 }
254
255 @Override
256 public void onDestroy() {
257 super.onDestroy();
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800258 if (mLoadingProgressBar != null) {
259 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700260 }
261 mFullResImageView.recycle();
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700262 if (USE_NEW_UI) {
263 mWorkspaceSurfaceCallback.cleanUp();
264 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700265 }
266
267 @Override
268 public void onSaveInstanceState(Bundle outState) {
269 super.onSaveInstanceState(outState);
270
271 final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(mBottomSheet);
272 outState.putInt(KEY_BOTTOM_SHEET_STATE, bottomSheetBehavior.getState());
273 }
274
275 @Override
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700276 protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
277 if (USE_NEW_UI) {
278 super.onBottomActionBarReady(bottomActionBar);
279
280 mWallpaperInfoView = (WallpaperInfoView)
281 LayoutInflater.from(getContext()).inflate(
282 R.layout.wallpaper_info_view, /* root= */null);
283 mBottomActionBar.attachViewToBottomSheetAndBindAction(mWallpaperInfoView, INFORMATION);
284 mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY);
285 mBottomActionBar.bindBackButtonToSystemBackKey(getActivity());
286 mBottomActionBar.setActionClickListener(EDIT, v -> {
287 setEditingEnabled(mBottomActionBar.isActionSelected(EDIT));
288 });
289 mBottomActionBar.setActionClickListener(APPLY, v -> {
290 onSetWallpaperClicked(v);
291 setEditingEnabled(false);
292 });
293 mBottomActionBar.show();
294 }
295 }
296
297 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700298 protected void setBottomSheetContentAlpha(float alpha) {
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800299 mInfoPageController.setContentAlpha(alpha);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700300 }
301
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800302 @Override
303 protected CharSequence getExploreButtonLabel(Context context) {
304 return context.getString(mWallpaper.getActionLabelRes(context));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700305 }
306
307 /**
308 * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
309 * initializing a zoom-scroll observer and click listener.
310 */
311 private void initFullResView() {
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700312 setEditingEnabled(!USE_NEW_UI);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700313 mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
314
315 // Set a solid black "page bitmap" so MosaicView draws a black background while waiting
316 // for the image to load or a transparent one if a thumbnail already loaded.
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700317 Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
318 int preColor = USE_NEW_UI ? ContextCompat.getColor(getContext(),
319 R.color.fullscreen_preview_background) : Color.BLACK;
320 int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT;
321 backgroundBitmap.setPixel(0, 0, color);
322 mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700323
324 // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual
325 // (lower res) version of the image to be displayed.
326 Point targetPageBitmapSize = new Point(mRawWallpaperSize);
327 mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y,
328 pageBitmap -> {
329 // Check that the activity is still around since the decoding task started.
330 if (getActivity() == null) {
331 return;
332 }
333
334 // Some of these may be null depending on if the Fragment is paused, stopped,
335 // or destroyed.
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800336 if (mLoadingProgressBar != null) {
337 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700338 }
339 // The page bitmap may be null if there was a decoding error, so show an
340 // error dialog.
341 if (pageBitmap == null) {
342 showLoadWallpaperErrorDialog();
343 return;
344 }
345 if (mFullResImageView != null) {
346 // Set page bitmap.
347 mFullResImageView.setImage(ImageSource.bitmap(pageBitmap));
348
349 setDefaultWallpaperZoomAndScroll();
350 crossFadeInMosaicView();
351 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700352 getActivity().invalidateOptionsMenu();
353
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700354 if (USE_NEW_UI) {
chihhangchuangde9e8722020-05-02 17:22:13 +0800355 if (mWallpaperInfoView != null && mWallpaper != null) {
chihhangchuangc687d912020-05-04 14:33:05 +0800356 mWallpaperInfoView.populateWallpaperInfo(mWallpaper, mActionLabel,
357 mExploreIntent, this::onExploreClicked);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700358 }
359 } else {
360 populateInfoPage(mInfoPageController);
361 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700362 });
363 }
364
365 /**
366 * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading
367 * indicator.
368 */
369 private void crossFadeInMosaicView() {
370 long shortAnimationDuration = getResources().getInteger(
371 android.R.integer.config_shortAnimTime);
372
373 mFullResImageView.setAlpha(0f);
374 mFullResImageView.animate()
375 .alpha(1f)
376 .setDuration(shortAnimationDuration)
377 .setListener(new AnimatorListenerAdapter() {
378 @Override
379 public void onAnimationEnd(Animator animation) {
380 // Clear the thumbnail bitmap reference to save memory since it's no longer
381 // visible.
382 if (mLowResImageView != null) {
383 mLowResImageView.setImageBitmap(null);
384 }
385 }
386 });
387
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800388 mLoadingProgressBar.animate()
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700389 .alpha(0f)
390 .setDuration(shortAnimationDuration)
391 .setListener(new AnimatorListenerAdapter() {
392 @Override
393 public void onAnimationEnd(Animator animation) {
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800394 if (mLoadingProgressBar != null) {
395 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700396 }
397 }
398 });
399 }
400
401 /**
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800402 * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra
403 * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as
404 * possible on the crop surface and align screen to crop surface such that the default preview
405 * matches what would be seen by the user in the left-most home screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700406 *
407 * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded
408 * and rendered to the layout.
409 */
410 private void setDefaultWallpaperZoomAndScroll() {
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700411 final float defaultWallpaperZoom;
412 final float minWallpaperZoom;
413 final PointF centerPosition;
414
415 if (USE_NEW_UI) {
416 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
417 int cropWidth = mWorkspaceSurface.getMeasuredWidth();
418 int cropHeight = mWorkspaceSurface.getMeasuredHeight();
419 Point crop = new Point(cropWidth, cropHeight);
420 Rect visibleRawWallpaperRect =
421 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
422 WallpaperCropUtils.adjustCurrentWallpaperCropRect(getContext(), mRawWallpaperSize,
423 visibleRawWallpaperRect);
424 Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
425 visibleRawWallpaperRect.height());
426
427 defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
428 visibleRawWallpaperSize, crop);
429 minWallpaperZoom = defaultWallpaperZoom;
430
431 centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
432 visibleRawWallpaperRect.centerY());
433 } else {
434 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
435 defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
436 mRawWallpaperSize, mDefaultCropSurfaceSize);
437 minWallpaperZoom =
438 WallpaperCropUtils.calculateMinZoom(mRawWallpaperSize, mScreenSize);
439
440 centerPosition = new PointF(mRawWallpaperSize.x / 2f, mRawWallpaperSize.y / 2f);
441 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700442
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700443 // Set min wallpaper zoom and max zoom on MosaicView widget.
444 mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
445 mFullResImageView.setMinScale(minWallpaperZoom);
446
447 // Set center to composite positioning between scaled wallpaper and screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700448 mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
449 }
450
451 private Rect calculateCropRect() {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700452 float wallpaperZoom = mFullResImageView.getScale();
Chuck Liao5182a312020-03-04 20:04:18 +0800453 Context context = requireContext().getApplicationContext();
454 Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay();
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700455
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700456 Rect visibleFileRect = new Rect();
457 mFullResImageView.visibleFileRect(visibleFileRect);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700458
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700459 if (USE_NEW_UI) {
460 int cropWidth = mWorkspaceSurface.getMeasuredWidth();
461 int cropHeight = mWorkspaceSurface.getMeasuredHeight();
462 Point hostViewSize = new Point(cropWidth, cropHeight);
463 Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize,
464 hostViewSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
465 WallpaperCropUtils.adjustCropRect(context, cropRect, false /* zoomIn */);
466 return cropRect;
467 }
Chuck Liao5182a312020-03-04 20:04:18 +0800468 return WallpaperCropUtils.calculateCropRect(context, defaultDisplay, mRawWallpaperSize,
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700469 visibleFileRect, wallpaperZoom);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700470 }
471
472 @Override
473 protected void setCurrentWallpaper(@Destination int destination) {
474 mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
475 destination, mFullResImageView.getScale(), calculateCropRect(),
476 new SetWallpaperCallback() {
477 @Override
“Chuckffd832c2020-03-22 02:15:58 +0800478 public void onSuccess(WallpaperInfo wallpaperInfo) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700479 finishActivityWithResultOk();
480 }
481
482 @Override
483 public void onError(@Nullable Throwable throwable) {
484 showSetWallpaperErrorDialog(destination);
485 }
486 });
487 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700488
489 private void renderWorkspaceSurface() {
490 mWorkspaceSurface.setZOrderMediaOverlay(true);
491 mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback);
492 }
493
494 private void renderImageWallpaper() {
495 mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
496 }
497
498 // TODO(tracyzhou): Refactor this into a utility class.
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700499 private final SurfaceHolder.Callback mWallpaperSurfaceCallback = new SurfaceHolder.Callback() {
500
501 private Surface mLastSurface;
502
503 @Override
504 public void surfaceCreated(SurfaceHolder holder) {
505 if (mLastSurface != holder.getSurface()) {
506 mLastSurface = holder.getSurface();
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700507 if (mFullResImageView != null) {
508 mFullResImageView.recycle();
509 }
510 View wallpaperPreviewContainer = LayoutInflater.from(getContext()).inflate(
511 R.layout.fullscreen_wallpaper_preview, null);
512 mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image);
513 mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image);
514 wallpaperPreviewContainer.measure(
515 makeMeasureSpec(mWallpaperSurface.getWidth(), EXACTLY),
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700516 makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY));
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700517 wallpaperPreviewContainer.layout(0, 0, mWallpaperSurface.getWidth(),
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700518 mWallpaperSurface.getHeight());
Ching-Sung Lib9404622020-05-18 22:38:45 +0800519 mTouchForwardingLayout.setTargetView(mFullResImageView);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700520
521 SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(),
522 getContext().getDisplay(), mWallpaperSurface.getHostToken());
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700523 host.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
524 wallpaperPreviewContainer.getHeight());
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700525 mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
526 }
527 }
528
529 @Override
530 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
531
532 @Override
533 public void surfaceDestroyed(SurfaceHolder holder) { }
534 };
535
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700536 private void setEditingEnabled(boolean enabled) {
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700537 if (USE_NEW_UI) {
538 mTouchForwardingLayout.setForwardingEnabled(enabled);
539 }
540 }
541
542 private void showHomescreenPreview() {
543 // TODO (b/156129610): Update the behavior here.
544 mWorkspaceSurface.setVisibility(View.VISIBLE);
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800545 mLockOverlay.setVisibility(View.GONE);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700546 }
547
548 private void showLockscreenPreview() {
549 // TODO (b/156129610): Update the behavior here.
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800550 mWorkspaceSurface.setVisibility(View.GONE);
551 mLockOverlay.setVisibility(View.VISIBLE);
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700552 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700553}