Fetch the wallpaper category from the server instead of cache when launching the app(activity).

Bug: 169029398
Change-Id: Icbbb11c33ae4d4dd7d376a7792d0f685ad809421
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/TopLevelPickerActivity.java b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
index 40627b5..8e667cb 100755
--- a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
+++ b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
@@ -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/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;