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 | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 108 | return R.layout.fragment_image_preview_v2; |
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 | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 130 | // 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 136 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 137 | // 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 143 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 144 | 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 Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 152 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 153 | 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 Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 158 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 159 | onBottomActionBarReady(mBottomActionBar); |
| 160 | view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY), |
| 161 | makeMeasureSpec(mScreenSize.y, EXACTLY)); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 162 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 163 | ((CardView) mWorkspaceSurface.getParent()) |
| 164 | .setRadius(SizeCalculator.getPreviewCornerRadius( |
| 165 | activity, mContainer.getMeasuredWidth())); |
| 166 | renderImageWallpaper(); |
| 167 | renderWorkspaceSurface(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 168 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 169 | mInfoPageController = new InfoPageController(view.findViewById(R.id.page_info), |
| 170 | mPreviewMode); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 171 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 172 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 177 | |
| 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 Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 181 | mHandler.post(() -> |
| 182 | mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK, |
| 183 | new WallpaperPreviewBitmapTransformation( |
| 184 | activity.getApplicationContext(), isRtl()))); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 185 | } |
| 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; |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 200 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 201 | setUpExploreIntentAndLabel(ImagePreviewFragment.this::initFullResView); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 202 | }); |
| 203 | |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 204 | setUpLoadingIndicator(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 205 | |
| 206 | return view; |
| 207 | } |
| 208 | |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 209 | @Override |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 210 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 211 | super.onViewCreated(view, savedInstanceState); |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 212 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 213 | WallpaperColorsLoader.getWallpaperColors( |
| 214 | getContext(), |
| 215 | mWallpaperAsset, |
| 216 | mWallpaperSurface.getMeasuredWidth(), |
| 217 | mWallpaperSurface.getMeasuredHeight(), |
| 218 | mLockScreenOverlayUpdater::setColor); |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 221 | @Override |
| 222 | protected boolean isLoaded() { |
| 223 | return mFullResImageView != null && mFullResImageView.hasImage(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 226 | @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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 237 | if (mLoadingProgressBar != null) { |
| 238 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 239 | } |
| 240 | mFullResImageView.recycle(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 241 | |
| 242 | mWorkspaceSurfaceCallback.cleanUp(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 243 | } |
| 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 Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 254 | protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 255 | super.onBottomActionBarReady(bottomActionBar); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 256 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 257 | 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 Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 264 | setEditingEnabled(mBottomActionBar.isActionSelected(EDIT)) |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 265 | ); |
| 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 Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 277 | protected void setBottomSheetContentAlpha(float alpha) { |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 278 | mInfoPageController.setContentAlpha(alpha); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 281 | @Override |
| 282 | protected CharSequence getExploreButtonLabel(Context context) { |
| 283 | return context.getString(mWallpaper.getActionLabelRes(context)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 284 | } |
| 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 Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 291 | setEditingEnabled(false); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 292 | 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 296 | Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 297 | int preColor = ContextCompat.getColor(getContext(), R.color.fullscreen_preview_background); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 298 | int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT; |
| 299 | backgroundBitmap.setPixel(0, 0, color); |
| 300 | mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 301 | |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 314 | if (mLoadingProgressBar != null) { |
| 315 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 316 | } |
| 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 330 | getActivity().invalidateOptionsMenu(); |
| 331 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 332 | if (mWallpaperInfoView != null && mWallpaper != null) { |
| 333 | mWallpaperInfoView.populateWallpaperInfo(mWallpaper, mActionLabel, |
| 334 | mExploreIntent, this::onExploreClicked); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 335 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 336 | }); |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 362 | mLoadingProgressBar.animate() |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 363 | .alpha(0f) |
| 364 | .setDuration(shortAnimationDuration) |
| 365 | .setListener(new AnimatorListenerAdapter() { |
| 366 | @Override |
| 367 | public void onAnimationEnd(Animator animation) { |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 368 | if (mLoadingProgressBar != null) { |
| 369 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | }); |
| 373 | } |
| 374 | |
| 375 | /** |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 376 | * 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 380 | * |
| 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 Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 385 | // 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 395 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 396 | final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom( |
| 397 | visibleRawWallpaperSize, crop); |
| 398 | final float minWallpaperZoom = defaultWallpaperZoom; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 399 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 400 | final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(), |
| 401 | visibleRawWallpaperRect.centerY()); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 402 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 403 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 408 | mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition); |
| 409 | } |
| 410 | |
| 411 | private Rect calculateCropRect() { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 412 | float wallpaperZoom = mFullResImageView.getScale(); |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 413 | Context context = requireContext().getApplicationContext(); |
| 414 | Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay(); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 415 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 416 | Rect visibleFileRect = new Rect(); |
| 417 | mFullResImageView.visibleFileRect(visibleFileRect); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 418 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 419 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 426 | } |
| 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 |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 434 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 435 | finishActivityWithResultOk(); |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public void onError(@Nullable Throwable throwable) { |
| 440 | showSetWallpaperErrorDialog(destination); |
| 441 | } |
| 442 | }); |
| 443 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 444 | |
| 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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 455 | 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 463 | 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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 472 | makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY)); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 473 | wallpaperPreviewContainer.layout(0, 0, mWallpaperSurface.getWidth(), |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 474 | mWallpaperSurface.getHeight()); |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 475 | mTouchForwardingLayout.setTargetView(mFullResImageView); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 476 | |
| 477 | SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(), |
| 478 | getContext().getDisplay(), mWallpaperSurface.getHostToken()); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 479 | host.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(), |
| 480 | wallpaperPreviewContainer.getHeight()); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 481 | 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 Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 492 | private void setEditingEnabled(boolean enabled) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame^] | 493 | mTouchForwardingLayout.setForwardingEnabled(enabled); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | private void showHomescreenPreview() { |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 497 | mWorkspaceSurface.setVisibility(View.VISIBLE); |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 498 | mLockOverlay.setVisibility(View.GONE); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | private void showLockscreenPreview() { |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 502 | mWorkspaceSurface.setVisibility(View.GONE); |
| 503 | mLockOverlay.setVisibility(View.VISIBLE); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 504 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 505 | } |