Merge "Refactor SummaryLoader to set summary on subsetting pages."
This commit is contained in:
committed by
Android (Google) Code Review
commit
36a6cb0370
@@ -48,7 +48,7 @@ import java.util.ArrayList;
|
||||
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 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
|
||||
recountItems();
|
||||
}
|
||||
|
||||
public boolean isShowingAll() {
|
||||
return mIsShowingAll;
|
||||
}
|
||||
|
||||
public void notifyChanged(Tile tile) {
|
||||
@Override
|
||||
public void notifySummaryChanged(Tile tile) {
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
|
@@ -31,19 +31,9 @@ public interface DashboardFeatureProvider {
|
||||
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.
|
||||
|
@@ -48,18 +48,8 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
||||
}
|
||||
|
||||
@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
|
||||
|
@@ -40,13 +40,15 @@ import java.util.Map;
|
||||
* 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 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
|
||||
@Override
|
||||
public void onCategoriesChanged() {
|
||||
final DashboardCategory category = getDashboardTiles();
|
||||
final DashboardCategory category =
|
||||
mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
|
||||
if (category == null) {
|
||||
return;
|
||||
}
|
||||
@@ -73,11 +76,12 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
@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;
|
||||
@@ -85,6 +89,19 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
@@ -100,6 +117,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
mSummaryLoader.setListening(false);
|
||||
if (mListeningToCategoryChange) {
|
||||
final Activity activity = getActivity();
|
||||
if (activity instanceof SettingsDrawerActivity) {
|
||||
@@ -119,9 +137,9 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
*/
|
||||
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 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
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)) {
|
||||
|
@@ -38,6 +38,7 @@ import com.android.settings.dashboard.conditional.ConditionManager;
|
||||
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 @@ public class DashboardSummary extends InstrumentedFragment
|
||||
.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 @@ public class DashboardSummary extends InstrumentedFragment
|
||||
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 @@ public class DashboardSummary extends InstrumentedFragment
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -36,7 +36,6 @@ import com.android.settingslib.drawer.SettingsDrawerActivity;
|
||||
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 @@ public class SummaryLoader {
|
||||
|
||||
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 class SummaryLoader {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ public class SummaryLoader {
|
||||
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 @@ public class SummaryLoader {
|
||||
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 @@ public class SummaryLoader {
|
||||
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 @@ public class SummaryLoader {
|
||||
void setListening(boolean listening);
|
||||
}
|
||||
|
||||
public interface SummaryConsumer {
|
||||
void notifySummaryChanged(Tile tile);
|
||||
}
|
||||
|
||||
public interface SummaryProviderFactory {
|
||||
SummaryProvider createSummaryProvider(Activity activity, SummaryLoader summaryLoader);
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ import com.android.settings.dashboard.DashboardFragment;
|
||||
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 @@ public class StorageDashboardFragment extends DashboardFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DashboardCategory getDashboardTiles() {
|
||||
return mDashboardFeatureProvider.getTilesForStorageCategory();
|
||||
protected String getCategoryKey() {
|
||||
return CategoryKey.CATEGORY_STORAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -25,7 +25,7 @@ import com.android.settings.deviceinfo.SystemUpdatePreferenceController;
|
||||
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 @@ public class SystemDashboardFragment extends DashboardFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DashboardCategory getDashboardTiles() {
|
||||
return mDashboardFeatureProvider.getTilesForSystemCategory();
|
||||
protected String getCategoryKey() {
|
||||
return CategoryKey.CATEGORY_SYSTEM;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -24,6 +24,7 @@ import com.android.settings.TestConfig;
|
||||
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 java.util.List;
|
||||
|
||||
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 @@ public class DashboardFragmentTest {
|
||||
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 @@ public class DashboardFragmentTest {
|
||||
|
||||
@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 @@ public class DashboardFragmentTest {
|
||||
private final Context mContext;
|
||||
@Mock
|
||||
public PreferenceScreen mScreen;
|
||||
public DashboardCategory mCategory;
|
||||
|
||||
|
||||
public TestFragment(Context context) {
|
||||
mContext = context;
|
||||
@@ -152,8 +153,8 @@ public class DashboardFragmentTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DashboardCategory getDashboardTiles() {
|
||||
return mCategory;
|
||||
protected String getCategoryKey() {
|
||||
return CategoryKey.CATEGORY_HOMEPAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user