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