Merging from ub-launcher3-rvc-qpr-dev @ build 6861538

Bug:150504032
Test: manual, presubmit on the source branch
x20/teams/android-launcher/merge/ub-launcher3-rvc-qpr-dev_rvc-qpr-dev_6861538.html

Change-Id: I743cfd1a726d223ed6e1e5c0cfb9dc64a9d0c306
diff --git a/res/values-ar/strings.xml b/res/values-ar/strings.xml
index 9f03248..166a289 100644
--- a/res/values-ar/strings.xml
+++ b/res/values-ar/strings.xml
@@ -54,7 +54,7 @@
     <string name="next_wallpaper" msgid="3911873152952596232">"الخلفية التالية"</string>
     <string name="wallpaper_disabled_message" msgid="7309484130562148185">"تم إيقاف إمكانية تعيين خلفية على هذا الجهاز."</string>
     <string name="wallpaper_disabled_by_administrator_message" msgid="1551430406714747884">"أوقف مشرف الجهاز إمكانية تعيين خلفية."</string>
-    <string name="wallpaper_set_successfully_message" msgid="2958998799111688578">"تم تعيين الخلفية بنجاح."</string>
+    <string name="wallpaper_set_successfully_message" msgid="2958998799111688578">"تم ضبط الخلفية بنجاح."</string>
     <string name="wallpapers_unavailable_offline_message" msgid="8136405438621689532">"يلزمك الاتصال بالإنترنت للاطّلاع على الخلفيات. يُرجى الاتصال وإعادة المحاولة."</string>
     <string name="currently_set_home_wallpaper_thumbnail" msgid="4022381436821898917">"صورة مصغَّرة لخلفية الشاشة الرئيسية المعيَّنة حاليًا"</string>
     <string name="currently_set_lock_wallpaper_thumbnail" msgid="2094209303934569997">"صورة مصغَّرة لخلفية شاشة القفل المعيَّنة حاليًا"</string>
diff --git a/src/com/android/wallpaper/model/CategoryProvider.java b/src/com/android/wallpaper/model/CategoryProvider.java
index ceef6b4..b86e6df 100755
--- a/src/com/android/wallpaper/model/CategoryProvider.java
+++ b/src/com/android/wallpaper/model/CategoryProvider.java
@@ -59,4 +59,9 @@
      * Checks if the categories are fetched.
      */
     boolean isCategoriesFetched();
+
+    /**
+     * Resets the fetched categories if needed.
+     */
+    void resetIfNeeded();
 }
diff --git a/src/com/android/wallpaper/module/DefaultCategoryProvider.java b/src/com/android/wallpaper/module/DefaultCategoryProvider.java
index e9b6955..e687bf5 100755
--- a/src/com/android/wallpaper/module/DefaultCategoryProvider.java
+++ b/src/com/android/wallpaper/module/DefaultCategoryProvider.java
@@ -15,6 +15,8 @@
  */
 package com.android.wallpaper.module;
 
+import static com.android.wallpaper.module.NetworkStatusNotifier.NETWORK_NOT_INITIALIZED;
+
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
@@ -41,6 +43,7 @@
 import com.android.wallpaper.model.WallpaperCategory;
 import com.android.wallpaper.model.WallpaperInfo;
 import com.android.wallpaper.module.FormFactorChecker.FormFactor;
+import com.android.wallpaper.module.NetworkStatusNotifier.NetworkStatus;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
@@ -76,9 +79,16 @@
     protected ArrayList<Category> mCategories;
     protected boolean mFetchedCategories;
 
+    private NetworkStatusNotifier mNetworkStatusNotifier;
+    // The network status of the last fetch from the server.
+    @NetworkStatus
+    private int mNetworkStatus;
+
     public DefaultCategoryProvider(Context context) {
         mAppContext = context.getApplicationContext();
         mCategories = new ArrayList<>();
+        mNetworkStatusNotifier = InjectorProvider.getInjector().getNetworkStatusNotifier(context);
+        mNetworkStatus = NETWORK_NOT_INITIALIZED;
     }
 
     @Override
@@ -94,6 +104,7 @@
             mFetchedCategories = false;
         }
 
+        mNetworkStatus = mNetworkStatusNotifier.getNetworkStatus();
         doFetch(receiver, forceRefresh);
     }
 
@@ -127,6 +138,14 @@
         return mFetchedCategories;
     }
 
