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); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 143 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 144 | // Set aspect ratio on the preview card dynamically. |
| 145 | ConstraintSet set = new ConstraintSet(); |
| 146 | set.clone(mContainer); |
chihhangchuang | 923e29b | 2020-07-02 19:09:36 +0800 | [diff] [blame] | 147 | String ratio = String.format(Locale.US, "%d:%d", mScreenSize.x, mScreenSize.y); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 148 | set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio); |
| 149 | set.applyTo(mContainer); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 150 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 151 | mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface); |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 152 | mWorkspaceSurfaceCallback = createWorkspaceSurfaceCallback(mWorkspaceSurface); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 153 | mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface); |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 154 | mLockPreviewContainer = mContainer.findViewById(R.id.lock_screen_preview_container); |
Chihhang Chuang | a70d90e | 2021-04-09 15:42:27 +0800 | [diff] [blame] | 155 | mLockScreenPreviewer = new LockScreenPreviewer2(getLifecycle(), getContext(), |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 156 | mLockPreviewContainer); |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 157 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 158 | setUpTabs(view.findViewById(R.id.pill_tabs)); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 159 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 160 | view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY), |
| 161 | makeMeasureSpec(mScreenSize.y, EXACTLY)); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 162 | ((CardView) mWorkspaceSurface.getParent()) |
| 163 | .setRadius(SizeCalculator.getPreviewCornerRadius( |
| 164 | activity, mContainer.getMeasuredWidth())); |
Chihhang Chuang | 57daaa9 | 2021-04-20 21:40:01 +0800 | [diff] [blame] | 165 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 166 | renderImageWallpaper(); |
| 167 | renderWorkspaceSurface(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 168 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 169 | // Trim some memory from Glide to make room for the full-size image in this fragment. |
| 170 | Glide.get(activity).setMemoryCategory(MemoryCategory.LOW); |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 171 | setUpLoadingIndicator(); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame^] | 172 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 173 | return view; |
| 174 | } |
| 175 | |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 176 | @Override |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 177 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 178 | super.onViewCreated(view, savedInstanceState); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 179 | 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 Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Chihhang Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 202 | protected void onWallpaperColorsChanged(@Nullable WallpaperColors colors) { |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 203 | mLockScreenPreviewer.setColor(colors); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame^] | 204 | |
| 205 | mFullScreenAnimation.setFullScreenTextColor( |
| 206 | colors == null || (colors.getColorHints() |
| 207 | & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0 |
| 208 | ? FullScreenAnimation.FullScreenTextColor.LIGHT |
| 209 | : FullScreenAnimation.FullScreenTextColor.DARK); |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 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 | } |
Chihhang Chuang | 57daaa9 | 2021-04-20 21:40:01 +0800 | [diff] [blame] | 231 | |
| 232 | if (mFullResImageView != null) { |
| 233 | mFullResImageView.recycle(); |
| 234 | } |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 235 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 236 | mWallpaperSurfaceCallback.cleanUp(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 237 | mWorkspaceSurfaceCallback.cleanUp(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 241 | protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 242 | super.onBottomActionBarReady(bottomActionBar); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 243 | 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 Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame^] | 248 | |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 249 | mBottomActionBar.setActionClickListener(APPLY, this::onSetWallpaperClicked); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 250 | |
| 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 Wang | b8e51a2 | 2020-07-02 18:40:31 +0800 | [diff] [blame] | 256 | mContainer.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void onBottomSheetExpanded() { |
Wesley.CW Wang | b8e51a2 | 2020-07-02 18:40:31 +0800 | [diff] [blame] | 261 | mContainer.setImportantForAccessibility( |
| 262 | View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 263 | } |
| 264 | }); |
| 265 | |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 266 | // Will trigger onActionSelected callback to update the editing state. |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 267 | mBottomActionBar.show(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 268 | // Loads wallpaper info and populate into view. |
| 269 | setUpExploreIntentAndLabel(this::populateWallpaperInfo); |
| 270 | // To avoid applying the wallpaper when the wallpaper's not parsed. |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 271 | mBottomActionBar.disableActions(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 272 | // If the wallpaper is parsed, enable the bottom action bar. |
| 273 | if (mRawWallpaperSize != null) { |
| 274 | mBottomActionBar.enableActions(); |
| 275 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 278 | 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 Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 284 | mLastSelectedTabPositionOptional = Optional.of(tab.getPosition()); |
| 285 | updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0); |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 286 | } |
| 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 Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 298 | int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1); |
| 299 | tabs.getTabAt(tabPosition).select(); |
| 300 | updateScreenPreview(/* isHomeSelected= */ tabPosition == 0); |
Chihhang Chuang | 58bd0ca | 2021-04-27 00:41:13 +0800 | [diff] [blame] | 301 | } |
| 302 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 303 | /** |
| 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 Zhou | dee0789 | 2020-06-16 20:14:30 -0700 | [diff] [blame] | 308 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 313 | |
| 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 316 | Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 317 | int preColor = ResourceUtils.getColorAttr(getActivity(), android.R.attr.colorSecondary); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 318 | int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT; |
| 319 | backgroundBitmap.setPixel(0, 0, color); |
| 320 | mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 321 | |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 334 | if (mLoadingProgressBar != null) { |
| 335 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 336 | } |
| 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 Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 347 | setDefaultWallpaperZoomAndScroll( |
| 348 | mWallpaperAsset instanceof CurrentWallpaperAssetVN); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 349 | crossFadeInMosaicView(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 350 | 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 Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 363 | mFullResImageView.post(this::recalculateColors); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 364 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 365 | getActivity().invalidateOptionsMenu(); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 366 | }); |
| 367 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 368 | |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 369 | 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 Chuang | c9c108a | 2021-04-28 18:35:14 +0800 | [diff] [blame] | 375 | onWallpaperColorsChanged(WallpaperColors.fromBitmap(croppedBitmap)); |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void onError(@Nullable Throwable e) { |
| 380 | Log.w(TAG, "Recalculate colors, crop and scale bitmap failed.", e); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 381 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 382 | }); |
| 383 | } |
| 384 | |
Chihhang Chuang | 4f97a7d | 2021-04-27 18:27:54 +0800 | [diff] [blame] | 385 | 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 396 | /** |
| 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 Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 419 | mLoadingProgressBar.animate() |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 420 | .alpha(0f) |
| 421 | .setDuration(shortAnimationDuration) |
| 422 | .setListener(new AnimatorListenerAdapter() { |
| 423 | @Override |
| 424 | public void onAnimationEnd(Animator animation) { |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 425 | if (mLoadingProgressBar != null) { |
| 426 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | }); |
| 430 | } |
| 431 | |
| 432 | /** |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 433 | * 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 437 | * |
| 438 | * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded |
| 439 | * and rendered to the layout. |
Chuck Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 440 | * |
Santiago Etchebehere | 16cf060 | 2020-12-11 17:04:18 -0800 | [diff] [blame] | 441 | * @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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 443 | */ |
Santiago Etchebehere | 16cf060 | 2020-12-11 17:04:18 -0800 | [diff] [blame] | 444 | private void setDefaultWallpaperZoomAndScroll(boolean offsetToStart) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 445 | // 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] | 446 | int cropWidth = mWallpaperSurface.getMeasuredWidth(); |
| 447 | int cropHeight = mWallpaperSurface.getMeasuredHeight(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 448 | Point crop = new Point(cropWidth, cropHeight); |
| 449 | Rect visibleRawWallpaperRect = |
| 450 | WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop); |
Santiago Etchebehere | 16cf060 | 2020-12-11 17:04:18 -0800 | [diff] [blame] | 451 | 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 Liao | dc424e8 | 2020-09-04 00:17:44 +0800 | [diff] [blame] | 458 | } |
Santiago Etchebehere | e95b6d9 | 2020-06-08 14:19:46 -0700 | [diff] [blame] | 459 | |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 460 | final PointF centerPosition = new PointF(visibleRawWallpaperRect.centerX(), |
| 461 | visibleRawWallpaperRect.centerY()); |
Santiago Etchebehere | e95b6d9 | 2020-06-08 14:19:46 -0700 | [diff] [blame] | 462 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 463 | Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(), |
| 464 | visibleRawWallpaperRect.height()); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 465 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 466 | final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom( |
| 467 | visibleRawWallpaperSize, crop); |
| 468 | final float minWallpaperZoom = defaultWallpaperZoom; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 469 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 470 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 471 | // 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 Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 476 | mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition); |
| 477 | } |
| 478 | |
| 479 | private Rect calculateCropRect() { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 480 | float wallpaperZoom = mFullResImageView.getScale(); |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 481 | Context context = requireContext().getApplicationContext(); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 482 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 483 | Rect visibleFileRect = new Rect(); |
| 484 | mFullResImageView.visibleFileRect(visibleFileRect); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 485 | |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 486 | int cropWidth = mWallpaperSurface.getMeasuredWidth(); |
| 487 | int cropHeight = mWallpaperSurface.getMeasuredHeight(); |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 488 | int maxCrop = Math.max(cropWidth, cropHeight); |
| 489 | int minCrop = Math.min(cropWidth, cropHeight); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 490 | Point hostViewSize = new Point(cropWidth, cropHeight); |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 491 | |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 492 | Resources res = context.getResources(); |
Santiago Etchebehere | bcd6bb6 | 2020-07-09 12:09:21 -0700 | [diff] [blame] | 493 | Point cropSurfaceSize = WallpaperCropUtils.calculateCropSurfaceSize(res, maxCrop, minCrop); |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 494 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 495 | Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize, |
Santiago Etchebehere | f172aef | 2020-07-04 18:48:35 -0700 | [diff] [blame] | 496 | cropSurfaceSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 497 | return cropRect; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 498 | } |
| 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 |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 506 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
Ching-Sung Li | b5e9bd7 | 2020-06-15 14:10:55 +0800 | [diff] [blame] | 507 | finishActivity(/* success= */ true); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void onError(@Nullable Throwable throwable) { |
| 512 | showSetWallpaperErrorDialog(destination); |
| 513 | } |
| 514 | }); |
| 515 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 516 | |
| 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 Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 526 | private class WallpaperSurfaceCallback implements SurfaceHolder.Callback { |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 527 | private Surface mLastSurface; |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 528 | private SurfaceControlViewHost mHost; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 529 | |
| 530 | @Override |
| 531 | public void surfaceCreated(SurfaceHolder holder) { |
| 532 | if (mLastSurface != holder.getSurface()) { |
| 533 | mLastSurface = holder.getSurface(); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 534 | if (mFullResImageView != null) { |
| 535 | mFullResImageView.recycle(); |
| 536 | } |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 537 | Context context = getContext(); |
| 538 | View wallpaperPreviewContainer = LayoutInflater.from(context).inflate( |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 539 | 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 Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 542 | // 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 Liao | ca1d129 | 2020-06-19 01:29:53 +0800 | [diff] [blame] | 565 | // 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 Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 573 | wallpaperPreviewContainer.measure( |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 574 | makeMeasureSpec(width, EXACTLY), |
| 575 | makeMeasureSpec(height, EXACTLY)); |
| 576 | wallpaperPreviewContainer.layout(0, 0, width, height); |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 577 | mTouchForwardingLayout.setTargetView(mFullResImageView); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 578 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 579 | cleanUp(); |
Santiago Etchebehere | 8557c46 | 2020-12-02 18:41:40 -0800 | [diff] [blame] | 580 | mHost = new SurfaceControlViewHost(context, |
| 581 | context.getDisplay(), mWallpaperSurface.getHostToken()); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 582 | mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(), |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 583 | wallpaperPreviewContainer.getHeight()); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 584 | mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage()); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 585 | } |
| 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 Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 593 | |
| 594 | public void cleanUp() { |
| 595 | if (mHost != null) { |
| 596 | mHost.release(); |
| 597 | mHost = null; |
| 598 | } |
| 599 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 600 | }; |
| 601 | |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame^] | 602 | @Override |
| 603 | protected void setFullScreen(boolean fullScreen) { |
| 604 | super.setFullScreen(fullScreen); |
| 605 | mTouchForwardingLayout.setForwardingEnabled(fullScreen); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Wesley.CW Wang | f21486b | 2020-06-02 18:30:32 +0800 | [diff] [blame] | 608 | private void updateScreenPreview(boolean isHomeSelected) { |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 609 | mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE); |
| 610 | mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame^] | 611 | |
| 612 | mFullScreenAnimation.setIsHomeSelected(isHomeSelected); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 613 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 614 | } |