Delay showing the loading progress spinner in Managed App.

In warm load, the app list is populated much faster, and we should not
show the spinner if we can show the app list immediately.

Adding 100ms delay in showing the loading container. If the app list is
ready, show the list container directly without first showing the
loading container.

Change-Id: I8b3ea88d2382ece9226d5c7734fd1aa406160cf2
Fix: 36375109
Test: make RunSettingsRoboTests
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 15e4eb3..cfa8377 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -943,6 +943,8 @@
         return result;
     }
 
+    // TODO: move this out of Utils to a mixin or a controller or a helper class.
+    @Deprecated
     public static void handleLoadingContainer(View loading, View doneLoading, boolean done,
             boolean animate) {
         setViewShown(loading, !done, animate);
diff --git a/src/com/android/settings/applications/ManageApplications.java b/src/com/android/settings/applications/ManageApplications.java
index d6ae7b2..07632da 100644
--- a/src/com/android/settings/applications/ManageApplications.java
+++ b/src/com/android/settings/applications/ManageApplications.java
@@ -346,7 +346,6 @@
 
         mRootView = inflater.inflate(R.layout.manage_applications_apps, null);
         mLoadingContainer = mRootView.findViewById(R.id.loading_container);
-        mLoadingContainer.setVisibility(View.VISIBLE);
         mListContainer = mRootView.findViewById(R.id.list_container);
         if (mListContainer != null) {
             // Create adapter and list view here
@@ -395,7 +394,8 @@
         return mRootView;
     }
 
-    private void createHeader() {
+    @VisibleForTesting
+    void createHeader() {
         Activity activity = getActivity();
         FrameLayout pinnedHeader = (FrameLayout) mRootView.findViewById(R.id.pinned_header);
         mSpinnerHeader = activity.getLayoutInflater()
@@ -834,6 +834,10 @@
     static class ApplicationsAdapter extends BaseAdapter implements Filterable,
             ApplicationsState.Callbacks, AppStateBaseBridge.Callback,
             AbsListView.RecyclerListener, SectionIndexer {
+
+        // how long to wait for app list to populate without showing the loading container
+        private static final long DELAY_SHOW_LOADING_CONTAINER_THRESHOLD_MS = 100L;
+
         private static final SectionInfo[] EMPTY_SECTIONS = new SectionInfo[0];
 
         private final ApplicationsState mState;
@@ -889,6 +893,13 @@
             }
         };
 
+        private Runnable mShowLoadingContainerRunnable = new Runnable() {
+            public void run() {
+                Utils.handleLoadingContainer(mManageApplications.mLoadingContainer,
+                        mManageApplications.mListContainer, false /* done */, false /* animate */);
+            }
+        };
+
         public ApplicationsAdapter(ApplicationsState state, ManageApplications manageApplications,
                 int filterMode) {
             mState = state;
@@ -1097,6 +1108,9 @@
 
             if (mSession.getAllApps().size() != 0
                     && mManageApplications.mListContainer.getVisibility() != View.VISIBLE) {
+                // Cancel any pending task to show the loading animation and show the list of
+                // apps directly.
+                mFgHandler.removeCallbacks(mShowLoadingContainerRunnable);
                 Utils.handleLoadingContainer(mManageApplications.mLoadingContainer,
                         mManageApplications.mListContainer, true, true);
             }
@@ -1148,10 +1162,16 @@
             }
         }
 
-        private void updateLoading() {
-            Utils.handleLoadingContainer(mManageApplications.mLoadingContainer,
-                    mManageApplications.mListContainer,
-                    mHasReceivedLoadEntries && mSession.getAllApps().size() != 0, false);
+        @VisibleForTesting
+        void updateLoading() {
+            final boolean appLoaded = mHasReceivedLoadEntries && mSession.getAllApps().size() != 0;
+            if (appLoaded) {
+                Utils.handleLoadingContainer(mManageApplications.mLoadingContainer,
+                        mManageApplications.mListContainer, true /* done */, false /* animate */);
+            } else {
+                mFgHandler.postDelayed(
+                        mShowLoadingContainerRunnable, DELAY_SHOW_LOADING_CONTAINER_THRESHOLD_MS);
+            }
         }
 
         ArrayList<ApplicationsState.AppEntry> applyPrefixFilter(CharSequence prefix,