blob: 83af285472bb290f146fbcadba74ff54587d7584 [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;
30import android.graphics.Bitmap.Config;
31import android.graphics.Color;
32import android.graphics.Point;
33import android.graphics.PointF;
34import android.graphics.Rect;
35import android.os.Bundle;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070036import android.os.Message;
37import android.os.RemoteException;
38import android.util.DisplayMetrics;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070039import android.view.Display;
40import android.view.LayoutInflater;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070041import android.view.Surface;
42import android.view.SurfaceControlViewHost;
43import android.view.SurfaceHolder;
44import android.view.SurfaceView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070045import android.view.View;
46import android.view.ViewGroup;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070047import android.widget.FrameLayout;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070048import android.widget.ImageView;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070049import android.widget.TextView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070050
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070051import androidx.annotation.IntDef;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070052import androidx.annotation.Nullable;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070053import androidx.appcompat.widget.Toolbar;
54import androidx.cardview.widget.CardView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070055import androidx.fragment.app.FragmentActivity;
56
57import com.android.wallpaper.R;
58import com.android.wallpaper.asset.Asset;
“Chuckffd832c2020-03-22 02:15:58 +080059import com.android.wallpaper.model.WallpaperInfo;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070060import com.android.wallpaper.module.WallpaperPersister.Destination;
61import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070062import com.android.wallpaper.util.PreviewUtils;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070063import com.android.wallpaper.util.ScreenSizeCalculator;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070064import com.android.wallpaper.util.SurfaceViewUtils;
65import com.android.wallpaper.util.TileSizeCalculator;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070066import com.android.wallpaper.util.WallpaperCropUtils;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070067import com.android.wallpaper.widget.BottomActionBar;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070068
69import com.bumptech.glide.Glide;
70import com.bumptech.glide.MemoryCategory;
71import com.davemorrissey.labs.subscaleview.ImageSource;
72import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
73import com.google.android.material.bottomsheet.BottomSheetBehavior;
74
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070075import java.lang.annotation.Retention;
76import java.lang.annotation.RetentionPolicy;
77
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070078/**
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -080079 * Fragment which displays the UI for previewing an individual static wallpaper and its attribution
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070080 * information.
81 */
82public class ImagePreviewFragment extends PreviewFragment {
83
84 private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f;
85
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070086 private static final int MODE_DEFAULT = 0;
87 private static final int MODE_SHOW_TABS = 1;
88
89 @IntDef({MODE_DEFAULT, MODE_SHOW_TABS})
90 @Retention(RetentionPolicy.SOURCE)
91 private @interface Mode {}
92
93 private @Mode int mMode = MODE_DEFAULT;
94
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070095 private SubsamplingScaleImageView mFullResImageView;
96 private Asset mWallpaperAsset;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070097 private Point mDefaultCropSurfaceSize;
98 private Point mScreenSize;
99 private Point mRawWallpaperSize; // Native size of wallpaper image.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700100 private ImageView mLowResImageView;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700101 private TouchForwardingLayout mTouchForwardingLayout;
102 private FrameLayout mWorkspaceContainer;
103 private SurfaceView mWorkspaceSurface;
104 private SurfaceView mWallpaperSurface;
105 private View mTabs;
106 private PreviewUtils mPreviewUtils;
107 private BottomActionBar mBottomActionBar;
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800108 private InfoPageController mInfoPageController;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700109
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700110 @Override
111 public void onCreate(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113 mWallpaperAsset = mWallpaper.getAsset(requireContext().getApplicationContext());
114 }
115
116 @Override
117 protected int getLayoutResId() {
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700118 return USE_NEW_UI ? R.layout.fragment_image_preview_v2 : R.layout.fragment_image_preview;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700119 }
120
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800121
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700122 protected int getBottomSheetResId() {
123 return R.id.bottom_sheet;
124 }
125
126 @Override
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800127 protected int getLoadingIndicatorResId() {
128 return R.id.loading_indicator;
129 }
130
131 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700132 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800133 Bundle savedInstanceState) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700134 View view = super.onCreateView(inflater, container, savedInstanceState);
135
136 Activity activity = requireActivity();
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700137 mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize(
138 activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700139
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700140 mLowResImageView = view.findViewById(R.id.low_res_image);
141 if (USE_NEW_UI) {
142 // TODO: Switch to use AppbarFragment functionality to setTitle when switching
143 // PreviewFragment to extend AppbarFragment
144 Toolbar toolbar = view.findViewById(R.id.toolbar);
145 TextView toolbarTitleView = toolbar.findViewById(R.id.custom_toolbar_title);
146 toolbarTitleView.setText((R.string.preview));
147 // TODO: Consider moving some part of this to the base class when live preview is ready.
148 mTouchForwardingLayout = view.findViewById(R.id.touch_forwarding_layout);
149 mWorkspaceContainer = mTouchForwardingLayout.findViewById(R.id.workspace_container);
150 mWorkspaceSurface = mWorkspaceContainer.findViewById(R.id.workspace_surface);
151 mWallpaperSurface = view.findViewById(R.id.wallpaper_surface);
152 mTabs = view.findViewById(R.id.tabs_container);
153 mPreviewUtils = new PreviewUtils(getContext(),
154 getString(R.string.grid_control_metadata_name));
155 mBottomActionBar = view.findViewById(R.id.bottom_actionbar);
156 mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY);
157 mBottomActionBar.bindBackButtonToSystemBackKey(getActivity());
158 mBottomActionBar.show();
159 view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY),
160 makeMeasureSpec(mScreenSize.y, EXACTLY));
161
162 renderImageWallpaper();
163 setupPreview();
164 renderWorkspaceSurface();
165 } else {
166 mFullResImageView = view.findViewById(R.id.full_res_image);
167 }
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()) {
181 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK,
182 new WallpaperPreviewBitmapTransformation(activity.getApplicationContext(),
183 isRtl()));
184 }
185
186 mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
187 // Don't continue loading the wallpaper if the Fragment is detached.
188 if (getActivity() == null) {
189 return;
190 }
191
192 // Return early and show a dialog if dimensions are null (signaling a decoding error).
193 if (dimensions == null) {
194 showLoadWallpaperErrorDialog();
195 return;
196 }
197
198 mRawWallpaperSize = dimensions;
199 setUpExploreIntent(ImagePreviewFragment.this::initFullResView);
200 });
201
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800202 setUpLoadingIndicator();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700203
204 return view;
205 }
206
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800207 @Override
208 protected void setUpBottomSheetView(ViewGroup bottomSheet) {
209 // Nothing needed here.
210 }
211
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800212 @Override
213 protected boolean isLoaded() {
214 return mFullResImageView != null && mFullResImageView.hasImage();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700215 }
216
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700217 @Override
218 public void onClickOk() {
219 FragmentActivity activity = getActivity();
220 if (activity != null) {
221 activity.finish();
222 }
223 }
224
225 @Override
226 public void onDestroy() {
227 super.onDestroy();
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800228 if (mLoadingProgressBar != null) {
229 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700230 }
231 mFullResImageView.recycle();
232 }
233
234 @Override
235 public void onSaveInstanceState(Bundle outState) {
236 super.onSaveInstanceState(outState);
237
238 final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(mBottomSheet);
239 outState.putInt(KEY_BOTTOM_SHEET_STATE, bottomSheetBehavior.getState());
240 }
241
242 @Override
243 protected void setBottomSheetContentAlpha(float alpha) {
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800244 mInfoPageController.setContentAlpha(alpha);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700245 }
246
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800247 @Override
248 protected CharSequence getExploreButtonLabel(Context context) {
249 return context.getString(mWallpaper.getActionLabelRes(context));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700250 }
251
252 /**
253 * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
254 * initializing a zoom-scroll observer and click listener.
255 */
256 private void initFullResView() {
257 mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
258
259 // Set a solid black "page bitmap" so MosaicView draws a black background while waiting
260 // for the image to load or a transparent one if a thumbnail already loaded.
261 Bitmap blackBitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
262 int color = (mLowResImageView.getDrawable() == null) ? Color.BLACK : Color.TRANSPARENT;
263 blackBitmap.setPixel(0, 0, color);
264 mFullResImageView.setImage(ImageSource.bitmap(blackBitmap));
265
266 // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual
267 // (lower res) version of the image to be displayed.
268 Point targetPageBitmapSize = new Point(mRawWallpaperSize);
269 mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y,
270 pageBitmap -> {
271 // Check that the activity is still around since the decoding task started.
272 if (getActivity() == null) {
273 return;
274 }
275
276 // Some of these may be null depending on if the Fragment is paused, stopped,
277 // or destroyed.
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800278 if (mLoadingProgressBar != null) {
279 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700280 }
281 // The page bitmap may be null if there was a decoding error, so show an
282 // error dialog.
283 if (pageBitmap == null) {
284 showLoadWallpaperErrorDialog();
285 return;
286 }
287 if (mFullResImageView != null) {
288 // Set page bitmap.
289 mFullResImageView.setImage(ImageSource.bitmap(pageBitmap));
290
291 setDefaultWallpaperZoomAndScroll();
292 crossFadeInMosaicView();
293 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700294 getActivity().invalidateOptionsMenu();
295
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700296 if (USE_NEW_UI) {
297 if (mWallpaper != null) {
298 mBottomActionBar.populateInfoPage(
299 mWallpaper.getAttributions(getContext()),
300 shouldShowMetadataInPreview(mWallpaper));
301 }
302 } else {
303 populateInfoPage(mInfoPageController);
304 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700305 });
306 }
307
308 /**
309 * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading
310 * indicator.
311 */
312 private void crossFadeInMosaicView() {
313 long shortAnimationDuration = getResources().getInteger(
314 android.R.integer.config_shortAnimTime);
315
316 mFullResImageView.setAlpha(0f);
317 mFullResImageView.animate()
318 .alpha(1f)
319 .setDuration(shortAnimationDuration)
320 .setListener(new AnimatorListenerAdapter() {
321 @Override
322 public void onAnimationEnd(Animator animation) {
323 // Clear the thumbnail bitmap reference to save memory since it's no longer
324 // visible.
325 if (mLowResImageView != null) {
326 mLowResImageView.setImageBitmap(null);
327 }
328 }
329 });
330
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800331 mLoadingProgressBar.animate()
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700332 .alpha(0f)
333 .setDuration(shortAnimationDuration)
334 .setListener(new AnimatorListenerAdapter() {
335 @Override
336 public void onAnimationEnd(Animator animation) {
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800337 if (mLoadingProgressBar != null) {
338 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700339 }
340 }
341 });
342 }
343
344 /**
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800345 * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra
346 * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as
347 * possible on the crop surface and align screen to crop surface such that the default preview
348 * matches what would be seen by the user in the left-most home screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700349 *
350 * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded
351 * and rendered to the layout.
352 */
353 private void setDefaultWallpaperZoomAndScroll() {
354 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
355 float defaultWallpaperZoom =
356 WallpaperCropUtils.calculateMinZoom(mRawWallpaperSize, mDefaultCropSurfaceSize);
357 float minWallpaperZoom =
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700358 WallpaperCropUtils.calculateMinZoom(mRawWallpaperSize,
359 USE_NEW_UI ? new Point(mTouchForwardingLayout.getWidth(),
360 mTouchForwardingLayout.getHeight()) : mScreenSize);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700361
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700362 // Set min wallpaper zoom and max zoom on MosaicView widget.
363 mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
364 mFullResImageView.setMinScale(minWallpaperZoom);
365
366 // Set center to composite positioning between scaled wallpaper and screen.
367 PointF centerPosition = new PointF(
368 mRawWallpaperSize.x / 2f,
369 mRawWallpaperSize.y / 2f);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700370
371 mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
372 }
373
374 private Rect calculateCropRect() {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700375 float wallpaperZoom = mFullResImageView.getScale();
Chuck Liao5182a312020-03-04 20:04:18 +0800376 Context context = requireContext().getApplicationContext();
377 Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700378 Rect rect = new Rect();
379 mFullResImageView.visibleFileRect(rect);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700380
Chuck Liao5182a312020-03-04 20:04:18 +0800381 return WallpaperCropUtils.calculateCropRect(context, defaultDisplay, mRawWallpaperSize,
382 rect, wallpaperZoom);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700383 }
384
385 @Override
386 protected void setCurrentWallpaper(@Destination int destination) {
387 mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
388 destination, mFullResImageView.getScale(), calculateCropRect(),
389 new SetWallpaperCallback() {
390 @Override
“Chuckffd832c2020-03-22 02:15:58 +0800391 public void onSuccess(WallpaperInfo wallpaperInfo) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700392 finishActivityWithResultOk();
393 }
394
395 @Override
396 public void onError(@Nullable Throwable throwable) {
397 showSetWallpaperErrorDialog(destination);
398 }
399 });
400 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700401
402 private void renderWorkspaceSurface() {
403 mWorkspaceSurface.setZOrderMediaOverlay(true);
404 mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback);
405 }
406
407 private void renderImageWallpaper() {
408 mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
409 }
410
411 // TODO(tracyzhou): Refactor this into a utility class.
412 private final SurfaceHolder.Callback mWorkspaceSurfaceCallback = new SurfaceHolder.Callback() {
413
414 private Surface mLastSurface;
415 private Message mCallback;
416
417 @Override
418 public void surfaceCreated(SurfaceHolder holder) {
419 if (mPreviewUtils.supportsPreview() && mLastSurface != holder.getSurface()) {
420 mLastSurface = holder.getSurface();
421 Bundle result = mPreviewUtils.renderPreview(
422 SurfaceViewUtils.createSurfaceViewRequest(mWorkspaceSurface));
423 if (result != null) {
424 mWorkspaceSurface.setChildSurfacePackage(
425 SurfaceViewUtils.getSurfacePackage(result));
426 mCallback = SurfaceViewUtils.getCallback(result);
427 }
428 }
429 }
430
431 @Override
432 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
433
434 @Override
435 public void surfaceDestroyed(SurfaceHolder holder) {
436 if (mCallback != null) {
437 try {
438 mCallback.replyTo.send(mCallback);
439 } catch (RemoteException e) {
440 e.printStackTrace();
441 } finally {
442 mCallback = null;
443 }
444 }
445 }
446 };
447
448 // TODO(tracyzhou): Refactor this into a utility class.
449 private final SurfaceHolder.Callback mWallpaperSurfaceCallback = new SurfaceHolder.Callback() {
450
451 private Surface mLastSurface;
452
453 @Override
454 public void surfaceCreated(SurfaceHolder holder) {
455 if (mLastSurface != holder.getSurface()) {
456 mLastSurface = holder.getSurface();
457 mFullResImageView = new SubsamplingScaleImageView(getContext());
458 mFullResImageView.measure(makeMeasureSpec(mWallpaperSurface.getWidth(), EXACTLY),
459 makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY));
460 mFullResImageView.layout(0, 0, mWallpaperSurface.getWidth(),
461 mWallpaperSurface.getHeight());
462 mTouchForwardingLayout.setView(mFullResImageView);
463
464 SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(),
465 getContext().getDisplay(), mWallpaperSurface.getHostToken());
466 host.setView(mFullResImageView, mFullResImageView.getWidth(),
467 mFullResImageView.getHeight());
468 mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
469 }
470 }
471
472 @Override
473 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
474
475 @Override
476 public void surfaceDestroyed(SurfaceHolder holder) { }
477 };
478
479 // TODO(chriscsli): Investigate the possibility of moving this to the base class.
480 private void setupPreview() {
481 if (USE_NEW_UI) {
482 mTabs.setVisibility(mMode == MODE_SHOW_TABS ? View.VISIBLE : View.GONE);
483
484 final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
485 int containerWidth = mWorkspaceContainer.getMeasuredWidth();
486 int containerHeight = mWorkspaceContainer.getMeasuredHeight();
487
488 int topPadding = dpToPx(24, displayMetrics.density);
489 int bottomPadding = dpToPx(getBottomPaddingInDp(), displayMetrics.density);
490 double horizontalPadding = containerWidth
491 - (containerHeight - topPadding - bottomPadding) * 1.0
492 * displayMetrics.widthPixels / displayMetrics.heightPixels;
493 int leftPadding = (int) horizontalPadding / 2;
494 int rightPadding = (int) horizontalPadding - leftPadding;
495 mWorkspaceContainer.setPaddingRelative(leftPadding, topPadding, rightPadding,
496 bottomPadding);
497 ((CardView) mWorkspaceSurface.getParent())
498 .setRadius(TileSizeCalculator.getPreviewCornerRadius(
499 getActivity(),
500 (int) (mWorkspaceContainer.getMeasuredWidth() - horizontalPadding)));
501 }
502 }
503
504 private int getBottomPaddingInDp() {
505 switch (mMode) {
506 case MODE_SHOW_TABS:
507 return 0;
508 case MODE_DEFAULT:
509 default:
510 return 32;
511 }
512 }
513
514 private static int dpToPx(int dp, float density) {
515 return (int) (dp * density + 0.5f);
516 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700517}