Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.wallpaper.picker; |
| 17 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 18 | import static android.view.View.MeasureSpec.EXACTLY; |
| 19 | import static android.view.View.MeasureSpec.makeMeasureSpec; |
| 20 | |
| 21 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY; |
| 22 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT; |
| 23 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION; |
| 24 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 25 | import android.animation.Animator; |
| 26 | import android.animation.AnimatorListenerAdapter; |
| 27 | import android.app.Activity; |
| 28 | import android.content.Context; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
| 31 | import android.graphics.Point; |
| 32 | import android.graphics.PointF; |
| 33 | import android.graphics.Rect; |
| 34 | import android.os.Bundle; |
Tracy Zhou | c5a6bad | 2020-05-20 23:28:22 -0700 | [diff] [blame] | 35 | import android.os.Handler; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 36 | import android.view.Display; |
| 37 | import android.view.LayoutInflater; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 38 | import android.view.Surface; |
| 39 | import android.view.SurfaceControlViewHost; |
| 40 | import android.view.SurfaceHolder; |
| 41 | import android.view.SurfaceView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 42 | import android.view.View; |
| 43 | import android.view.ViewGroup; |
| 44 | import android.widget.ImageView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 45 | |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 46 | import androidx.annotation.NonNull; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 47 | import androidx.annotation.Nullable; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 48 | import androidx.cardview.widget.CardView; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 49 | import androidx.constraintlayout.widget.ConstraintLayout; |
| 50 | import androidx.constraintlayout.widget.ConstraintSet; |
| 51 | import androidx.core.content.ContextCompat; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 52 | import androidx.fragment.app.FragmentActivity; |
| 53 | |
| 54 | import com.android.wallpaper.R; |
| 55 | import com.android.wallpaper.asset.Asset; |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 56 | import com.android.wallpaper.model.WallpaperInfo; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 57 | import com.android.wallpaper.module.WallpaperPersister.Destination; |
| 58 | import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback; |
| 59 | import com.android.wallpaper.util.ScreenSizeCalculator; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 60 | import com.android.wallpaper.util.SizeCalculator; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 61 | import com.android.wallpaper.util.WallpaperCropUtils; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 62 | import com.android.wallpaper.widget.BottomActionBar; |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 63 | import com.android.wallpaper.widget.LockScreenOverlayUpdater; |
| 64 | import com.android.wallpaper.widget.WallpaperColorsLoader; |
Tracy Zhou | 3599742 | 2020-05-01 13:42:06 -0700 | [diff] [blame] | 65 | import com.android.wallpaper.widget.WallpaperInfoView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 66 | |
| 67 | import com.bumptech.glide.Glide; |
| 68 | import com.bumptech.glide.MemoryCategory; |
| 69 | import com.davemorrissey.labs.subscaleview.ImageSource; |
| 70 | import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; |
| 71 | import com.google.android.material.bottomsheet.BottomSheetBehavior; |
| 72 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 73 | /** |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 74 | * Fragment which displays the UI for previewing an individual static wallpaper and its attribution |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 75 | * information. |
| 76 | */ |
| 77 | public class ImagePreviewFragment extends PreviewFragment { |
| 78 | |
| 79 | private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f; |
| 80 | |
Tracy Zhou | c5a6bad | 2020-05-20 23:28:22 -0700 | [diff] [blame] | 81 | private final Handler mHandler = new Handler(); |
| 82 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 83 | private SubsamplingScaleImageView mFullResImageView; |
| 84 | private Asset mWallpaperAsset; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 85 | private Point mDefaultCropSurfaceSize; |
| 86 | private Point mScreenSize; |
| 87 | private Point mRawWallpaperSize; // Native size of wallpaper image. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 88 | private ImageView mLowResImageView; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 89 | private TouchForwardingLayout mTouchForwardingLayout; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 90 | private ConstraintLayout mContainer; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 91 | private SurfaceView mWorkspaceSurface; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 92 | private WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 93 | private SurfaceView mWallpaperSurface; |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 94 | private View mLockOverlay; |
| 95 | private LockScreenOverlayUpdater mLockScreenOverlayUpdater; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 96 | private View mTabs; |
Tracy Zhou | 3599742 | 2020-05-01 13:42:06 -0700 | [diff] [blame] | 97 | private WallpaperInfoView mWallpaperInfoView; |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 98 | private InfoPageController mInfoPageController; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 99 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 100 | @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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 108 | return USE_NEW_UI ? R.layout.fragment_image_preview_v2 : R.layout.fragment_image_preview; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 111 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 112 | protected int getBottomSheetResId() { |
| 113 | return R.id.bottom_sheet; |
| 114 | } |
| 115 | |
| 116 | @Override |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 117 | protected int getLoadingIndicatorResId() { |
| 118 | return R.id.loading_indicator; |
| 119 | } |
| 120 | |
| 121 | @Override |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 122 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 123 | Bundle savedInstanceState) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 124 | View view = super.onCreateView(inflater, container, savedInstanceState); |
| 125 | |
| 126 | Activity activity = requireActivity(); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 127 | mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize( |
| 128 | activity.getWindowManager().getDefaultDisplay()); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 129 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 130 | if (USE_NEW_UI) { |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 131 | // TODO: Consider moving some part of this to the base class when live preview is ready. |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 132 | 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 Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 149 | mLockOverlay = mContainer.findViewById(R.id.lock_overlay); |
| 150 | mLockScreenOverlayUpdater = new LockScreenOverlayUpdater( |
| 151 | getContext(), mLockOverlay, getLifecycle()); |
| 152 | mLockScreenOverlayUpdater.adjustOverlayLayout(true); |
| 153 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 154 | mTabs = view.findViewById(R.id.tabs_container); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 155 | 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 Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame^] | 159 | |
| 160 | onBottomActionBarReady(mBottomActionBar); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 161 | view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY), |
| 162 | makeMeasureSpec(mScreenSize.y, EXACTLY)); |
| 163 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 164 | ((CardView) mWorkspaceSurface.getParent()) |
| 165 | .setRadius(SizeCalculator.getPreviewCornerRadius( |
| 166 | activity, mContainer.getMeasuredWidth())); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 167 | renderImageWallpaper(); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 168 | renderWorkspaceSurface(); |
| 169 | } else { |
| 170 | mFullResImageView = view.findViewById(R.id.full_res_image); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 171 | mLowResImageView = view.findViewById(R.id.low_res_image); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 172 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 173 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 174 | mInfoPageController = new InfoPageController(view.findViewById(R.id.page_info), |
| 175 | mPreviewMode); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 176 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 177 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 182 | |
| 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 Zhou | c5a6bad | 2020-05-20 23:28:22 -0700 | [diff] [blame] | 186 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 196 | } |
| 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; |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 211 | |
| 212 | if (USE_NEW_UI) { |
| 213 | setUpExploreIntentAndLabel(ImagePreviewFragment.this::initFullResView); |
| 214 | } else { |
| 215 | setUpExploreIntent(ImagePreviewFragment.this::initFullResView); |
| 216 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 217 | }); |
| 218 | |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 219 | setUpLoadingIndicator(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 220 | |
| 221 | return view; |
| 222 | } |
| 223 | |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 224 | @Override |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 225 | 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 Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 238 | protected void setUpBottomSheetView(ViewGroup bottomSheet) { |
| 239 | // Nothing needed here. |
| 240 | } |
| 241 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 242 | @Override |
| 243 | protected boolean isLoaded() { |
| 244 | return mFullResImageView != null && mFullResImageView.hasImage(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 247 | @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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 258 | if (mLoadingProgressBar != null) { |
| 259 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 260 | } |
| 261 | mFullResImageView.recycle(); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 262 | if (USE_NEW_UI) { |
| 263 | mWorkspaceSurfaceCallback.cleanUp(); |
| 264 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 265 | } |
| 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 Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame^] | 276 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 298 | protected void setBottomSheetContentAlpha(float alpha) { |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 299 | mInfoPageController.setContentAlpha(alpha); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 302 | @Override |
| 303 | protected CharSequence getExploreButtonLabel(Context context) { |
| 304 | return context.getString(mWallpaper.getActionLabelRes(context)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 305 | } |
| 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 312 | setEditingEnabled(!USE_NEW_UI); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 313 | 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 317 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 323 | |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 336 | if (mLoadingProgressBar != null) { |
| 337 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 338 | } |
| 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 352 | getActivity().invalidateOptionsMenu(); |
| 353 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 354 | if (USE_NEW_UI) { |
chihhangchuang | de9e872 | 2020-05-02 17:22:13 +0800 | [diff] [blame] | 355 | if (mWallpaperInfoView != null && mWallpaper != null) { |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 356 | mWallpaperInfoView.populateWallpaperInfo(mWallpaper, mActionLabel, |
| 357 | mExploreIntent, this::onExploreClicked); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 358 | } |
| 359 | } else { |
| 360 | populateInfoPage(mInfoPageController); |
| 361 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 362 | }); |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 388 | mLoadingProgressBar.animate() |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 389 | .alpha(0f) |
| 390 | .setDuration(shortAnimationDuration) |
| 391 | .setListener(new AnimatorListenerAdapter() { |
| 392 | @Override |
| 393 | public void onAnimationEnd(Animator animation) { |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 394 | if (mLoadingProgressBar != null) { |
| 395 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | }); |
| 399 | } |
| 400 | |
| 401 | /** |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 402 | * 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 406 | * |
| 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 411 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 442 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 443 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 448 | mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition); |
| 449 | } |
| 450 | |
| 451 | private Rect calculateCropRect() { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 452 | float wallpaperZoom = mFullResImageView.getScale(); |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 453 | Context context = requireContext().getApplicationContext(); |
| 454 | Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay(); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 455 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 456 | Rect visibleFileRect = new Rect(); |
| 457 | mFullResImageView.visibleFileRect(visibleFileRect); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 458 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 459 | 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 Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 468 | return WallpaperCropUtils.calculateCropRect(context, defaultDisplay, mRawWallpaperSize, |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 469 | visibleFileRect, wallpaperZoom); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 470 | } |
| 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 |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 478 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 479 | finishActivityWithResultOk(); |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public void onError(@Nullable Throwable throwable) { |
| 484 | showSetWallpaperErrorDialog(destination); |
| 485 | } |
| 486 | }); |
| 487 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 488 | |
| 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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 499 | 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 507 | 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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 516 | makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY)); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 517 | wallpaperPreviewContainer.layout(0, 0, mWallpaperSurface.getWidth(), |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 518 | mWallpaperSurface.getHeight()); |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 519 | mTouchForwardingLayout.setTargetView(mFullResImageView); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 520 | |
| 521 | SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(), |
| 522 | getContext().getDisplay(), mWallpaperSurface.getHostToken()); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 523 | host.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(), |
| 524 | wallpaperPreviewContainer.getHeight()); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 525 | 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 Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 536 | private void setEditingEnabled(boolean enabled) { |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 537 | 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 Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 545 | mLockOverlay.setVisibility(View.GONE); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | private void showLockscreenPreview() { |
| 549 | // TODO (b/156129610): Update the behavior here. |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 550 | mWorkspaceSurface.setVisibility(View.GONE); |
| 551 | mLockOverlay.setVisibility(View.VISIBLE); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 552 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 553 | } |