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