blob: a1a6fcb6d8a1e796183717d67e0aec53afb941e2 [file] [log] [blame]
Chuck Liao1e0501f2020-02-17 18:20:54 +08001/*
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 */
16package com.android.wallpaper.picker;
17
18import android.app.Activity;
Chuck Liaof646e052020-09-24 12:24:59 +080019import android.app.AlertDialog;
Chuck Liao1e0501f2020-02-17 18:20:54 +080020import android.content.Intent;
21import android.graphics.Point;
22import android.graphics.PorterDuff;
23import android.graphics.Rect;
24import android.net.Uri;
25import android.os.Bundle;
26import android.provider.Settings;
27import android.util.DisplayMetrics;
28import android.util.Log;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ImageView;
33import android.widget.ProgressBar;
34import android.widget.TextView;
35
36import androidx.annotation.NonNull;
37import androidx.annotation.Nullable;
Chuck Liao1e0501f2020-02-17 18:20:54 +080038import androidx.cardview.widget.CardView;
39import androidx.fragment.app.Fragment;
40import androidx.recyclerview.widget.GridLayoutManager;
41import androidx.recyclerview.widget.RecyclerView;
42
43import com.android.wallpaper.R;
44import com.android.wallpaper.asset.Asset;
45import com.android.wallpaper.model.Category;
46import com.android.wallpaper.module.InjectorProvider;
47import com.android.wallpaper.module.UserEventLogger;
Chuck Liaof6b4b192020-08-07 02:31:32 +080048import com.android.wallpaper.util.DeepLinkUtils;
Chuck Liao1e0501f2020-02-17 18:20:54 +080049import com.android.wallpaper.util.DisplayMetricsRetriever;
Santiago Etchebehere53c63432020-05-07 18:55:35 -070050import com.android.wallpaper.util.SizeCalculator;
Wesley.CW Wangdc68fde2020-06-15 19:12:33 +080051import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate;
52import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost;
Chuck Liao1e0501f2020-02-17 18:20:54 +080053
54import com.bumptech.glide.Glide;
55
56import java.util.ArrayList;
57import java.util.List;
58
59/**
60 * Displays the UI which contains the categories of the wallpaper.
61 */
Chuck Liao58aca1c2021-03-17 01:20:55 +080062public class CategorySelectorFragment extends AppbarFragment {
Chuck Liao1e0501f2020-02-17 18:20:54 +080063
64 // The number of ViewHolders that don't pertain to category tiles.
65 // Currently 2: one for the metadata section and one for the "Select wallpaper" header.
66 private static final int NUM_NON_CATEGORY_VIEW_HOLDERS = 0;
67 private static final int SETTINGS_APP_INFO_REQUEST_CODE = 1;
68 private static final String TAG = "CategorySelectorFragment";
69
70 /**
71 * Interface to be implemented by an Fragment hosting a {@link CategorySelectorFragment}
72 */
73 public interface CategorySelectorFragmentHost {
74
75 /**
76 * Requests to show the Android custom photo picker for the sake of picking a photo
77 * to set as the device's wallpaper.
78 */
79 void requestCustomPhotoPicker(MyPhotosStarter.PermissionChangedListener listener);
80
81 /**
82 * Shows the wallpaper page of the specific category.
83 *
Chuck Liaoa63f1bf2020-06-11 01:35:42 +080084 * @param category the wallpaper's {@link Category}
Chuck Liao1e0501f2020-02-17 18:20:54 +080085 */
Chuck Liaoa63f1bf2020-06-11 01:35:42 +080086 void show(Category category);
Chuck Liao0088bca2020-05-28 16:03:23 +080087
Chuck Liao58aca1c2021-03-17 01:20:55 +080088
Chuck Liao0088bca2020-05-28 16:03:23 +080089 /**
Chuck Liao58aca1c2021-03-17 01:20:55 +080090 * Indicates if the host has toolbar to show the title. If it does, we should set the title
91 * there.
92 */
93 boolean isHostToolbarShown();
94
95 /**
96 * Sets the title in the host's toolbar.
Chuck Liao0088bca2020-05-28 16:03:23 +080097 */
98 void setToolbarTitle(CharSequence title);
Chuck Liaof6b4b192020-08-07 02:31:32 +080099
100 /**
101 * Fetches the wallpaper categories.
102 */
103 void fetchCategories();
Chuck Liao81145b22020-09-03 09:52:25 +0800104
105 /**
Chuck Liao3abf15b2020-12-17 22:33:02 +0800106 * Cleans up the listeners which will be notified when there's a package event.
107 */
108 void cleanUp();
109
110 /**
Chuck Liao81145b22020-09-03 09:52:25 +0800111 * Hides the {@link com.android.wallpaper.widget.BottomActionBar}.
112 */
113 void hideBottomActionBar();
Chuck Liao1e0501f2020-02-17 18:20:54 +0800114 }
115
116 private RecyclerView mImageGrid;
117 private CategoryAdapter mAdapter;
118 private ArrayList<Category> mCategories = new ArrayList<>();
119 private Point mTileSizePx;
120 private boolean mAwaitingCategories;
121
122 public CategorySelectorFragment() {
123 mAdapter = new CategoryAdapter(mCategories);
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 Liao1e0501f2020-02-17 18:20:54 +0800132 mImageGrid = view.findViewById(R.id.category_grid);
133 mImageGrid.addItemDecoration(new GridPaddingDecoration(
134 getResources().getDimensionPixelSize(R.dimen.grid_padding)));
135
Santiago Etchebehere53c63432020-05-07 18:55:35 -0700136 mTileSizePx = SizeCalculator.getCategoryTileSize(getActivity());
Chuck Liao1e0501f2020-02-17 18:20:54 +0800137
138 mImageGrid.setAdapter(mAdapter);
139
140 GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), getNumColumns());
141 mImageGrid.setLayoutManager(gridLayoutManager);
Wesley.CW Wang27ff4262020-06-12 19:19:57 +0800142 mImageGrid.setAccessibilityDelegateCompat(
Wesley.CW Wangdc68fde2020-06-15 19:12:33 +0800143 new WallpaperPickerRecyclerViewAccessibilityDelegate(
144 mImageGrid, (BottomSheetHost) getParentFragment(), getNumColumns()));
Chuck Liao58aca1c2021-03-17 01:20:55 +0800145
146 if (getCategorySelectorFragmentHost().isHostToolbarShown()) {
147 view.findViewById(R.id.header_bar).setVisibility(View.GONE);
148 getCategorySelectorFragmentHost().setToolbarTitle(getText(R.string.wallpaper_title));
149 } else {
150 setUpToolbar(view);
151 setTitle(getText(R.string.wallpaper_title));
152 }
Chuck Liao1e0501f2020-02-17 18:20:54 +0800153
Chuck Liaof6b4b192020-08-07 02:31:32 +0800154 if (!DeepLinkUtils.isDeepLink(getActivity().getIntent())) {
155 getCategorySelectorFragmentHost().fetchCategories();
156 }
157
Chuck Liao81145b22020-09-03 09:52:25 +0800158 getCategorySelectorFragmentHost().hideBottomActionBar();
159
Chihhang Chuange6c73222021-04-14 22:36:41 +0800160 // 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 Liao1e0501f2020-02-17 18:20:54 +0800169 return view;
170 }
171
Chuck Liao3abf15b2020-12-17 22:33:02 +0800172 @Override
173 public void onDestroyView() {
174 getCategorySelectorFragmentHost().cleanUp();
175 super.onDestroyView();
176 }
177
Chuck Liao1e0501f2020-02-17 18:20:54 +0800178 /**
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 Liaoe2fe0302020-06-29 21:15:35 +0800248 return activity == null ? 1 : SizeCalculator.getNumCategoryColumns(activity);
Chuck Liao1e0501f2020-02-17 18:20:54 +0800249 }
250
251
252 private CategorySelectorFragmentHost getCategorySelectorFragmentHost() {
Chuck Liao8cbe49f2021-03-15 20:28:04 +0800253 Fragment parentFragment = getParentFragment();
254 if (parentFragment != null) {
255 return (CategorySelectorFragmentHost) parentFragment;
256 } else {
257 return (CategorySelectorFragmentHost) getActivity();
258 }
Chuck Liao1e0501f2020-02-17 18:20:54 +0800259 }
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 Liaoa63f1bf2020-06-11 01:35:42 +0800304 getCategorySelectorFragmentHost().show(mCategory);
Chuck Liao1e0501f2020-02-17 18:20:54 +0800305 }
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,
334 getResources().getColor(R.color.secondary_color));
335 } else {
336 // TODO(orenb): Replace this workaround for b/62584914 with a proper way of
337 // unloading the ImageView such that no incorrect image is improperly loaded upon
338 // rapid scroll.
339 Object nullObj = null;
340 Glide.with(getActivity())
341 .asDrawable()
342 .load(nullObj)
343 .into(mImageView);
344
345 }
346 }
347 }
348
349 /**
350 * ViewHolder subclass for the loading indicator ("spinner") shown when categories are being
351 * fetched.
352 */
353 private class LoadingIndicatorHolder extends RecyclerView.ViewHolder {
354 private LoadingIndicatorHolder(View view) {
355 super(view);
356 ProgressBar progressBar = view.findViewById(R.id.loading_indicator);
357 progressBar.getIndeterminateDrawable().setColorFilter(
358 getResources().getColor(R.color.accent_color), PorterDuff.Mode.SRC_IN);
359 }
360 }
361
362 /**
363 * RecyclerView Adapter subclass for the category tiles in the RecyclerView.
364 */
365 private class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
366 implements MyPhotosStarter.PermissionChangedListener {
367 private static final int ITEM_VIEW_TYPE_CATEGORY = 3;
368 private static final int ITEM_VIEW_TYPE_LOADING_INDICATOR = 4;
369 private List<Category> mCategories;
370
371 private CategoryAdapter(List<Category> categories) {
372 mCategories = categories;
373 }
374
375 @Override
376 public int getItemViewType(int position) {
377 if (mAwaitingCategories && position == getItemCount() - 1) {
378 return ITEM_VIEW_TYPE_LOADING_INDICATOR;
379 }
380
381 return ITEM_VIEW_TYPE_CATEGORY;
382 }
383
384 @Override
385 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
386 LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
387 View view;
388
389 switch (viewType) {
390 case ITEM_VIEW_TYPE_LOADING_INDICATOR:
391 view = layoutInflater.inflate(R.layout.grid_item_loading_indicator,
392 parent, /* attachToRoot= */ false);
393 return new LoadingIndicatorHolder(view);
394 case ITEM_VIEW_TYPE_CATEGORY:
395 view = layoutInflater.inflate(R.layout.grid_item_category,
396 parent, /* attachToRoot= */ false);
397 return new CategoryHolder(view);
398 default:
399 Log.e(TAG, "Unsupported viewType " + viewType + " in CategoryAdapter");
400 return null;
401 }
402 }
403
404 @Override
405 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
406 int viewType = getItemViewType(position);
407
408 switch (viewType) {
409 case ITEM_VIEW_TYPE_CATEGORY:
410 // Offset position to get category index to account for the non-category view
411 // holders.
412 Category category = mCategories.get(position - NUM_NON_CATEGORY_VIEW_HOLDERS);
413 ((CategoryHolder) holder).bindCategory(category);
414 break;
415 case ITEM_VIEW_TYPE_LOADING_INDICATOR:
416 // No op.
417 break;
418 default:
419 Log.e(TAG, "Unsupported viewType " + viewType + " in CategoryAdapter");
420 }
421 }
422
423 @Override
424 public int getItemCount() {
425 // Add to size of categories to account for the metadata related views.
426 // Add 1 more for the loading indicator if not yet done loading.
427 int size = mCategories.size() + NUM_NON_CATEGORY_VIEW_HOLDERS;
428 if (mAwaitingCategories) {
429 size += 1;
430 }
431
432 return size;
433 }
434
435 @Override
436 public void onPermissionsGranted() {
437 notifyDataSetChanged();
438 }
439
440 @Override
441 public void onPermissionsDenied(boolean dontAskAgain) {
442 if (!dontAskAgain) {
443 return;
444 }
445
446 String permissionNeededMessage =
447 getString(R.string.permission_needed_explanation_go_to_settings);
448 AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme)
449 .setMessage(permissionNeededMessage)
450 .setPositiveButton(android.R.string.ok, null /* onClickListener */)
451 .setNegativeButton(
452 R.string.settings_button_label,
453 (dialogInterface, i) -> {
454 Intent appInfoIntent =
455 new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
456 Uri uri = Uri.fromParts("package",
457 getActivity().getPackageName(), /* fragment= */ null);
458 appInfoIntent.setData(uri);
459 startActivityForResult(
460 appInfoIntent, SETTINGS_APP_INFO_REQUEST_CODE);
461 })
462 .create();
463 dialog.show();
464 }
465 }
466
467 private class GridPaddingDecoration extends RecyclerView.ItemDecoration {
468
469 private int mPadding;
470
471 GridPaddingDecoration(int padding) {
472 mPadding = padding;
473 }
474
475 @Override
476 public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
477 RecyclerView.State state) {
478 int position = parent.getChildAdapterPosition(view) - NUM_NON_CATEGORY_VIEW_HOLDERS;
479 if (position >= 0) {
480 outRect.left = mPadding;
481 outRect.right = mPadding;
482 }
483 }
484 }
485
486 /**
487 * SpanSizeLookup subclass which provides that the item in the first position spans the number
488 * of columns in the RecyclerView and all other items only take up a single span.
489 */
490 private class CategorySpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
491 CategoryAdapter mAdapter;
492
493 private CategorySpanSizeLookup(CategoryAdapter adapter) {
494 mAdapter = adapter;
495 }
496
497 @Override
498 public int getSpanSize(int position) {
499 if (position < NUM_NON_CATEGORY_VIEW_HOLDERS
500 || mAdapter.getItemViewType(position)
501 == CategoryAdapter.ITEM_VIEW_TYPE_LOADING_INDICATOR) {
502 return getNumColumns();
503 }
504
505 return 1;
506 }
507 }
Chuck Liao1e0501f2020-02-17 18:20:54 +0800508}