Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.individual; |
| 17 | |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 18 | import android.annotation.MenuRes; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.ProgressDialog; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 21 | import android.app.WallpaperManager; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 23 | import android.content.DialogInterface; |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 24 | import android.content.Intent; |
Santiago Etchebehere | b185447 | 2019-06-06 17:44:54 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 26 | import android.content.res.Resources.NotFoundException; |
| 27 | import android.graphics.Point; |
Chuck Liao | ba40123 | 2020-03-13 20:11:04 +0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 29 | import android.os.Build.VERSION; |
| 30 | import android.os.Build.VERSION_CODES; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.Handler; |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 33 | import android.service.wallpaper.WallpaperService; |
Chuck Liao | ec053b0 | 2021-04-24 01:30:50 +0800 | [diff] [blame] | 34 | import android.text.TextUtils; |
| 35 | import android.util.ArraySet; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.view.LayoutInflater; |
Chuck Liao | 33f1af4 | 2021-04-13 20:52:35 +0800 | [diff] [blame] | 38 | import android.view.MenuItem; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 39 | import android.view.View; |
| 40 | import android.view.ViewGroup; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 41 | import android.widget.Toast; |
| 42 | |
Santiago Etchebehere | b185447 | 2019-06-06 17:44:54 -0700 | [diff] [blame] | 43 | import androidx.annotation.NonNull; |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 44 | import androidx.annotation.Nullable; |
Ching-Sung Li | 1ac024f | 2021-06-08 14:43:28 +0800 | [diff] [blame] | 45 | import androidx.cardview.widget.CardView; |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 46 | import androidx.core.widget.ContentLoadingProgressBar; |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 47 | import androidx.fragment.app.DialogFragment; |
Chuck Liao | 7e33372 | 2021-02-27 02:21:20 +0800 | [diff] [blame] | 48 | import androidx.fragment.app.Fragment; |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 49 | import androidx.recyclerview.widget.GridLayoutManager; |
| 50 | import androidx.recyclerview.widget.RecyclerView; |
| 51 | import androidx.recyclerview.widget.RecyclerView.OnScrollListener; |
| 52 | import androidx.recyclerview.widget.RecyclerView.ViewHolder; |
| 53 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 54 | import com.android.wallpaper.R; |
| 55 | import com.android.wallpaper.asset.Asset; |
| 56 | import com.android.wallpaper.asset.Asset.DrawableLoadedListener; |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 57 | import com.android.wallpaper.model.Category; |
| 58 | import com.android.wallpaper.model.CategoryProvider; |
| 59 | import com.android.wallpaper.model.CategoryReceiver; |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 60 | import com.android.wallpaper.model.LiveWallpaperInfo; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 61 | import com.android.wallpaper.model.WallpaperCategory; |
| 62 | import com.android.wallpaper.model.WallpaperInfo; |
| 63 | import com.android.wallpaper.model.WallpaperReceiver; |
| 64 | import com.android.wallpaper.model.WallpaperRotationInitializer; |
| 65 | import com.android.wallpaper.model.WallpaperRotationInitializer.Listener; |
| 66 | import com.android.wallpaper.model.WallpaperRotationInitializer.NetworkPreference; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 67 | import com.android.wallpaper.module.FormFactorChecker; |
| 68 | import com.android.wallpaper.module.FormFactorChecker.FormFactor; |
| 69 | import com.android.wallpaper.module.Injector; |
| 70 | import com.android.wallpaper.module.InjectorProvider; |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 71 | import com.android.wallpaper.module.PackageStatusNotifier; |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 72 | import com.android.wallpaper.module.UserEventLogger; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 73 | import com.android.wallpaper.module.WallpaperChangedNotifier; |
| 74 | import com.android.wallpaper.module.WallpaperPersister; |
| 75 | import com.android.wallpaper.module.WallpaperPersister.Destination; |
| 76 | import com.android.wallpaper.module.WallpaperPreferences; |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 77 | import com.android.wallpaper.module.WallpaperSetter; |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 78 | import com.android.wallpaper.picker.AppbarFragment; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 79 | import com.android.wallpaper.picker.BaseActivity; |
| 80 | import com.android.wallpaper.picker.CurrentWallpaperBottomSheetPresenter; |
Chuck Liao | b17d150 | 2020-09-07 21:55:30 +0800 | [diff] [blame] | 81 | import com.android.wallpaper.picker.FragmentTransactionChecker; |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 82 | import com.android.wallpaper.picker.MyPhotosStarter.MyPhotosStarterProvider; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 83 | import com.android.wallpaper.picker.RotationStarter; |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 84 | import com.android.wallpaper.picker.SetWallpaperDialogFragment; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 85 | import com.android.wallpaper.picker.SetWallpaperErrorDialogFragment; |
| 86 | import com.android.wallpaper.picker.StartRotationDialogFragment; |
| 87 | import com.android.wallpaper.picker.StartRotationErrorDialogFragment; |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 88 | import com.android.wallpaper.picker.WallpaperInfoHelper; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 89 | import com.android.wallpaper.picker.WallpapersUiContainer; |
| 90 | import com.android.wallpaper.picker.individual.SetIndividualHolder.OnSetListener; |
| 91 | import com.android.wallpaper.util.DiskBasedLogger; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 92 | import com.android.wallpaper.util.SizeCalculator; |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 93 | import com.android.wallpaper.widget.WallpaperInfoView; |
Wesley.CW Wang | dc68fde | 2020-06-15 19:12:33 +0800 | [diff] [blame] | 94 | import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate; |
| 95 | import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost; |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame] | 96 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 97 | import com.bumptech.glide.Glide; |
| 98 | import com.bumptech.glide.MemoryCategory; |
| 99 | |
| 100 | import java.util.ArrayList; |
| 101 | import java.util.Date; |
| 102 | import java.util.List; |
Chuck Liao | 11f4a76 | 2020-04-08 13:24:43 +0800 | [diff] [blame] | 103 | import java.util.Optional; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 104 | import java.util.Random; |
Chuck Liao | ec053b0 | 2021-04-24 01:30:50 +0800 | [diff] [blame] | 105 | import java.util.Set; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Displays the Main UI for picking an individual wallpaper image. |
| 109 | */ |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 110 | public class IndividualPickerFragment extends AppbarFragment |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 111 | implements RotationStarter, StartRotationErrorDialogFragment.Listener, |
| 112 | CurrentWallpaperBottomSheetPresenter.RefreshListener, |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 113 | SetWallpaperErrorDialogFragment.Listener, SetWallpaperDialogFragment.Listener, |
| 114 | StartRotationDialogFragment.Listener { |
Chuck Liao | 46644b9 | 2020-06-08 14:20:50 +0800 | [diff] [blame] | 115 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 116 | /** |
| 117 | * Position of a special tile that doesn't belong to an individual wallpaper of the category, |
| 118 | * such as "my photos" or "daily rotation". |
| 119 | */ |
| 120 | static final int SPECIAL_FIXED_TILE_ADAPTER_POSITION = 0; |
| 121 | static final String ARG_CATEGORY_COLLECTION_ID = "category_collection_id"; |
| 122 | |
Chuck Liao | 6db3eba | 2021-06-16 23:50:45 +0800 | [diff] [blame] | 123 | protected static final int MAX_CAPACITY_IN_FEWER_COLUMN_LAYOUT = 8; |
| 124 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 125 | private static final String TAG = "IndividualPickerFrgmnt"; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 126 | private static final int UNUSED_REQUEST_CODE = 1; |
| 127 | private static final String TAG_START_ROTATION_DIALOG = "start_rotation_dialog"; |
| 128 | private static final String TAG_START_ROTATION_ERROR_DIALOG = "start_rotation_error_dialog"; |
| 129 | private static final String PROGRESS_DIALOG_NO_TITLE = null; |
| 130 | private static final boolean PROGRESS_DIALOG_INDETERMINATE = true; |
| 131 | private static final String TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT = |
| 132 | "individual_set_wallpaper_error_dialog"; |
Santiago Etchebehere | b185447 | 2019-06-06 17:44:54 -0700 | [diff] [blame] | 133 | private static final String KEY_NIGHT_MODE = "IndividualPickerFragment.NIGHT_MODE"; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 134 | |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 135 | /** |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 136 | * An interface for updating the thumbnail with the specific wallpaper. |
| 137 | */ |
| 138 | public interface ThumbnailUpdater { |
| 139 | /** |
| 140 | * Updates the thumbnail with the specific wallpaper. |
| 141 | */ |
| 142 | void updateThumbnail(WallpaperInfo wallpaperInfo); |
| 143 | |
| 144 | /** |
| 145 | * Restores to the thumbnails of the wallpapers which were applied. |
| 146 | */ |
| 147 | void restoreThumbnails(); |
| 148 | } |
| 149 | |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 150 | /** |
| 151 | * An interface for receiving the destination of the new applied wallpaper. |
| 152 | */ |
| 153 | public interface WallpaperDestinationCallback { |
| 154 | /** |
| 155 | * Called when the destination of the wallpaper is set. |
| 156 | * |
| 157 | * @param destination the destination which a wallpaper may be set. |
| 158 | * See {@link Destination} for more details. |
| 159 | */ |
| 160 | void onDestinationSet(@Destination int destination); |
| 161 | } |
| 162 | |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 163 | /** |
| 164 | * The listener which will be notified when the wallpaper is selected. |
| 165 | */ |
| 166 | public interface WallpaperSelectedListener { |
| 167 | /** |
| 168 | * Called when the wallpaper is selected. |
| 169 | * |
| 170 | * @param position the position of the selected wallpaper |
| 171 | */ |
| 172 | void onWallpaperSelected(int position); |
| 173 | } |
| 174 | |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 175 | /** |
Chuck Liao | 7e33372 | 2021-02-27 02:21:20 +0800 | [diff] [blame] | 176 | * Interface to be implemented by a Fragment(or an Activity) hosting |
| 177 | * a {@link IndividualPickerFragment}. |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 178 | */ |
| 179 | public interface IndividualPickerFragmentHost { |
| 180 | /** |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 181 | * Indicates if the host has toolbar to show the title. If it does, we should set the title |
| 182 | * there. |
| 183 | */ |
| 184 | boolean isHostToolbarShown(); |
| 185 | |
| 186 | /** |
| 187 | * Sets the title in the host's toolbar. |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 188 | */ |
| 189 | void setToolbarTitle(CharSequence title); |
| 190 | |
| 191 | /** |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 192 | * Configures the menu in the toolbar. |
| 193 | * |
| 194 | * @param menuResId the resource id of the menu |
| 195 | */ |
| 196 | void setToolbarMenu(@MenuRes int menuResId); |
| 197 | |
| 198 | /** |
| 199 | * Removes the menu in the toolbar. |
| 200 | */ |
| 201 | void removeToolbarMenu(); |
| 202 | |
| 203 | /** |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 204 | * Moves to the previous fragment. |
| 205 | */ |
| 206 | void moveToPreviousFragment(); |
| 207 | } |
| 208 | |
Ching-Sung Li | ed6560f | 2020-05-04 19:25:09 +0800 | [diff] [blame] | 209 | WallpaperPersister mWallpaperPersister; |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 210 | WallpaperPreferences mWallpaperPreferences; |
| 211 | WallpaperChangedNotifier mWallpaperChangedNotifier; |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 212 | RecyclerView mImageGrid; |
| 213 | IndividualAdapter mAdapter; |
| 214 | WallpaperCategory mCategory; |
| 215 | WallpaperRotationInitializer mWallpaperRotationInitializer; |
| 216 | List<WallpaperInfo> mWallpapers; |
| 217 | Point mTileSizePx; |
| 218 | WallpapersUiContainer mWallpapersUiContainer; |
| 219 | @FormFactor |
| 220 | int mFormFactor; |
| 221 | PackageStatusNotifier mPackageStatusNotifier; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 222 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 223 | Handler mHandler; |
| 224 | Random mRandom; |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 225 | boolean mIsWallpapersReceived; |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 226 | |
| 227 | WallpaperChangedNotifier.Listener mWallpaperChangedListener = |
| 228 | new WallpaperChangedNotifier.Listener() { |
| 229 | @Override |
| 230 | public void onWallpaperChanged() { |
| 231 | if (mFormFactor != FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | ViewHolder selectedViewHolder = mImageGrid.findViewHolderForAdapterPosition( |
| 236 | mAdapter.mSelectedAdapterPosition); |
| 237 | |
| 238 | // Null remote ID => My Photos wallpaper, so deselect whatever was previously selected. |
| 239 | if (mWallpaperPreferences.getHomeWallpaperRemoteId() == null) { |
| 240 | if (selectedViewHolder instanceof SelectableHolder) { |
| 241 | ((SelectableHolder) selectedViewHolder).setSelectionState( |
| 242 | SelectableHolder.SELECTION_STATE_DESELECTED); |
| 243 | } |
| 244 | } else { |
| 245 | mAdapter.updateSelectedTile(mAdapter.mPendingSelectedAdapterPosition); |
| 246 | } |
| 247 | } |
| 248 | }; |
| 249 | PackageStatusNotifier.Listener mAppStatusListener; |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 250 | WallpaperInfoView mWallpaperInfoView; |
Ching-Sung Li | 073812b | 2020-04-07 21:19:21 +0800 | [diff] [blame] | 251 | @Nullable WallpaperInfo mSelectedWallpaperInfo; |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 252 | |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 253 | private UserEventLogger mUserEventLogger; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 254 | private ProgressDialog mProgressDialog; |
| 255 | private boolean mTestingMode; |
| 256 | private CurrentWallpaperBottomSheetPresenter mCurrentWallpaperBottomSheetPresenter; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 257 | private SetIndividualHolder mPendingSetIndividualHolder; |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 258 | private ContentLoadingProgressBar mLoading; |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 259 | private CategoryProvider mCategoryProvider; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Staged error dialog fragments that were unable to be shown when the activity didn't allow |
| 263 | * committing fragment transactions. |
| 264 | */ |
| 265 | private SetWallpaperErrorDialogFragment mStagedSetWallpaperErrorDialogFragment; |
| 266 | private StartRotationErrorDialogFragment mStagedStartRotationErrorDialogFragment; |
| 267 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 268 | private Runnable mCurrentWallpaperBottomSheetExpandedRunnable; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Whether {@code mUpdateDailyWallpaperThumbRunnable} has been run at least once in this |
| 272 | * invocation of the fragment. |
| 273 | */ |
| 274 | private boolean mWasUpdateRunnableRun; |
| 275 | |
| 276 | /** |
| 277 | * A Runnable which regularly updates the thumbnail for the "Daily wallpapers" tile in desktop |
| 278 | * mode. |
| 279 | */ |
| 280 | private Runnable mUpdateDailyWallpaperThumbRunnable = new Runnable() { |
| 281 | @Override |
| 282 | public void run() { |
| 283 | ViewHolder viewHolder = mImageGrid.findViewHolderForAdapterPosition( |
| 284 | SPECIAL_FIXED_TILE_ADAPTER_POSITION); |
| 285 | if (viewHolder instanceof DesktopRotationHolder) { |
| 286 | updateDesktopDailyRotationThumbnail((DesktopRotationHolder) viewHolder); |
| 287 | } else { // viewHolder is null |
| 288 | // If the rotation tile is unavailable (because user has scrolled down, causing the |
| 289 | // ViewHolder to be recycled), schedule the update for some time later. Once user scrolls up |
| 290 | // again, the ViewHolder will be re-bound and its thumbnail will be updated. |
| 291 | mHandler.postDelayed(mUpdateDailyWallpaperThumbRunnable, |
| 292 | DesktopRotationHolder.CROSSFADE_DURATION_MILLIS |
| 293 | + DesktopRotationHolder.CROSSFADE_DURATION_PAUSE_MILLIS); |
| 294 | } |
| 295 | } |
| 296 | }; |
| 297 | |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 298 | private WallpaperSetter mWallpaperSetter; |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 299 | private WallpaperInfo mAppliedWallpaperInfo; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 300 | private WallpaperManager mWallpaperManager; |
| 301 | private int mWallpaperDestination; |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 302 | private WallpaperSelectedListener mWallpaperSelectedListener; |
Chuck Liao | ec053b0 | 2021-04-24 01:30:50 +0800 | [diff] [blame] | 303 | private Set<String> mAppliedWallpaperIds; |
Chuck Liao | 8ec38e0 | 2020-02-26 20:59:32 +0800 | [diff] [blame] | 304 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 305 | public static IndividualPickerFragment newInstance(String collectionId) { |
| 306 | Bundle args = new Bundle(); |
| 307 | args.putString(ARG_CATEGORY_COLLECTION_ID, collectionId); |
| 308 | |
| 309 | IndividualPickerFragment fragment = new IndividualPickerFragment(); |
| 310 | fragment.setArguments(args); |
| 311 | return fragment; |
| 312 | } |
| 313 | |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 314 | /** |
| 315 | * Highlights the applied wallpaper (if it exists) according to the destination a wallpaper |
| 316 | * would be set. |
| 317 | * |
| 318 | * @param wallpaperDestination the destination a wallpaper would be set. |
| 319 | * It will be either {@link Destination#DEST_HOME_SCREEN} |
| 320 | * or {@link Destination#DEST_LOCK_SCREEN}. |
| 321 | */ |
| 322 | public void highlightAppliedWallpaper(@Destination int wallpaperDestination) { |
| 323 | mWallpaperDestination = wallpaperDestination; |
Chuck Liao | 11f4a76 | 2020-04-08 13:24:43 +0800 | [diff] [blame] | 324 | if (mWallpapers != null) { |
| 325 | refreshAppliedWallpaper(); |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 329 | private void updateDesktopDailyRotationThumbnail(DesktopRotationHolder holder) { |
| 330 | int wallpapersIndex = mRandom.nextInt(mWallpapers.size()); |
| 331 | Asset newThumbnailAsset = mWallpapers.get(wallpapersIndex).getThumbAsset( |
| 332 | getActivity()); |
| 333 | holder.updateThumbnail(newThumbnailAsset, new DrawableLoadedListener() { |
| 334 | @Override |
| 335 | public void onDrawableLoaded() { |
| 336 | if (getActivity() == null) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | // Schedule the next update of the thumbnail. |
| 341 | int delayMillis = DesktopRotationHolder.CROSSFADE_DURATION_MILLIS |
| 342 | + DesktopRotationHolder.CROSSFADE_DURATION_PAUSE_MILLIS; |
| 343 | mHandler.postDelayed(mUpdateDailyWallpaperThumbRunnable, delayMillis); |
| 344 | } |
| 345 | }); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void onCreate(Bundle savedInstanceState) { |
| 350 | super.onCreate(savedInstanceState); |
| 351 | |
| 352 | Injector injector = InjectorProvider.getInjector(); |
| 353 | Context appContext = getContext().getApplicationContext(); |
| 354 | mWallpaperPreferences = injector.getPreferences(appContext); |
| 355 | |
| 356 | mWallpaperChangedNotifier = WallpaperChangedNotifier.getInstance(); |
| 357 | mWallpaperChangedNotifier.registerListener(mWallpaperChangedListener); |
| 358 | |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 359 | mWallpaperManager = WallpaperManager.getInstance(appContext); |
| 360 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 361 | mFormFactor = injector.getFormFactorChecker(appContext).getFormFactor(); |
| 362 | |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 363 | mPackageStatusNotifier = injector.getPackageStatusNotifier(appContext); |
| 364 | |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 365 | mUserEventLogger = injector.getUserEventLogger(appContext); |
| 366 | |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 367 | mWallpaperPersister = injector.getWallpaperPersister(appContext); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 368 | mWallpaperSetter = new WallpaperSetter( |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 369 | mWallpaperPersister, |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 370 | injector.getPreferences(appContext), |
| 371 | injector.getUserEventLogger(appContext), |
| 372 | false); |
| 373 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 374 | mWallpapers = new ArrayList<>(); |
| 375 | mRandom = new Random(); |
| 376 | mHandler = new Handler(); |
| 377 | |
Santiago Etchebehere | b185447 | 2019-06-06 17:44:54 -0700 | [diff] [blame] | 378 | // Clear Glide's cache if night-mode changed to ensure thumbnails are reloaded |
| 379 | if (savedInstanceState != null && (savedInstanceState.getInt(KEY_NIGHT_MODE) |
| 380 | != (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK))) { |
| 381 | Glide.get(getContext()).clearMemory(); |
| 382 | } |
| 383 | |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 384 | mCategoryProvider = injector.getCategoryProvider(appContext); |
| 385 | mCategoryProvider.fetchCategories(new CategoryReceiver() { |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public void onCategoryReceived(Category category) { |
| 388 | // Do nothing. |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void doneFetchingCategories() { |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 393 | Category category = mCategoryProvider.getCategory( |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 394 | getArguments().getString(ARG_CATEGORY_COLLECTION_ID)); |
Chuck Liao | f44433e | 2020-09-21 22:58:26 +0800 | [diff] [blame] | 395 | if (category != null && !(category instanceof WallpaperCategory)) { |
Chuck Liao | 7e36d17 | 2020-09-18 01:09:17 +0800 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | mCategory = (WallpaperCategory) category; |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 399 | if (mCategory == null) { |
| 400 | DiskBasedLogger.e(TAG, "Failed to find the category.", getContext()); |
| 401 | |
| 402 | // The absence of this category in the CategoryProvider indicates a broken |
| 403 | // state, see b/38030129. Hence, finish the activity and return. |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 404 | getIndividualPickerFragmentHost().moveToPreviousFragment(); |
| 405 | Toast.makeText(getContext(), R.string.collection_not_exist_msg, |
| 406 | Toast.LENGTH_SHORT).show(); |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | onCategoryLoaded(); |
| 410 | } |
| 411 | }, false); |
| 412 | } |
| 413 | |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 414 | |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 415 | protected void onCategoryLoaded() { |
Chuck Liao | 7e36d17 | 2020-09-18 01:09:17 +0800 | [diff] [blame] | 416 | if (getIndividualPickerFragmentHost() == null) { |
| 417 | return; |
| 418 | } |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 419 | if (getIndividualPickerFragmentHost().isHostToolbarShown()) { |
| 420 | getIndividualPickerFragmentHost().setToolbarTitle(mCategory.getTitle()); |
| 421 | } else { |
| 422 | setTitle(mCategory.getTitle()); |
| 423 | } |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 424 | mWallpaperRotationInitializer = mCategory.getWallpaperRotationInitializer(); |
Chuck Liao | 33f1af4 | 2021-04-13 20:52:35 +0800 | [diff] [blame] | 425 | if (mToolbar != null && isRotationEnabled()) { |
| 426 | setUpToolbarMenu(R.menu.individual_picker_menu); |
| 427 | } |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 428 | fetchWallpapers(false); |
| 429 | |
| 430 | if (mCategory.supportsThirdParty()) { |
| 431 | mAppStatusListener = (packageName, status) -> { |
| 432 | if (status != PackageStatusNotifier.PackageStatus.REMOVED || |
| 433 | mCategory.containsThirdParty(packageName)) { |
| 434 | fetchWallpapers(true); |
| 435 | } |
| 436 | }; |
| 437 | mPackageStatusNotifier.addListener(mAppStatusListener, |
| 438 | WallpaperService.SERVICE_INTERFACE); |
| 439 | } |
| 440 | } |
| 441 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 442 | void fetchWallpapers(boolean forceReload) { |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 443 | mWallpapers.clear(); |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 444 | mIsWallpapersReceived = false; |
| 445 | updateLoading(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 446 | mCategory.fetchWallpapers(getActivity().getApplicationContext(), new WallpaperReceiver() { |
| 447 | @Override |
| 448 | public void onWallpapersReceived(List<WallpaperInfo> wallpapers) { |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 449 | mIsWallpapersReceived = true; |
| 450 | updateLoading(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 451 | for (WallpaperInfo wallpaper : wallpapers) { |
| 452 | mWallpapers.add(wallpaper); |
| 453 | } |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 454 | maybeSetUpImageGrid(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 455 | |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 456 | // Wallpapers may load after the adapter is initialized, in which case we have |
| 457 | // to explicitly notify that the data set has changed. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 458 | if (mAdapter != null) { |
| 459 | mAdapter.notifyDataSetChanged(); |
| 460 | } |
| 461 | |
| 462 | if (mWallpapersUiContainer != null) { |
| 463 | mWallpapersUiContainer.onWallpapersReady(); |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 464 | } else { |
| 465 | if (wallpapers.isEmpty()) { |
| 466 | // If there are no more wallpapers and we're on phone, just finish the |
| 467 | // Activity. |
| 468 | Activity activity = getActivity(); |
| 469 | if (activity != null |
| 470 | && mFormFactor == FormFactorChecker.FORM_FACTOR_MOBILE) { |
| 471 | activity.finish(); |
| 472 | } |
| 473 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 474 | } |
| 475 | } |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 476 | }, forceReload); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 479 | void updateLoading() { |
| 480 | if (mLoading == null) { |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | if (mIsWallpapersReceived) { |
| 485 | mLoading.hide(); |
| 486 | } else { |
| 487 | mLoading.show(); |
| 488 | } |
| 489 | } |
| 490 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 491 | @Override |
Santiago Etchebehere | b185447 | 2019-06-06 17:44:54 -0700 | [diff] [blame] | 492 | public void onSaveInstanceState(@NonNull Bundle outState) { |
| 493 | super.onSaveInstanceState(outState); |
Santiago Etchebehere | 0ec065c | 2019-06-13 11:30:21 -0700 | [diff] [blame] | 494 | outState.putInt(KEY_NIGHT_MODE, |
Santiago Etchebehere | b185447 | 2019-06-06 17:44:54 -0700 | [diff] [blame] | 495 | getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK); |
| 496 | } |
| 497 | |
| 498 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 499 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 500 | Bundle savedInstanceState) { |
| 501 | View view = inflater.inflate(R.layout.fragment_individual_picker, container, false); |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 502 | if (getIndividualPickerFragmentHost().isHostToolbarShown()) { |
| 503 | view.findViewById(R.id.header_bar).setVisibility(View.GONE); |
Kunhung Li | d017436 | 2021-04-05 15:31:41 +0800 | [diff] [blame] | 504 | setUpArrowEnabled(/* upArrow= */ true); |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 505 | if (isRotationEnabled()) { |
| 506 | getIndividualPickerFragmentHost().setToolbarMenu(R.menu.individual_picker_menu); |
| 507 | } |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 508 | } else { |
| 509 | setUpToolbar(view); |
Chuck Liao | 33f1af4 | 2021-04-13 20:52:35 +0800 | [diff] [blame] | 510 | if (isRotationEnabled()) { |
| 511 | setUpToolbarMenu(R.menu.individual_picker_menu); |
| 512 | } |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 513 | if (mCategory != null) { |
| 514 | setTitle(mCategory.getTitle()); |
| 515 | } |
| 516 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 517 | |
Chuck Liao | ec053b0 | 2021-04-24 01:30:50 +0800 | [diff] [blame] | 518 | mAppliedWallpaperIds = getAppliedWallpaperIds(); |
| 519 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 520 | mImageGrid = (RecyclerView) view.findViewById(R.id.wallpaper_grid); |
| 521 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 522 | int gridPaddingPx = getResources().getDimensionPixelSize(R.dimen.grid_padding_desktop); |
| 523 | updateImageGridPadding(false /* addExtraBottomSpace */); |
| 524 | mImageGrid.setScrollBarSize(gridPaddingPx); |
| 525 | } |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 526 | mLoading = view.findViewById(R.id.loading_indicator); |
| 527 | updateLoading(); |
Santiago Etchebehere | 460fbcb | 2019-08-20 11:47:30 -0700 | [diff] [blame] | 528 | maybeSetUpImageGrid(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 529 | setUpBottomSheet(); |
Chihhang Chuang | 5ebbfea | 2021-04-15 09:50:24 +0800 | [diff] [blame] | 530 | // For nav bar edge-to-edge effect. |
Kunhung Li | 1b0cb47 | 2021-04-26 20:52:04 +0800 | [diff] [blame] | 531 | view.setOnApplyWindowInsetsListener((v, windowInsets) -> { |
| 532 | // For status bar height. |
Chihhang Chuang | 5ebbfea | 2021-04-15 09:50:24 +0800 | [diff] [blame] | 533 | v.setPadding( |
| 534 | v.getPaddingLeft(), |
Kunhung Li | 1b0cb47 | 2021-04-26 20:52:04 +0800 | [diff] [blame] | 535 | windowInsets.getSystemWindowInsetTop(), |
Chihhang Chuang | 5ebbfea | 2021-04-15 09:50:24 +0800 | [diff] [blame] | 536 | v.getPaddingRight(), |
Kunhung Li | 1b0cb47 | 2021-04-26 20:52:04 +0800 | [diff] [blame] | 537 | v.getPaddingBottom()); |
| 538 | |
| 539 | View gridView = v.findViewById(R.id.wallpaper_grid); |
| 540 | gridView.setPadding( |
| 541 | gridView.getPaddingLeft(), |
| 542 | gridView.getPaddingTop(), |
| 543 | gridView.getPaddingRight(), |
Chihhang Chuang | 5ebbfea | 2021-04-15 09:50:24 +0800 | [diff] [blame] | 544 | windowInsets.getSystemWindowInsetBottom()); |
| 545 | return windowInsets.consumeSystemWindowInsets(); |
| 546 | }); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 547 | return view; |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void onClickTryAgain(@Destination int unused) { |
| 552 | if (mPendingSetIndividualHolder != null) { |
| 553 | mPendingSetIndividualHolder.setWallpaper(); |
| 554 | } |
| 555 | } |
| 556 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 557 | void updateImageGridPadding(boolean addExtraBottomSpace) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 558 | int gridPaddingPx = getResources().getDimensionPixelSize(R.dimen.grid_padding_desktop); |
| 559 | int bottomSheetHeightPx = getResources().getDimensionPixelSize( |
| 560 | R.dimen.current_wallpaper_bottom_sheet_layout_height); |
| 561 | int paddingBottomPx = addExtraBottomSpace ? bottomSheetHeightPx : 0; |
| 562 | // Only left and top may be set in order for the GridMarginDecoration to work properly. |
| 563 | mImageGrid.setPadding( |
| 564 | gridPaddingPx, gridPaddingPx, 0, paddingBottomPx); |
| 565 | } |
| 566 | |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 567 | private IndividualPickerFragmentHost getIndividualPickerFragmentHost() { |
Chuck Liao | 7e33372 | 2021-02-27 02:21:20 +0800 | [diff] [blame] | 568 | Fragment parentFragment = getParentFragment(); |
| 569 | if (parentFragment != null) { |
| 570 | return (IndividualPickerFragmentHost) parentFragment; |
| 571 | } else { |
| 572 | return (IndividualPickerFragmentHost) getActivity(); |
| 573 | } |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 574 | } |
| 575 | |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 576 | protected void maybeSetUpImageGrid() { |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 577 | // Skip if mImageGrid been initialized yet |
| 578 | if (mImageGrid == null) { |
| 579 | return; |
| 580 | } |
| 581 | // Skip if category hasn't loaded yet |
| 582 | if (mCategory == null) { |
| 583 | return; |
| 584 | } |
Chuck Liao | 658ee89 | 2021-06-30 10:17:12 +0800 | [diff] [blame] | 585 | if (getContext() == null) { |
| 586 | return; |
| 587 | } |
Chuck Liao | 6db3eba | 2021-06-16 23:50:45 +0800 | [diff] [blame] | 588 | |
| 589 | // Wallpaper count could change, so we may need to change the layout(2 or 3 columns layout) |
| 590 | GridLayoutManager gridLayoutManager = (GridLayoutManager) mImageGrid.getLayoutManager(); |
| 591 | boolean needUpdateLayout = |
| 592 | gridLayoutManager != null && gridLayoutManager.getSpanCount() != getNumColumns(); |
| 593 | |
| 594 | // Skip if the adapter was already created and don't need to change the layout |
| 595 | if (mAdapter != null && !needUpdateLayout) { |
Santiago Etchebehere | 8648bb8 | 2019-08-06 17:09:02 -0700 | [diff] [blame] | 596 | return; |
| 597 | } |
Chuck Liao | 6db3eba | 2021-06-16 23:50:45 +0800 | [diff] [blame] | 598 | |
| 599 | // Clear the old decoration |
| 600 | int decorationCount = mImageGrid.getItemDecorationCount(); |
| 601 | for (int i = 0; i < decorationCount; i++) { |
| 602 | mImageGrid.removeItemDecorationAt(i); |
| 603 | } |
| 604 | |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 605 | mImageGrid.addItemDecoration(new GridPaddingDecoration(getGridItemPaddingHorizontal(), |
| 606 | getGridItemPaddingBottom())); |
| 607 | int edgePadding = getEdgePadding(); |
| 608 | mImageGrid.setPadding(edgePadding, mImageGrid.getPaddingTop(), edgePadding, |
| 609 | mImageGrid.getPaddingBottom()); |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 610 | mTileSizePx = isFewerColumnLayout() |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 611 | ? SizeCalculator.getFeaturedIndividualTileSize(getActivity()) |
| 612 | : SizeCalculator.getIndividualTileSize(getActivity()); |
Santiago Etchebehere | 460fbcb | 2019-08-20 11:47:30 -0700 | [diff] [blame] | 613 | setUpImageGrid(); |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 614 | mImageGrid.setAccessibilityDelegateCompat( |
| 615 | new WallpaperPickerRecyclerViewAccessibilityDelegate( |
| 616 | mImageGrid, (BottomSheetHost) getParentFragment(), getNumColumns())); |
Santiago Etchebehere | 460fbcb | 2019-08-20 11:47:30 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Ching-Sung Li | b69e993 | 2021-05-25 20:03:01 +0800 | [diff] [blame] | 619 | boolean isFewerColumnLayout() { |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 620 | return mWallpapers != null && mWallpapers.size() <= MAX_CAPACITY_IN_FEWER_COLUMN_LAYOUT; |
| 621 | } |
| 622 | |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 623 | private int getGridItemPaddingHorizontal() { |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 624 | return isFewerColumnLayout() |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 625 | ? getResources().getDimensionPixelSize( |
| 626 | R.dimen.grid_item_featured_individual_padding_horizontal) |
| 627 | : getResources().getDimensionPixelSize( |
| 628 | R.dimen.grid_item_individual_padding_horizontal); |
| 629 | } |
| 630 | |
| 631 | private int getGridItemPaddingBottom() { |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 632 | return isFewerColumnLayout() |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 633 | ? getResources().getDimensionPixelSize( |
| 634 | R.dimen.grid_item_featured_individual_padding_bottom) |
| 635 | : getResources().getDimensionPixelSize(R.dimen.grid_item_individual_padding_bottom); |
| 636 | } |
| 637 | |
| 638 | private int getEdgePadding() { |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 639 | return isFewerColumnLayout() |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 640 | ? getResources().getDimensionPixelSize(R.dimen.featured_wallpaper_grid_edge_space) |
| 641 | : getResources().getDimensionPixelSize(R.dimen.wallpaper_grid_edge_space); |
| 642 | } |
| 643 | |
Santiago Etchebehere | 460fbcb | 2019-08-20 11:47:30 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Create the adapter and assign it to mImageGrid. |
| 646 | * Both mImageGrid and mCategory are guaranteed to not be null when this method is called. |
| 647 | */ |
| 648 | void setUpImageGrid() { |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 649 | mAdapter = new IndividualAdapter(mWallpapers); |
| 650 | mImageGrid.setAdapter(mAdapter); |
| 651 | mImageGrid.setLayoutManager(new GridLayoutManager(getActivity(), getNumColumns())); |
| 652 | } |
| 653 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 654 | /** |
| 655 | * Enables and populates the "Currently set" wallpaper BottomSheet. |
| 656 | */ |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 657 | void setUpBottomSheet() { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 658 | mImageGrid.addOnScrollListener(new OnScrollListener() { |
| 659 | @Override |
| 660 | public void onScrolled(RecyclerView recyclerView, int dx, final int dy) { |
| 661 | if (mCurrentWallpaperBottomSheetPresenter == null) { |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | if (mCurrentWallpaperBottomSheetExpandedRunnable != null) { |
| 666 | mHandler.removeCallbacks(mCurrentWallpaperBottomSheetExpandedRunnable); |
| 667 | } |
| 668 | mCurrentWallpaperBottomSheetExpandedRunnable = new Runnable() { |
| 669 | @Override |
| 670 | public void run() { |
| 671 | if (dy > 0) { |
| 672 | mCurrentWallpaperBottomSheetPresenter.setCurrentWallpapersExpanded(false); |
| 673 | } else { |
| 674 | mCurrentWallpaperBottomSheetPresenter.setCurrentWallpapersExpanded(true); |
| 675 | } |
| 676 | } |
| 677 | }; |
| 678 | mHandler.postDelayed(mCurrentWallpaperBottomSheetExpandedRunnable, 100); |
| 679 | } |
| 680 | }); |
| 681 | } |
| 682 | |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 683 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 684 | public void onResume() { |
| 685 | super.onResume(); |
| 686 | |
Santiago Etchebehere | 8cad0dd | 2019-10-17 10:52:39 -0700 | [diff] [blame] | 687 | WallpaperPreferences preferences = InjectorProvider.getInjector() |
| 688 | .getPreferences(getActivity()); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 689 | preferences.setLastAppActiveTimestamp(new Date().getTime()); |
| 690 | |
| 691 | // Reset Glide memory settings to a "normal" level of usage since it may have been lowered in |
| 692 | // PreviewFragment. |
| 693 | Glide.get(getActivity()).setMemoryCategory(MemoryCategory.NORMAL); |
| 694 | |
| 695 | // Show the staged 'start rotation' error dialog fragment if there is one that was unable to be |
| 696 | // shown earlier when this fragment's hosting activity didn't allow committing fragment |
| 697 | // transactions. |
| 698 | if (mStagedStartRotationErrorDialogFragment != null) { |
| 699 | mStagedStartRotationErrorDialogFragment.show( |
| 700 | getFragmentManager(), TAG_START_ROTATION_ERROR_DIALOG); |
| 701 | mStagedStartRotationErrorDialogFragment = null; |
| 702 | } |
| 703 | |
| 704 | // Show the staged 'load wallpaper' or 'set wallpaper' error dialog fragments if there is one |
| 705 | // that was unable to be shown earlier when this fragment's hosting activity didn't allow |
| 706 | // committing fragment transactions. |
| 707 | if (mStagedSetWallpaperErrorDialogFragment != null) { |
| 708 | mStagedSetWallpaperErrorDialogFragment.show( |
| 709 | getFragmentManager(), TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT); |
| 710 | mStagedSetWallpaperErrorDialogFragment = null; |
| 711 | } |
| 712 | |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 713 | if (shouldShowRotationTile() && mWasUpdateRunnableRun && !mWallpapers.isEmpty()) { |
| 714 | // Must be resuming from a previously stopped state, so re-schedule the update of the |
| 715 | // daily wallpapers tile thumbnail. |
| 716 | mUpdateDailyWallpaperThumbRunnable.run(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 717 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | @Override |
| 721 | public void onStop() { |
| 722 | super.onStop(); |
| 723 | mHandler.removeCallbacks(mUpdateDailyWallpaperThumbRunnable); |
| 724 | } |
| 725 | |
| 726 | @Override |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 727 | public void onDestroyView() { |
| 728 | super.onDestroyView(); |
| 729 | getIndividualPickerFragmentHost().removeToolbarMenu(); |
| 730 | } |
| 731 | |
| 732 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 733 | public void onDestroy() { |
| 734 | super.onDestroy(); |
| 735 | if (mProgressDialog != null) { |
| 736 | mProgressDialog.dismiss(); |
| 737 | } |
| 738 | mWallpaperChangedNotifier.unregisterListener(mWallpaperChangedListener); |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 739 | if (mAppStatusListener != null) { |
| 740 | mPackageStatusNotifier.removeListener(mAppStatusListener); |
| 741 | } |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 742 | mWallpaperSetter.cleanUp(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | @Override |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 746 | public void onStartRotationDialogDismiss(@NonNull DialogInterface dialog) { |
chihhangchuang | 8f9da5f | 2020-06-17 00:32:18 +0800 | [diff] [blame] | 747 | // TODO(b/159310028): Refactor fragment layer to make it able to restore from config change. |
| 748 | // This is to handle config change with StartRotationDialog popup, the StartRotationDialog |
| 749 | // still holds a reference to the destroyed Fragment and is calling |
| 750 | // onStartRotationDialogDismissed on that destroyed Fragment. |
Chihhang Chuang | 85f099a | 2020-06-16 18:04:39 +0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 754 | public void retryStartRotation(@NetworkPreference int networkPreference) { |
| 755 | startRotation(networkPreference); |
| 756 | } |
| 757 | |
| 758 | public void setCurrentWallpaperBottomSheetPresenter( |
| 759 | CurrentWallpaperBottomSheetPresenter presenter) { |
| 760 | mCurrentWallpaperBottomSheetPresenter = presenter; |
| 761 | } |
| 762 | |
| 763 | public void setWallpapersUiContainer(WallpapersUiContainer uiContainer) { |
| 764 | mWallpapersUiContainer = uiContainer; |
| 765 | } |
| 766 | |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 767 | public void setOnWallpaperSelectedListener( |
| 768 | WallpaperSelectedListener wallpaperSelectedListener) { |
| 769 | mWallpaperSelectedListener = wallpaperSelectedListener; |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Resizes the layout's height. |
| 774 | */ |
| 775 | public void resizeLayout(int height) { |
| 776 | mImageGrid.getLayoutParams().height = height; |
| 777 | mImageGrid.requestLayout(); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Scrolls to the specific item. |
| 782 | * |
| 783 | * @param position the position of the item |
| 784 | */ |
| 785 | public void scrollToPosition(int position) { |
| 786 | ((GridLayoutManager) mImageGrid.getLayoutManager()) |
| 787 | .scrollToPositionWithOffset(position, /* offset= */ 0); |
| 788 | } |
| 789 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 790 | /** |
| 791 | * Enable a test mode of operation -- in which certain UI features are disabled to allow for |
| 792 | * UI tests to run correctly. Works around issue in ProgressDialog currently where the dialog |
| 793 | * constantly keeps the UI thread alive and blocks a test forever. |
| 794 | * |
| 795 | * @param testingMode |
| 796 | */ |
| 797 | void setTestingMode(boolean testingMode) { |
| 798 | mTestingMode = testingMode; |
| 799 | } |
| 800 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 801 | @Override |
| 802 | public void startRotation(@NetworkPreference final int networkPreference) { |
| 803 | if (!isRotationEnabled()) { |
| 804 | Log.e(TAG, "Rotation is not enabled for this category " + mCategory.getTitle()); |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | // ProgressDialog endlessly updates the UI thread, keeping it from going idle which therefore |
| 809 | // causes Espresso to hang once the dialog is shown. |
| 810 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_MOBILE && !mTestingMode) { |
| 811 | int themeResId; |
| 812 | if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) { |
| 813 | themeResId = R.style.ProgressDialogThemePreL; |
| 814 | } else { |
| 815 | themeResId = R.style.LightDialogTheme; |
| 816 | } |
| 817 | mProgressDialog = new ProgressDialog(getActivity(), themeResId); |
| 818 | |
| 819 | mProgressDialog.setTitle(PROGRESS_DIALOG_NO_TITLE); |
| 820 | mProgressDialog.setMessage( |
| 821 | getResources().getString(R.string.start_rotation_progress_message)); |
| 822 | mProgressDialog.setIndeterminate(PROGRESS_DIALOG_INDETERMINATE); |
| 823 | mProgressDialog.show(); |
| 824 | } |
| 825 | |
| 826 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 827 | mAdapter.mPendingSelectedAdapterPosition = SPECIAL_FIXED_TILE_ADAPTER_POSITION; |
| 828 | } |
| 829 | |
| 830 | final Context appContext = getActivity().getApplicationContext(); |
| 831 | |
| 832 | mWallpaperRotationInitializer.setFirstWallpaperInRotation( |
| 833 | appContext, |
| 834 | networkPreference, |
| 835 | new Listener() { |
| 836 | @Override |
| 837 | public void onFirstWallpaperInRotationSet() { |
| 838 | if (mProgressDialog != null) { |
| 839 | mProgressDialog.dismiss(); |
| 840 | } |
| 841 | |
| 842 | // The fragment may be detached from its containing activity if the user exits the |
| 843 | // app before the first wallpaper image in rotation finishes downloading. |
| 844 | Activity activity = getActivity(); |
| 845 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 846 | |
Samuel Fufa | 03bc6ac | 2019-09-19 12:01:50 -0700 | [diff] [blame] | 847 | if (mWallpaperRotationInitializer.startRotation(appContext)) { |
| 848 | if (activity != null |
| 849 | && mFormFactor == FormFactorChecker.FORM_FACTOR_MOBILE) { |
| 850 | try { |
| 851 | Toast.makeText(getActivity(), |
| 852 | R.string.wallpaper_set_successfully_message, |
| 853 | Toast.LENGTH_SHORT).show(); |
| 854 | } catch (NotFoundException e) { |
| 855 | Log.e(TAG, "Could not show toast " + e); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 856 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 857 | |
Samuel Fufa | 03bc6ac | 2019-09-19 12:01:50 -0700 | [diff] [blame] | 858 | activity.setResult(Activity.RESULT_OK); |
| 859 | activity.finish(); |
| 860 | } else if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 861 | mAdapter.updateSelectedTile(SPECIAL_FIXED_TILE_ADAPTER_POSITION); |
| 862 | } |
| 863 | } else { // Failed to start rotation. |
| 864 | showStartRotationErrorDialog(networkPreference); |
| 865 | |
| 866 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 867 | DesktopRotationHolder rotationViewHolder = |
| 868 | (DesktopRotationHolder) |
| 869 | mImageGrid.findViewHolderForAdapterPosition( |
| 870 | SPECIAL_FIXED_TILE_ADAPTER_POSITION); |
| 871 | rotationViewHolder.setSelectionState( |
| 872 | SelectableHolder.SELECTION_STATE_DESELECTED); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | @Override |
| 878 | public void onError() { |
| 879 | if (mProgressDialog != null) { |
| 880 | mProgressDialog.dismiss(); |
| 881 | } |
| 882 | |
| 883 | showStartRotationErrorDialog(networkPreference); |
| 884 | |
| 885 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 886 | DesktopRotationHolder rotationViewHolder = |
| 887 | (DesktopRotationHolder) mImageGrid.findViewHolderForAdapterPosition( |
| 888 | SPECIAL_FIXED_TILE_ADAPTER_POSITION); |
| 889 | rotationViewHolder.setSelectionState(SelectableHolder.SELECTION_STATE_DESELECTED); |
| 890 | } |
| 891 | } |
| 892 | }); |
| 893 | } |
| 894 | |
| 895 | private void showStartRotationErrorDialog(@NetworkPreference int networkPreference) { |
Chuck Liao | b17d150 | 2020-09-07 21:55:30 +0800 | [diff] [blame] | 896 | FragmentTransactionChecker activity = (FragmentTransactionChecker) getActivity(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 897 | if (activity != null) { |
| 898 | StartRotationErrorDialogFragment startRotationErrorDialogFragment = |
| 899 | StartRotationErrorDialogFragment.newInstance(networkPreference); |
| 900 | startRotationErrorDialogFragment.setTargetFragment( |
| 901 | IndividualPickerFragment.this, UNUSED_REQUEST_CODE); |
| 902 | |
| 903 | if (activity.isSafeToCommitFragmentTransaction()) { |
| 904 | startRotationErrorDialogFragment.show( |
| 905 | getFragmentManager(), TAG_START_ROTATION_ERROR_DIALOG); |
| 906 | } else { |
| 907 | mStagedStartRotationErrorDialogFragment = startRotationErrorDialogFragment; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 912 | int getNumColumns() { |
Ching-Sung Li | 9df77a3 | 2019-07-10 11:45:30 +0800 | [diff] [blame] | 913 | Activity activity = getActivity(); |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 914 | if (activity == null) { |
| 915 | return 1; |
| 916 | } |
Chuck Liao | b10d2a3 | 2021-04-27 20:59:53 +0800 | [diff] [blame] | 917 | return isFewerColumnLayout() |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 918 | ? SizeCalculator.getNumFeaturedIndividualColumns(activity) |
| 919 | : SizeCalculator.getNumIndividualColumns(activity); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Returns whether rotation is enabled for this category. |
| 924 | */ |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 925 | boolean isRotationEnabled() { |
Samuel Fufa | 03bc6ac | 2019-09-19 12:01:50 -0700 | [diff] [blame] | 926 | return mWallpaperRotationInitializer != null; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | @Override |
| 930 | public void onCurrentWallpaperRefreshed() { |
| 931 | mCurrentWallpaperBottomSheetPresenter.setCurrentWallpapersExpanded(true); |
| 932 | } |
| 933 | |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 934 | @Override |
| 935 | public void onSet(int destination) { |
| 936 | if (mSelectedWallpaperInfo == null) { |
| 937 | Log.e(TAG, "Unable to set wallpaper since the selected wallpaper info is null"); |
| 938 | return; |
| 939 | } |
| 940 | |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 941 | mWallpaperPersister.setWallpaperInfoInPreview(mSelectedWallpaperInfo); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 942 | if (mSelectedWallpaperInfo instanceof LiveWallpaperInfo) { |
| 943 | mWallpaperSetter.setCurrentWallpaper(getActivity(), mSelectedWallpaperInfo, null, |
Santiago Etchebehere | c320c5f | 2021-05-14 17:35:57 -0700 | [diff] [blame^] | 944 | destination, 0, null, null, mSetWallpaperCallback); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 945 | } else { |
| 946 | mWallpaperSetter.setCurrentWallpaper( |
| 947 | getActivity(), mSelectedWallpaperInfo, destination, mSetWallpaperCallback); |
| 948 | } |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 949 | onWallpaperDestinationSet(destination); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | private WallpaperPersister.SetWallpaperCallback mSetWallpaperCallback = |
| 953 | new WallpaperPersister.SetWallpaperCallback() { |
| 954 | @Override |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 955 | public void onSuccess(WallpaperInfo wallpaperInfo) { |
chihhangchuang | 210c960 | 2020-06-02 12:38:14 +0800 | [diff] [blame] | 956 | mWallpaperPersister.onLiveWallpaperSet(); |
Chuck Liao | 6e52fff | 2020-04-23 16:11:23 +0800 | [diff] [blame] | 957 | Toast.makeText(getActivity(), R.string.wallpaper_set_successfully_message, |
| 958 | Toast.LENGTH_SHORT).show(); |
chihhangchuang | 210c960 | 2020-06-02 12:38:14 +0800 | [diff] [blame] | 959 | getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out); |
| 960 | getActivity().finish(); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | @Override |
| 964 | public void onError(@Nullable Throwable throwable) { |
| 965 | Log.e(TAG, "Can't apply the wallpaper."); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 966 | } |
| 967 | }; |
| 968 | |
| 969 | @Override |
Chuck Liao | 33f1af4 | 2021-04-13 20:52:35 +0800 | [diff] [blame] | 970 | public boolean onMenuItemClick(MenuItem item) { |
| 971 | if (item.getItemId() == R.id.daily_rotation) { |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 972 | showRotationDialog(); |
Chuck Liao | 33f1af4 | 2021-04-13 20:52:35 +0800 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | return super.onMenuItemClick(item); |
| 976 | } |
| 977 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 978 | /** |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 979 | * Popups a daily rotation dialog for the uses to confirm. |
| 980 | */ |
| 981 | public void showRotationDialog() { |
| 982 | DialogFragment startRotationDialogFragment = new StartRotationDialogFragment(); |
| 983 | startRotationDialogFragment.setTargetFragment( |
| 984 | IndividualPickerFragment.this, UNUSED_REQUEST_CODE); |
| 985 | startRotationDialogFragment.show(getFragmentManager(), TAG_START_ROTATION_DIALOG); |
| 986 | } |
| 987 | |
| 988 | /** |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 989 | * Shows a "set wallpaper" error dialog with a failure message and button to try again. |
| 990 | */ |
| 991 | private void showSetWallpaperErrorDialog() { |
| 992 | SetWallpaperErrorDialogFragment dialogFragment = SetWallpaperErrorDialogFragment.newInstance( |
| 993 | R.string.set_wallpaper_error_message, WallpaperPersister.DEST_BOTH); |
| 994 | dialogFragment.setTargetFragment(this, UNUSED_REQUEST_CODE); |
| 995 | |
| 996 | if (((BaseActivity) getActivity()).isSafeToCommitFragmentTransaction()) { |
| 997 | dialogFragment.show(getFragmentManager(), TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT); |
| 998 | } else { |
| 999 | mStagedSetWallpaperErrorDialogFragment = dialogFragment; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 1003 | private void updateThumbnail(WallpaperInfo selectedWallpaperInfo) { |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 1004 | ThumbnailUpdater thumbnailUpdater = (ThumbnailUpdater) getParentFragment(); |
Chuck Liao | 69630f1 | 2020-03-05 19:01:25 +0800 | [diff] [blame] | 1005 | if (thumbnailUpdater == null) { |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | if (selectedWallpaperInfo != null) { |
| 1010 | thumbnailUpdater.updateThumbnail(selectedWallpaperInfo); |
| 1011 | } else { |
| 1012 | thumbnailUpdater.restoreThumbnails(); |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 1016 | private void onWallpaperDestinationSet(int destination) { |
| 1017 | WallpaperDestinationCallback wallpaperDestinationCallback = |
| 1018 | (WallpaperDestinationCallback) getParentFragment(); |
| 1019 | if (wallpaperDestinationCallback == null) { |
| 1020 | return; |
| 1021 | } |
| 1022 | |
| 1023 | wallpaperDestinationCallback.onDestinationSet(destination); |
| 1024 | } |
| 1025 | |
Ching-Sung Li | 073812b | 2020-04-07 21:19:21 +0800 | [diff] [blame] | 1026 | void onWallpaperSelected(@Nullable WallpaperInfo newSelectedWallpaperInfo, |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 1027 | int position) { |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 1028 | if (mSelectedWallpaperInfo == newSelectedWallpaperInfo) { |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1029 | return; |
| 1030 | } |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 1031 | // Update current wallpaper. |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1032 | updateActivatedStatus(mSelectedWallpaperInfo == null |
| 1033 | ? mAppliedWallpaperInfo : mSelectedWallpaperInfo, false); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 1034 | // Update new selected wallpaper. |
| 1035 | updateActivatedStatus(newSelectedWallpaperInfo == null |
| 1036 | ? mAppliedWallpaperInfo : newSelectedWallpaperInfo, true); |
| 1037 | |
| 1038 | mSelectedWallpaperInfo = newSelectedWallpaperInfo; |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 1039 | updateThumbnail(mSelectedWallpaperInfo); |
chihhangchuang | de9e872 | 2020-05-02 17:22:13 +0800 | [diff] [blame] | 1040 | // Populate wallpaper info into view. |
chihhangchuang | 1a29e75 | 2020-04-28 18:22:53 +0800 | [diff] [blame] | 1041 | if (mSelectedWallpaperInfo != null && mWallpaperInfoView != null) { |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 1042 | WallpaperInfoHelper.loadExploreIntent( |
| 1043 | getContext(), |
| 1044 | mSelectedWallpaperInfo, |
| 1045 | (actionLabel, exploreIntent) -> |
| 1046 | mWallpaperInfoView.populateWallpaperInfo( |
Chuck Liao | 9991d68 | 2021-01-20 17:45:14 +0800 | [diff] [blame] | 1047 | mSelectedWallpaperInfo, |
| 1048 | actionLabel, |
| 1049 | WallpaperInfoHelper.shouldShowExploreButton( |
| 1050 | getContext(), exploreIntent), |
| 1051 | v -> onExploreClicked(exploreIntent)) |
| 1052 | ); |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | if (mWallpaperSelectedListener != null) { |
| 1056 | mWallpaperSelectedListener.onWallpaperSelected(position); |
chihhangchuang | 22aa0cc | 2020-03-25 19:12:42 +0800 | [diff] [blame] | 1057 | } |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 1058 | } |
| 1059 | |
chihhangchuang | c687d91 | 2020-05-04 14:33:05 +0800 | [diff] [blame] | 1060 | private void onExploreClicked(Intent exploreIntent) { |
| 1061 | if (getContext() == null) { |
| 1062 | return; |
| 1063 | } |
| 1064 | Context context = getContext(); |
| 1065 | mUserEventLogger.logActionClicked(mSelectedWallpaperInfo.getCollectionId(context), |
| 1066 | mSelectedWallpaperInfo.getActionLabelRes(context)); |
| 1067 | |
| 1068 | startActivity(exploreIntent); |
| 1069 | } |
| 1070 | |
Chuck Liao | 6a37a1c | 2020-03-07 03:39:43 +0800 | [diff] [blame] | 1071 | private void updateActivatedStatus(WallpaperInfo wallpaperInfo, boolean isActivated) { |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1072 | if (wallpaperInfo == null) { |
| 1073 | return; |
| 1074 | } |
Chuck Liao | 6a37a1c | 2020-03-07 03:39:43 +0800 | [diff] [blame] | 1075 | int index = mWallpapers.indexOf(wallpaperInfo); |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1076 | index = (shouldShowRotationTile() || mCategory.supportsCustomPhotos()) |
Chuck Liao | 6a37a1c | 2020-03-07 03:39:43 +0800 | [diff] [blame] | 1077 | ? index + 1 : index; |
| 1078 | ViewHolder holder = mImageGrid.findViewHolderForAdapterPosition(index); |
| 1079 | if (holder != null) { |
Tianguang Zhang | 6406edb | 2021-03-18 14:42:36 +0100 | [diff] [blame] | 1080 | CustomShapeImageView thumbnail = holder.itemView.findViewById(R.id.thumbnail); |
Tianguang Zhang | d913daf | 2021-03-03 20:21:26 +0100 | [diff] [blame] | 1081 | thumbnail.setClipped(isActivated); |
Chuck Liao | 6a37a1c | 2020-03-07 03:39:43 +0800 | [diff] [blame] | 1082 | } else { |
| 1083 | // Item is not visible, make sure the item is re-bound when it becomes visible. |
| 1084 | mAdapter.notifyItemChanged(index); |
| 1085 | } |
| 1086 | } |
| 1087 | |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1088 | private void updateAppliedStatus(WallpaperInfo wallpaperInfo, boolean isApplied) { |
| 1089 | if (wallpaperInfo == null) { |
| 1090 | return; |
| 1091 | } |
| 1092 | int index = mWallpapers.indexOf(wallpaperInfo); |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1093 | index = (shouldShowRotationTile() || mCategory.supportsCustomPhotos()) |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1094 | ? index + 1 : index; |
| 1095 | ViewHolder holder = mImageGrid.findViewHolderForAdapterPosition(index); |
Tianguang Zhang | d913daf | 2021-03-03 20:21:26 +0100 | [diff] [blame] | 1096 | if (holder == null) { |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1097 | // Item is not visible, make sure the item is re-bound when it becomes visible. |
| 1098 | mAdapter.notifyItemChanged(index); |
| 1099 | } |
| 1100 | } |
| 1101 | |
Chuck Liao | 11f4a76 | 2020-04-08 13:24:43 +0800 | [diff] [blame] | 1102 | private void refreshAppliedWallpaper() { |
| 1103 | // Clear the check mark and blue border(if it shows) of the old applied wallpaper. |
| 1104 | showCheckMarkAndBorderForAppliedWallpaper(false); |
| 1105 | |
| 1106 | // Update to the new applied wallpaper. |
| 1107 | String appliedWallpaperId = getAppliedWallpaperId(); |
| 1108 | Optional<WallpaperInfo> wallpaperInfoOptional = mWallpapers |
| 1109 | .stream() |
| 1110 | .filter(wallpaper -> wallpaper.getWallpaperId() != null) |
| 1111 | .filter(wallpaper -> wallpaper.getWallpaperId().equals(appliedWallpaperId)) |
| 1112 | .findFirst(); |
| 1113 | mAppliedWallpaperInfo = wallpaperInfoOptional.orElse(null); |
| 1114 | |
| 1115 | // Set the check mark and blue border(if user doesn't select) of the new applied wallpaper. |
| 1116 | showCheckMarkAndBorderForAppliedWallpaper(true); |
| 1117 | } |
| 1118 | |
| 1119 | private String getAppliedWallpaperId() { |
| 1120 | WallpaperPreferences prefs = |
| 1121 | InjectorProvider.getInjector().getPreferences(getContext()); |
| 1122 | android.app.WallpaperInfo wallpaperInfo = mWallpaperManager.getWallpaperInfo(); |
| 1123 | boolean isDestinationBoth = |
| 1124 | mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK) < 0; |
| 1125 | |
| 1126 | if (isDestinationBoth || mWallpaperDestination == WallpaperPersister.DEST_HOME_SCREEN) { |
| 1127 | return wallpaperInfo != null |
| 1128 | ? wallpaperInfo.getServiceName() : prefs.getHomeWallpaperRemoteId(); |
| 1129 | } else { |
| 1130 | return prefs.getLockWallpaperRemoteId(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Chuck Liao | ec053b0 | 2021-04-24 01:30:50 +0800 | [diff] [blame] | 1134 | private Set<String> getAppliedWallpaperIds() { |
| 1135 | WallpaperPreferences prefs = |
| 1136 | InjectorProvider.getInjector().getPreferences(getContext()); |
| 1137 | android.app.WallpaperInfo wallpaperInfo = mWallpaperManager.getWallpaperInfo(); |
| 1138 | Set<String> appliedWallpaperIds = new ArraySet<>(); |
| 1139 | |
| 1140 | String homeWallpaperId = wallpaperInfo != null ? wallpaperInfo.getServiceName() |
| 1141 | : prefs.getHomeWallpaperRemoteId(); |
| 1142 | if (!TextUtils.isEmpty(homeWallpaperId)) { |
| 1143 | appliedWallpaperIds.add(homeWallpaperId); |
| 1144 | } |
| 1145 | |
| 1146 | boolean isLockWallpaperApplied = |
| 1147 | mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK) >= 0; |
| 1148 | String lockWallpaperId = prefs.getLockWallpaperRemoteId(); |
| 1149 | if (isLockWallpaperApplied && !TextUtils.isEmpty(lockWallpaperId)) { |
| 1150 | appliedWallpaperIds.add(lockWallpaperId); |
| 1151 | } |
| 1152 | |
| 1153 | return appliedWallpaperIds; |
| 1154 | } |
| 1155 | |
Chuck Liao | 11f4a76 | 2020-04-08 13:24:43 +0800 | [diff] [blame] | 1156 | private void showCheckMarkAndBorderForAppliedWallpaper(boolean show) { |
| 1157 | updateAppliedStatus(mAppliedWallpaperInfo, show); |
| 1158 | if (mSelectedWallpaperInfo == null) { |
| 1159 | updateActivatedStatus(mAppliedWallpaperInfo, show); |
| 1160 | } |
| 1161 | } |
| 1162 | |
Ching-Sung Li | b69e993 | 2021-05-25 20:03:01 +0800 | [diff] [blame] | 1163 | boolean shouldShowRotationTile() { |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1164 | return mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP && isRotationEnabled(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Tianguang Zhang | d913daf | 2021-03-03 20:21:26 +0100 | [diff] [blame] | 1167 | class EmptySelectionAnimator implements SelectionAnimator{ |
| 1168 | EmptySelectionAnimator() {} |
| 1169 | |
| 1170 | public boolean isSelected() { |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | /** |
| 1175 | * Sets the UI to selected immediately with no animation. |
| 1176 | */ |
| 1177 | public void selectImmediately() {} |
| 1178 | |
| 1179 | /** |
| 1180 | * Sets the UI to deselected immediately with no animation. |
| 1181 | */ |
| 1182 | public void deselectImmediately() {} |
| 1183 | |
| 1184 | /** |
| 1185 | * Sets the UI to selected with a smooth animation. |
| 1186 | */ |
| 1187 | public void animateSelected() {} |
| 1188 | |
| 1189 | /** |
| 1190 | * Sets the UI to deselected with a smooth animation. |
| 1191 | */ |
| 1192 | public void animateDeselected() {} |
| 1193 | |
| 1194 | /** |
| 1195 | * Sets the UI to show a loading indicator. |
| 1196 | */ |
| 1197 | public void showLoading() {} |
| 1198 | |
| 1199 | /** |
| 1200 | * Sets the UI to hide the loading indicator. |
| 1201 | */ |
| 1202 | public void showNotLoading() {} |
| 1203 | |
| 1204 | } |
| 1205 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1206 | /** |
| 1207 | * RecyclerView Adapter subclass for the wallpaper tiles in the RecyclerView. |
| 1208 | */ |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 1209 | class IndividualAdapter extends RecyclerView.Adapter<ViewHolder> { |
| 1210 | static final int ITEM_VIEW_TYPE_ROTATION = 1; |
| 1211 | static final int ITEM_VIEW_TYPE_INDIVIDUAL_WALLPAPER = 2; |
| 1212 | static final int ITEM_VIEW_TYPE_MY_PHOTOS = 3; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1213 | |
| 1214 | private final List<WallpaperInfo> mWallpapers; |
| 1215 | |
| 1216 | private int mPendingSelectedAdapterPosition; |
| 1217 | private int mSelectedAdapterPosition; |
| 1218 | |
| 1219 | IndividualAdapter(List<WallpaperInfo> wallpapers) { |
| 1220 | mWallpapers = wallpapers; |
| 1221 | mPendingSelectedAdapterPosition = -1; |
| 1222 | mSelectedAdapterPosition = -1; |
| 1223 | } |
| 1224 | |
| 1225 | @Override |
| 1226 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 1227 | switch (viewType) { |
| 1228 | case ITEM_VIEW_TYPE_ROTATION: |
| 1229 | return createRotationHolder(parent); |
| 1230 | case ITEM_VIEW_TYPE_INDIVIDUAL_WALLPAPER: |
| 1231 | return createIndividualHolder(parent); |
| 1232 | case ITEM_VIEW_TYPE_MY_PHOTOS: |
| 1233 | return createMyPhotosHolder(parent); |
| 1234 | default: |
| 1235 | Log.e(TAG, "Unsupported viewType " + viewType + " in IndividualAdapter"); |
| 1236 | return null; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | @Override |
| 1241 | public int getItemViewType(int position) { |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1242 | if (shouldShowRotationTile() && position == SPECIAL_FIXED_TILE_ADAPTER_POSITION) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1243 | return ITEM_VIEW_TYPE_ROTATION; |
| 1244 | } |
| 1245 | |
| 1246 | // A category cannot have both a "start rotation" tile and a "my photos" tile. |
| 1247 | if (mCategory.supportsCustomPhotos() |
| 1248 | && !isRotationEnabled() |
| 1249 | && position == SPECIAL_FIXED_TILE_ADAPTER_POSITION) { |
| 1250 | return ITEM_VIEW_TYPE_MY_PHOTOS; |
| 1251 | } |
| 1252 | |
| 1253 | return ITEM_VIEW_TYPE_INDIVIDUAL_WALLPAPER; |
| 1254 | } |
| 1255 | |
| 1256 | @Override |
| 1257 | public void onBindViewHolder(ViewHolder holder, int position) { |
| 1258 | int viewType = getItemViewType(position); |
| 1259 | |
| 1260 | switch (viewType) { |
| 1261 | case ITEM_VIEW_TYPE_ROTATION: |
| 1262 | onBindRotationHolder(holder, position); |
| 1263 | break; |
| 1264 | case ITEM_VIEW_TYPE_INDIVIDUAL_WALLPAPER: |
| 1265 | onBindIndividualHolder(holder, position); |
| 1266 | break; |
| 1267 | case ITEM_VIEW_TYPE_MY_PHOTOS: |
| 1268 | ((MyPhotosViewHolder) holder).bind(); |
| 1269 | break; |
| 1270 | default: |
| 1271 | Log.e(TAG, "Unsupported viewType " + viewType + " in IndividualAdapter"); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | @Override |
| 1276 | public int getItemCount() { |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1277 | return (shouldShowRotationTile() || mCategory.supportsCustomPhotos()) |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1278 | ? mWallpapers.size() + 1 |
| 1279 | : mWallpapers.size(); |
| 1280 | } |
| 1281 | |
| 1282 | private ViewHolder createRotationHolder(ViewGroup parent) { |
| 1283 | LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1284 | View view = layoutInflater.inflate(R.layout.grid_item_rotation_desktop, parent, false); |
Tianguang Zhang | d913daf | 2021-03-03 20:21:26 +0100 | [diff] [blame] | 1285 | SelectionAnimator selectionAnimator = new EmptySelectionAnimator(); |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1286 | return new DesktopRotationHolder(getActivity(), mTileSizePx.y, view, selectionAnimator, |
| 1287 | IndividualPickerFragment.this); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | private ViewHolder createIndividualHolder(ViewGroup parent) { |
| 1291 | LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); |
| 1292 | View view = layoutInflater.inflate(R.layout.grid_item_image, parent, false); |
| 1293 | |
| 1294 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
Tianguang Zhang | d913daf | 2021-03-03 20:21:26 +0100 | [diff] [blame] | 1295 | SelectionAnimator selectionAnimator = new EmptySelectionAnimator(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1296 | return new SetIndividualHolder( |
| 1297 | getActivity(), mTileSizePx.y, view, |
| 1298 | selectionAnimator, |
| 1299 | new OnSetListener() { |
| 1300 | @Override |
| 1301 | public void onPendingWallpaperSet(int adapterPosition) { |
| 1302 | // Deselect and hide loading indicator for any previously pending tile. |
| 1303 | if (mPendingSelectedAdapterPosition != -1) { |
| 1304 | ViewHolder oldViewHolder = mImageGrid.findViewHolderForAdapterPosition( |
| 1305 | mPendingSelectedAdapterPosition); |
| 1306 | if (oldViewHolder instanceof SelectableHolder) { |
| 1307 | ((SelectableHolder) oldViewHolder).setSelectionState( |
| 1308 | SelectableHolder.SELECTION_STATE_DESELECTED); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | if (mSelectedAdapterPosition != -1) { |
| 1313 | ViewHolder oldViewHolder = mImageGrid.findViewHolderForAdapterPosition( |
| 1314 | mSelectedAdapterPosition); |
| 1315 | if (oldViewHolder instanceof SelectableHolder) { |
| 1316 | ((SelectableHolder) oldViewHolder).setSelectionState( |
| 1317 | SelectableHolder.SELECTION_STATE_DESELECTED); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | mPendingSelectedAdapterPosition = adapterPosition; |
| 1322 | } |
| 1323 | |
| 1324 | @Override |
| 1325 | public void onWallpaperSet(int adapterPosition) { |
| 1326 | // No-op -- UI handles a new wallpaper being set by reacting to the |
| 1327 | // WallpaperChangedNotifier. |
| 1328 | } |
| 1329 | |
| 1330 | @Override |
| 1331 | public void onWallpaperSetFailed(SetIndividualHolder holder) { |
| 1332 | showSetWallpaperErrorDialog(); |
| 1333 | mPendingSetIndividualHolder = holder; |
| 1334 | } |
| 1335 | }); |
| 1336 | } else { // MOBILE |
Chuck Liao | 3d1a51c | 2020-02-17 18:29:34 +0800 | [diff] [blame] | 1337 | return new PreviewIndividualHolder(getActivity(), mTileSizePx.y, view); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | private ViewHolder createMyPhotosHolder(ViewGroup parent) { |
| 1342 | LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); |
| 1343 | View view = layoutInflater.inflate(R.layout.grid_item_my_photos, parent, false); |
| 1344 | |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 1345 | return new MyPhotosViewHolder(getActivity(), |
| 1346 | ((MyPhotosStarterProvider) getActivity()).getMyPhotosStarter(), |
| 1347 | mTileSizePx.y, view); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /** |
| 1351 | * Marks the tile at the given position as selected with a visual indication. Also updates the |
| 1352 | * "currently selected" BottomSheet to reflect the newly selected tile. |
| 1353 | */ |
| 1354 | private void updateSelectedTile(int newlySelectedPosition) { |
| 1355 | // Prevent multiple spinners from appearing with a user tapping several tiles in rapid |
| 1356 | // succession. |
| 1357 | if (mPendingSelectedAdapterPosition == mSelectedAdapterPosition) { |
| 1358 | return; |
| 1359 | } |
| 1360 | |
| 1361 | if (mCurrentWallpaperBottomSheetPresenter != null) { |
| 1362 | mCurrentWallpaperBottomSheetPresenter.refreshCurrentWallpapers( |
| 1363 | IndividualPickerFragment.this); |
| 1364 | |
| 1365 | if (mCurrentWallpaperBottomSheetExpandedRunnable != null) { |
| 1366 | mHandler.removeCallbacks(mCurrentWallpaperBottomSheetExpandedRunnable); |
| 1367 | } |
| 1368 | mCurrentWallpaperBottomSheetExpandedRunnable = new Runnable() { |
| 1369 | @Override |
| 1370 | public void run() { |
| 1371 | mCurrentWallpaperBottomSheetPresenter.setCurrentWallpapersExpanded(true); |
| 1372 | } |
| 1373 | }; |
| 1374 | mHandler.postDelayed(mCurrentWallpaperBottomSheetExpandedRunnable, 100); |
| 1375 | } |
| 1376 | |
| 1377 | // User may have switched to another category, thus detaching this fragment, so check here. |
| 1378 | // NOTE: We do this check after updating the current wallpaper BottomSheet so that the update |
| 1379 | // still occurs in the UI after the user selects that other category. |
| 1380 | if (getActivity() == null) { |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | // Update the newly selected wallpaper ViewHolder and the old one so that if |
| 1385 | // selection UI state applies (desktop UI), it is updated. |
| 1386 | if (mSelectedAdapterPosition >= 0) { |
| 1387 | ViewHolder oldViewHolder = mImageGrid.findViewHolderForAdapterPosition( |
| 1388 | mSelectedAdapterPosition); |
| 1389 | if (oldViewHolder instanceof SelectableHolder) { |
| 1390 | ((SelectableHolder) oldViewHolder).setSelectionState( |
| 1391 | SelectableHolder.SELECTION_STATE_DESELECTED); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | // Animate selection of newly selected tile. |
| 1396 | ViewHolder newViewHolder = mImageGrid |
| 1397 | .findViewHolderForAdapterPosition(newlySelectedPosition); |
| 1398 | if (newViewHolder instanceof SelectableHolder) { |
| 1399 | ((SelectableHolder) newViewHolder).setSelectionState( |
| 1400 | SelectableHolder.SELECTION_STATE_SELECTED); |
| 1401 | } |
| 1402 | |
| 1403 | mSelectedAdapterPosition = newlySelectedPosition; |
| 1404 | |
| 1405 | // If the tile was in the last row of the grid, add space below it so the user can scroll down |
| 1406 | // and up to see the BottomSheet without it fully overlapping the newly selected tile. |
| 1407 | int spanCount = ((GridLayoutManager) mImageGrid.getLayoutManager()).getSpanCount(); |
| 1408 | int numRows = (int) Math.ceil((float) getItemCount() / spanCount); |
| 1409 | int rowOfNewlySelectedTile = newlySelectedPosition / spanCount; |
| 1410 | boolean isInLastRow = rowOfNewlySelectedTile == numRows - 1; |
| 1411 | |
| 1412 | updateImageGridPadding(isInLastRow /* addExtraBottomSpace */); |
| 1413 | } |
| 1414 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 1415 | void onBindRotationHolder(ViewHolder holder, int position) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1416 | if (mFormFactor == FormFactorChecker.FORM_FACTOR_DESKTOP) { |
| 1417 | String collectionId = mCategory.getCollectionId(); |
| 1418 | ((DesktopRotationHolder) holder).bind(collectionId); |
| 1419 | |
| 1420 | if (mWallpaperPreferences.getWallpaperPresentationMode() |
| 1421 | == WallpaperPreferences.PRESENTATION_MODE_ROTATING |
| 1422 | && collectionId.equals(mWallpaperPreferences.getHomeWallpaperCollectionId())) { |
| 1423 | mSelectedAdapterPosition = position; |
| 1424 | } |
| 1425 | |
| 1426 | if (!mWasUpdateRunnableRun && !mWallpapers.isEmpty()) { |
| 1427 | updateDesktopDailyRotationThumbnail((DesktopRotationHolder) holder); |
| 1428 | mWasUpdateRunnableRun = true; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 1433 | void onBindIndividualHolder(ViewHolder holder, int position) { |
Chuck Liao | 5a4243b | 2020-05-20 23:56:39 +0800 | [diff] [blame] | 1434 | int wallpaperIndex = (shouldShowRotationTile() || mCategory.supportsCustomPhotos()) |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1435 | ? position - 1 : position; |
| 1436 | WallpaperInfo wallpaper = mWallpapers.get(wallpaperIndex); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 1437 | wallpaper.computePlaceholderColor(holder.itemView.getContext()); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1438 | ((IndividualHolder) holder).bindWallpaper(wallpaper); |
Chuck Liao | ec053b0 | 2021-04-24 01:30:50 +0800 | [diff] [blame] | 1439 | boolean isWallpaperApplied = mAppliedWallpaperIds.contains(wallpaper.getWallpaperId()); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1440 | |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1441 | if (isWallpaperApplied) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1442 | mSelectedAdapterPosition = position; |
“Chuck | ffd832c | 2020-03-22 02:15:58 +0800 | [diff] [blame] | 1443 | mAppliedWallpaperInfo = wallpaper; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1444 | } |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 1445 | |
Ching-Sung Li | 1ac024f | 2021-06-08 14:43:28 +0800 | [diff] [blame] | 1446 | CardView container = holder.itemView.findViewById(R.id.wallpaper_container); |
| 1447 | int radiusId = isFewerColumnLayout() ? R.dimen.grid_item_all_radius |
| 1448 | : R.dimen.grid_item_all_radius_small; |
| 1449 | container.setRadius(getResources().getDimension(radiusId)); |
Tianguang Zhang | 6406edb | 2021-03-18 14:42:36 +0100 | [diff] [blame] | 1450 | CustomShapeImageView thumbnail = holder.itemView.findViewById(R.id.thumbnail); |
Tianguang Zhang | d913daf | 2021-03-03 20:21:26 +0100 | [diff] [blame] | 1451 | thumbnail.setClipped(isWallpaperApplied); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1452 | } |
| 1453 | } |
Chuck Liao | ba40123 | 2020-03-13 20:11:04 +0800 | [diff] [blame] | 1454 | |
| 1455 | private class GridPaddingDecoration extends RecyclerView.ItemDecoration { |
| 1456 | |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 1457 | private final int mPaddingHorizontal; |
| 1458 | private final int mPaddingBottom; |
Chuck Liao | ba40123 | 2020-03-13 20:11:04 +0800 | [diff] [blame] | 1459 | |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 1460 | GridPaddingDecoration(int paddingHorizontal, int paddingBottom) { |
| 1461 | mPaddingHorizontal = paddingHorizontal; |
| 1462 | mPaddingBottom = paddingBottom; |
Chuck Liao | ba40123 | 2020-03-13 20:11:04 +0800 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | @Override |
| 1466 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, |
| 1467 | RecyclerView.State state) { |
| 1468 | int position = parent.getChildAdapterPosition(view); |
| 1469 | if (position >= 0) { |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 1470 | outRect.left = mPaddingHorizontal; |
| 1471 | outRect.right = mPaddingHorizontal; |
| 1472 | outRect.bottom = mPaddingBottom; |
Chuck Liao | ba40123 | 2020-03-13 20:11:04 +0800 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1476 | } |