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