+    @Override
+    public void resetIfNeeded() {
+        if (mNetworkStatus != mNetworkStatusNotifier.getNetworkStatus()) {
+            mCategories.clear();
+            mFetchedCategories = false;
+        }
+    }
+
     protected void doFetch(final CategoryReceiver receiver, boolean forceRefresh) {
         CategoryReceiver delegatingReceiver = new CategoryReceiver() {
             @Override
diff --git a/src/com/android/wallpaper/module/NetworkStatusNotifier.java b/src/com/android/wallpaper/module/NetworkStatusNotifier.java
index 50e52e4..def7e6e 100755
--- a/src/com/android/wallpaper/module/NetworkStatusNotifier.java
+++ b/src/com/android/wallpaper/module/NetworkStatusNotifier.java
@@ -22,6 +22,7 @@
  * used for testability.
  */
 public interface NetworkStatusNotifier {
+    static int NETWORK_NOT_INITIALIZED = -1;
     static int NETWORK_NOT_CONNECTED = 0;
     static int NETWORK_CONNECTED = 1;
 
@@ -40,6 +41,7 @@
      * Possible network statuses .
      */
     @IntDef({
+            NETWORK_NOT_INITIALIZED,
             NETWORK_NOT_CONNECTED,
             NETWORK_CONNECTED
     })
diff --git a/src/com/android/wallpaper/picker/CategoryFragment.java b/src/com/android/wallpaper/picker/CategoryFragment.java
index 42566eb..a90acc9 100755
--- a/src/com/android/wallpaper/picker/CategoryFragment.java
+++ b/src/com/android/wallpaper/picker/CategoryFragment.java
@@ -19,6 +19,7 @@
 import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED;
 
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.app.WallpaperColors;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -39,7 +40,6 @@
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
-import androidx.appcompat.app.AlertDialog;
 import androidx.cardview.widget.CardView;
 import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.fragment.app.Fragment;
diff --git a/src/com/android/wallpaper/picker/CategorySelectorFragment.java b/src/com/android/wallpaper/picker/CategorySelectorFragment.java
index 887eb92..bb5d3d2 100644
--- a/src/com/android/wallpaper/picker/CategorySelectorFragment.java
+++ b/src/com/android/wallpaper/picker/CategorySelectorFragment.java
@@ -16,6 +16,7 @@
 package com.android.wallpaper.picker;
 
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.content.Intent;
 import android.graphics.Point;
 import android.graphics.PorterDuff;
@@ -34,7 +35,6 @@
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
-import androidx.appcompat.app.AlertDialog;
 import androidx.cardview.widget.CardView;
 import androidx.fragment.app.Fragment;
 import androidx.recyclerview.widget.GridLayoutManager;
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index 308e907..c827b5a 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -250,10 +250,6 @@
 
         mBottomActionBar.disableActions();
         mWallpaperAsset.decodeRawDimensions(getActivity(), dimensions -> {
-            if (mBottomActionBar != null) {
-                mBottomActionBar.enableActions();
-            }
-
             // Don't continue loading the wallpaper if the Fragment is detached.
             if (getActivity() == null) {
                 return;
@@ -265,6 +261,10 @@
                 return;
             }
 
+            if (mBottomActionBar != null) {
+                mBottomActionBar.enableActions();
+            }
+
             mRawWallpaperSize = dimensions;
 
             setUpExploreIntentAndLabel(ImagePreviewFragment.this::initFullResView);
diff --git a/src/com/android/wallpaper/picker/LoadWallpaperErrorDialogFragment.java b/src/com/android/wallpaper/picker/LoadWallpaperErrorDialogFragment.java
index 0d9e9af..603d931 100755
--- a/src/com/android/wallpaper/picker/LoadWallpaperErrorDialogFragment.java
+++ b/src/com/android/wallpaper/picker/LoadWallpaperErrorDialogFragment.java
@@ -15,6 +15,7 @@
  */
 package com.android.wallpaper.picker;
 
+import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
@@ -22,7 +23,6 @@
 import com.android.wallpaper.R;
 
 import androidx.annotation.NonNull;
-import androidx.appcompat.app.AlertDialog;
 import androidx.fragment.app.DialogFragment;
 
 /**
diff --git a/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java b/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java
index 00772c9..4ed2e13 100755
--- a/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java
+++ b/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java
@@ -15,6 +15,7 @@
  */
 package com.android.wallpaper.picker;
 
+import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -24,7 +25,6 @@
 
 import androidx.annotation.NonNull;
 import androidx.annotation.StringRes;
-import androidx.appcompat.app.AlertDialog;
 import androidx.appcompat.view.ContextThemeWrapper;
 import androidx.fragment.app.DialogFragment;
 
diff --git a/src/com/android/wallpaper/picker/SetWallpaperErrorDialogFragment.java b/src/com/android/wallpaper/picker/SetWallpaperErrorDialogFragment.java
index 89a7d02..b484d1a 100755
--- a/src/com/android/wallpaper/picker/SetWallpaperErrorDialogFragment.java
+++ b/src/com/android/wallpaper/picker/SetWallpaperErrorDialogFragment.java
@@ -16,6 +16,7 @@
 package com.android.wallpaper.picker;
 
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
@@ -23,7 +24,6 @@
 import com.android.wallpaper.R;
 import com.android.wallpaper.module.WallpaperPersister.Destination;
 
-import androidx.appcompat.app.AlertDialog;
 import androidx.fragment.app.DialogFragment;
 import androidx.fragment.app.Fragment;
 
diff --git a/src/com/android/wallpaper/picker/StartRotationErrorDialogFragment.java b/src/com/android/wallpaper/picker/StartRotationErrorDialogFragment.java
index 9f98b70..d8f67c1 100755
--- a/src/com/android/wallpaper/picker/StartRotationErrorDialogFragment.java
+++ b/src/com/android/wallpaper/picker/StartRotationErrorDialogFragment.java
@@ -15,6 +15,7 @@
  */
 package com.android.wallpaper.picker;
 
+import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
@@ -23,7 +24,6 @@
 import com.android.wallpaper.model.WallpaperRotationInitializer.NetworkPreference;
 
 import androidx.annotation.NonNull;
-import androidx.appcompat.app.AlertDialog;
 import androidx.fragment.app.DialogFragment;
 
 /**
diff --git a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
index 40627b5..f74a445 100755
--- a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
+++ b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
@@ -16,6 +16,7 @@
 package com.android.wallpaper.picker;
 
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.app.ProgressDialog;
 import android.content.Context;
 import android.content.Intent;
@@ -41,7 +42,6 @@
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
-import androidx.appcompat.app.AlertDialog;
 import androidx.appcompat.widget.Toolbar;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentManager;
@@ -169,6 +169,8 @@
         mWallpaperPreferences = injector.getPreferences(this);
         mWasCustomPhotoWallpaperSet = false;
 
+        mDelegate.getCategoryProvider().resetIfNeeded();
+
         @WallpaperSupportLevel int wallpaperSupportLevel = mDelegate.getWallpaperSupportLevel();
         if (wallpaperSupportLevel != WallpaperDisabledFragment.SUPPORTED_CAN_SET) {
             setContentView(R.layout.activity_top_level_picker);
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index 260d121..0925f39 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -369,8 +369,12 @@
 
             @Override
             public void doneFetchingCategories() {
-                mCategory = (WallpaperCategory) categoryProvider.getCategory(
+                Category category = categoryProvider.getCategory(
                         getArguments().getString(ARG_CATEGORY_COLLECTION_ID));
+                if (category != null && !(category instanceof WallpaperCategory)) {
+                    return;
+                }
+                mCategory = (WallpaperCategory) category;
                 if (mCategory == null) {
                     DiskBasedLogger.e(TAG, "Failed to find the category.", getContext());
 
@@ -388,6 +392,9 @@
 
 
     protected void onCategoryLoaded() {
+        if (getIndividualPickerFragmentHost() == null) {
+            return;
+        }
         getIndividualPickerFragmentHost().setToolbarTitle(mCategory.getTitle());
         mWallpaperRotationInitializer = mCategory.getWallpaperRotationInitializer();
         // Avoids the "rotation" action is not shown correctly
diff --git a/tests/src/com/android/wallpaper/testing/TestCategoryProvider.java b/tests/src/com/android/wallpaper/testing/TestCategoryProvider.java
index c7d7c95..9354958 100644
--- a/tests/src/com/android/wallpaper/testing/TestCategoryProvider.java
+++ b/tests/src/com/android/wallpaper/testing/TestCategoryProvider.java
@@ -92,6 +92,11 @@
         return false;
     }
 
+    @Override
+    public void resetIfNeeded() {
+        mCategories.clear();
+    }
+
     /** Returns a list of test Category objects used by this TestCategoryProvider. */
     public List<Category> getTestCategories() {
         return mCategories;