Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.wallpaper.picker; |
| 17 | |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 18 | import static android.view.View.MeasureSpec.EXACTLY; |
| 19 | import static android.view.View.MeasureSpec.makeMeasureSpec; |
| 20 | |
| 21 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY; |
| 22 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT; |
| 23 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION; |
| 24 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 25 | import android.animation.Animator; |
| 26 | import android.animation.AnimatorListenerAdapter; |
| 27 | import android.app.Activity; |
| 28 | import android.content.Context; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
| 31 | import android.graphics.Point; |
| 32 | import android.graphics.PointF; |
| 33 | import android.graphics.Rect; |
| 34 | import android.os.Bundle; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 36 | import android.view.Surface; |
| 37 | import android.view.SurfaceControlViewHost; |
| 38 | import android.view.SurfaceHolder; |
| 39 | import android.view.SurfaceView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 40 | import android.view.View; |
| 41 | import android.view.ViewGroup; |
| 42 | import android.widget.ImageView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 43 | |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 44 | import androidx.annotation.NonNull; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 45 | import androidx.annotation.Nullable; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 46 | import androidx.cardview.widget.CardView; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 47 | import androidx.constraintlayout.widget.ConstraintLayout; |
| 48 | import androidx.constraintlayout.widget.ConstraintSet; |
| 49 | import androidx.core.content.ContextCompat; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 50 | import androidx.fragment.app.FragmentActivity; |
| 51 | |
| 52 | import com.android.wallpaper.R; |
| 53 | import com.android.wallpaper.asset.Asset; |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 54 | import com.android.wallpaper.model.WallpaperInfo; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 55 | import com.android.wallpaper.module.WallpaperPersister.Destination; |
| 56 | import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback; |
| 57 | import com.android.wallpaper.util.ScreenSizeCalculator; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 58 | import com.android.wallpaper.util.SizeCalculator; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 59 | import com.android.wallpaper.util.WallpaperCropUtils; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 60 | import com.android.wallpaper.widget.BottomActionBar; |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 61 | import com.android.wallpaper.widget.BottomActionBar.AccessibilityCallback; |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 62 | import com.android.wallpaper.widget.LockScreenPreviewer; |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 63 | import com.android.wallpaper.widget.WallpaperColorsLoader; |
Tracy Zhou | 3599742 | 2020-05-01 13:42:06 -0700 | [diff] [blame] | 64 | import com.android.wallpaper.widget.WallpaperInfoView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 65 | |
| 66 | import com.bumptech.glide.Glide; |
| 67 | import com.bumptech.glide.MemoryCategory; |
| 68 | import com.davemorrissey.labs.subscaleview.ImageSource; |
| 69 | import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 70 | |
chihhangchuang | 923e29b | 2020-07-02 19:09:36 +0800 | [diff] [blame^] | 71 | import java.util.Locale; |
| 72 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 73 | /** |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 74 | * 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] | 75 | * information. |
| 76 | */ |
| 77 | public class ImagePreviewFragment extends PreviewFragment { |
| 78 | |
| 79 | private static final float DEFAULT_WALLPAPER_MAX_ZOOM = 8f; |
| 80 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 81 | private final WallpaperSurfaceCallback mWallpaperSurfaceCallback = |
| 82 | new WallpaperSurfaceCallback(); |
Tracy Zhou | c5a6bad | 2020-05-20 23:28:22 -0700 | [diff] [blame] | 83 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 84 | private SubsamplingScaleImageView mFullResImageView; |
| 85 | private Asset mWallpaperAsset; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 86 | private Point mScreenSize; |
| 87 | private Point mRawWallpaperSize; // Native size of wallpaper image. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 88 | private ImageView mLowResImageView; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 89 | private TouchForwardingLayout mTouchForwardingLayout; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 90 | private ConstraintLayout mContainer; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 91 | private SurfaceView mWorkspaceSurface; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 92 | private WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 93 | private SurfaceView mWallpaperSurface; |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 94 | private ViewGroup mLockPreviewContainer; |
| 95 | private LockScreenPreviewer mLockScreenPreviewer; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 96 | private View mTabs; |
Tracy Zhou | 3599742 | 2020-05-01 13:42:06 -0700 | [diff] [blame] | 97 | private WallpaperInfoView mWallpaperInfoView; |
Wesley.CW Wang | f21486b | 2020-06-02 18:30:32 +0800 | [diff] [blame] | 98 | private View mLock; |
| 99 | private View mHome; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 100 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public void onCreate(Bundle savedInstanceState) { |
| 103 | super.onCreate(savedInstanceState); |
| 104 | mWallpaperAsset = mWallpaper.getAsset(requireContext().getApplicationContext()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected int getLayoutResId() { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 109 | return R.layout.fragment_image_preview_v2; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 112 | @Override |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 113 | protected int getLoadingIndicatorResId() { |
| 114 | return R.id.loading_indicator; |
| 115 | } |
| 116 | |
| 117 | @Override |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 118 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 119 | Bundle savedInstanceState) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 120 | View view = super.onCreateView(inflater, container, savedInstanceState); |
| 121 | |
| 122 | Activity activity = requireActivity(); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 123 | mScreenSize = ScreenSizeCalculator.getInstance().getScreenSize( |
| 124 | activity.getWindowManager().getDefaultDisplay()); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 125 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 126 | // TODO: Consider moving some part of this to the base class when live preview is ready. |
| 127 | view.findViewById(R.id.low_res_image).setVisibility(View.GONE); |
| 128 | view.findViewById(R.id.full_res_image).setVisibility(View.GONE); |
| 129 | mLoadingProgressBar.hide(); |
| 130 | mContainer = view.findViewById(R.id.container); |
| 131 | mTouchForwardingLayout = mContainer.findViewById(R.id.touch_forwarding_layout); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 132 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 133 | // Set aspect ratio on the preview card dynamically. |
| 134 | ConstraintSet set = new ConstraintSet(); |
| 135 | set.clone(mContainer); |
chihhangchuang | 923e29b | 2020-07-02 19:09:36 +0800 | [diff] [blame^] | 136 | String ratio = String.format(Locale.US, "%d:%d", mScreenSize.x, mScreenSize.y); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 137 | set.setDimensionRatio(mTouchForwardingLayout.getId(), ratio); |
| 138 | set.applyTo(mContainer); |
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 | mWorkspaceSurface = mContainer.findViewById(R.id.workspace_surface); |
| 141 | mWorkspaceSurfaceCallback = new WorkspaceSurfaceHolderCallback(mWorkspaceSurface, |
| 142 | getContext()); |
| 143 | mWallpaperSurface = mContainer.findViewById(R.id.wallpaper_surface); |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 144 | mLockPreviewContainer = mContainer.findViewById(R.id.lock_screen_preview_container); |
| 145 | mLockScreenPreviewer = new LockScreenPreviewer(getLifecycle(), getActivity(), |
| 146 | mLockPreviewContainer); |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 147 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 148 | mTabs = view.findViewById(R.id.tabs_container); |
Wesley.CW Wang | f21486b | 2020-06-02 18:30:32 +0800 | [diff] [blame] | 149 | mLock = mTabs.findViewById(R.id.lock); |
| 150 | mHome = mTabs.findViewById(R.id.home); |
| 151 | mLock.setOnClickListener(v -> updateScreenPreview(/* isHomeSelected= */ false)); |
| 152 | mHome.setOnClickListener(v -> updateScreenPreview(/* isHomeSelected= */ true)); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 153 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 154 | view.measure(makeMeasureSpec(mScreenSize.x, EXACTLY), |
| 155 | makeMeasureSpec(mScreenSize.y, EXACTLY)); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 156 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 157 | ((CardView) mWorkspaceSurface.getParent()) |
| 158 | .setRadius(SizeCalculator.getPreviewCornerRadius( |
| 159 | activity, mContainer.getMeasuredWidth())); |
| 160 | renderImageWallpaper(); |
| 161 | renderWorkspaceSurface(); |
Ching-Sung Li | ba4f1fe | 2020-06-10 15:59:15 +0800 | [diff] [blame] | 162 | updateScreenPreview(/* isHomeSelected= */ mViewAsHome); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 163 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 164 | // Trim some memory from Glide to make room for the full-size image in this fragment. |
| 165 | Glide.get(activity).setMemoryCategory(MemoryCategory.LOW); |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 166 | setUpLoadingIndicator(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 167 | return view; |
| 168 | } |
| 169 | |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 170 | @Override |
Chuck Liao | fc1b8c9 | 2020-05-21 20:54:15 +0800 | [diff] [blame] | 171 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 172 | super.onViewCreated(view, savedInstanceState); |
chihhangchuang | 90f902c | 2020-06-03 16:14:40 +0800 | [diff] [blame] | 173 | WallpaperColorsLoader.getWallpaperColors(getContext(), |
| 174 | mWallpaper.getThumbAsset(getContext()), |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 175 | mLockScreenPreviewer::setColor); |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 178 | @Override |
| 179 | protected boolean isLoaded() { |
| 180 | return mFullResImageView != null && mFullResImageView.hasImage(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public void onClickOk() { |
| 185 | FragmentActivity activity = getActivity(); |
| 186 | if (activity != null) { |
| 187 | activity.finish(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void onDestroy() { |
| 193 | super.onDestroy(); |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 194 | if (mLoadingProgressBar != null) { |
| 195 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 196 | } |
| 197 | mFullResImageView.recycle(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 198 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 199 | mWallpaperSurfaceCallback.cleanUp(); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 200 | mWorkspaceSurfaceCallback.cleanUp(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 204 | protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 205 | super.onBottomActionBarReady(bottomActionBar); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 206 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 207 | mWallpaperInfoView = (WallpaperInfoView) |
| 208 | LayoutInflater.from(getContext()).inflate( |
| 209 | R.layout.wallpaper_info_view, /* root= */null); |
| 210 | mBottomActionBar.attachViewToBottomSheetAndBindAction(mWallpaperInfoView, INFORMATION); |
| 211 | mBottomActionBar.showActionsOnly(INFORMATION, EDIT, APPLY); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 212 | mBottomActionBar.setActionClickListener(EDIT, v -> |
Tracy Zhou | 16112e3 | 2020-05-27 13:13:23 -0700 | [diff] [blame] | 213 | setEditingEnabled(mBottomActionBar.isActionSelected(EDIT)) |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 214 | ); |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 215 | mBottomActionBar.setActionSelectedListener(EDIT, this::setEditingEnabled); |
| 216 | mBottomActionBar.setActionClickListener(APPLY, this::onSetWallpaperClicked); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 217 | |
| 218 | // Update target view's accessibility param since it will be blocked by the bottom sheet |
| 219 | // when expanded. |
| 220 | mBottomActionBar.setAccessibilityCallback(new AccessibilityCallback() { |
| 221 | @Override |
| 222 | public void onBottomSheetCollapsed() { |
Wesley.CW Wang | b8e51a2 | 2020-07-02 18:40:31 +0800 | [diff] [blame] | 223 | mContainer.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 224 | mTabs.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onBottomSheetExpanded() { |
Wesley.CW Wang | b8e51a2 | 2020-07-02 18:40:31 +0800 | [diff] [blame] | 229 | mContainer.setImportantForAccessibility( |
| 230 | View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
Wesley.CW Wang | 61c28ec | 2020-06-19 18:58:50 +0800 | [diff] [blame] | 231 | mTabs.setImportantForAccessibility( |
| 232 | View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
| 233 | } |
| 234 | }); |
| 235 | |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 236 | // Will trigger onActionSelected callback to update the editing state. |
| 237 | mBottomActionBar.setDefaultSelectedButton(EDIT); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 238 | mBottomActionBar.show(); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 239 | |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 240 | mBottomActionBar.disableActions(); |
| 241 | mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> { |
| 242 | if (mBottomActionBar != null) { |
| 243 | mBottomActionBar.enableActions(); |
| 244 | } |
| 245 | |
| 246 | // Don't continue loading the wallpaper if the Fragment is detached. |
| 247 | if (getActivity() == null) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // Return early and show a dialog if dimensions are null (signaling a decoding error). |
| 252 | if (dimensions == null) { |
| 253 | showLoadWallpaperErrorDialog(); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | mRawWallpaperSize = dimensions; |
| 258 | |
| 259 | setUpExploreIntentAndLabel(ImagePreviewFragment.this::initFullResView); |
| 260 | }); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and |
| 265 | * initializing a zoom-scroll observer and click listener. |
| 266 | */ |
| 267 | private void initFullResView() { |
Tracy Zhou | dee0789 | 2020-06-16 20:14:30 -0700 | [diff] [blame] | 268 | // Minimum scale will only be respected under this scale type. |
| 269 | mFullResImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM); |
| 270 | // When we set a minimum scale bigger than the scale with which the full image is shown, |
| 271 | // disallow user to pan outside the view we show the wallpaper in. |
| 272 | mFullResImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 273 | |
| 274 | // Set a solid black "page bitmap" so MosaicView draws a black background while waiting |
| 275 | // for the image to load or a transparent one if a thumbnail already loaded. |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 276 | Bitmap backgroundBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 277 | int preColor = ContextCompat.getColor(getContext(), R.color.fullscreen_preview_background); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 278 | int color = (mLowResImageView.getDrawable() == null) ? preColor : Color.TRANSPARENT; |
| 279 | backgroundBitmap.setPixel(0, 0, color); |
| 280 | mFullResImageView.setImage(ImageSource.bitmap(backgroundBitmap)); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 281 | |
| 282 | // Then set a fallback "page bitmap" to cover the whole MosaicView, which is an actual |
| 283 | // (lower res) version of the image to be displayed. |
| 284 | Point targetPageBitmapSize = new Point(mRawWallpaperSize); |
| 285 | mWallpaperAsset.decodeBitmap(targetPageBitmapSize.x, targetPageBitmapSize.y, |
| 286 | pageBitmap -> { |
| 287 | // Check that the activity is still around since the decoding task started. |
| 288 | if (getActivity() == null) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | // Some of these may be null depending on if the Fragment is paused, stopped, |
| 293 | // or destroyed. |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 294 | if (mLoadingProgressBar != null) { |
| 295 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 296 | } |
| 297 | // The page bitmap may be null if there was a decoding error, so show an |
| 298 | // error dialog. |
| 299 | if (pageBitmap == null) { |
| 300 | showLoadWallpaperErrorDialog(); |
| 301 | return; |
| 302 | } |
| 303 | if (mFullResImageView != null) { |
| 304 | // Set page bitmap. |
| 305 | mFullResImageView.setImage(ImageSource.bitmap(pageBitmap)); |
| 306 | |
| 307 | setDefaultWallpaperZoomAndScroll(); |
| 308 | crossFadeInMosaicView(); |
| 309 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 310 | getActivity().invalidateOptionsMenu(); |
| 311 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 312 | if (mWallpaperInfoView != null && mWallpaper != null) { |
| 313 | mWallpaperInfoView.populateWallpaperInfo(mWallpaper, mActionLabel, |
| 314 | mExploreIntent, this::onExploreClicked); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 315 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 316 | }); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Makes the MosaicView visible with an alpha fade-in animation while fading out the loading |
| 321 | * indicator. |
| 322 | */ |
| 323 | private void crossFadeInMosaicView() { |
| 324 | long shortAnimationDuration = getResources().getInteger( |
| 325 | android.R.integer.config_shortAnimTime); |
| 326 | |
| 327 | mFullResImageView.setAlpha(0f); |
| 328 | mFullResImageView.animate() |
| 329 | .alpha(1f) |
| 330 | .setDuration(shortAnimationDuration) |
| 331 | .setListener(new AnimatorListenerAdapter() { |
| 332 | @Override |
| 333 | public void onAnimationEnd(Animator animation) { |
| 334 | // Clear the thumbnail bitmap reference to save memory since it's no longer |
| 335 | // visible. |
| 336 | if (mLowResImageView != null) { |
| 337 | mLowResImageView.setImageBitmap(null); |
| 338 | } |
| 339 | } |
| 340 | }); |
| 341 | |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 342 | mLoadingProgressBar.animate() |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 343 | .alpha(0f) |
| 344 | .setDuration(shortAnimationDuration) |
| 345 | .setListener(new AnimatorListenerAdapter() { |
| 346 | @Override |
| 347 | public void onAnimationEnd(Animator animation) { |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 348 | if (mLoadingProgressBar != null) { |
| 349 | mLoadingProgressBar.hide(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | }); |
| 353 | } |
| 354 | |
| 355 | /** |
Michel Comin Escude | b3e1fef | 2019-11-22 11:41:53 -0800 | [diff] [blame] | 356 | * Sets the default wallpaper zoom and scroll position based on a "crop surface" (with extra |
| 357 | * width to account for parallax) superimposed on the screen. Shows as much of the wallpaper as |
| 358 | * possible on the crop surface and align screen to crop surface such that the default preview |
| 359 | * 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] | 360 | * |
| 361 | * <p>This method is called once in the Fragment lifecycle after the wallpaper asset has loaded |
| 362 | * and rendered to the layout. |
| 363 | */ |
| 364 | private void setDefaultWallpaperZoomAndScroll() { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 365 | // Determine minimum zoom to fit maximum visible area of wallpaper on crop surface. |
| 366 | int cropWidth = mWorkspaceSurface.getMeasuredWidth(); |
| 367 | int cropHeight = mWorkspaceSurface.getMeasuredHeight(); |
| 368 | Point crop = new Point(cropWidth, cropHeight); |
| 369 | Rect visibleRawWallpaperRect = |
| 370 | WallpaperCropUtils.calculateVisibleRect(mRawWallpaperSize, crop); |
Santiago Etchebehere | e95b6d9 | 2020-06-08 14:19:46 -0700 | [diff] [blame] | 371 | |
| 372 | final PointF centerPosition = WallpaperCropUtils.calculateDefaultCenter(requireContext(), |
| 373 | mRawWallpaperSize, visibleRawWallpaperRect); |
| 374 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 375 | Point visibleRawWallpaperSize = new Point(visibleRawWallpaperRect.width(), |
| 376 | visibleRawWallpaperRect.height()); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 377 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 378 | final float defaultWallpaperZoom = WallpaperCropUtils.calculateMinZoom( |
| 379 | visibleRawWallpaperSize, crop); |
| 380 | final float minWallpaperZoom = defaultWallpaperZoom; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 381 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 382 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 383 | // Set min wallpaper zoom and max zoom on MosaicView widget. |
| 384 | mFullResImageView.setMaxScale(Math.max(DEFAULT_WALLPAPER_MAX_ZOOM, defaultWallpaperZoom)); |
| 385 | mFullResImageView.setMinScale(minWallpaperZoom); |
| 386 | |
| 387 | // Set center to composite positioning between scaled wallpaper and screen. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 388 | mFullResImageView.setScaleAndCenter(minWallpaperZoom, centerPosition); |
| 389 | } |
| 390 | |
| 391 | private Rect calculateCropRect() { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 392 | float wallpaperZoom = mFullResImageView.getScale(); |
Chuck Liao | 5182a31 | 2020-03-04 20:04:18 +0800 | [diff] [blame] | 393 | Context context = requireContext().getApplicationContext(); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 394 | |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 395 | Rect visibleFileRect = new Rect(); |
| 396 | mFullResImageView.visibleFileRect(visibleFileRect); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 397 | |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 398 | int cropWidth = mWorkspaceSurface.getMeasuredWidth(); |
| 399 | int cropHeight = mWorkspaceSurface.getMeasuredHeight(); |
| 400 | Point hostViewSize = new Point(cropWidth, cropHeight); |
| 401 | Rect cropRect = WallpaperCropUtils.calculateCropRect(context, hostViewSize, |
| 402 | hostViewSize, mRawWallpaperSize, visibleFileRect, wallpaperZoom); |
| 403 | WallpaperCropUtils.adjustCropRect(context, cropRect, false /* zoomIn */); |
| 404 | return cropRect; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | protected void setCurrentWallpaper(@Destination int destination) { |
| 409 | mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset, |
| 410 | destination, mFullResImageView.getScale(), calculateCropRect(), |
| 411 | new SetWallpaperCallback() { |
| 412 | @Override |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 413 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
Ching-Sung Li | b5e9bd7 | 2020-06-15 14:10:55 +0800 | [diff] [blame] | 414 | finishActivity(/* success= */ true); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void onError(@Nullable Throwable throwable) { |
| 419 | showSetWallpaperErrorDialog(destination); |
| 420 | } |
| 421 | }); |
| 422 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 423 | |
| 424 | private void renderWorkspaceSurface() { |
| 425 | mWorkspaceSurface.setZOrderMediaOverlay(true); |
| 426 | mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback); |
| 427 | } |
| 428 | |
| 429 | private void renderImageWallpaper() { |
| 430 | mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback); |
| 431 | } |
| 432 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 433 | private class WallpaperSurfaceCallback implements SurfaceHolder.Callback { |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 434 | |
| 435 | private Surface mLastSurface; |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 436 | private SurfaceControlViewHost mHost; |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 437 | |
| 438 | @Override |
| 439 | public void surfaceCreated(SurfaceHolder holder) { |
| 440 | if (mLastSurface != holder.getSurface()) { |
| 441 | mLastSurface = holder.getSurface(); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 442 | if (mFullResImageView != null) { |
| 443 | mFullResImageView.recycle(); |
| 444 | } |
| 445 | View wallpaperPreviewContainer = LayoutInflater.from(getContext()).inflate( |
| 446 | R.layout.fullscreen_wallpaper_preview, null); |
| 447 | mFullResImageView = wallpaperPreviewContainer.findViewById(R.id.full_res_image); |
| 448 | mLowResImageView = wallpaperPreviewContainer.findViewById(R.id.low_res_image); |
Chuck Liao | ca1d129 | 2020-06-19 01:29:53 +0800 | [diff] [blame] | 449 | // Load a low-res placeholder image if there's a thumbnail available from the asset |
| 450 | // that can be shown to the user more quickly than the full-sized image. |
| 451 | if (mWallpaperAsset.hasLowResDataSource()) { |
| 452 | Activity activity = requireActivity(); |
| 453 | mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, Color.BLACK, |
| 454 | new WallpaperPreviewBitmapTransformation( |
| 455 | activity.getApplicationContext(), isRtl())); |
| 456 | } |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 457 | wallpaperPreviewContainer.measure( |
| 458 | makeMeasureSpec(mWallpaperSurface.getWidth(), EXACTLY), |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 459 | makeMeasureSpec(mWallpaperSurface.getHeight(), EXACTLY)); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 460 | wallpaperPreviewContainer.layout(0, 0, mWallpaperSurface.getWidth(), |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 461 | mWallpaperSurface.getHeight()); |
Ching-Sung Li | b940462 | 2020-05-18 22:38:45 +0800 | [diff] [blame] | 462 | mTouchForwardingLayout.setTargetView(mFullResImageView); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 463 | |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 464 | cleanUp(); |
| 465 | mHost = new SurfaceControlViewHost(getContext(), |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 466 | getContext().getDisplay(), mWallpaperSurface.getHostToken()); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 467 | mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(), |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 468 | wallpaperPreviewContainer.getHeight()); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 469 | mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage()); |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } |
| 475 | |
| 476 | @Override |
| 477 | public void surfaceDestroyed(SurfaceHolder holder) { } |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 478 | |
| 479 | public void cleanUp() { |
| 480 | if (mHost != null) { |
| 481 | mHost.release(); |
| 482 | mHost = null; |
| 483 | } |
| 484 | } |
Tracy Zhou | a19cbdd | 2020-04-26 12:12:11 -0700 | [diff] [blame] | 485 | }; |
| 486 | |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 487 | private void setEditingEnabled(boolean enabled) { |
Tracy Zhou | ecfd6eb | 2020-05-27 23:56:35 -0700 | [diff] [blame] | 488 | mTouchForwardingLayout.setForwardingEnabled(enabled); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Wesley.CW Wang | f21486b | 2020-06-02 18:30:32 +0800 | [diff] [blame] | 491 | private void updateScreenPreview(boolean isHomeSelected) { |
| 492 | mHome.setSelected(isHomeSelected); |
| 493 | mLock.setSelected(!isHomeSelected); |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 494 | mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE); |
| 495 | mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE); |
Tracy Zhou | 67fd7e3 | 2020-05-01 18:35:36 -0700 | [diff] [blame] | 496 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 497 | } |