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