Refactor DashboardFragment.

Merged refreshAllPreferences into DashboardFragment. This hopefully
makes it more modular to manage preference display logic in each
dashboardFragment, and makes it more efficient to monitor category
changes.

Now subclasses needs to implement 2 methods:
- displayResourceTiles(): for 'static' preferences from xml
- getDashboardTiles(): returns a list of dashboard tiles and superclass
  will wire it up to preference screen.

If getDashboardTiles() return null (aka no dashboardCategory available),
the fragment will not attempt to monitor category change. The edge case
is that if a package starts to provide a tile for this category, we will
not be notified. I have not seen this case coming up. If we indeed need
to handle this case, the category listener needs to have a way to
monitor specific category rather than globally.

Bug: 31781480
Test: make RunSettingsRoboTests -j40
Change-Id: Ia9f9541b95816214df0d0bb27e3e41078c36c5ca
This commit is contained in:
Fan Zhang
2016-10-07 08:38:48 -07:00
parent cb054fff54
commit 36924659f5
5 changed files with 117 additions and 61 deletions

View File

@@ -17,6 +17,7 @@ package com.android.settings.dashboard;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
@@ -45,6 +46,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
new ArrayMap<>();
protected DashboardFeatureProvider mDashboardFeatureProvider;
private boolean mListeningToCategoryChange;
@Override
public void onAttach(Context context) {
@@ -53,11 +55,32 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
FeatureFactory.getFactory(context).getDashboardFeatureProvider(context);
}
@Override
public void onCategoriesChanged() {
final DashboardCategory category = getDashboardTiles();
if (category == null) {
return;
}
refreshAllPreferences(getLogTag());
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
refreshAllPreferences(getLogTag());
}
@Override
public void onStart() {
super.onStart();
final DashboardCategory category = getDashboardTiles();
if (category == null) {
return;
}
final Activity activity = getActivity();
if (activity instanceof SettingsDrawerActivity) {
mListeningToCategoryChange = true;
((SettingsDrawerActivity) activity).addCategoryListener(this);
}
}
@@ -77,9 +100,12 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
@Override
public void onStop() {
super.onStop();
final Activity activity = getActivity();
if (activity instanceof SettingsDrawerActivity) {
((SettingsDrawerActivity) activity).remCategoryListener(this);
if (mListeningToCategoryChange) {
final Activity activity = getActivity();
if (activity instanceof SettingsDrawerActivity) {
((SettingsDrawerActivity) activity).remCategoryListener(this);
}
mListeningToCategoryChange = false;
}
}
@@ -92,9 +118,28 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
mPreferenceControllers.put(controller.getClass(), controller);
}
protected final void displayTilesAsPreference(String TAG, PreferenceScreen screen,
DashboardCategory category) {
/**
* Returns {@link DashboardCategory} for this fragment.
*/
protected abstract DashboardCategory getDashboardTiles();
/**
* Displays resource based tiles.
*/
protected abstract void displayResourceTiles();
protected abstract String getLogTag();
/**
* Displays dashboard tiles as preference.
*/
private final void displayDashboardTiles(final String TAG, PreferenceScreen screen) {
final Context context = getContext();
final DashboardCategory category = getDashboardTiles();
if (category == null) {
Log.d(TAG, "NO dynamic tiles for " + TAG);
return;
}
List<Tile> tiles = category.tiles;
if (tiles == null) {
Log.d(TAG, "tile list is empty, skipping category " + category.title);
@@ -123,4 +168,19 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
screen.addPreference(pref);
}
}
/**
* Refresh preference items using system category dashboard items.
*/
private void refreshAllPreferences(final String TAG) {
// First remove old preferences.
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
screen.removeAll();
}
// Add resource based tiles.
displayResourceTiles();
// Add dashboard tiles.
displayDashboardTiles(TAG, getPreferenceScreen());
}
}