blob: 98e739b478dfd2ce13d366d1358413ddf9486972 [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;
19import android.content.Intent;
20import android.graphics.Point;
21import android.graphics.PorterDuff;
22import android.graphics.Rect;
23import android.net.Uri;
24import android.os.Bundle;
25import android.provider.Settings;
26import android.util.DisplayMetrics;
27import android.util.Log;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.ImageView;
32import android.widget.ProgressBar;
33import android.widget.TextView;
34
35import androidx.annotation.NonNull;
36import androidx.annotation.Nullable;
37import androidx.appcompat.app.AlertDialog;
38import 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;
48import com.android.wallpaper.util.DisplayMetricsRetriever;
Santiago Etchebehere53c63432020-05-07 18:55:35 -070049import com.android.wallpaper.util.SizeCalculator;
Wesley.CW Wangdc68fde2020-06-15 19:12:33 +080050import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate;
51import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost;
Chuck Liao1e0501f2020-02-17 18:20:54 +080052
53import com.bumptech.glide.Glide;
54
55import java.util.ArrayList;
56import java.util.List;
57
58/**
59 * Displays the UI which contains the categories of the wallpaper.
60 */
61public class CategorySelectorFragment extends Fragment {
62
63 // The number of ViewHolders that don't pertain to category tiles.
64 // Currently 2: one for the metadata section and one for the "Select wallpaper" header.
65 private static final int NUM_NON_CATEGORY_VIEW_HOLDERS = 0;
66 private static final int SETTINGS_APP_INFO_REQUEST_CODE = 1;
67 private static final String TAG = "CategorySelectorFragment";
68
69 /**
70 * Interface to be implemented by an Fragment hosting a {@link CategorySelectorFragment}
71 */
72 public interface CategorySelectorFragmentHost {
73
74 /**
75 * Requests to show the Android custom photo picker for the sake of picking a photo
76 * to set as the device's wallpaper.
77 */
78 void requestCustomPhotoPicker(MyPhotosStarter.PermissionChangedListener listener);
79
80 /**
81 * Shows the wallpaper page of the specific category.
82 *
Chuck Liaoa63f1bf2020-06-11 01:35:42 +080083 * @param category the wallpaper's {@link Category}
Chuck Liao1e0501f2020-02-17 18:20:54 +080084 */
Chuck Liaoa63f1bf2020-06-11 01:35:42 +080085 void show(Category category);
Chuck Liao0088bca2020-05-28 16:03:23 +080086
87 /**
88 * Sets the title in the toolbar.
89 */
90 void setToolbarTitle(CharSequence title);
Chuck Liao1e0501f2020-02-17 18:20:54 +080091 }
92
93 private RecyclerView mImageGrid;
94 private CategoryAdapter mAdapter;
95 private ArrayList<Category> mCategories = new ArrayList<>();
96 private Point mTileSizePx;
97 private boolean mAwaitingCategories;
98
99 public CategorySelectorFragment() {
100 mAdapter = new CategoryAdapter(mCategories);
101 }
102
103 @Nullable
104 @Override
105 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
106 @Nullable Bundle savedInstanceState) {
107 View view = inflater.inflate(R.layout.fragment_category_selector, container,
108 /* attachToRoot= */ false);
Chuck Liao1e0501f2020-02-17 18:20:54 +0800109 mImageGrid = view.findViewById(R.id.category_grid);
110 mImageGrid.addItemDecoration(new GridPaddingDecoration(
111 getResources().getDimensionPixelSize(R.dimen.grid_padding)));
112
Santiago Etchebehere53c63432020-05-07 18:55:35 -0700113 mTileSizePx = SizeCalculator.getCategoryTileSize(getActivity());
Chuck Liao1e0501f2020-02-17 18:20:54 +0800114
115 mImageGrid.setAdapter(mAdapter);
116
117 GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), getNumColumns());
118 mImageGrid.setLayoutManager(gridLayoutManager);
Wesley.CW Wang27ff4262020-06-12 19:19:57 +0800119 mImageGrid.setAccessibilityDelegateCompat(
Wesley.CW Wangdc68fde2020-06-15 19:12:33 +0800120 new WallpaperPickerRecyclerViewAccessibilityDelegate(
121 mImageGrid, (BottomSheetHost) getParentFragment(), getNumColumns()));
Chuck Liao0088bca2020-05-28 16:03:23 +0800122 getCategorySelectorFragmentHost().setToolbarTitle(getText(R.string.wallpaper_title));
Chuck Liao1e0501f2020-02-17 18:20:54 +0800123
124 return view;
125 }
126
127 /**
128 * Inserts the given category into the categories list in priority order.
129 */
130 void addCategory(Category category, boolean loading) {
131 // If not previously waiting for categories, enter the waiting state by showing the loading
132 // indicator.
133 if (loading && !mAwaitingCategories) {
134 mAdapter.notifyItemChanged(getNumColumns());
135 mAdapter.notifyItemInserted(getNumColumns());
136 mAwaitingCategories = true;
137 }
138 // Not add existing category to category list
139 if (mCategories.indexOf(category) >= 0) {
140 updateCategory(category);
141 return;
142 }
143
144 int priority = category.getPriority();
145
146 int index = 0;
147 while (index < mCategories.size() && priority >= mCategories.get(index).getPriority()) {
148 index++;
149 }
150
151 mCategories.add(index, category);
152 if (mAdapter != null) {
153 // Offset the index because of the static metadata element at beginning of RecyclerView.
154 mAdapter.notifyItemInserted(index + NUM_NON_CATEGORY_VIEW_HOLDERS);
155 }
156 }
157
158 void removeCategory(Category category) {
159 int index = mCategories.indexOf(category);
160 if (index >= 0) {
161 mCategories.remove(index);
162 mAdapter.notifyItemRemoved(index + NUM_NON_CATEGORY_VIEW_HOLDERS);
163 }
164 }
165
166 void updateCategory(Category category) {
167 int index = mCategories.indexOf(category);
168 if (index >= 0) {
169 mCategories.remove(index);
170 mCategories.add(index, category);
171 mAdapter.notifyItemChanged(index + NUM_NON_CATEGORY_VIEW_HOLDERS);
172 }
173 }
174
175 void clearCategories() {
176 mCategories.clear();
177 mAdapter.notifyDataSetChanged();
178 }
179
180 /**
181 * Notifies the CategoryFragment that no further categories are expected so it may hide
182 * the loading indicator.
183 */
184 void doneFetchingCategories() {
185 if (mAwaitingCategories) {
186 mAdapter.notifyItemRemoved(mAdapter.getItemCount() - 1);
187 mAwaitingCategories = false;
188 }
189 }
190
191 void notifyDataSetChanged() {
192 mAdapter.notifyDataSetChanged();
193 }
194
195 private int getNumColumns() {
196 Activity activity = getActivity();
Chuck Liaoe2fe0302020-06-29 21:15:35 +0800197 return activity == null ? 1 : SizeCalculator.getNumCategoryColumns(activity);
Chuck Liao1e0501f2020-02-17 18:20:54 +0800198 }
199
200
201 private CategorySelectorFragmentHost getCategorySelectorFragmentHost() {
202 return (CategorySelectorFragmentHost) getParentFragment();
203 }
204
205 /**
206 * ViewHolder subclass for a category tile in the RecyclerView.
207 */
208 private class CategoryHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
209 private Category mCategory;
210 private ImageView mImageView;
211 private ImageView mOverlayIconView;
212 private TextView mTitleView;
213
214 CategoryHolder(View itemView) {
215 super(itemView);
216 itemView.setOnClickListener(this);
217
218 mImageView = itemView.findViewById(R.id.image);
219 mOverlayIconView = itemView.findViewById(R.id.overlay_icon);
220 mTitleView = itemView.findViewById(R.id.category_title);
221
222 CardView categoryView = itemView.findViewById(R.id.category);
223 categoryView.getLayoutParams().height = mTileSizePx.y;
224 }
225
226 @Override
227 public void onClick(View view) {
228 final UserEventLogger eventLogger =
229 InjectorProvider.getInjector().getUserEventLogger(getActivity());
230 eventLogger.logCategorySelected(mCategory.getCollectionId());
231
232 if (mCategory.supportsCustomPhotos()) {
233 getCategorySelectorFragmentHost().requestCustomPhotoPicker(
234 new MyPhotosStarter.PermissionChangedListener() {
235 @Override
236 public void onPermissionsGranted() {
237 drawThumbnailAndOverlayIcon();
238 }
239
240 @Override
241 public void onPermissionsDenied(boolean dontAskAgain) {
242 // No-op
243 }
244 });
245 return;
246 }
247
Chuck Liaoa63f1bf2020-06-11 01:35:42 +0800248 getCategorySelectorFragmentHost().show(mCategory);
Chuck Liao0088bca2020-05-28 16:03:23 +0800249 getCategorySelectorFragmentHost().setToolbarTitle(mCategory.getTitle());
Chuck Liao1e0501f2020-02-17 18:20:54 +0800250 }
251
252 /**
253 * Binds the given category to this CategoryHolder.
254 */
255 private void bindCategory(Category category) {
256 mCategory = category;
257 mTitleView.setText(category.getTitle());
258 drawThumbnailAndOverlayIcon();
259 }
260
261 /**
262 * Draws the CategoryHolder's thumbnail and overlay icon.
263 */
264 private void drawThumbnailAndOverlayIcon() {
265 mOverlayIconView.setImageDrawable(mCategory.getOverlayIcon(
266 getActivity().getApplicationContext()));
267
268 // Size the overlay icon according to the category.
269 int overlayIconDimenDp = mCategory.getOverlayIconSizeDp();
270 DisplayMetrics metrics = DisplayMetricsRetriever.getInstance().getDisplayMetrics(
271 getResources(), getActivity().getWindowManager().getDefaultDisplay());
272 int overlayIconDimenPx = (int) (overlayIconDimenDp * metrics.density);
273 mOverlayIconView.getLayoutParams().width = overlayIconDimenPx;
274 mOverlayIconView.getLayoutParams().height = overlayIconDimenPx;
275
276 Asset thumbnail = mCategory.getThumbnail(getActivity().getApplicationContext());
277 if (thumbnail != null) {
278 thumbnail.loadDrawable(getActivity(), mImageView,
279 getResources().getColor(R.color.secondary_color));
280 } else {
281 // TODO(orenb): Replace this workaround for b/62584914 with a proper way of
282 // unloading the ImageView such that no incorrect image is improperly loaded upon
283 // rapid scroll.
284 Object nullObj = null;
285 Glide.with(getActivity())
286 .asDrawable()
287 .load(nullObj)
288 .into(mImageView);
289
290 }
291 }
292 }
293
294 /**
295 * ViewHolder subclass for the loading indicator ("spinner") shown when categories are being
296 * fetched.
297 */
298 private class LoadingIndicatorHolder extends RecyclerView.ViewHolder {
299 private LoadingIndicatorHolder(View view) {
300 super(view);
301 ProgressBar progressBar = view.findViewById(R.id.loading_indicator);
302 progressBar.getIndeterminateDrawable().setColorFilter(
303 getResources().getColor(R.color.accent_color), PorterDuff.Mode.SRC_IN);
304 }
305 }
306
307 /**
308 * RecyclerView Adapter subclass for the category tiles in the RecyclerView.
309 */
310 private class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
311 implements MyPhotosStarter.PermissionChangedListener {
312 private static final int ITEM_VIEW_TYPE_CATEGORY = 3;
313 private static final int ITEM_VIEW_TYPE_LOADING_INDICATOR = 4;
314 private List<Category> mCategories;
315
316 private CategoryAdapter(List<Category> categories) {
317 mCategories = categories;
318 }
319
320 @Override
321 public int getItemViewType(int position) {
322 if (mAwaitingCategories && position == getItemCount() - 1) {
323 return ITEM_VIEW_TYPE_LOADING_INDICATOR;
324 }
325
326 return ITEM_VIEW_TYPE_CATEGORY;
327 }
328
329 @Override
330 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
331 LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
332 View view;
333
334 switch (viewType) {
335 case ITEM_VIEW_TYPE_LOADING_INDICATOR:
336 view = layoutInflater.inflate(R.layout.grid_item_loading_indicator,
337 parent, /* attachToRoot= */ false);
338 return new LoadingIndicatorHolder(view);
339 case ITEM_VIEW_TYPE_CATEGORY:
340 view = layoutInflater.inflate(R.layout.grid_item_category,
341 parent, /* attachToRoot= */ false);
342 return new CategoryHolder(view);
343 default:
344 Log.e(TAG, "Unsupported viewType " + viewType + " in CategoryAdapter");
345 return null;
346 }
347 }
348
349 @Override
350 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
351 int viewType = getItemViewType(position);
352
353 switch (viewType) {
354 case ITEM_VIEW_TYPE_CATEGORY:
355 // Offset position to get category index to account for the non-category view
356 // holders.
357 Category category = mCategories.get(position - NUM_NON_CATEGORY_VIEW_HOLDERS);
358 ((CategoryHolder) holder).bindCategory(category);
359 break;
360 case ITEM_VIEW_TYPE_LOADING_INDICATOR:
361 // No op.
362 break;
363 default:
364 Log.e(TAG, "Unsupported viewType " + viewType + " in CategoryAdapter");
365 }
366 }
367
368 @Override
369 public int getItemCount() {
370 // Add to size of categories to account for the metadata related views.
371 // Add 1 more for the loading indicator if not yet done loading.
372 int size = mCategories.size() + NUM_NON_CATEGORY_VIEW_HOLDERS;
373 if (mAwaitingCategories) {
374 size += 1;
375 }
376
377 return size;
378 }
379
380 @Override
381 public void onPermissionsGranted() {
382 notifyDataSetChanged();
383 }
384
385 @Override
386 public void onPermissionsDenied(boolean dontAskAgain) {
387 if (!dontAskAgain) {
388 return;
389 }
390
391 String permissionNeededMessage =
392 getString(R.string.permission_needed_explanation_go_to_settings);
393 AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme)
394 .setMessage(permissionNeededMessage)
395 .setPositiveButton(android.R.string.ok, null /* onClickListener */)
396 .setNegativeButton(
397 R.string.settings_button_label,
398 (dialogInterface, i) -> {
399 Intent appInfoIntent =
400 new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
401 Uri uri = Uri.fromParts("package",
402 getActivity().getPackageName(), /* fragment= */ null);
403 appInfoIntent.setData(uri);
404 startActivityForResult(
405 appInfoIntent, SETTINGS_APP_INFO_REQUEST_CODE);
406 })
407 .create();
408 dialog.show();
409 }
410 }
411
412 private class GridPaddingDecoration extends RecyclerView.ItemDecoration {
413
414 private int mPadding;
415
416 GridPaddingDecoration(int padding) {
417 mPadding = padding;
418 }
419
420 @Override
421 public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
422 RecyclerView.State state) {
423 int position = parent.getChildAdapterPosition(view) - NUM_NON_CATEGORY_VIEW_HOLDERS;
424 if (position >= 0) {
425 outRect.left = mPadding;
426 outRect.right = mPadding;
427 }
428 }
429 }
430
431 /**
432 * SpanSizeLookup subclass which provides that the item in the first position spans the number
433 * of columns in the RecyclerView and all other items only take up a single span.
434 */
435 private class CategorySpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
436 CategoryAdapter mAdapter;
437
438 private CategorySpanSizeLookup(CategoryAdapter adapter) {
439 mAdapter = adapter;
440 }
441
442 @Override
443 public int getSpanSize(int position) {
444 if (position < NUM_NON_CATEGORY_VIEW_HOLDERS
445 || mAdapter.getItemViewType(position)
446 == CategoryAdapter.ITEM_VIEW_TYPE_LOADING_INDICATOR) {
447 return getNumColumns();
448 }
449
450 return 1;
451 }
452 }
Chuck Liao1e0501f2020-02-17 18:20:54 +0800453}