blob: 5088608a1946fea6c3ad8a17b7bd4704edf626fc [file] [log] [blame]
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.wallpaper.picker;
17
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070018import static android.view.View.MeasureSpec.EXACTLY;
19import static android.view.View.MeasureSpec.makeMeasureSpec;
20
21import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY;
22import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT;
23import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION;
24
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070025import android.animation.Animator;
26import android.animation.AnimatorListenerAdapter;
27import android.app.Activity;
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +080028import android.app.WallpaperColors;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070029import android.content.Context;
Santiago Etchebeheref172aef2020-07-04 18:48:35 -070030import android.content.res.Resources;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070031import android.graphics.Bitmap;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070032import android.graphics.Color;
33import android.graphics.Point;
34import android.graphics.PointF;
35import android.graphics.Rect;
36import android.os.Bundle;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080037import android.util.Log;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070038import android.view.LayoutInflater;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070039import android.view.Surface;
40import android.view.SurfaceControlViewHost;
41import android.view.SurfaceHolder;
42import android.view.SurfaceView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070043import android.view.View;
44import android.view.ViewGroup;
Santiago Etchebehere8557c462020-12-02 18:41:40 -080045import android.view.ViewGroup.LayoutParams;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070046import android.widget.ImageView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070047
Chuck Liaofc1b8c92020-05-21 20:54:15 +080048import androidx.annotation.NonNull;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070049import androidx.annotation.Nullable;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070050import androidx.cardview.widget.CardView;
Tracy Zhou3eb234c2020-05-05 15:17:33 -070051import androidx.constraintlayout.widget.ConstraintLayout;
52import androidx.constraintlayout.widget.ConstraintSet;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070053import androidx.fragment.app.FragmentActivity;
54
55import com.android.wallpaper.R;
56import com.android.wallpaper.asset.Asset;
Chuck Liaodc424e82020-09-04 00:17:44 +080057import com.android.wallpaper.asset.CurrentWallpaperAssetVN;
“Chuckffd832c2020-03-22 02:15:58 +080058import com.android.wallpaper.model.WallpaperInfo;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080059import com.android.wallpaper.module.BitmapCropper;
60import com.android.wallpaper.module.InjectorProvider;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070061import com.android.wallpaper.module.WallpaperPersister.Destination;
62import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
Tianguang Zhang893187f2021-05-07 22:14:04 +020063import com.android.wallpaper.util.FullScreenAnimation;
Tianguang Zhangd3e4f632021-04-14 00:30:15 +020064import com.android.wallpaper.util.ResourceUtils;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070065import com.android.wallpaper.util.ScreenSizeCalculator;
Santiago Etchebehere53c63432020-05-07 18:55:35 -070066import com.android.wallpaper.util.SizeCalculator;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070067import com.android.wallpaper.util.WallpaperCropUtils;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -070068import com.android.wallpaper.widget.BottomActionBar;
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +080069import com.android.wallpaper.widget.BottomActionBar.AccessibilityCallback;
Chihhang Chuanga70d90e2021-04-09 15:42:27 +080070import com.android.wallpaper.widget.LockScreenPreviewer2;
Tracy Zhou35997422020-05-01 13:42:06 -070071import com.android.wallpaper.widget.WallpaperInfoView;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070072
73import com.bumptech.glide.Glide;
74import com.bumptech.glide.MemoryCategory;
75import com.davemorrissey.labs.subscaleview.ImageSource;
76import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
chihhangchuang45d94132020-07-08 16:18:37 +080077import com.google.android.material.tabs.TabLayout;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070078
chihhangchuang923e29b2020-07-02 19:09:36 +080079import java.util.Locale;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080080import java.util.Optional;
81import java.util.concurrent.atomic.AtomicInteger;
chihhangchuang923e29b2020-07-02 19:09:36 +080082
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070083/**
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -080084 * Fragment which displays the UI for previewing an individual static wallpaper and its attribution
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070085 * information.
86 */
Chihhang Chuangc9c108a2021-04-28 18:35:14 +080087public class ImagePreviewFragment extends PreviewFragment {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070088
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +080089 private static final String TAG = "ImagePreviewFragment";
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070090 private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f;
91
Tracy Zhou4421a3d2020-06-15 14:56:18 -070092 private final WallpaperSurfaceCallback mWallpaperSurfaceCallback =
93 new WallpaperSurfaceCallback();
Tracy Zhouc5a6bad2020-05-20 23:28:22 -070094
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070095 private SubsamplingScaleImageView mFullResImageView;
96 private Asset mWallpaperAsset;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070097 private Point mScreenSize;
98 private Point mRawWallpaperSize; // Native size of wallpaper image.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -070099 private ImageView mLowResImageView;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700100 private TouchForwardingLayout mTouchForwardingLayout;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700101 private ConstraintLayout mContainer;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700102 private SurfaceView mWallpaperSurface;
Tracy Zhou35997422020-05-01 13:42:06 -0700103 private WallpaperInfoView mWallpaperInfoView;
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800104 private Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty();
105 private AtomicInteger mImageScaleChangeCounter = new AtomicInteger(0);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700106
Santiago Etchebehere62287292021-04-30 15:53:22 -0700107 protected SurfaceView mWorkspaceSurface;
Santiago Etchebeheredbdfb662021-04-26 10:25:13 -0700108 protected WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback;
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800109 protected ViewGroup mLockPreviewContainer;
110 protected LockScreenPreviewer2 mLockScreenPreviewer;
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800111
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700112 @Override
113 public void onCreate(Bundle savedInstanceState) {
114 super.onCreate(savedInstanceState);
115 mWallpaperAsset = mWallpaper.getAsset(requireContext().getApplicationContext());
116 }
117
118 @Override
119 protected int getLayoutResId() {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700120 return R.layout.fragment_image_preview_v2;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700121 }
122
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700123 @Override
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800124 protected int getLoadingIndicatorResId() {
125 return R.id.loading_indicator;
126 }
127
128 @Override
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700129 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800130 Bundle savedInstanceState) {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700131 View view = super.onCreateView(inflater, container, savedInstanceState);
132
133 Activity activity = requireActivity();
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700134 mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize(
135 activity.getWindowManager().getDefaultDisplay());
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700136
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700137 // TODO: Consider moving some part of this to the base class when live preview is ready.
138 view.findViewById(R.id.low_res_image).setVisibility(View.GONE);
139 view.findViewById(R.id.full_res_image).setVisibility(View.GONE);
140 mLoadingProgressBar.hide();
141 mContainer = view.findViewById(R.id.container);
142 mTouchForwardingLayout = mContainer.findViewById(R.id.touch_forwarding_layout);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700143
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700144 // Set aspect ratio on the preview card dynamically.
145 ConstraintSet set = new ConstraintSet();
146 set.clone(mContainer);
chihhangchuang923e29b2020-07-02 19:09:36 +0800147 String ratio = String.format(Locale.US, "%d:%d", mScreenSize.x, mScreenSize.y);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700148 set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio);
149 set.applyTo(mContainer);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700150
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700151 mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface);
Santiago Etchebeheredbdfb662021-04-26 10:25:13 -0700152 mWorkspaceSurfaceCallback = createWorkspaceSurfaceCallback(mWorkspaceSurface);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700153 mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface);
chihhangchuang287d4b72020-06-25 01:09:09 +0800154 mLockPreviewContainer = mContainer.findViewById(R.id.lock_screen_preview_container);
Chihhang Chuanga70d90e2021-04-09 15:42:27 +0800155 mLockScreenPreviewer = new LockScreenPreviewer2(getLifecycle(), getContext(),
chihhangchuang287d4b72020-06-25 01:09:09 +0800156 mLockPreviewContainer);
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800157
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800158 setUpTabs(view.findViewById(R.id.pill_tabs));
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700159
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700160 view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY),
161 makeMeasureSpec(mScreenSize.y, EXACTLY));
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700162 ((CardView) mWorkspaceSurface.getParent())
163 .setRadius(SizeCalculator.getPreviewCornerRadius(
164 activity, mContainer.getMeasuredWidth()));
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800165
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800166 renderImageWallpaper();
167 renderWorkspaceSurface();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700168
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700169 // Trim some memory from Glide to make room for the full-size image in this fragment.
170 Glide.get(activity).setMemoryCategory(MemoryCategory.LOW);
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800171 setUpLoadingIndicator();
Tianguang Zhang893187f2021-05-07 22:14:04 +0200172
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700173 return view;
174 }
175
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800176 @Override
Chuck Liaofc1b8c92020-05-21 20:54:15 +0800177 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
178 super.onViewCreated(view, savedInstanceState);
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800179 mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
180 // Don't continue loading the wallpaper if the Fragment is detached.
181 if (getActivity() == null) {
182 return;
183 }
184
185 // Return early and show a dialog if dimensions are null (signaling a decoding error).
186 if (dimensions == null) {
187 showLoadWallpaperErrorDialog();
188 return;
189 }
190
191 // To avoid applying the wallpaper when it's not parsed. Now it's parsed, enable the
192 // bottom action bar to allow applying the wallpaper.
193 if (mBottomActionBar != null) {
194 mBottomActionBar.enableActions();
195 }
196
197 mRawWallpaperSize = dimensions;
198 initFullResView();
199 });
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800200 }
201
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800202 protected void onWallpaperColorsChanged(@Nullable WallpaperColors colors) {
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800203 mLockScreenPreviewer.setColor(colors);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200204
205 mFullScreenAnimation.setFullScreenTextColor(
206 colors == null || (colors.getColorHints()
207 & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0
208 ? FullScreenAnimation.FullScreenTextColor.LIGHT
209 : FullScreenAnimation.FullScreenTextColor.DARK);
Santiago Etchebehere4ecb6aa2019-11-07 11:48:58 -0800210 }
211
Santiago Etchebeheredc55b192019-11-08 13:56:52 -0800212 @Override
213 protected boolean isLoaded() {
214 return mFullResImageView != null && mFullResImageView.hasImage();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700215 }
216
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700217 @Override
218 public void onClickOk() {
219 FragmentActivity activity = getActivity();
220 if (activity != null) {
221 activity.finish();
222 }
223 }
224
225 @Override
226 public void onDestroy() {
227 super.onDestroy();
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800228 if (mLoadingProgressBar != null) {
229 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700230 }
Chihhang Chuang57daaa92021-04-20 21:40:01 +0800231
232 if (mFullResImageView != null) {
233 mFullResImageView.recycle();
234 }
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700235
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700236 mWallpaperSurfaceCallback.cleanUp();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700237 mWorkspaceSurfaceCallback.cleanUp();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700238 }
239
240 @Override
Tracy Zhoue4a5f262020-05-26 17:40:19 -0700241 protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700242 super.onBottomActionBarReady(bottomActionBar);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700243 mWallpaperInfoView = (WallpaperInfoView)
244 LayoutInflater.from(getContext()).inflate(
245 R.layout.wallpaper_info_view, /* root= */null);
246 mBottomActionBar.attachViewToBottomSheetAndBindAction(mWallpaperInfoView, INFORMATION);
247 mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200248
Chihhang Chuangca7c8252020-06-17 17:32:46 +0800249 mBottomActionBar.setActionClickListener(APPLY, this::onSetWallpaperClicked);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800250
251 // Update target view's accessibility param since it will be blocked by the bottom sheet
252 // when expanded.
253 mBottomActionBar.setAccessibilityCallback(new AccessibilityCallback() {
254 @Override
255 public void onBottomSheetCollapsed() {
Wesley.CW Wangb8e51a22020-07-02 18:40:31 +0800256 mContainer.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800257 }
258
259 @Override
260 public void onBottomSheetExpanded() {
Wesley.CW Wangb8e51a22020-07-02 18:40:31 +0800261 mContainer.setImportantForAccessibility(
262 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
Wesley.CW Wang61c28ec2020-06-19 18:58:50 +0800263 }
264 });
265
Chihhang Chuangca7c8252020-06-17 17:32:46 +0800266 // Will trigger onActionSelected callback to update the editing state.
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700267 mBottomActionBar.show();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800268 // Loads wallpaper info and populate into view.
269 setUpExploreIntentAndLabel(this::populateWallpaperInfo);
270 // To avoid applying the wallpaper when the wallpaper's not parsed.
chihhangchuang13cb1fe2020-06-30 16:46:59 +0800271 mBottomActionBar.disableActions();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800272 // If the wallpaper is parsed, enable the bottom action bar.
273 if (mRawWallpaperSize != null) {
274 mBottomActionBar.enableActions();
275 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700276 }
277
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800278 protected void setUpTabs(TabLayout tabs) {
279 tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.home_screen_message)));
280 tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.lock_screen_message)));
281 tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
282 @Override
283 public void onTabSelected(TabLayout.Tab tab) {
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800284 mLastSelectedTabPositionOptional = Optional.of(tab.getPosition());
285 updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0);
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800286 }
287
288 @Override
289 public void onTabUnselected(TabLayout.Tab tab) {}
290
291 @Override
292 public void onTabReselected(TabLayout.Tab tab) {}
293 });
294
295 // The TabLayout only contains below tabs
296 // 0. Home tab
297 // 1. Lock tab
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800298 int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1);
299 tabs.getTabAt(tabPosition).select();
300 updateScreenPreview(/* isHomeSelected= */ tabPosition == 0);
Chihhang Chuang58bd0ca2021-04-27 00:41:13 +0800301 }
302
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700303 /**
304 * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
305 * initializing a zoom-scroll observer and click listener.
306 */
307 private void initFullResView() {
Tracy Zhoudee07892020-06-16 20:14:30 -0700308 // Minimum scale will only be respected under this scale type.
309 mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
310 // When we set a minimum scale bigger than the scale with which the full image is shown,
311 // disallow user to pan outside the view we show the wallpaper in.
312 mFullResImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700313
314 // Set a solid black "page bitmap" so MosaicView draws a black background while waiting
315 // for the image to load or a transparent one if a thumbnail already loaded.
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700316 Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Tianguang Zhangd3e4f632021-04-14 00:30:15 +0200317 int preColor = ResourceUtils.getColorAttr(getActivity(), android.R.attr.colorSecondary);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700318 int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT;
319 backgroundBitmap.setPixel(0, 0, color);
320 mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap));
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700321
322 // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual
323 // (lower res) version of the image to be displayed.
324 Point targetPageBitmapSize = new Point(mRawWallpaperSize);
325 mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y,
326 pageBitmap -> {
327 // Check that the activity is still around since the decoding task started.
328 if (getActivity() == null) {
329 return;
330 }
331
332 // Some of these may be null depending on if the Fragment is paused, stopped,
333 // or destroyed.
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800334 if (mLoadingProgressBar != null) {
335 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700336 }
337 // The page bitmap may be null if there was a decoding error, so show an
338 // error dialog.
339 if (pageBitmap == null) {
340 showLoadWallpaperErrorDialog();
341 return;
342 }
343 if (mFullResImageView != null) {
344 // Set page bitmap.
345 mFullResImageView.setImage(ImageSource.bitmap(pageBitmap));
346
Chuck Liaodc424e82020-09-04 00:17:44 +0800347 setDefaultWallpaperZoomAndScroll(
348 mWallpaperAsset instanceof CurrentWallpaperAssetVN);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700349 crossFadeInMosaicView();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800350 mFullResImageView.setOnStateChangedListener(
351 new SubsamplingScaleImageView.DefaultOnStateChangedListener() {
352 @Override
353 public void onCenterChanged(PointF newCenter, int origin) {
354 super.onCenterChanged(newCenter, origin);
355 mImageScaleChangeCounter.incrementAndGet();
356 mFullResImageView.postDelayed(() -> {
357 if (mImageScaleChangeCounter.decrementAndGet() == 0) {
358 recalculateColors();
359 }
360 }, /* delayMillis= */ 100);
361 }
362 });
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800363 mFullResImageView.post(this::recalculateColors);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700364 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700365 getActivity().invalidateOptionsMenu();
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800366 });
367 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700368
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800369 private void recalculateColors() {
370 BitmapCropper bitmapCropper = InjectorProvider.getInjector().getBitmapCropper();
371 bitmapCropper.cropAndScaleBitmap(mWallpaperAsset, mFullResImageView.getScale(),
372 calculateCropRect(), /* adjustForRtl= */ false, new BitmapCropper.Callback() {
373 @Override
374 public void onBitmapCropped(Bitmap croppedBitmap) {
Chihhang Chuangc9c108a2021-04-28 18:35:14 +0800375 onWallpaperColorsChanged(WallpaperColors.fromBitmap(croppedBitmap));
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800376 }
377
378 @Override
379 public void onError(@Nullable Throwable e) {
380 Log.w(TAG, "Recalculate colors, crop and scale bitmap failed.", e);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700381 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700382 });
383 }
384
Chihhang Chuang4f97a7d2021-04-27 18:27:54 +0800385 private void populateWallpaperInfo() {
386 if (mWallpaperInfoView != null && mWallpaper != null) {
387 mWallpaperInfoView.populateWallpaperInfo(
388 mWallpaper,
389 mActionLabel,
390 WallpaperInfoHelper.shouldShowExploreButton(
391 getContext(), mExploreIntent),
392 this::onExploreClicked);
393 }
394 }
395
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700396 /**
397 * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading
398 * indicator.
399 */
400 private void crossFadeInMosaicView() {
401 long shortAnimationDuration = getResources().getInteger(
402 android.R.integer.config_shortAnimTime);
403
404 mFullResImageView.setAlpha(0f);
405 mFullResImageView.animate()
406 .alpha(1f)
407 .setDuration(shortAnimationDuration)
408 .setListener(new AnimatorListenerAdapter() {
409 @Override
410 public void onAnimationEnd(Animator animation) {
411 // Clear the thumbnail bitmap reference to save memory since it's no longer
412 // visible.
413 if (mLowResImageView != null) {
414 mLowResImageView.setImageBitmap(null);
415 }
416 }
417 });
418
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800419 mLoadingProgressBar.animate()
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700420 .alpha(0f)
421 .setDuration(shortAnimationDuration)
422 .setListener(new AnimatorListenerAdapter() {
423 @Override
424 public void onAnimationEnd(Animator animation) {
Santiago Etchebeheref4549ed2019-11-13 14:18:57 -0800425 if (mLoadingProgressBar != null) {
426 mLoadingProgressBar.hide();
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700427 }
428 }
429 });
430 }
431
432 /**
Michel Comin Escudeb3e1fef2019-11-22 11:41:53 -0800433 * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra
434 * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as
435 * possible on the crop surface and align screen to crop surface such that the default preview
436 * matches what would be seen by the user in the left-most home screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700437 *
438 * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded
439 * and rendered to the layout.
Chuck Liaodc424e82020-09-04 00:17:44 +0800440 *
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800441 * @param offsetToStart {@code true} if we want to offset the visible rectangle to the start
442 * side of the raw wallpaper; {@code false} otherwise.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700443 */
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800444 private void setDefaultWallpaperZoomAndScroll(boolean offsetToStart) {
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700445 // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface.
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800446 int cropWidth = mWallpaperSurface.getMeasuredWidth();
447 int cropHeight = mWallpaperSurface.getMeasuredHeight();
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700448 Point crop = new Point(cropWidth, cropHeight);
449 Rect visibleRawWallpaperRect =
450 WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop);
Santiago Etchebehere16cf0602020-12-11 17:04:18 -0800451 if (offsetToStart) {
452 if (WallpaperCropUtils.isRtl(requireContext())) {
453 visibleRawWallpaperRect.offsetTo(mRawWallpaperSize.x
454 - visibleRawWallpaperRect.width(), visibleRawWallpaperRect.top);
455 } else {
456 visibleRawWallpaperRect.offsetTo(/* newLeft= */ 0, visibleRawWallpaperRect.top);
457 }
Chuck Liaodc424e82020-09-04 00:17:44 +0800458 }
Santiago Etchebeheree95b6d92020-06-08 14:19:46 -0700459
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800460 final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(),
461 visibleRawWallpaperRect.centerY());
Santiago Etchebeheree95b6d92020-06-08 14:19:46 -0700462
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700463 Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(),
464 visibleRawWallpaperRect.height());
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700465
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700466 final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom(
467 visibleRawWallpaperSize, crop);
468 final float minWallpaperZoom = defaultWallpaperZoom;
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700469
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700470
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700471 // Set min wallpaper zoom and max zoom on MosaicView widget.
472 mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom));
473 mFullResImageView.setMinScale(minWallpaperZoom);
474
475 // Set center to composite positioning between scaled wallpaper and screen.
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700476 mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition);
477 }
478
479 private Rect calculateCropRect() {
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700480 float wallpaperZoom = mFullResImageView.getScale();
Chuck Liao5182a312020-03-04 20:04:18 +0800481 Context context = requireContext().getApplicationContext();
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700482
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700483 Rect visibleFileRect = new Rect();
484 mFullResImageView.visibleFileRect(visibleFileRect);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700485
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800486 int cropWidth = mWallpaperSurface.getMeasuredWidth();
487 int cropHeight = mWallpaperSurface.getMeasuredHeight();
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700488 int maxCrop = Math.max(cropWidth, cropHeight);
489 int minCrop = Math.min(cropWidth, cropHeight);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700490 Point hostViewSize = new Point(cropWidth, cropHeight);
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700491
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700492 Resources res = context.getResources();
Santiago Etchebeherebcd6bb62020-07-09 12:09:21 -0700493 Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop);
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700494
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700495 Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize,
Santiago Etchebeheref172aef2020-07-04 18:48:35 -0700496 cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom);
Tracy Zhouecfd6eb2020-05-27 23:56:35 -0700497 return cropRect;
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700498 }
499
500 @Override
501 protected void setCurrentWallpaper(@Destination int destination) {
502 mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
503 destination, mFullResImageView.getScale(), calculateCropRect(),
504 new SetWallpaperCallback() {
505 @Override
“Chuckffd832c2020-03-22 02:15:58 +0800506 public void onSuccess(WallpaperInfo wallpaperInfo) {
Ching-Sung Lib5e9bd72020-06-15 14:10:55 +0800507 finishActivity(/* success= */ true);
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700508 }
509
510 @Override
511 public void onError(@Nullable Throwable throwable) {
512 showSetWallpaperErrorDialog(destination);
513 }
514 });
515 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700516
517 private void renderWorkspaceSurface() {
518 mWorkspaceSurface.setZOrderMediaOverlay(true);
519 mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback);
520 }
521
522 private void renderImageWallpaper() {
523 mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
524 }
525
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700526 private class WallpaperSurfaceCallback implements SurfaceHolder.Callback {
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700527 private Surface mLastSurface;
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700528 private SurfaceControlViewHost mHost;
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700529
530 @Override
531 public void surfaceCreated(SurfaceHolder holder) {
532 if (mLastSurface != holder.getSurface()) {
533 mLastSurface = holder.getSurface();
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700534 if (mFullResImageView != null) {
535 mFullResImageView.recycle();
536 }
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800537 Context context = getContext();
538 View wallpaperPreviewContainer = LayoutInflater.from(context).inflate(
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700539 R.layout.fullscreen_wallpaper_preview, null);
540 mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image);
541 mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image);
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800542 // Scale the mWallpaperSurface based on system zoom's scale so that the wallpaper is
543 // rendered in a larger surface than what preview shows, simulating the behavior of
544 // the actual wallpaper surface.
545 float scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context);
546 int origWidth = mWallpaperSurface.getWidth();
547 int width = (int) (origWidth * scale);
548 int origHeight = mWallpaperSurface.getHeight();
549 int height = (int) (origHeight * scale);
550 int left = (origWidth - width) / 2;
551 int top = (origHeight - height) / 2;
552
553 if (WallpaperCropUtils.isRtl(context)) {
554 left *= -1;
555 }
556
557 LayoutParams params = mWallpaperSurface.getLayoutParams();
558 params.width = width;
559 params.height = height;
560 mWallpaperSurface.setX(left);
561 mWallpaperSurface.setY(top);
562 mWallpaperSurface.setLayoutParams(params);
563 mWallpaperSurface.requestLayout();
564
Chuck Liaoca1d1292020-06-19 01:29:53 +0800565 // Load a low-res placeholder image if there's a thumbnail available from the asset
566 // that can be shown to the user more quickly than the full-sized image.
567 if (mWallpaperAsset.hasLowResDataSource()) {
568 Activity activity = requireActivity();
569 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK,
570 new WallpaperPreviewBitmapTransformation(
571 activity.getApplicationContext(), isRtl()));
572 }
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700573 wallpaperPreviewContainer.measure(
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800574 makeMeasureSpec(width, EXACTLY),
575 makeMeasureSpec(height, EXACTLY));
576 wallpaperPreviewContainer.layout(0, 0, width, height);
Ching-Sung Lib9404622020-05-18 22:38:45 +0800577 mTouchForwardingLayout.setTargetView(mFullResImageView);
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700578
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700579 cleanUp();
Santiago Etchebehere8557c462020-12-02 18:41:40 -0800580 mHost = new SurfaceControlViewHost(context,
581 context.getDisplay(), mWallpaperSurface.getHostToken());
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700582 mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700583 wallpaperPreviewContainer.getHeight());
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700584 mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700585 }
586 }
587
588 @Override
589 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
590
591 @Override
592 public void surfaceDestroyed(SurfaceHolder holder) { }
Tracy Zhou4421a3d2020-06-15 14:56:18 -0700593
594 public void cleanUp() {
595 if (mHost != null) {
596 mHost.release();
597 mHost = null;
598 }
599 }
Tracy Zhoua19cbdd2020-04-26 12:12:11 -0700600 };
601
Tianguang Zhang893187f2021-05-07 22:14:04 +0200602 @Override
603 protected void setFullScreen(boolean fullScreen) {
604 super.setFullScreen(fullScreen);
605 mTouchForwardingLayout.setForwardingEnabled(fullScreen);
Tracy Zhou3eb234c2020-05-05 15:17:33 -0700606 }
607
Wesley.CW Wangf21486b2020-06-02 18:30:32 +0800608 private void updateScreenPreview(boolean isHomeSelected) {
chihhangchuang287d4b72020-06-25 01:09:09 +0800609 mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE);
610 mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE);
Tianguang Zhang893187f2021-05-07 22:14:04 +0200611
612 mFullScreenAnimation.setIsHomeSelected(isHomeSelected);
Tracy Zhou67fd7e32020-05-01 18:35:36 -0700613 }
Santiago Etchebehere6d3d1e62019-09-26 11:11:11 -0700614}