Merge "Refactor SummaryLoader to set summary on subsetting pages."
diff --git a/src/com/android/settings/dashboard/DashboardAdapter.java b/src/com/android/settings/dashboard/DashboardAdapter.java
index a054e4d..f501dfa 100644
--- a/src/com/android/settings/dashboard/DashboardAdapter.java
+++ b/src/com/android/settings/dashboard/DashboardAdapter.java
@@ -48,7 +48,7 @@
 import java.util.List;
 
 public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.DashboardItemHolder>
-        implements View.OnClickListener {
+        implements View.OnClickListener, SummaryLoader.SummaryConsumer {
     public static final String TAG = "DashboardAdapter";
     private static final String STATE_SUGGESTION_LIST = "suggestion_list";
     private static final String STATE_CATEGORY_LIST = "category_list";
@@ -141,11 +141,8 @@
         recountItems();
     }
 
-    public boolean isShowingAll() {
-        return mIsShowingAll;
-    }
-
-    public void notifyChanged(Tile tile) {
+    @Override
+    public void notifySummaryChanged(Tile tile) {
         notifyDataSetChanged();
     }
 
diff --git a/src/com/android/settings/dashboard/DashboardFeatureProvider.java b/src/com/android/settings/dashboard/DashboardFeatureProvider.java
index ede8d81..51efdc9 100644
--- a/src/com/android/settings/dashboard/DashboardFeatureProvider.java
+++ b/src/com/android/settings/dashboard/DashboardFeatureProvider.java
@@ -31,19 +31,9 @@
     boolean isEnabled();
 
     /**
-     * Get tiles (wrapped in {@link DashboardCategory}) for homepage.
+     * Get tiles (wrapped in {@link DashboardCategory}) for key defined in CategoryKey.
      */
-    DashboardCategory getTilesForHomepage();
-
-    /**
-     * Get tiles (wrapped in {@link DashboardCategory}) for storage category.
-     */
-    DashboardCategory getTilesForStorageCategory();
-
-    /**
-     * Get tiles (wrapped in {@link DashboardCategory}) for system category.
-     */
-    DashboardCategory getTilesForSystemCategory();
+    DashboardCategory getTilesForCategory(String key);
 
     /**
      * Get all tiles, grouped by category.
diff --git a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
index 4f7e84c..ca0236e 100644
--- a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
@@ -48,18 +48,8 @@
     }
 
     @Override
-    public DashboardCategory getTilesForHomepage() {
-        return mCategoryManager.getTilesByCategory(mContext, CategoryKey.CATEGORY_HOMEPAGE);
-    }
-
-    @Override
-    public DashboardCategory getTilesForStorageCategory() {
-        return mCategoryManager.getTilesByCategory(mContext, CategoryKey.CATEGORY_STORAGE);
-    }
-
-    @Override
-    public DashboardCategory getTilesForSystemCategory() {
-        return mCategoryManager.getTilesByCategory(mContext, CategoryKey.CATEGORY_SYSTEM);
+    public DashboardCategory getTilesForCategory(String key) {
+        return mCategoryManager.getTilesByCategory(mContext, key);
     }
 
     @Override
diff --git a/src/com/android/settings/dashboard/DashboardFragment.java b/src/com/android/settings/dashboard/DashboardFragment.java
index 1388be7..0aee0b5 100644
--- a/src/com/android/settings/dashboard/DashboardFragment.java
+++ b/src/com/android/settings/dashboard/DashboardFragment.java
@@ -40,13 +40,15 @@
  * Base fragment for dashboard style UI containing a list of static and dynamic setting items.
  */
 public abstract class DashboardFragment extends SettingsPreferenceFragment
-        implements SettingsDrawerActivity.CategoryListener, Indexable {
+        implements SettingsDrawerActivity.CategoryListener, Indexable,
+        SummaryLoader.SummaryConsumer {
 
     private final Map<Class, PreferenceController> mPreferenceControllers =
             new ArrayMap<>();
 
     protected DashboardFeatureProvider mDashboardFeatureProvider;
     private boolean mListeningToCategoryChange;
+    private SummaryLoader mSummaryLoader;
 
     @Override
     public void onAttach(Context context) {
@@ -57,7 +59,8 @@
 
     @Override
     public void onCategoriesChanged() {
-        final DashboardCategory category = getDashboardTiles();
+        final DashboardCategory category =
+                mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
         if (category == null) {
             return;
         }
@@ -73,11 +76,12 @@
     @Override
     public void onStart() {
         super.onStart();
-        final DashboardCategory category = getDashboardTiles();
+        final DashboardCategory category =
+                mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
         if (category == null) {
             return;
         }
-
+        mSummaryLoader.setListening(true);
         final Activity activity = getActivity();
         if (activity instanceof SettingsDrawerActivity) {
             mListeningToCategoryChange = true;
@@ -86,6 +90,19 @@
     }
 
     @Override
+    public void notifySummaryChanged(Tile tile) {
+        final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile);
+        final Preference pref = findPreference(key);
+        if (pref == null) {
+            Log.d(getLogTag(),
+                    String.format("Can't find pref by key %s, skipping update summary %s/%s",
+                            key, tile.title, tile.summary));
+            return;
+        }
+        pref.setSummary(tile.summary);
+    }
+
+    @Override
     public boolean onPreferenceTreeClick(Preference preference) {
         Collection<PreferenceController> controllers = mPreferenceControllers.values();
         // Give all controllers a chance to handle click.
@@ -100,6 +117,7 @@
     @Override
     public void onStop() {
         super.onStop();
+        mSummaryLoader.setListening(false);
         if (mListeningToCategoryChange) {
             final Activity activity = getActivity();
             if (activity instanceof SettingsDrawerActivity) {
@@ -119,9 +137,9 @@
     }
 
     /**
-     * Returns {@link DashboardCategory} for this fragment.
+     * Returns the CategoryKey for loading {@link DashboardCategory} for this fragment.
      */
-    protected abstract DashboardCategory getDashboardTiles();
+    protected abstract String getCategoryKey();
 
     /**
      * Displays resource based tiles.
@@ -135,7 +153,8 @@
      */
     private final void displayDashboardTiles(final String TAG, PreferenceScreen screen) {
         final Context context = getContext();
-        final DashboardCategory category = getDashboardTiles();
+        final DashboardCategory category =
+                mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
         if (category == null) {
             Log.d(TAG, "NO dynamic tiles for " + TAG);
             return;
@@ -145,6 +164,13 @@
             Log.d(TAG, "tile list is empty, skipping category " + category.title);
             return;
         }
+        // There are dashboard tiles, so we need to install SummaryLoader.
+        if (mSummaryLoader != null) {
+            mSummaryLoader.release();
+        }
+        mSummaryLoader = new SummaryLoader(getActivity(), getCategoryKey());
+        mSummaryLoader.setSummaryConsumer(this);
+        // Install dashboard tiles.
         for (Tile tile : tiles) {
             final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile);
             if (TextUtils.isEmpty(key)) {
diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java
index 68069dd..754a7bb 100644
--- a/src/com/android/settings/dashboard/DashboardSummary.java
+++ b/src/com/android/settings/dashboard/DashboardSummary.java
@@ -38,6 +38,7 @@
 import com.android.settings.dashboard.conditional.FocusRecyclerView;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.SuggestionParser;
+import com.android.settingslib.drawer.CategoryKey;
 import com.android.settingslib.drawer.DashboardCategory;
 import com.android.settingslib.drawer.SettingsDrawerActivity;
 import com.android.settingslib.drawer.Tile;
@@ -95,8 +96,7 @@
                 .getDashboardFeatureProvider(activity);
 
         if (mDashboardFeatureProvider.isEnabled()) {
-            mSummaryLoader = new SummaryLoader(activity,
-                    mDashboardFeatureProvider.getTilesForHomepage());
+            mSummaryLoader = new SummaryLoader(activity, CategoryKey.CATEGORY_HOMEPAGE);
         } else {
             mSummaryLoader = new SummaryLoader(activity,
                     ((SettingsActivity) getActivity()).getDashboardCategories());
@@ -222,7 +222,7 @@
         mAdapter = new DashboardAdapter(getContext(), mSuggestionParser, mMetricsFeatureProvider,
                 bundle, mConditionManager.getConditions());
         mDashboard.setAdapter(mAdapter);
-        mSummaryLoader.setAdapter(mAdapter);
+        mSummaryLoader.setSummaryConsumer(mAdapter);
         ConditionAdapterUtils.addDismiss(mDashboard);
         if (DEBUG_TIMING) {
             Log.d(TAG, "onViewCreated took "
@@ -297,13 +297,13 @@
                 // Temporary hack to wrap homepage category into a list. Soon we will create adapter
                 // API that takes a single category.
                 List<DashboardCategory> categories = new ArrayList<>();
-                categories.add(mDashboardFeatureProvider.getTilesForHomepage());
+                categories.add(mDashboardFeatureProvider.getTilesForCategory(
+                        CategoryKey.CATEGORY_HOMEPAGE));
                 mAdapter.setCategoriesAndSuggestions(categories, tiles);
             } else {
                 mAdapter.setCategoriesAndSuggestions(
                         ((SettingsActivity) activity).getDashboardCategories(), tiles);
             }
-
         }
     }
 }
diff --git a/src/com/android/settings/dashboard/SummaryLoader.java b/src/com/android/settings/dashboard/SummaryLoader.java
index 9a6c944..ff69a0b 100644
--- a/src/com/android/settings/dashboard/SummaryLoader.java
+++ b/src/com/android/settings/dashboard/SummaryLoader.java
@@ -36,7 +36,6 @@
 import com.android.settingslib.drawer.Tile;
 
 import java.lang.reflect.Field;
-import java.util.ArrayList;
 import java.util.List;
 
 public class SummaryLoader {
@@ -47,18 +46,22 @@
 
     private final Activity mActivity;
     private final ArrayMap<SummaryProvider, ComponentName> mSummaryMap = new ArrayMap<>();
-    private final List<Tile> mTiles = new ArrayList<>();
+    private final DashboardFeatureProvider mDashboardFeatureProvider;
+    private final String mCategoryKey;
 
     private final Worker mWorker;
     private final Handler mHandler;
     private final HandlerThread mWorkerThread;
 
-    private DashboardAdapter mAdapter;
+    private SummaryConsumer mSummaryConsumer;
     private boolean mListening;
     private boolean mWorkerListening;
     private ArraySet<BroadcastReceiver> mReceivers = new ArraySet<>();
 
     public SummaryLoader(Activity activity, List<DashboardCategory> categories) {
+        mDashboardFeatureProvider = FeatureFactory.getFactory(activity)
+                .getDashboardFeatureProvider(activity);
+        mCategoryKey = null;
         mHandler = new Handler();
         mWorkerThread = new HandlerThread("SummaryLoader", Process.THREAD_PRIORITY_BACKGROUND);
         mWorkerThread.start();
@@ -73,14 +76,18 @@
         }
     }
 
-    public SummaryLoader(Activity activity, DashboardCategory categories) {
+    public SummaryLoader(Activity activity, String categoryKey) {
+        mDashboardFeatureProvider = FeatureFactory.getFactory(activity)
+                .getDashboardFeatureProvider(activity);
+        mCategoryKey = categoryKey;
         mHandler = new Handler();
         mWorkerThread = new HandlerThread("SummaryLoader", Process.THREAD_PRIORITY_BACKGROUND);
         mWorkerThread.start();
         mWorker = new Worker(mWorkerThread.getLooper());
         mActivity = activity;
-        List<Tile> tiles = categories.tiles;
-        for (Tile tile :tiles) {
+
+        List<Tile> tiles = mDashboardFeatureProvider.getTilesForCategory(categoryKey).tiles;
+        for (Tile tile : tiles) {
             mWorker.obtainMessage(Worker.MSG_GET_PROVIDER, tile).sendToTarget();
         }
     }
@@ -91,8 +98,8 @@
         setListeningW(false);
     }
 
-    public void setAdapter(DashboardAdapter adapter) {
-        mAdapter = adapter;
+    public void setSummaryConsumer(SummaryConsumer summaryConsumer) {
+        mSummaryConsumer = summaryConsumer;
     }
 
     public void setSummary(SummaryProvider provider, final CharSequence summary) {
@@ -100,21 +107,20 @@
         mHandler.post(new Runnable() {
             @Override
             public void run() {
-                // Since tiles are not always cached (like on locale change for instance),
-                // we need to always get the latest one.
-                if (!(mActivity instanceof SettingsDrawerActivity)) {
-                    if (DEBUG) {
-                        Log.d(TAG, "Can't get category list.");
-                    }
-                    return;
-                }
+
                 final Tile tile;
-                final DashboardFeatureProvider dashboardFeatureProvider =
-                        FeatureFactory.getFactory(mActivity).getDashboardFeatureProvider(mActivity);
-                if (dashboardFeatureProvider.isEnabled()) {
-                    tile = getTileFromCategory(dashboardFeatureProvider.getTilesForHomepage(),
-                            component);
+                if (mDashboardFeatureProvider.isEnabled()) {
+                    tile = getTileFromCategory(
+                            mDashboardFeatureProvider.getTilesForCategory(mCategoryKey), component);
                 } else {
+                    // Since tiles are not always cached (like on locale change for instance),
+                    // we need to always get the latest one.
+                    if (!(mActivity instanceof SettingsDrawerActivity)) {
+                        if (DEBUG) {
+                            Log.d(TAG, "Can't get category list.");
+                        }
+                        return;
+                    }
                     tile = getTileFromCategory(
                             ((SettingsDrawerActivity) mActivity).getDashboardCategories(),
                             component);
@@ -130,7 +136,14 @@
                     Log.d(TAG, "setSummary " + tile.title + " - " + summary);
                 }
                 tile.summary = summary;
-                mAdapter.notifyChanged(tile);
+                if (mSummaryConsumer != null) {
+                    mSummaryConsumer.notifySummaryChanged(tile);
+                } else {
+                    if (DEBUG) {
+                        Log.d(TAG, "SummaryConsumer is null, skipping summary update for "
+                                + tile.title);
+                    }
+                }
             }
         });
     }
@@ -259,6 +272,10 @@
         void setListening(boolean listening);
     }
 
+    public interface SummaryConsumer {
+        void notifySummaryChanged(Tile tile);
+    }
+
     public interface SummaryProviderFactory {
         SummaryProvider createSummaryProvider(Activity activity, SummaryLoader summaryLoader);
     }
diff --git a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
index 92de6b7..7e8d925 100644
--- a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
+++ b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
@@ -24,7 +24,7 @@
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
-import com.android.settingslib.drawer.DashboardCategory;
+import com.android.settingslib.drawer.CategoryKey;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -51,8 +51,8 @@
     }
 
     @Override
-    protected DashboardCategory getDashboardTiles() {
-        return mDashboardFeatureProvider.getTilesForStorageCategory();
+    protected String getCategoryKey() {
+        return CategoryKey.CATEGORY_STORAGE;
     }
 
     @Override
diff --git a/src/com/android/settings/system/SystemDashboardFragment.java b/src/com/android/settings/system/SystemDashboardFragment.java
index a2e9152..e5c5119 100644
--- a/src/com/android/settings/system/SystemDashboardFragment.java
+++ b/src/com/android/settings/system/SystemDashboardFragment.java
@@ -25,7 +25,7 @@
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
-import com.android.settingslib.drawer.DashboardCategory;
+import com.android.settingslib.drawer.CategoryKey;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -62,8 +62,8 @@
     }
 
     @Override
-    protected DashboardCategory getDashboardTiles() {
-        return mDashboardFeatureProvider.getTilesForSystemCategory();
+    protected String getCategoryKey() {
+        return CategoryKey.CATEGORY_SYSTEM;
     }
 
     /**
diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java
index f4371a6..710165a 100644
--- a/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/DashboardFragmentTest.java
@@ -24,6 +24,7 @@
 import com.android.settings.core.PreferenceController;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settingslib.drawer.CategoryKey;
 import com.android.settingslib.drawer.DashboardCategory;
 import com.android.settingslib.drawer.Tile;
 
@@ -42,6 +43,7 @@
 
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -68,7 +70,8 @@
         mDashboardCategory.tiles.add(new Tile());
         mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
         mTestFragment.onAttach(mContext);
-        mTestFragment.mCategory = mDashboardCategory;
+        when(mFakeFeatureFactory.dashboardFeatureProvider.getTilesForCategory(anyString()))
+                .thenReturn(mDashboardCategory);
     }
 
     @Test
@@ -100,7 +103,7 @@
 
     @Test
     public void displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles() {
-        mTestFragment.mCategory.tiles = null;
+        mDashboardCategory.tiles = null;
         mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
 
         verify(mTestFragment.mScreen, never()).addPreference(any(DashboardTilePreference.class));
@@ -133,8 +136,6 @@
         private final Context mContext;
         @Mock
         public PreferenceScreen mScreen;
-        public DashboardCategory mCategory;
-
 
         public TestFragment(Context context) {
             mContext = context;
@@ -152,8 +153,8 @@
         }
 
         @Override
-        protected DashboardCategory getDashboardTiles() {
-            return mCategory;
+        protected String getCategoryKey() {
+            return CategoryKey.CATEGORY_HOMEPAGE;
         }
 
         @Override