Ensure category is loaded in individual picker
Reload the category when starting the Individual Picker
Activity and Fragment, since we cannot assume that they're
loaded (these were only loaded in TopLevelActivity before,
and not when the IndividualPickerActivity got restarted from
a config change for example).
If the categories are already loaded, fetching them is a
no-op.
Bug: 136241900
Change-Id: I6cab551d0eef3c714c72983496dd4ff842ad3661
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index a33bf97..8f1d380 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -48,6 +48,9 @@
import com.android.wallpaper.asset.Asset;
import com.android.wallpaper.asset.Asset.DrawableLoadedListener;
import com.android.wallpaper.config.Flags;
+import com.android.wallpaper.model.Category;
+import com.android.wallpaper.model.CategoryProvider;
+import com.android.wallpaper.model.CategoryReceiver;
import com.android.wallpaper.model.WallpaperCategory;
import com.android.wallpaper.model.WallpaperInfo;
import com.android.wallpaper.model.WallpaperReceiver;
@@ -257,27 +260,38 @@
mRandom = new Random();
mHandler = new Handler();
- String collectionId = getArguments().getString(ARG_CATEGORY_COLLECTION_ID);
- mCategory = (WallpaperCategory) injector.getCategoryProvider(appContext).getCategory(
- collectionId);
- if (mCategory == null) {
- DiskBasedLogger.e(TAG, "Failed to find the category.", appContext);
-
- // The absence of this category in the CategoryProvider indicates a broken state, probably due
- // to a relaunch into this activity/fragment following a crash immediately prior; see
- // b//38030129. Hence, finish the activity and return.
- getActivity().finish();
- return;
- }
-
- mWallpaperRotationInitializer = mCategory.getWallpaperRotationInitializer();
-
// Clear Glide's cache if night-mode changed to ensure thumbnails are reloaded
if (savedInstanceState != null && (savedInstanceState.getInt(KEY_NIGHT_MODE)
!= (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK))) {
Glide.get(getContext()).clearMemory();
}
+ CategoryProvider categoryProvider = injector.getCategoryProvider(appContext);
+ categoryProvider.fetchCategories(new CategoryReceiver() {
+ @Override
+ public void onCategoryReceived(Category category) {
+ // Do nothing.
+ }
+
+ @Override
+ public void doneFetchingCategories() {
+ mCategory = (WallpaperCategory) categoryProvider.getCategory(
+ getArguments().getString(ARG_CATEGORY_COLLECTION_ID));
+ if (mCategory == null) {
+ DiskBasedLogger.e(TAG, "Failed to find the category.", getContext());
+
+ // The absence of this category in the CategoryProvider indicates a broken
+ // state, see b/38030129. Hence, finish the activity and return.
+ getActivity().finish();
+ return;
+ }
+ onCategoryLoaded();
+ }
+ }, false);
+ }
+
+ protected void onCategoryLoaded() {
+ mWallpaperRotationInitializer = mCategory.getWallpaperRotationInitializer();
fetchWallpapers(false);
if (mCategory.supportsThirdParty()) {
@@ -290,6 +304,8 @@
mPackageStatusNotifier.addListener(mAppStatusListener,
WallpaperService.SERVICE_INTERFACE);
}
+
+ setUpImageGrid();
}
void fetchWallpapers(boolean forceReload) {
@@ -347,6 +363,7 @@
GridMarginDecoration.applyTo(mImageGrid);
setUpImageGrid();
+
setUpBottomSheet();
return view;
@@ -370,6 +387,18 @@
}
void setUpImageGrid() {
+ // Skip if mImageGrid been initialized yet
+ if (mImageGrid == null) {
+ return;
+ }
+ // Skip if category hasn't loaded yet
+ if (mCategory == null) {
+ return;
+ }
+ // Skip if the adapter was already created
+ if (mAdapter != null) {
+ return;
+ }
mAdapter = new IndividualAdapter(mWallpapers);
mImageGrid.setAdapter(mAdapter);
mImageGrid.setLayoutManager(new GridLayoutManager(getActivity(), getNumColumns()));