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; |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 28 | import android.app.WallpaperColors; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 30 | import android.content.res.Resources; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 32 | import android.graphics.Color; |
| 33 | import android.graphics.Point; |
| 34 | import android.graphics.PointF; |
| 35 | import android.graphics.Rect; |
| 36 | import android.os.Bundle; |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 37 | import android.util.Log; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 39 | import android.view.Surface; |
| 40 | import android.view.SurfaceControlViewHost; |
| 41 | import android.view.SurfaceHolder; |
| 42 | import android.view.SurfaceView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 43 | import android.view.View; |
| 44 | import android.view.ViewGroup; |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 45 | import android.view.ViewGroup.LayoutParams; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 46 | import android.widget.ImageView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 47 | |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 48 | import androidx.annotation.NonNull; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 49 | import androidx.annotation.Nullable; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 50 | import androidx.cardview.widget.CardView; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 51 | import androidx.constraintlayout.widget.ConstraintLayout; |
| 52 | import androidx.constraintlayout.widget.ConstraintSet; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 53 | import androidx.fragment.app.FragmentActivity; |
| 54 | |
| 55 | import com.android.wallpaper.R; |
| 56 | import com.android.wallpaper.asset.Asset; |
Chuck Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 57 | import com.android.wallpaper.asset.CurrentWallpaperAssetVN; |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 58 | import com.android.wallpaper.model.WallpaperInfo; |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 59 | import com.android.wallpaper.module.BitmapCropper; |
| 60 | import com.android.wallpaper.module.InjectorProvider; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 61 | import com.android.wallpaper.module.WallpaperPersister.Destination; |
| 62 | import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback; |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 63 | import com.android.wallpaper.util.FullScreenAnimation; |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 64 | import com.android.wallpaper.util.ResourceUtils; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 65 | import com.android.wallpaper.util.ScreenSizeCalculator; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 66 | import com.android.wallpaper.util.SizeCalculator; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 67 | import com.android.wallpaper.util.WallpaperCropUtils; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 68 | import com.android.wallpaper.widget.BottomActionBar; |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 69 | import com.android.wallpaper.widget.BottomActionBar.AccessibilityCallback; |
Chihhang Chuang | a70d90e | 2021-04-09 15:42:27 +0800 | [diff] [blame] | 70 | import com.android.wallpaper.widget.LockScreenPreviewer2; |
Tracy Zhou | 3599742 | 2020-05-01 13:42:06 -0700 | [diff] [blame] | 71 | import com.android.wallpaper.widget.WallpaperInfoView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 72 | |
| 73 | import com.bumptech.glide.Glide; |
| 74 | import com.bumptech.glide.MemoryCategory; |
| 75 | import com.davemorrissey.labs.subscaleview.ImageSource; |
| 76 | import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; |
chihhangchuang | 45d9413 | 2020-07-08 16:18:37 +0800 | [diff] [blame] | 77 | import com.google.android.material.tabs.TabLayout; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 78 | |
chihhangchuang | 923e29b | 2020-07-02 19:09:36 +0800 | [diff] [blame] | 79 | import java.util.Locale; |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 80 | import java.util.Optional; |
| 81 | import java.util.concurrent.atomic.AtomicInteger; |
chihhangchuang | 923e29b | 2020-07-02 19:09:36 +0800 | [diff] [blame] | 82 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 83 | /** |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 84 | * 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] | 85 | * information. |
| 86 | */ |
Chihhang Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 87 | public class ImagePreviewFragment extends PreviewFragment { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 88 | |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 89 | private static final String TAG = "ImagePreviewFragment"; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 90 | private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f; |
| 91 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 92 | private final WallpaperSurfaceCallback mWallpaperSurfaceCallback = |
| 93 | new WallpaperSurfaceCallback(); |
Tracy Zhou | c5a6bad | 2020-05-20 23:28:22 -0700 | [diff] [blame] | 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 mScreenSize; |
| 98 | private Point mRawWallpaperSize; // Native size of wallpaper image. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 99 | private ImageView mLowResImageView; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 100 | private TouchForwardingLayout mTouchForwardingLayout; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 101 | private ConstraintLayout mContainer; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 102 | private SurfaceView mWallpaperSurface; |
Tracy Zhou | 3599742 | 2020-05-01 13:42:06 -0700 | [diff] [blame] | 103 | private WallpaperInfoView mWallpaperInfoView; |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 104 | private Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty(); |
| 105 | private AtomicInteger mImageScaleChangeCounter = new AtomicInteger(0); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 106 | |
Santiago Etchebehere | 6228729 | 2021-04-30 15:53:22 -0700 | [diff] [blame] | 107 | protected SurfaceView mWorkspaceSurface; |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 108 | protected WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback; |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 109 | protected ViewGroup mLockPreviewContainer; |
| 110 | protected LockScreenPreviewer2 mLockScreenPreviewer; |
Chihhang Chuang | 57daaa9 | 2021-04-20 21:40:01 +0800 | [diff] [blame] | 111 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 112 | @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 Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 120 | return R.layout.fragment_image_preview_v2; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 123 | @Override |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 124 | protected int getLoadingIndicatorResId() { |
| 125 | return R.id.loading_indicator; |
| 126 | } |
| 127 | |
| 128 | @Override |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 129 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 130 | Bundle savedInstanceState) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 131 | View view = super.onCreateView(inflater, container, savedInstanceState); |
| 132 | |
| 133 | Activity activity = requireActivity(); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 134 | mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize( |
| 135 | activity.getWindowManager().getDefaultDisplay()); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 136 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 137 | // 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); |
Chihhang Chuang | 60a5bc2 | 2021-05-14 23:00:01 +0800 | [diff] [blame] | 143 | mTouchForwardingLayout.setForwardingEnabled(true); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 144 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 145 | // Set aspect ratio on the preview card dynamically. |
| 146 | ConstraintSet set = new ConstraintSet(); |
| 147 | set.clone(mContainer); |
chihhangchuang | 923e29b | 2020-07-02 19:09:36 +0800 | [diff] [blame] | 148 | String ratio = String.format(Locale.US, "%d:%d", mScreenSize.x, mScreenSize.y); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 149 | set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio); |
| 150 | set.applyTo(mContainer); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 151 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 152 | mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface); |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 153 | mWorkspaceSurfaceCallback = createWorkspaceSurfaceCallback(mWorkspaceSurface); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 154 | mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface); |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 155 | mLockPreviewContainer = mContainer.findViewById(R.id.lock_screen_preview_container); |
Chihhang Chuang | a70d90e | 2021-04-09 15:42:27 +0800 | [diff] [blame] | 156 | mLockScreenPreviewer = new LockScreenPreviewer2(getLifecycle(), getContext(), |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 157 | mLockPreviewContainer); |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 158 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 159 | setUpTabs(view.findViewById(R.id.pill_tabs)); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 160 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 161 | view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY), |
| 162 | makeMeasureSpec(mScreenSize.y, EXACTLY)); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 163 | ((CardView) mWorkspaceSurface.getParent()) |
| 164 | .setRadius(SizeCalculator.getPreviewCornerRadius( |
| 165 | activity, mContainer.getMeasuredWidth())); |
Chihhang Chuang | 57daaa9 | 2021-04-20 21:40:01 +0800 | [diff] [blame] | 166 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 167 | renderImageWallpaper(); |
| 168 | renderWorkspaceSurface(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 169 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 170 | // Trim some memory from Glide to make room for the full-size image in this fragment. |
| 171 | Glide.get(activity).setMemoryCategory(MemoryCategory.LOW); |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 172 | setUpLoadingIndicator(); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 173 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 174 | return view; |
| 175 | } |
| 176 | |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 177 | @Override |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 178 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 179 | super.onViewCreated(view, savedInstanceState); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 180 | mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> { |
| 181 | // Don't continue loading the wallpaper if the Fragment is detached. |
| 182 | if (getActivity() == null) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | // Return early and show a dialog if dimensions are null (signaling a decoding error). |
| 187 | if (dimensions == null) { |
| 188 | showLoadWallpaperErrorDialog(); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // To avoid applying the wallpaper when it's not parsed. Now it's parsed, enable the |
| 193 | // bottom action bar to allow applying the wallpaper. |
| 194 | if (mBottomActionBar != null) { |
| 195 | mBottomActionBar.enableActions(); |
| 196 | } |
| 197 | |
| 198 | mRawWallpaperSize = dimensions; |
| 199 | initFullResView(); |
| 200 | }); |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Chihhang Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 203 | protected void onWallpaperColorsChanged(@Nullable WallpaperColors colors) { |
Chihhang Chuang | 1e8bece | 2021-05-17 15:08:06 +0800 | [diff] [blame^] | 204 | // Make it enabled since the buttons are disabled while wallpaper is moving. |
| 205 | mBottomActionBar.enableActionButtonsWithBottomSheet(true); |
| 206 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 207 | mLockScreenPreviewer.setColor(colors); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 208 | |
| 209 | mFullScreenAnimation.setFullScreenTextColor( |
| 210 | colors == null || (colors.getColorHints() |
| 211 | & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0 |
| 212 | ? FullScreenAnimation.FullScreenTextColor.LIGHT |
| 213 | : FullScreenAnimation.FullScreenTextColor.DARK); |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 216 | @Override |
| 217 | protected boolean isLoaded() { |
| 218 | return mFullResImageView != null && mFullResImageView.hasImage(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public void onClickOk() { |
| 223 | FragmentActivity activity = getActivity(); |
| 224 | if (activity != null) { |
| 225 | activity.finish(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onDestroy() { |
| 231 | super.onDestroy(); |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 232 | if (mLoadingProgressBar != null) { |
| 233 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 234 | } |
Chihhang Chuang | 57daaa9 | 2021-04-20 21:40:01 +0800 | [diff] [blame] | 235 | |
| 236 | if (mFullResImageView != null) { |
| 237 | mFullResImageView.recycle(); |
| 238 | } |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 239 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 240 | mWallpaperSurfaceCallback.cleanUp(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 241 | mWorkspaceSurfaceCallback.cleanUp(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 245 | protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 246 | super.onBottomActionBarReady(bottomActionBar); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 247 | mWallpaperInfoView = (WallpaperInfoView) |
| 248 | LayoutInflater.from(getContext()).inflate( |
| 249 | R.layout.wallpaper_info_view, /* root= */null); |
| 250 | mBottomActionBar.attachViewToBottomSheetAndBindAction(mWallpaperInfoView, INFORMATION); |
| 251 | mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 252 | |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 253 | mBottomActionBar.setActionClickListener(APPLY, this::onSetWallpaperClicked); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 254 | |
| 255 | // Update target view's accessibility param since it will be blocked by the bottom sheet |
| 256 | // when expanded. |
| 257 | mBottomActionBar.setAccessibilityCallback(new AccessibilityCallback() { |
| 258 | @Override |
| 259 | public void onBottomSheetCollapsed() { |
Wesley.CW Wang | b8e51a2 | 2020-07-02 18:40:31 +0800 | [diff] [blame] | 260 | mContainer.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void onBottomSheetExpanded() { |
Wesley.CW Wang | b8e51a2 | 2020-07-02 18:40:31 +0800 | [diff] [blame] | 265 | mContainer.setImportantForAccessibility( |
| 266 | View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 267 | } |
| 268 | }); |
| 269 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 270 | mBottomActionBar.show(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 271 | // Loads wallpaper info and populate into view. |
| 272 | setUpExploreIntentAndLabel(this::populateWallpaperInfo); |
| 273 | // To avoid applying the wallpaper when the wallpaper's not parsed. |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 274 | mBottomActionBar.disableActions(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 275 | // If the wallpaper is parsed, enable the bottom action bar. |
| 276 | if (mRawWallpaperSize != null) { |
| 277 | mBottomActionBar.enableActions(); |
| 278 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 281 | protected void setUpTabs(TabLayout tabs) { |
| 282 | tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.home_screen_message))); |
| 283 | tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.lock_screen_message))); |
| 284 | tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { |
| 285 | @Override |
| 286 | public void onTabSelected(TabLayout.Tab tab) { |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 287 | mLastSelectedTabPositionOptional = Optional.of(tab.getPosition()); |
| 288 | updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0); |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void onTabUnselected(TabLayout.Tab tab) {} |
| 293 | |
| 294 | @Override |
| 295 | public void onTabReselected(TabLayout.Tab tab) {} |
| 296 | }); |
| 297 | |
| 298 | // The TabLayout only contains below tabs |
| 299 | // 0. Home tab |
| 300 | // 1. Lock tab |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 301 | int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1); |
| 302 | tabs.getTabAt(tabPosition).select(); |
| 303 | updateScreenPreview(/* isHomeSelected= */ tabPosition == 0); |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 304 | } |
| 305 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and |
| 308 | * initializing a zoom-scroll observer and click listener. |
| 309 | */ |
| 310 | private void initFullResView() { |
Chihhang Chuang | 9c1d4e4 | 2021-05-14 23:53:16 +0800 | [diff] [blame] | 311 | if (mRawWallpaperSize == null || mFullResImageView == null) { |
| 312 | return; |
| 313 | } |
| 314 | |
Tracy Zhou | dee0789 | 2020-06-16 20:14:30 -0700 | [diff] [blame] | 315 | // Minimum scale will only be respected under this scale type. |
| 316 | mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM); |
| 317 | // When we set a minimum scale bigger than the scale with which the full image is shown, |
| 318 | // disallow user to pan outside the view we show the wallpaper in. |
| 319 | mFullResImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 320 | |
| 321 | // Set a solid black "page bitmap" so MosaicView draws a black background while waiting |
| 322 | // 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] | 323 | Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 324 | int preColor = ResourceUtils.getColorAttr(getActivity(), android.R.attr.colorSecondary); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 325 | int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT; |
| 326 | backgroundBitmap.setPixel(0, 0, color); |
| 327 | mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 328 | |
| 329 | // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual |
| 330 | // (lower res) version of the image to be displayed. |
| 331 | Point targetPageBitmapSize = new Point(mRawWallpaperSize); |
| 332 | mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y, |
| 333 | pageBitmap -> { |
| 334 | // Check that the activity is still around since the decoding task started. |
| 335 | if (getActivity() == null) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | // Some of these may be null depending on if the Fragment is paused, stopped, |
| 340 | // or destroyed. |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 341 | if (mLoadingProgressBar != null) { |
| 342 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 343 | } |
| 344 | // The page bitmap may be null if there was a decoding error, so show an |
| 345 | // error dialog. |
| 346 | if (pageBitmap == null) { |
| 347 | showLoadWallpaperErrorDialog(); |
| 348 | return; |
| 349 | } |
| 350 | if (mFullResImageView != null) { |
| 351 | // Set page bitmap. |
| 352 | mFullResImageView.setImage(ImageSource.bitmap(pageBitmap)); |
| 353 | |
Chuck Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 354 | setDefaultWallpaperZoomAndScroll( |
| 355 | mWallpaperAsset instanceof CurrentWallpaperAssetVN); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 356 | crossFadeInMosaicView(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 357 | mFullResImageView.setOnStateChangedListener( |
| 358 | new SubsamplingScaleImageView.DefaultOnStateChangedListener() { |
| 359 | @Override |
| 360 | public void onCenterChanged(PointF newCenter, int origin) { |
| 361 | super.onCenterChanged(newCenter, origin); |
Chihhang Chuang | 1e8bece | 2021-05-17 15:08:06 +0800 | [diff] [blame^] | 362 | // Disallow bottom sheet to popup when wallpaper is moving |
| 363 | // by user dragging. |
| 364 | mBottomActionBar.enableActionButtonsWithBottomSheet(false); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 365 | mImageScaleChangeCounter.incrementAndGet(); |
| 366 | mFullResImageView.postDelayed(() -> { |
| 367 | if (mImageScaleChangeCounter.decrementAndGet() == 0) { |
| 368 | recalculateColors(); |
| 369 | } |
| 370 | }, /* delayMillis= */ 100); |
| 371 | } |
| 372 | }); |
Chihhang Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 373 | mFullResImageView.post(this::recalculateColors); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 374 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 375 | getActivity().invalidateOptionsMenu(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 376 | }); |
Chihhang Chuang | 1e8bece | 2021-05-17 15:08:06 +0800 | [diff] [blame^] | 377 | |
| 378 | mFullResImageView.setOnTouchListener((v, ev) -> { |
| 379 | // Consume the touch event for collapsing bottom sheet while it is expanded or |
| 380 | // dragging (not collapsed). |
| 381 | if (mBottomActionBar != null && !mBottomActionBar.isBottomSheetCollapsed()) { |
| 382 | mBottomActionBar.collapseBottomSheetIfExpanded(); |
| 383 | return true; |
| 384 | } |
| 385 | return false; |
| 386 | }); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 387 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 388 | |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 389 | private void recalculateColors() { |
| 390 | BitmapCropper bitmapCropper = InjectorProvider.getInjector().getBitmapCropper(); |
| 391 | bitmapCropper.cropAndScaleBitmap(mWallpaperAsset, mFullResImageView.getScale(), |
| 392 | calculateCropRect(), /* adjustForRtl= */ false, new BitmapCropper.Callback() { |
| 393 | @Override |
| 394 | public void onBitmapCropped(Bitmap croppedBitmap) { |
Chihhang Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 395 | onWallpaperColorsChanged(WallpaperColors.fromBitmap(croppedBitmap)); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void onError(@Nullable Throwable e) { |
| 400 | Log.w(TAG, "Recalculate colors, crop and scale bitmap failed.", e); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 401 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 402 | }); |
| 403 | } |
| 404 | |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 405 | private void populateWallpaperInfo() { |
| 406 | if (mWallpaperInfoView != null && mWallpaper != null) { |
| 407 | mWallpaperInfoView.populateWallpaperInfo( |
| 408 | mWallpaper, |
| 409 | mActionLabel, |
| 410 | WallpaperInfoHelper.shouldShowExploreButton( |
| 411 | getContext(), mExploreIntent), |
| 412 | this::onExploreClicked); |
| 413 | } |
| 414 | } |
| 415 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 416 | /** |
| 417 | * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading |
| 418 | * indicator. |
| 419 | */ |
| 420 | private void crossFadeInMosaicView() { |
| 421 | long shortAnimationDuration = getResources().getInteger( |
| 422 | android.R.integer.config_shortAnimTime); |
| 423 | |
| 424 | mFullResImageView.setAlpha(0f); |
| 425 | mFullResImageView.animate() |
| 426 | .alpha(1f) |
| 427 | .setDuration(shortAnimationDuration) |
| 428 | .setListener(new AnimatorListenerAdapter() { |
| 429 | @Override |
| 430 | public void onAnimationEnd(Animator animation) { |
| 431 | // Clear the thumbnail bitmap reference to save memory since it's no longer |
| 432 | // visible. |
| 433 | if (mLowResImageView != null) { |
| 434 | mLowResImageView.setImageBitmap(null); |
| 435 | } |
| 436 | } |
| 437 | }); |
| 438 | |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 439 | mLoadingProgressBar.animate() |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 440 | .alpha(0f) |
| 441 | .setDuration(shortAnimationDuration) |
| 442 | .setListener(new AnimatorListenerAdapter() { |
| 443 | @Override |
| 444 | public void onAnimationEnd(Animator animation) { |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 445 | if (mLoadingProgressBar != null) { |
| 446 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | }); |
| 450 | } |
| 451 | |
| 452 | /** |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 453 | * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra |
| 454 | * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as |
| 455 | * possible on the crop surface and align screen to crop surface such that the default preview |
| 456 | * 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] | 457 | * |
| 458 | * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded |
| 459 | * and rendered to the layout. |
Chuck Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 460 | * |
Santiago Etchebehere | 16cf060 | 2020-12-11 17:04:18 -0800 | [diff] [blame] | 461 | * @param offsetToStart {@code true} if we want to offset the visible rectangle to the start |
| 462 | * side of the raw wallpaper; {@code false} otherwise. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 463 | */ |
Santiago Etchebehere | 16cf060 | 2020-12-11 17:04:18 -0800 | [diff] [blame] | 464 | private void setDefaultWallpaperZoomAndScroll(boolean offsetToStart) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 465 | // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface. |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 466 | int cropWidth = mWallpaperSurface.getMeasuredWidth(); |
| 467 | int cropHeight = mWallpaperSurface.getMeasuredHeight(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 468 | Point crop = new Point(cropWidth, cropHeight); |
| 469 | Rect visibleRawWallpaperRect = |
| 470 | WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop); |
Santiago Etchebehere | 16cf060 | 2020-12-11 17:04:18 -0800 | [diff] [blame] | 471 | if (offsetToStart) { |
| 472 | if (WallpaperCropUtils.isRtl(requireContext())) { |
| 473 | visibleRawWallpaperRect.offsetTo(mRawWallpaperSize.x |
| 474 | - visibleRawWallpaperRect.width(), visibleRawWallpaperRect.top); |
| 475 | } else { |
| 476 | visibleRawWallpaperRect.offsetTo(/* newLeft= */ 0, visibleRawWallpaperRect.top); |
| 477 | } |
Chuck Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 478 | } |
Santiago Etchebehere | e95b6d9 | 2020-06-08 14:19:46 -0700 | [diff] [blame] | 479 | |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 480 | final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(), |
| 481 | visibleRawWallpaperRect.centerY()); |
Santiago Etchebehere | e95b6d9 | 2020-06-08 14:19:46 -0700 | [diff] [blame] | 482 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 483 | Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(), |
| 484 | visibleRawWallpaperRect.height()); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 485 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 486 | final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom( |
| 487 | visibleRawWallpaperSize, crop); |
| 488 | final float minWallpaperZoom = defaultWallpaperZoom; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 489 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 490 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 491 | // Set min wallpaper zoom and max zoom on MosaicView widget. |
| 492 | mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom)); |
| 493 | mFullResImageView.setMinScale(minWallpaperZoom); |
| 494 | |
| 495 | // Set center to composite positioning between scaled wallpaper and screen. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 496 | mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition); |
| 497 | } |
| 498 | |
| 499 | private Rect calculateCropRect() { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 500 | float wallpaperZoom = mFullResImageView.getScale(); |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 501 | Context context = requireContext().getApplicationContext(); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 502 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 503 | Rect visibleFileRect = new Rect(); |
| 504 | mFullResImageView.visibleFileRect(visibleFileRect); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 505 | |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 506 | int cropWidth = mWallpaperSurface.getMeasuredWidth(); |
| 507 | int cropHeight = mWallpaperSurface.getMeasuredHeight(); |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 508 | int maxCrop = Math.max(cropWidth, cropHeight); |
| 509 | int minCrop = Math.min(cropWidth, cropHeight); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 510 | Point hostViewSize = new Point(cropWidth, cropHeight); |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 511 | |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 512 | Resources res = context.getResources(); |
Santiago Etchebehere | bcd6bb6 | 2020-07-09 12:09:21 -0700 | [diff] [blame] | 513 | Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop); |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 514 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 515 | Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize, |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 516 | cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 517 | return cropRect; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Override |
| 521 | protected void setCurrentWallpaper(@Destination int destination) { |
| 522 | mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset, |
| 523 | destination, mFullResImageView.getScale(), calculateCropRect(), |
| 524 | new SetWallpaperCallback() { |
| 525 | @Override |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 526 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
Ching-Sung Li | b5e9bd7 | 2020-06-15 14:10:55 +0800 | [diff] [blame] | 527 | finishActivity(/* success= */ true); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public void onError(@Nullable Throwable throwable) { |
| 532 | showSetWallpaperErrorDialog(destination); |
| 533 | } |
| 534 | }); |
| 535 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 536 | |
| 537 | private void renderWorkspaceSurface() { |
| 538 | mWorkspaceSurface.setZOrderMediaOverlay(true); |
| 539 | mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback); |
| 540 | } |
| 541 | |
| 542 | private void renderImageWallpaper() { |
| 543 | mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback); |
| 544 | } |
| 545 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 546 | private class WallpaperSurfaceCallback implements SurfaceHolder.Callback { |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 547 | private Surface mLastSurface; |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 548 | private SurfaceControlViewHost mHost; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 549 | |
| 550 | @Override |
| 551 | public void surfaceCreated(SurfaceHolder holder) { |
| 552 | if (mLastSurface != holder.getSurface()) { |
| 553 | mLastSurface = holder.getSurface(); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 554 | if (mFullResImageView != null) { |
| 555 | mFullResImageView.recycle(); |
| 556 | } |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 557 | Context context = getContext(); |
| 558 | View wallpaperPreviewContainer = LayoutInflater.from(context).inflate( |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 559 | R.layout.fullscreen_wallpaper_preview, null); |
| 560 | mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image); |
| 561 | mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image); |
Chihhang Chuang | 9c1d4e4 | 2021-05-14 23:53:16 +0800 | [diff] [blame] | 562 | initFullResView(); |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 563 | // Scale the mWallpaperSurface based on system zoom's scale so that the wallpaper is |
| 564 | // rendered in a larger surface than what preview shows, simulating the behavior of |
| 565 | // the actual wallpaper surface. |
| 566 | float scale = WallpaperCropUtils.getSystemWallpaperMaximumScale(context); |
| 567 | int origWidth = mWallpaperSurface.getWidth(); |
| 568 | int width = (int) (origWidth * scale); |
| 569 | int origHeight = mWallpaperSurface.getHeight(); |
| 570 | int height = (int) (origHeight * scale); |
| 571 | int left = (origWidth - width) / 2; |
| 572 | int top = (origHeight - height) / 2; |
| 573 | |
| 574 | if (WallpaperCropUtils.isRtl(context)) { |
| 575 | left *= -1; |
| 576 | } |
| 577 | |
| 578 | LayoutParams params = mWallpaperSurface.getLayoutParams(); |
| 579 | params.width = width; |
| 580 | params.height = height; |
| 581 | mWallpaperSurface.setX(left); |
| 582 | mWallpaperSurface.setY(top); |
| 583 | mWallpaperSurface.setLayoutParams(params); |
| 584 | mWallpaperSurface.requestLayout(); |
| 585 | |
Chuck Liao | ca1d129 | 2020-06-19 01:29:53 +0800 | [diff] [blame] | 586 | // Load a low-res placeholder image if there's a thumbnail available from the asset |
| 587 | // that can be shown to the user more quickly than the full-sized image. |
| 588 | if (mWallpaperAsset.hasLowResDataSource()) { |
| 589 | Activity activity = requireActivity(); |
| 590 | mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK, |
| 591 | new WallpaperPreviewBitmapTransformation( |
| 592 | activity.getApplicationContext(), isRtl())); |
| 593 | } |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 594 | wallpaperPreviewContainer.measure( |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 595 | makeMeasureSpec(width, EXACTLY), |
| 596 | makeMeasureSpec(height, EXACTLY)); |
| 597 | wallpaperPreviewContainer.layout(0, 0, width, height); |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 598 | mTouchForwardingLayout.setTargetView(mFullResImageView); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 599 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 600 | cleanUp(); |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 601 | mHost = new SurfaceControlViewHost(context, |
| 602 | context.getDisplay(), mWallpaperSurface.getHostToken()); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 603 | mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(), |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 604 | wallpaperPreviewContainer.getHeight()); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 605 | mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage()); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } |
| 611 | |
| 612 | @Override |
| 613 | public void surfaceDestroyed(SurfaceHolder holder) { } |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 614 | |
| 615 | public void cleanUp() { |
| 616 | if (mHost != null) { |
| 617 | mHost.release(); |
| 618 | mHost = null; |
| 619 | } |
| 620 | } |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Wesley.CW Wang | f21486b | 2020-06-02 18:30:32 +0800 | [diff] [blame] | 623 | private void updateScreenPreview(boolean isHomeSelected) { |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 624 | mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE); |
| 625 | mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 626 | |
| 627 | mFullScreenAnimation.setIsHomeSelected(isHomeSelected); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 628 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 629 | } |