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/IndividualPickerActivity.java b/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java
index 3ed5bf3..367181a 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java
@@ -22,9 +22,6 @@
 import android.content.Intent;
 import android.content.res.Resources.NotFoundException;
 import android.graphics.Insets;
-import android.graphics.PorterDuff.Mode;
-import android.graphics.drawable.Drawable;
-import android.os.Build.VERSION;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.MenuItem;
@@ -33,13 +30,14 @@
 import android.widget.Toast;
 
 import androidx.appcompat.widget.Toolbar;
-import androidx.core.content.ContextCompat;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentManager;
 
 import com.android.wallpaper.R;
 import com.android.wallpaper.compat.BuildCompat;
 import com.android.wallpaper.model.Category;
+import com.android.wallpaper.model.CategoryProvider;
+import com.android.wallpaper.model.CategoryReceiver;
 import com.android.wallpaper.model.InlinePreviewIntentFactory;
 import com.android.wallpaper.model.LiveWallpaperInfo;
 import com.android.wallpaper.model.PickerIntentFactory;
@@ -93,18 +91,29 @@
         mCategoryCollectionId = (savedInstanceState == null)
                 ? getIntent().getStringExtra(EXTRA_CATEGORY_COLLECTION_ID)
                 : savedInstanceState.getString(KEY_CATEGORY_COLLECTION_ID);
-        mCategory = injector.getCategoryProvider(this).getCategory(mCategoryCollectionId);
-        if (mCategory == null) {
-            DiskBasedLogger.e(TAG, "Failed to find the category: " + mCategoryCollectionId, this);
-            // We either were called with an invalid collection Id, or we're restarting with no
-            // saved state, or with a collection id that doesn't exist anymore.
-            // In those cases, we cannot continue, so let's just go back.
-            finish();
-            return;
-        }
+        CategoryProvider categoryProvider = injector.getCategoryProvider(this);
+        categoryProvider.fetchCategories(new CategoryReceiver() {
+            @Override
+            public void onCategoryReceived(Category category) {
+                // Do nothing.
+            }
 
-        setTitle(mCategory.getTitle());
-        getSupportActionBar().setTitle(mCategory.getTitle());
+            @Override
+            public void doneFetchingCategories() {
+                mCategory = categoryProvider.getCategory(mCategoryCollectionId);
+                if (mCategory == null) {
+                    DiskBasedLogger.e(TAG, "Failed to find the category: " + mCategoryCollectionId,
+                            IndividualPickerActivity.this);
+                    // We either were called with an invalid collection Id, or we're restarting with
+                    // no saved state, or with a collection id that doesn't exist anymore.
+                    // In those cases, we cannot continue, so let's just go back.
+                    finish();
+                    return;
+                }
+                onCategoryLoaded();
+            }
+        }, false);
+
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 
         toolbar.getNavigationIcon().setTint(getColor(R.color.toolbar_icon_color));
@@ -142,6 +151,11 @@
         }
     }
 
+    private void onCategoryLoaded() {
+        setTitle(mCategory.getTitle());
+        getSupportActionBar().setTitle(mCategory.getTitle());
+    }
+
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         int id = item.getItemId();
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()));