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