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