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