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; |
| 30 | import android.graphics.Bitmap.Config; |
| 31 | import android.graphics.Color; |
| 32 | import android.graphics.Point; |
| 33 | import android.graphics.PointF; |
| 34 | import android.graphics.Rect; |
| 35 | import android.os.Bundle; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 36 | import android.os.Message; |
| 37 | import android.os.RemoteException; |
| 38 | import android.util.DisplayMetrics; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 39 | import android.view.Display; |
| 40 | import android.view.LayoutInflater; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 41 | import android.view.Surface; |
| 42 | import android.view.SurfaceControlViewHost; |
| 43 | import android.view.SurfaceHolder; |
| 44 | import android.view.SurfaceView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 47 | import android.widget.FrameLayout; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 48 | import android.widget.ImageView; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 49 | import android.widget.TextView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 50 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 51 | import androidx.annotation.IntDef; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 52 | import androidx.annotation.Nullable; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 53 | import androidx.appcompat.widget.Toolbar; |
| 54 | import androidx.cardview.widget.CardView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 55 | import androidx.fragment.app.FragmentActivity; |
| 56 | |
| 57 | import com.android.wallpaper.R; |
| 58 | import com.android.wallpaper.asset.Asset; |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 59 | import com.android.wallpaper.model.WallpaperInfo; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 60 | import com.android.wallpaper.module.WallpaperPersister.Destination; |
| 61 | import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 62 | import com.android.wallpaper.util.PreviewUtils; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 63 | import com.android.wallpaper.util.ScreenSizeCalculator; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 64 | import com.android.wallpaper.util.SurfaceViewUtils; |
| 65 | import com.android.wallpaper.util.TileSizeCalculator; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 66 | import com.android.wallpaper.util.WallpaperCropUtils; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 67 | import com.android.wallpaper.widget.BottomActionBar; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 68 | |
| 69 | import com.bumptech.glide.Glide; |
| 70 | import com.bumptech.glide.MemoryCategory; |
| 71 | import com.davemorrissey.labs.subscaleview.ImageSource; |
| 72 | import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; |
| 73 | import com.google.android.material.bottomsheet.BottomSheetBehavior; |
| 74 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 75 | import java.lang.annotation.Retention; |
| 76 | import java.lang.annotation.RetentionPolicy; |
| 77 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 78 | /** |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 79 | * 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] | 80 | * information. |
| 81 | */ |
| 82 | public class ImagePreviewFragment extends PreviewFragment { |
| 83 | |
| 84 | private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f; |
| 85 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 86 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 95 | private SubsamplingScaleImageView mFullResImageView; |
| 96 | private Asset mWallpaperAsset; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 97 | private Point mDefaultCropSurfaceSize; |
| 98 | private Point mScreenSize; |
| 99 | private Point mRawWallpaperSize; // Native size of wallpaper image. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 100 | private ImageView mLowResImageView; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 101 | 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 Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 108 | private InfoPageController mInfoPageController; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 109 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 110 | @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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 118 | 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] | 119 | } |
| 120 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 121 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 122 | protected int getBottomSheetResId() { |
| 123 | return R.id.bottom_sheet; |
| 124 | } |
| 125 | |
| 126 | @Override |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 127 | protected int getLoadingIndicatorResId() { |
| 128 | return R.id.loading_indicator; |
| 129 | } |
| 130 | |
| 131 | @Override |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 132 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 133 | Bundle savedInstanceState) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 134 | View view = super.onCreateView(inflater, container, savedInstanceState); |
| 135 | |
| 136 | Activity activity = requireActivity(); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 137 | mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize( |
| 138 | activity.getWindowManager().getDefaultDisplay()); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 139 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 140 | 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 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()) { |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 202 | setUpLoadingIndicator(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 203 | |
| 204 | return view; |
| 205 | } |
| 206 | |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 207 | @Override |
| 208 | protected void setUpBottomSheetView(ViewGroup bottomSheet) { |
| 209 | // Nothing needed here. |
| 210 | } |
| 211 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 212 | @Override |
| 213 | protected boolean isLoaded() { |
| 214 | return mFullResImageView != null && mFullResImageView.hasImage(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 217 | @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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 228 | if (mLoadingProgressBar != null) { |
| 229 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 230 | } |
| 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 Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 244 | mInfoPageController.setContentAlpha(alpha); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 247 | @Override |
| 248 | protected CharSequence getExploreButtonLabel(Context context) { |
| 249 | return context.getString(mWallpaper.getActionLabelRes(context)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 250 | } |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 278 | if (mLoadingProgressBar != null) { |
| 279 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 280 | } |
| 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 294 | getActivity().invalidateOptionsMenu(); |
| 295 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 296 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 305 | }); |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 331 | mLoadingProgressBar.animate() |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 332 | .alpha(0f) |
| 333 | .setDuration(shortAnimationDuration) |
| 334 | .setListener(new AnimatorListenerAdapter() { |
| 335 | @Override |
| 336 | public void onAnimationEnd(Animator animation) { |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 337 | if (mLoadingProgressBar != null) { |
| 338 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | }); |
| 342 | } |
| 343 | |
| 344 | /** |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 345 | * 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 349 | * |
| 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 Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 358 | WallpaperCropUtils.calculateMinZoom(mRawWallpaperSize, |
| 359 | USE_NEW_UI ? new Point(mTouchForwardingLayout.getWidth(), |
| 360 | mTouchForwardingLayout.getHeight()) : mScreenSize); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 361 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 362 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 370 | |
| 371 | mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition); |
| 372 | } |
| 373 | |
| 374 | private Rect calculateCropRect() { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 375 | float wallpaperZoom = mFullResImageView.getScale(); |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 376 | Context context = requireContext().getApplicationContext(); |
| 377 | Display defaultDisplay = requireActivity().getWindowManager().getDefaultDisplay(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 378 | Rect rect = new Rect(); |
| 379 | mFullResImageView.visibleFileRect(rect); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 380 | |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 381 | return WallpaperCropUtils.calculateCropRect(context, defaultDisplay, mRawWallpaperSize, |
| 382 | rect, wallpaperZoom); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 383 | } |
| 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 |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 391 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 392 | finishActivityWithResultOk(); |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void onError(@Nullable Throwable throwable) { |
| 397 | showSetWallpaperErrorDialog(destination); |
| 398 | } |
| 399 | }); |
| 400 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame^] | 401 | |
| 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 517 | } |