Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.wallpaper.picker; |
| 17 | |
Santiago Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame^] | 18 | import static com.android.wallpaper.picker.WallpaperPickerDelegate.PREVIEW_LIVE_WALLPAPER_REQUEST_CODE; |
| 19 | import static com.android.wallpaper.picker.WallpaperPickerDelegate.PREVIEW_WALLPAPER_REQUEST_CODE; |
| 20 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 21 | import android.app.Activity; |
Chuck Liao | f646e05 | 2020-09-24 12:24:59 +0800 | [diff] [blame] | 22 | import android.app.AlertDialog; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.graphics.Point; |
| 25 | import android.graphics.PorterDuff; |
| 26 | import android.graphics.Rect; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.Settings; |
| 30 | import android.util.DisplayMetrics; |
| 31 | import android.util.Log; |
| 32 | import android.view.LayoutInflater; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
| 35 | import android.widget.ImageView; |
| 36 | import android.widget.ProgressBar; |
| 37 | import android.widget.TextView; |
| 38 | |
| 39 | import androidx.annotation.NonNull; |
| 40 | import androidx.annotation.Nullable; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 41 | import androidx.cardview.widget.CardView; |
| 42 | import androidx.fragment.app.Fragment; |
| 43 | import androidx.recyclerview.widget.GridLayoutManager; |
| 44 | import androidx.recyclerview.widget.RecyclerView; |
| 45 | |
| 46 | import com.android.wallpaper.R; |
| 47 | import com.android.wallpaper.asset.Asset; |
| 48 | import com.android.wallpaper.model.Category; |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 49 | import com.android.wallpaper.model.CategoryProvider; |
Santiago Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame^] | 50 | import com.android.wallpaper.model.LiveWallpaperInfo; |
| 51 | import com.android.wallpaper.model.WallpaperInfo; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 52 | import com.android.wallpaper.module.InjectorProvider; |
| 53 | import com.android.wallpaper.module.UserEventLogger; |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 54 | import com.android.wallpaper.util.DeepLinkUtils; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 55 | import com.android.wallpaper.util.DisplayMetricsRetriever; |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 56 | import com.android.wallpaper.util.ResourceUtils; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 57 | import com.android.wallpaper.util.SizeCalculator; |
Wesley.CW Wang | dc68fde | 2020-06-15 19:12:33 +0800 | [diff] [blame] | 58 | import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate; |
| 59 | import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 60 | |
| 61 | import com.bumptech.glide.Glide; |
| 62 | |
| 63 | import java.util.ArrayList; |
| 64 | import java.util.List; |
| 65 | |
| 66 | /** |
| 67 | * Displays the UI which contains the categories of the wallpaper. |
| 68 | */ |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 69 | public class CategorySelectorFragment extends AppbarFragment { |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 70 | |
| 71 | // The number of ViewHolders that don't pertain to category tiles. |
| 72 | // Currently 2: one for the metadata section and one for the "Select wallpaper" header. |
| 73 | private static final int NUM_NON_CATEGORY_VIEW_HOLDERS = 0; |
| 74 | private static final int SETTINGS_APP_INFO_REQUEST_CODE = 1; |
| 75 | private static final String TAG = "CategorySelectorFragment"; |
| 76 | |
| 77 | /** |
| 78 | * Interface to be implemented by an Fragment hosting a {@link CategorySelectorFragment} |
| 79 | */ |
| 80 | public interface CategorySelectorFragmentHost { |
| 81 | |
| 82 | /** |
| 83 | * Requests to show the Android custom photo picker for the sake of picking a photo |
| 84 | * to set as the device's wallpaper. |
| 85 | */ |
| 86 | void requestCustomPhotoPicker(MyPhotosStarter.PermissionChangedListener listener); |
| 87 | |
| 88 | /** |
| 89 | * Shows the wallpaper page of the specific category. |
| 90 | * |
Chuck Liao | a63f1bf | 2020-06-11 01:35:42 +0800 | [diff] [blame] | 91 | * @param category the wallpaper's {@link Category} |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 92 | */ |
Chuck Liao | a63f1bf | 2020-06-11 01:35:42 +0800 | [diff] [blame] | 93 | void show(Category category); |
Chuck Liao | 0088bca | 2020-05-28 16:03:23 +0800 | [diff] [blame] | 94 | |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 95 | |
Chuck Liao | 0088bca | 2020-05-28 16:03:23 +0800 | [diff] [blame] | 96 | /** |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 97 | * Indicates if the host has toolbar to show the title. If it does, we should set the title |
| 98 | * there. |
| 99 | */ |
| 100 | boolean isHostToolbarShown(); |
| 101 | |
| 102 | /** |
| 103 | * Sets the title in the host's toolbar. |
Chuck Liao | 0088bca | 2020-05-28 16:03:23 +0800 | [diff] [blame] | 104 | */ |
| 105 | void setToolbarTitle(CharSequence title); |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Fetches the wallpaper categories. |
| 109 | */ |
| 110 | void fetchCategories(); |
Chuck Liao | 81145b2 | 2020-09-03 09:52:25 +0800 | [diff] [blame] | 111 | |
| 112 | /** |
Chuck Liao | 3abf15b | 2020-12-17 22:33:02 +0800 | [diff] [blame] | 113 | * Cleans up the listeners which will be notified when there's a package event. |
| 114 | */ |
| 115 | void cleanUp(); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 118 | private final CategoryProvider mCategoryProvider; |
| 119 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 120 | private RecyclerView mImageGrid; |
| 121 | private CategoryAdapter mAdapter; |
| 122 | private ArrayList<Category> mCategories = new ArrayList<>(); |
| 123 | private Point mTileSizePx; |
| 124 | private boolean mAwaitingCategories; |
| 125 | |
| 126 | public CategorySelectorFragment() { |
| 127 | mAdapter = new CategoryAdapter(mCategories); |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 128 | mCategoryProvider = InjectorProvider.getInjector().getCategoryProvider(getContext()); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Nullable |
| 132 | @Override |
| 133 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |
| 134 | @Nullable Bundle savedInstanceState) { |
| 135 | View view = inflater.inflate(R.layout.fragment_category_selector, container, |
| 136 | /* attachToRoot= */ false); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 137 | mImageGrid = view.findViewById(R.id.category_grid); |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 138 | mImageGrid.addItemDecoration(new GridPaddingDecoration(getResources().getDimensionPixelSize( |
| 139 | R.dimen.grid_item_category_padding_horizontal))); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 140 | |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 141 | mTileSizePx = SizeCalculator.getCategoryTileSize(getActivity()); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 142 | |
| 143 | mImageGrid.setAdapter(mAdapter); |
| 144 | |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 145 | GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), |
| 146 | getNumColumns() * CategorySpanSizeLookup.DEFAULT_CATEGORY_SPAN_SIZE); |
| 147 | gridLayoutManager.setSpanSizeLookup(new CategorySpanSizeLookup(mAdapter)); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 148 | mImageGrid.setLayoutManager(gridLayoutManager); |
Wesley.CW Wang | 27ff426 | 2020-06-12 19:19:57 +0800 | [diff] [blame] | 149 | mImageGrid.setAccessibilityDelegateCompat( |
Wesley.CW Wang | dc68fde | 2020-06-15 19:12:33 +0800 | [diff] [blame] | 150 | new WallpaperPickerRecyclerViewAccessibilityDelegate( |
| 151 | mImageGrid, (BottomSheetHost) getParentFragment(), getNumColumns())); |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 152 | |
| 153 | if (getCategorySelectorFragmentHost().isHostToolbarShown()) { |
| 154 | view.findViewById(R.id.header_bar).setVisibility(View.GONE); |
| 155 | getCategorySelectorFragmentHost().setToolbarTitle(getText(R.string.wallpaper_title)); |
| 156 | } else { |
| 157 | setUpToolbar(view); |
| 158 | setTitle(getText(R.string.wallpaper_title)); |
| 159 | } |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 160 | |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 161 | if (!DeepLinkUtils.isDeepLink(getActivity().getIntent())) { |
| 162 | getCategorySelectorFragmentHost().fetchCategories(); |
| 163 | } |
| 164 | |
Chihhang Chuang | e6c7322 | 2021-04-14 22:36:41 +0800 | [diff] [blame] | 165 | // For nav bar edge-to-edge effect. |
Kunhung Li | 1b0cb47 | 2021-04-26 20:52:04 +0800 | [diff] [blame] | 166 | view.setOnApplyWindowInsetsListener((v, windowInsets) -> { |
| 167 | // For status bar height. |
Chihhang Chuang | e6c7322 | 2021-04-14 22:36:41 +0800 | [diff] [blame] | 168 | v.setPadding( |
| 169 | v.getPaddingLeft(), |
Kunhung Li | 1b0cb47 | 2021-04-26 20:52:04 +0800 | [diff] [blame] | 170 | windowInsets.getSystemWindowInsetTop(), |
Chihhang Chuang | e6c7322 | 2021-04-14 22:36:41 +0800 | [diff] [blame] | 171 | v.getPaddingRight(), |
Kunhung Li | 1b0cb47 | 2021-04-26 20:52:04 +0800 | [diff] [blame] | 172 | v.getPaddingBottom()); |
| 173 | |
| 174 | View gridView = v.findViewById(R.id.category_grid); |
| 175 | gridView.setPadding( |
| 176 | gridView.getPaddingLeft(), |
| 177 | gridView.getPaddingTop(), |
| 178 | gridView.getPaddingRight(), |
Chihhang Chuang | e6c7322 | 2021-04-14 22:36:41 +0800 | [diff] [blame] | 179 | windowInsets.getSystemWindowInsetBottom()); |
| 180 | return windowInsets.consumeSystemWindowInsets(); |
| 181 | }); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 182 | return view; |
| 183 | } |
| 184 | |
Chuck Liao | 3abf15b | 2020-12-17 22:33:02 +0800 | [diff] [blame] | 185 | @Override |
| 186 | public void onDestroyView() { |
| 187 | getCategorySelectorFragmentHost().cleanUp(); |
| 188 | super.onDestroyView(); |
| 189 | } |
| 190 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 191 | /** |
| 192 | * Inserts the given category into the categories list in priority order. |
| 193 | */ |
| 194 | void addCategory(Category category, boolean loading) { |
| 195 | // If not previously waiting for categories, enter the waiting state by showing the loading |
| 196 | // indicator. |
| 197 | if (loading && !mAwaitingCategories) { |
| 198 | mAdapter.notifyItemChanged(getNumColumns()); |
| 199 | mAdapter.notifyItemInserted(getNumColumns()); |
| 200 | mAwaitingCategories = true; |
| 201 | } |
| 202 | // Not add existing category to category list |
| 203 | if (mCategories.indexOf(category) >= 0) { |
| 204 | updateCategory(category); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | int priority = category.getPriority(); |
| 209 | |
| 210 | int index = 0; |
| 211 | while (index < mCategories.size() && priority >= mCategories.get(index).getPriority()) { |
| 212 | index++; |
| 213 | } |
| 214 | |
| 215 | mCategories.add(index, category); |
| 216 | if (mAdapter != null) { |
| 217 | // Offset the index because of the static metadata element at beginning of RecyclerView. |
| 218 | mAdapter.notifyItemInserted(index + NUM_NON_CATEGORY_VIEW_HOLDERS); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void removeCategory(Category category) { |
| 223 | int index = mCategories.indexOf(category); |
| 224 | if (index >= 0) { |
| 225 | mCategories.remove(index); |
| 226 | mAdapter.notifyItemRemoved(index + NUM_NON_CATEGORY_VIEW_HOLDERS); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void updateCategory(Category category) { |
| 231 | int index = mCategories.indexOf(category); |
| 232 | if (index >= 0) { |
| 233 | mCategories.remove(index); |
| 234 | mCategories.add(index, category); |
| 235 | mAdapter.notifyItemChanged(index + NUM_NON_CATEGORY_VIEW_HOLDERS); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void clearCategories() { |
| 240 | mCategories.clear(); |
| 241 | mAdapter.notifyDataSetChanged(); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Notifies the CategoryFragment that no further categories are expected so it may hide |
| 246 | * the loading indicator. |
| 247 | */ |
| 248 | void doneFetchingCategories() { |
| 249 | if (mAwaitingCategories) { |
| 250 | mAdapter.notifyItemRemoved(mAdapter.getItemCount() - 1); |
| 251 | mAwaitingCategories = false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void notifyDataSetChanged() { |
| 256 | mAdapter.notifyDataSetChanged(); |
| 257 | } |
| 258 | |
| 259 | private int getNumColumns() { |
| 260 | Activity activity = getActivity(); |
Chuck Liao | e2fe030 | 2020-06-29 21:15:35 +0800 | [diff] [blame] | 261 | return activity == null ? 1 : SizeCalculator.getNumCategoryColumns(activity); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
| 265 | private CategorySelectorFragmentHost getCategorySelectorFragmentHost() { |
Chuck Liao | 8cbe49f | 2021-03-15 20:28:04 +0800 | [diff] [blame] | 266 | Fragment parentFragment = getParentFragment(); |
| 267 | if (parentFragment != null) { |
| 268 | return (CategorySelectorFragmentHost) parentFragment; |
| 269 | } else { |
| 270 | return (CategorySelectorFragmentHost) getActivity(); |
| 271 | } |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * ViewHolder subclass for a category tile in the RecyclerView. |
| 276 | */ |
| 277 | private class CategoryHolder extends RecyclerView.ViewHolder implements View.OnClickListener { |
| 278 | private Category mCategory; |
| 279 | private ImageView mImageView; |
| 280 | private ImageView mOverlayIconView; |
| 281 | private TextView mTitleView; |
| 282 | |
| 283 | CategoryHolder(View itemView) { |
| 284 | super(itemView); |
| 285 | itemView.setOnClickListener(this); |
| 286 | |
| 287 | mImageView = itemView.findViewById(R.id.image); |
| 288 | mOverlayIconView = itemView.findViewById(R.id.overlay_icon); |
| 289 | mTitleView = itemView.findViewById(R.id.category_title); |
| 290 | |
| 291 | CardView categoryView = itemView.findViewById(R.id.category); |
| 292 | categoryView.getLayoutParams().height = mTileSizePx.y; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void onClick(View view) { |
Santiago Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame^] | 297 | Activity activity = getActivity(); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 298 | final UserEventLogger eventLogger = |
Santiago Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame^] | 299 | InjectorProvider.getInjector().getUserEventLogger(activity); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 300 | eventLogger.logCategorySelected(mCategory.getCollectionId()); |
| 301 | |
| 302 | if (mCategory.supportsCustomPhotos()) { |
| 303 | getCategorySelectorFragmentHost().requestCustomPhotoPicker( |
| 304 | new MyPhotosStarter.PermissionChangedListener() { |
| 305 | @Override |
| 306 | public void onPermissionsGranted() { |
| 307 | drawThumbnailAndOverlayIcon(); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void onPermissionsDenied(boolean dontAskAgain) { |
| 312 | // No-op |
| 313 | } |
| 314 | }); |
| 315 | return; |
| 316 | } |
| 317 | |
Santiago Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame^] | 318 | if (mCategory.isSingleWallpaperCategory()) { |
| 319 | WallpaperInfo wallpaper = mCategory.getSingleWallpaper(); |
| 320 | // Log click on individual wallpaper |
| 321 | eventLogger.logIndividualWallpaperSelected(mCategory.getCollectionId()); |
| 322 | |
| 323 | InjectorProvider.getInjector().getWallpaperPersister(activity) |
| 324 | .setWallpaperInfoInPreview(wallpaper); |
| 325 | wallpaper.showPreview(activity, |
| 326 | new PreviewActivity.PreviewActivityIntentFactory(), |
| 327 | wallpaper instanceof LiveWallpaperInfo ? PREVIEW_LIVE_WALLPAPER_REQUEST_CODE |
| 328 | : PREVIEW_WALLPAPER_REQUEST_CODE); |
| 329 | return; |
| 330 | } |
| 331 | |
Chuck Liao | a63f1bf | 2020-06-11 01:35:42 +0800 | [diff] [blame] | 332 | getCategorySelectorFragmentHost().show(mCategory); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Binds the given category to this CategoryHolder. |
| 337 | */ |
| 338 | private void bindCategory(Category category) { |
| 339 | mCategory = category; |
| 340 | mTitleView.setText(category.getTitle()); |
| 341 | drawThumbnailAndOverlayIcon(); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Draws the CategoryHolder's thumbnail and overlay icon. |
| 346 | */ |
| 347 | private void drawThumbnailAndOverlayIcon() { |
| 348 | mOverlayIconView.setImageDrawable(mCategory.getOverlayIcon( |
| 349 | getActivity().getApplicationContext())); |
| 350 | |
| 351 | // Size the overlay icon according to the category. |
| 352 | int overlayIconDimenDp = mCategory.getOverlayIconSizeDp(); |
| 353 | DisplayMetrics metrics = DisplayMetricsRetriever.getInstance().getDisplayMetrics( |
| 354 | getResources(), getActivity().getWindowManager().getDefaultDisplay()); |
| 355 | int overlayIconDimenPx = (int) (overlayIconDimenDp * metrics.density); |
| 356 | mOverlayIconView.getLayoutParams().width = overlayIconDimenPx; |
| 357 | mOverlayIconView.getLayoutParams().height = overlayIconDimenPx; |
| 358 | |
| 359 | Asset thumbnail = mCategory.getThumbnail(getActivity().getApplicationContext()); |
| 360 | if (thumbnail != null) { |
| 361 | thumbnail.loadDrawable(getActivity(), mImageView, |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 362 | ResourceUtils.getColorAttr( |
| 363 | getActivity(), |
| 364 | android.R.attr.colorSecondary |
| 365 | )); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 366 | } else { |
| 367 | // TODO(orenb): Replace this workaround for b/62584914 with a proper way of |
| 368 | // unloading the ImageView such that no incorrect image is improperly loaded upon |
| 369 | // rapid scroll. |
| 370 | Object nullObj = null; |
| 371 | Glide.with(getActivity()) |
| 372 | .asDrawable() |
| 373 | .load(nullObj) |
| 374 | .into(mImageView); |
| 375 | |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 380 | private class FeaturedCategoryHolder extends CategoryHolder { |
| 381 | |
| 382 | FeaturedCategoryHolder(View itemView) { |
| 383 | super(itemView); |
| 384 | CardView categoryView = itemView.findViewById(R.id.category); |
| 385 | categoryView.getLayoutParams().height = |
| 386 | SizeCalculator.getFeaturedCategoryTileSize(getActivity()).y; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private class MyPhotosCategoryHolder extends CategoryHolder { |
| 391 | |
| 392 | MyPhotosCategoryHolder(View itemView) { |
| 393 | super(itemView); |
| 394 | // Reuse the height of featured category since My Photos category & featured category |
| 395 | // have the same height in current UI design. |
| 396 | CardView categoryView = itemView.findViewById(R.id.category); |
Tianguang Zhang | bae5764 | 2021-04-19 19:46:33 +0200 | [diff] [blame] | 397 | int height = SizeCalculator.getFeaturedCategoryTileSize(getActivity()).y; |
| 398 | categoryView.getLayoutParams().height = height; |
| 399 | // Use the height as the card corner radius for the "My photos" category |
| 400 | // for a stadium border. |
| 401 | categoryView.setRadius(height); |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 405 | /** |
| 406 | * ViewHolder subclass for the loading indicator ("spinner") shown when categories are being |
| 407 | * fetched. |
| 408 | */ |
| 409 | private class LoadingIndicatorHolder extends RecyclerView.ViewHolder { |
| 410 | private LoadingIndicatorHolder(View view) { |
| 411 | super(view); |
| 412 | ProgressBar progressBar = view.findViewById(R.id.loading_indicator); |
| 413 | progressBar.getIndeterminateDrawable().setColorFilter( |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 414 | ResourceUtils.getColorAttr( |
| 415 | getActivity(), |
| 416 | android.R.attr.colorAccent |
| 417 | ), PorterDuff.Mode.SRC_IN); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * RecyclerView Adapter subclass for the category tiles in the RecyclerView. |
| 423 | */ |
| 424 | private class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> |
| 425 | implements MyPhotosStarter.PermissionChangedListener { |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 426 | private static final int ITEM_VIEW_TYPE_MY_PHOTOS = 1; |
| 427 | private static final int ITEM_VIEW_TYPE_FEATURED_CATEGORY = 2; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 428 | private static final int ITEM_VIEW_TYPE_CATEGORY = 3; |
| 429 | private static final int ITEM_VIEW_TYPE_LOADING_INDICATOR = 4; |
| 430 | private List<Category> mCategories; |
| 431 | |
| 432 | private CategoryAdapter(List<Category> categories) { |
| 433 | mCategories = categories; |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public int getItemViewType(int position) { |
| 438 | if (mAwaitingCategories && position == getItemCount() - 1) { |
| 439 | return ITEM_VIEW_TYPE_LOADING_INDICATOR; |
| 440 | } |
| 441 | |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 442 | if (position == 0) { |
| 443 | return ITEM_VIEW_TYPE_MY_PHOTOS; |
| 444 | } |
| 445 | |
| 446 | if (mCategoryProvider.isFeaturedCategory(mCategories.get(position))) { |
| 447 | return ITEM_VIEW_TYPE_FEATURED_CATEGORY; |
| 448 | } |
| 449 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 450 | return ITEM_VIEW_TYPE_CATEGORY; |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 455 | LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); |
| 456 | View view; |
| 457 | |
| 458 | switch (viewType) { |
| 459 | case ITEM_VIEW_TYPE_LOADING_INDICATOR: |
| 460 | view = layoutInflater.inflate(R.layout.grid_item_loading_indicator, |
| 461 | parent, /* attachToRoot= */ false); |
| 462 | return new LoadingIndicatorHolder(view); |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 463 | case ITEM_VIEW_TYPE_MY_PHOTOS: |
| 464 | view = layoutInflater.inflate(R.layout.grid_item_category, |
| 465 | parent, /* attachToRoot= */ false); |
| 466 | return new MyPhotosCategoryHolder(view); |
| 467 | case ITEM_VIEW_TYPE_FEATURED_CATEGORY: |
| 468 | view = layoutInflater.inflate(R.layout.grid_item_category, |
| 469 | parent, /* attachToRoot= */ false); |
| 470 | return new FeaturedCategoryHolder(view); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 471 | case ITEM_VIEW_TYPE_CATEGORY: |
| 472 | view = layoutInflater.inflate(R.layout.grid_item_category, |
| 473 | parent, /* attachToRoot= */ false); |
| 474 | return new CategoryHolder(view); |
| 475 | default: |
| 476 | Log.e(TAG, "Unsupported viewType " + viewType + " in CategoryAdapter"); |
| 477 | return null; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 483 | int viewType = getItemViewType(position); |
| 484 | |
| 485 | switch (viewType) { |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 486 | case ITEM_VIEW_TYPE_MY_PHOTOS: |
| 487 | case ITEM_VIEW_TYPE_FEATURED_CATEGORY: |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 488 | case ITEM_VIEW_TYPE_CATEGORY: |
| 489 | // Offset position to get category index to account for the non-category view |
| 490 | // holders. |
| 491 | Category category = mCategories.get(position - NUM_NON_CATEGORY_VIEW_HOLDERS); |
| 492 | ((CategoryHolder) holder).bindCategory(category); |
| 493 | break; |
| 494 | case ITEM_VIEW_TYPE_LOADING_INDICATOR: |
| 495 | // No op. |
| 496 | break; |
| 497 | default: |
| 498 | Log.e(TAG, "Unsupported viewType " + viewType + " in CategoryAdapter"); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public int getItemCount() { |
| 504 | // Add to size of categories to account for the metadata related views. |
| 505 | // Add 1 more for the loading indicator if not yet done loading. |
| 506 | int size = mCategories.size() + NUM_NON_CATEGORY_VIEW_HOLDERS; |
| 507 | if (mAwaitingCategories) { |
| 508 | size += 1; |
| 509 | } |
| 510 | |
| 511 | return size; |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void onPermissionsGranted() { |
| 516 | notifyDataSetChanged(); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void onPermissionsDenied(boolean dontAskAgain) { |
| 521 | if (!dontAskAgain) { |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | String permissionNeededMessage = |
| 526 | getString(R.string.permission_needed_explanation_go_to_settings); |
| 527 | AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme) |
| 528 | .setMessage(permissionNeededMessage) |
| 529 | .setPositiveButton(android.R.string.ok, null /* onClickListener */) |
| 530 | .setNegativeButton( |
| 531 | R.string.settings_button_label, |
| 532 | (dialogInterface, i) -> { |
| 533 | Intent appInfoIntent = |
| 534 | new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 535 | Uri uri = Uri.fromParts("package", |
| 536 | getActivity().getPackageName(), /* fragment= */ null); |
| 537 | appInfoIntent.setData(uri); |
| 538 | startActivityForResult( |
| 539 | appInfoIntent, SETTINGS_APP_INFO_REQUEST_CODE); |
| 540 | }) |
| 541 | .create(); |
| 542 | dialog.show(); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | private class GridPaddingDecoration extends RecyclerView.ItemDecoration { |
| 547 | |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 548 | private final int mPadding; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 549 | |
| 550 | GridPaddingDecoration(int padding) { |
| 551 | mPadding = padding; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, |
| 556 | RecyclerView.State state) { |
| 557 | int position = parent.getChildAdapterPosition(view) - NUM_NON_CATEGORY_VIEW_HOLDERS; |
| 558 | if (position >= 0) { |
| 559 | outRect.left = mPadding; |
| 560 | outRect.right = mPadding; |
| 561 | } |
Chuck Liao | 1e6cb68 | 2021-04-22 00:51:11 +0800 | [diff] [blame] | 562 | |
| 563 | RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(view); |
| 564 | if (viewHolder instanceof MyPhotosCategoryHolder |
| 565 | || viewHolder instanceof FeaturedCategoryHolder) { |
| 566 | outRect.bottom = getResources().getDimensionPixelSize( |
| 567 | R.dimen.grid_item_featured_category_padding_bottom); |
| 568 | } else { |
| 569 | outRect.bottom = getResources().getDimensionPixelSize( |
| 570 | R.dimen.grid_item_category_padding_bottom); |
| 571 | } |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * SpanSizeLookup subclass which provides that the item in the first position spans the number |
| 577 | * of columns in the RecyclerView and all other items only take up a single span. |
| 578 | */ |
| 579 | private class CategorySpanSizeLookup extends GridLayoutManager.SpanSizeLookup { |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 580 | private static final int DEFAULT_CATEGORY_SPAN_SIZE = 2; |
| 581 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 582 | CategoryAdapter mAdapter; |
| 583 | |
| 584 | private CategorySpanSizeLookup(CategoryAdapter adapter) { |
| 585 | mAdapter = adapter; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public int getSpanSize(int position) { |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 590 | if (position < NUM_NON_CATEGORY_VIEW_HOLDERS || mAdapter.getItemViewType(position) |
| 591 | == CategoryAdapter.ITEM_VIEW_TYPE_LOADING_INDICATOR || mAdapter.getItemViewType( |
| 592 | position) == CategoryAdapter.ITEM_VIEW_TYPE_MY_PHOTOS) { |
| 593 | return getNumColumns() * DEFAULT_CATEGORY_SPAN_SIZE; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Chuck Liao | ddf2b52 | 2021-04-15 00:36:25 +0800 | [diff] [blame] | 596 | if (mAdapter.getItemViewType(position) |
| 597 | == CategoryAdapter.ITEM_VIEW_TYPE_FEATURED_CATEGORY) { |
| 598 | return getNumColumns() * DEFAULT_CATEGORY_SPAN_SIZE / 2; |
| 599 | } |
| 600 | |
| 601 | return DEFAULT_CATEGORY_SPAN_SIZE; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 602 | } |
| 603 | } |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 604 | } |