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