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