Reduce flickers when launching Storage Settings

This change remove unnecessary UI dpdate of
StorageItemPreferenceController.

- For StorageItemPreferenceController:
  Remove UI update at #displayPreference since onLoadFinished will update.
  Don't update order at #setVolume since onLoadFinished will update.

- StorageDashboardFragment & StorageCategoryFragment update
  StorageItemPreferenceController only when both StorageInfo
  and StorageResult are loaded.

Bug: 185547228
Test: atest com.android.settings.deviceinfo
      atest com.android.settings.deviceinfo.storage
      make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo
      make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo.storage
      manual visual
      Click each file category to count files size is the same as
      displayed in Storage Settings.
      manual visual
Change-Id: Id449003827a3e340e7a90f706152100b5568f834
diff --git a/src/com/android/settings/deviceinfo/StorageCategoryFragment.java b/src/com/android/settings/deviceinfo/StorageCategoryFragment.java
index 9ae835e..035c112 100644
--- a/src/com/android/settings/deviceinfo/StorageCategoryFragment.java
+++ b/src/com/android/settings/deviceinfo/StorageCategoryFragment.java
@@ -104,14 +104,16 @@
             return;
         }
 
-        mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
-
         if (!mSelectedStorageEntry.isMounted()) {
             // Set null volume to hide category stats.
             mPreferenceController.setVolume(null);
             return;
         }
         if (mSelectedStorageEntry.isPrivate()) {
+            mStorageInfo = null;
+            mAppsResult = null;
+            maybeSetLoading(isQuotaSupported());
+
             // Stats data is only available on private volumes.
             getLoaderManager().restartLoader(STORAGE_JOB_ID, Bundle.EMPTY, this);
             getLoaderManager()
@@ -156,7 +158,6 @@
     public void onViewCreated(View v, Bundle savedInstanceState) {
         super.onViewCreated(v, savedInstanceState);
         initializeCacheProvider();
-        maybeSetLoading(isQuotaSupported());
 
         EntityHeaderController.newInstance(getActivity(), this /*fragment*/,
                 null /* header view */)
@@ -179,33 +180,27 @@
     }
 
     private void onReceivedSizes() {
-        boolean stopLoading = false;
-        if (mStorageInfo != null) {
-            final long privateUsedBytes = mStorageInfo.totalBytes - mStorageInfo.freeBytes;
-            mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
-            mPreferenceController.setUsedSize(privateUsedBytes);
-            mPreferenceController.setTotalSize(mStorageInfo.totalBytes);
-            for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
-                final AbstractPreferenceController controller = mSecondaryUsers.get(i);
-                if (controller instanceof SecondaryUserController) {
-                    SecondaryUserController userController = (SecondaryUserController) controller;
-                    userController.setTotalSize(mStorageInfo.totalBytes);
-                }
-            }
-            stopLoading = true;
+        if (mStorageInfo == null || mAppsResult == null) {
+            return;
         }
 
-        if (mAppsResult != null) {
-            mPreferenceController.onLoadFinished(mAppsResult, mUserId);
-            updateSecondaryUserControllers(mSecondaryUsers, mAppsResult);
-            stopLoading = true;
+        final long privateUsedBytes = mStorageInfo.totalBytes - mStorageInfo.freeBytes;
+        mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
+        mPreferenceController.setUsedSize(privateUsedBytes);
+        mPreferenceController.setTotalSize(mStorageInfo.totalBytes);
+        for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
+            final AbstractPreferenceController controller = mSecondaryUsers.get(i);
+            if (controller instanceof SecondaryUserController) {
+                SecondaryUserController userController = (SecondaryUserController) controller;
+                userController.setTotalSize(mStorageInfo.totalBytes);
+            }
         }
 
-        // setLoading always causes a flicker, so let's avoid doing it.
-        if (stopLoading) {
-            if (getView().findViewById(R.id.loading_container).getVisibility() == View.VISIBLE) {
-                setLoading(false, true);
-            }
+        mPreferenceController.onLoadFinished(mAppsResult, mUserId);
+        updateSecondaryUserControllers(mSecondaryUsers, mAppsResult);
+
+        if (getView().findViewById(R.id.loading_container).getVisibility() == View.VISIBLE) {
+            setLoading(false, true);
         }
     }
 
diff --git a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
index 1145d7f..b76d1cb 100644
--- a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
+++ b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
@@ -232,14 +232,16 @@
         mOptionMenuController.setSelectedStorageEntry(mSelectedStorageEntry);
         getActivity().invalidateOptionsMenu();
 
-        mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
-
         if (!mSelectedStorageEntry.isMounted()) {
             // Set null volume to hide category stats.
             mPreferenceController.setVolume(null);
             return;
         }
         if (mSelectedStorageEntry.isPrivate()) {
+            mStorageInfo = null;
+            mAppsResult = null;
+            maybeSetLoading(isQuotaSupported());
+
             // Stats data is only available on private volumes.
             getLoaderManager().restartLoader(STORAGE_JOB_ID, Bundle.EMPTY, this);
             getLoaderManager()
@@ -315,7 +317,6 @@
     public void onViewCreated(View v, Bundle savedInstanceState) {
         super.onViewCreated(v, savedInstanceState);
         initializeCacheProvider();
-        maybeSetLoading(isQuotaSupported());
 
         EntityHeaderController.newInstance(getActivity(), this /*fragment*/,
                 null /* header view */)
@@ -350,33 +351,27 @@
     }
 
     private void onReceivedSizes() {
-        boolean stopLoading = false;
-        if (mStorageInfo != null) {
-            final long privateUsedBytes = mStorageInfo.totalBytes - mStorageInfo.freeBytes;
-            mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
-            mPreferenceController.setUsedSize(privateUsedBytes);
-            mPreferenceController.setTotalSize(mStorageInfo.totalBytes);
-            for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
-                final AbstractPreferenceController controller = mSecondaryUsers.get(i);
-                if (controller instanceof SecondaryUserController) {
-                    SecondaryUserController userController = (SecondaryUserController) controller;
-                    userController.setTotalSize(mStorageInfo.totalBytes);
-                }
-            }
-            stopLoading = true;
+        if (mStorageInfo == null || mAppsResult == null) {
+            return;
         }
 
-        if (mAppsResult != null) {
-            mPreferenceController.onLoadFinished(mAppsResult, mUserId);
-            updateSecondaryUserControllers(mSecondaryUsers, mAppsResult);
-            stopLoading = true;
+        final long privateUsedBytes = mStorageInfo.totalBytes - mStorageInfo.freeBytes;
+        mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
+        mPreferenceController.setUsedSize(privateUsedBytes);
+        mPreferenceController.setTotalSize(mStorageInfo.totalBytes);
+        for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
+            final AbstractPreferenceController controller = mSecondaryUsers.get(i);
+            if (controller instanceof SecondaryUserController) {
+                SecondaryUserController userController = (SecondaryUserController) controller;
+                userController.setTotalSize(mStorageInfo.totalBytes);
+            }
         }
 
-        // setLoading always causes a flicker, so let's avoid doing it.
-        if (stopLoading) {
-            if (getView().findViewById(R.id.loading_container).getVisibility() == View.VISIBLE) {
-                setLoading(false, true);
-            }
+        mPreferenceController.onLoadFinished(mAppsResult, mUserId);
+        updateSecondaryUserControllers(mSecondaryUsers, mAppsResult);
+
+        if (getView().findViewById(R.id.loading_container).getVisibility() == View.VISIBLE) {
+            setLoading(false, true);
         }
     }
 
diff --git a/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java b/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
index d57d81e..41e26ee 100644
--- a/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
@@ -222,7 +222,6 @@
         mVolume = volume;
 
         updateCategoryPreferencesVisibility();
-        updatePrivateStorageCategoryPreferencesOrder();
     }
 
     // Stats data is only available on private volumes.
@@ -353,14 +352,10 @@
         mDocumentsAndOtherPreference = screen.findPreference(DOCUMENTS_AND_OTHER_KEY);
         mSystemPreference = screen.findPreference(SYSTEM_KEY);
         mTrashPreference = screen.findPreference(TRASH_KEY);
-
-        updateCategoryPreferencesVisibility();
-        updatePrivateStorageCategoryPreferencesOrder();
     }
 
     /** Fragments use it to set storage result and update UI of this controller. */
-    public void onLoadFinished(SparseArray<StorageAsyncLoader.StorageResult> result,
-            int userId) {
+    public void onLoadFinished(SparseArray<StorageAsyncLoader.StorageResult> result, int userId) {
         final StorageAsyncLoader.StorageResult data = result.get(userId);
 
         mImagesPreference.setStorageSize(data.imagesSize, mTotalSize);
@@ -374,8 +369,6 @@
         if (mSystemPreference != null) {
             // Everything else that hasn't already been attributed is tracked as
             // belonging to system.
-            // TODO(b/170918505): Should revamp system size calculation with the data
-            // from media provider.
             long attributedSize = 0;
             for (int i = 0; i < result.size(); i++) {
                 final StorageAsyncLoader.StorageResult otherData = result.valueAt(i);
diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java
index d55975a..cfc9e93 100644
--- a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java
@@ -398,25 +398,6 @@
     }
 
     @Test
-    public void displayPreference_hideFilePreferenceWhenEmulatedStorageUnreadable() {
-        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
-        when(mVolume.isMountedReadable()).thenReturn(false);
-
-        mController.displayPreference(mPreferenceScreen);
-
-        assertThat(mController.mDocumentsAndOtherPreference.isVisible()).isFalse();
-    }
-
-    @Test
-    public void displayPreference_noEmulatedInternalStorage_hidePreference() {
-        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(null);
-
-        mController.displayPreference(mPreferenceScreen);
-
-        assertThat(mController.mDocumentsAndOtherPreference.isVisible()).isFalse();
-    }
-
-    @Test
     public void setVolume_updateFilePreferenceToHideAfterSettingVolume_hidePreference() {
         when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
         when(mVolume.getType()).thenReturn(VolumeInfo.TYPE_PRIVATE);
@@ -430,7 +411,6 @@
         assertThat(mController.mDocumentsAndOtherPreference.isVisible()).isFalse();
     }
 
-
     @Test
     public void setVolume_updateFilePreferenceToShowAfterSettingVolume_showPreference() {
         // This will hide it initially.