Merge "Refactor DashboardFragment."
This commit is contained in:
committed by
Android (Google) Code Review
commit
ce6affc371
@@ -17,6 +17,7 @@ package com.android.settings.dashboard;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@@ -45,6 +46,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
new ArrayMap<>();
|
new ArrayMap<>();
|
||||||
|
|
||||||
protected DashboardFeatureProvider mDashboardFeatureProvider;
|
protected DashboardFeatureProvider mDashboardFeatureProvider;
|
||||||
|
private boolean mListeningToCategoryChange;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
@@ -53,11 +55,32 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
FeatureFactory.getFactory(context).getDashboardFeatureProvider(context);
|
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
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
|
final DashboardCategory category = getDashboardTiles();
|
||||||
|
if (category == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
if (activity instanceof SettingsDrawerActivity) {
|
if (activity instanceof SettingsDrawerActivity) {
|
||||||
|
mListeningToCategoryChange = true;
|
||||||
((SettingsDrawerActivity) activity).addCategoryListener(this);
|
((SettingsDrawerActivity) activity).addCategoryListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,9 +100,12 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
final Activity activity = getActivity();
|
if (mListeningToCategoryChange) {
|
||||||
if (activity instanceof SettingsDrawerActivity) {
|
final Activity activity = getActivity();
|
||||||
((SettingsDrawerActivity) activity).remCategoryListener(this);
|
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);
|
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 Context context = getContext();
|
||||||
|
final DashboardCategory category = getDashboardTiles();
|
||||||
|
if (category == null) {
|
||||||
|
Log.d(TAG, "NO dynamic tiles for " + TAG);
|
||||||
|
return;
|
||||||
|
}
|
||||||
List<Tile> tiles = category.tiles;
|
List<Tile> tiles = category.tiles;
|
||||||
if (tiles == null) {
|
if (tiles == null) {
|
||||||
Log.d(TAG, "tile list is empty, skipping category " + category.title);
|
Log.d(TAG, "tile list is empty, skipping category " + category.title);
|
||||||
@@ -123,4 +168,19 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
screen.addPreference(pref);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -80,8 +80,7 @@ public class SummaryLoader {
|
|||||||
mWorker = new Worker(mWorkerThread.getLooper());
|
mWorker = new Worker(mWorkerThread.getLooper());
|
||||||
mActivity = activity;
|
mActivity = activity;
|
||||||
List<Tile> tiles = categories.tiles;
|
List<Tile> tiles = categories.tiles;
|
||||||
for (int j = 0; j < tiles.size(); j++) {
|
for (Tile tile :tiles) {
|
||||||
Tile tile = tiles.get(j);
|
|
||||||
mWorker.obtainMessage(Worker.MSG_GET_PROVIDER, tile).sendToTarget();
|
mWorker.obtainMessage(Worker.MSG_GET_PROVIDER, tile).sendToTarget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,16 +17,14 @@
|
|||||||
package com.android.settings.deviceinfo;
|
package com.android.settings.deviceinfo;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.UserManager;
|
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settings.search.BaseSearchIndexProvider;
|
import com.android.settings.search.BaseSearchIndexProvider;
|
||||||
import com.android.settings.search.Indexable;
|
import com.android.settings.search.Indexable;
|
||||||
|
import com.android.settingslib.drawer.DashboardCategory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -41,6 +39,11 @@ public class StorageDashboardFragment extends DashboardFragment {
|
|||||||
return STORAGE_CATEGORY_FRAGMENT;
|
return STORAGE_CATEGORY_FRAGMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
@@ -48,28 +51,16 @@ public class StorageDashboardFragment extends DashboardFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
protected DashboardCategory getDashboardTiles() {
|
||||||
super.onCreatePreferences(savedInstanceState, rootKey);
|
return mDashboardFeatureProvider.getTilesForStorageCategory();
|
||||||
refreshAllPreferences();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCategoriesChanged() {
|
protected void displayResourceTiles() {
|
||||||
refreshAllPreferences();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void refreshAllPreferences() {
|
|
||||||
PreferenceScreen screen = getPreferenceScreen();
|
|
||||||
if (screen != null) {
|
|
||||||
screen.removeAll();
|
|
||||||
}
|
|
||||||
addPreferencesFromResource(R.xml.storage_dashboard_fragment);
|
addPreferencesFromResource(R.xml.storage_dashboard_fragment);
|
||||||
|
|
||||||
getPreferenceController(ManageStoragePreferenceController.class)
|
getPreferenceController(ManageStoragePreferenceController.class)
|
||||||
.displayPreference(getPreferenceScreen());
|
.displayPreference(getPreferenceScreen());
|
||||||
|
|
||||||
displayTilesAsPreference(TAG, getPreferenceScreen(),
|
|
||||||
mDashboardFeatureProvider.getTilesForStorageCategory());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -16,10 +16,8 @@
|
|||||||
package com.android.settings.system;
|
package com.android.settings.system;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
@@ -27,15 +25,14 @@ import com.android.settings.deviceinfo.SystemUpdatePreferenceController;
|
|||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settings.search.BaseSearchIndexProvider;
|
import com.android.settings.search.BaseSearchIndexProvider;
|
||||||
import com.android.settings.search.Indexable;
|
import com.android.settings.search.Indexable;
|
||||||
import com.android.settingslib.drawer.SettingsDrawerActivity;
|
import com.android.settingslib.drawer.DashboardCategory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class SystemDashboardFragment extends DashboardFragment
|
public class SystemDashboardFragment extends DashboardFragment {
|
||||||
implements SettingsDrawerActivity.CategoryListener, Indexable {
|
|
||||||
|
|
||||||
private static final String TAG = "SystemDashboardFrag";
|
private static final String TAG = "SystemDashboardFrag";
|
||||||
|
|
||||||
@@ -44,6 +41,11 @@ public class SystemDashboardFragment extends DashboardFragment
|
|||||||
return SYSTEM_CATEGORY_FRAGMENT;
|
return SYSTEM_CATEGORY_FRAGMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
@@ -52,32 +54,16 @@ public class SystemDashboardFragment extends DashboardFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
protected void displayResourceTiles() {
|
||||||
super.onCreatePreferences(savedInstanceState, rootKey);
|
|
||||||
refreshAllPreferences();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCategoriesChanged() {
|
|
||||||
refreshAllPreferences();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Refresh preference items using system category dashboard items.
|
|
||||||
*/
|
|
||||||
private void refreshAllPreferences() {
|
|
||||||
PreferenceScreen screen = getPreferenceScreen();
|
|
||||||
if (screen != null) {
|
|
||||||
screen.removeAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.system_dashboard_fragment);
|
addPreferencesFromResource(R.xml.system_dashboard_fragment);
|
||||||
|
|
||||||
getPreferenceController(SystemUpdatePreferenceController.class)
|
getPreferenceController(SystemUpdatePreferenceController.class)
|
||||||
.displayPreference(getPreferenceScreen());
|
.displayPreference(getPreferenceScreen());
|
||||||
|
}
|
||||||
|
|
||||||
displayTilesAsPreference(TAG, getPreferenceScreen(),
|
@Override
|
||||||
mDashboardFeatureProvider.getTilesForSystemCategory());
|
protected DashboardCategory getDashboardTiles() {
|
||||||
|
return mDashboardFeatureProvider.getTilesForSystemCategory();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
package com.android.settings.dashboard;
|
package com.android.settings.dashboard;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -55,8 +57,6 @@ public class DashboardFragmentTest {
|
|||||||
private DashboardCategory mDashboardCategory;
|
private DashboardCategory mDashboardCategory;
|
||||||
@Mock
|
@Mock
|
||||||
private FakeFeatureFactory mFakeFeatureFactory;
|
private FakeFeatureFactory mFakeFeatureFactory;
|
||||||
@Mock
|
|
||||||
private PreferenceScreen mScreen;
|
|
||||||
private TestFragment mTestFragment;
|
private TestFragment mTestFragment;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -68,6 +68,7 @@ public class DashboardFragmentTest {
|
|||||||
mDashboardCategory.tiles.add(new Tile());
|
mDashboardCategory.tiles.add(new Tile());
|
||||||
mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
|
mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
|
||||||
mTestFragment.onAttach(mContext);
|
mTestFragment.onAttach(mContext);
|
||||||
|
mTestFragment.mCategory = mDashboardCategory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -85,24 +86,24 @@ public class DashboardFragmentTest {
|
|||||||
public void displayTilesAsPreference_shouldAddTilesWithIntent() {
|
public void displayTilesAsPreference_shouldAddTilesWithIntent() {
|
||||||
when(mFakeFeatureFactory.dashboardFeatureProvider.getDashboardKeyForTile(any(Tile.class)))
|
when(mFakeFeatureFactory.dashboardFeatureProvider.getDashboardKeyForTile(any(Tile.class)))
|
||||||
.thenReturn("test_key");
|
.thenReturn("test_key");
|
||||||
mTestFragment.displayTilesAsPreference("TEST_FRAGMENT", mScreen, mDashboardCategory);
|
mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
|
||||||
|
|
||||||
verify(mScreen).addPreference(any(DashboardTilePreference.class));
|
verify(mTestFragment.mScreen).addPreference(any(DashboardTilePreference.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayTilesAsPreference_shouldNotAddTilesWithoutIntent() {
|
public void displayTilesAsPreference_shouldNotAddTilesWithoutIntent() {
|
||||||
mTestFragment.displayTilesAsPreference("TEST_FRAGMENT", mScreen, mDashboardCategory);
|
mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
|
||||||
|
|
||||||
verify(mScreen, never()).addPreference(any(DashboardTilePreference.class));
|
verify(mTestFragment.mScreen, never()).addPreference(any(DashboardTilePreference.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles() {
|
public void displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles() {
|
||||||
mDashboardCategory.tiles = null;
|
mTestFragment.mCategory.tiles = null;
|
||||||
mTestFragment.displayTilesAsPreference("TEST_FRAGMENT", mScreen, mDashboardCategory);
|
mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
|
||||||
|
|
||||||
verify(mScreen, never()).addPreference(any(DashboardTilePreference.class));
|
verify(mTestFragment.mScreen, never()).addPreference(any(DashboardTilePreference.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TestPreferenceController extends PreferenceController {
|
public static class TestPreferenceController extends PreferenceController {
|
||||||
@@ -130,9 +131,14 @@ public class DashboardFragmentTest {
|
|||||||
public static class TestFragment extends DashboardFragment {
|
public static class TestFragment extends DashboardFragment {
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
@Mock
|
||||||
|
public PreferenceScreen mScreen;
|
||||||
|
public DashboardCategory mCategory;
|
||||||
|
|
||||||
|
|
||||||
public TestFragment(Context context) {
|
public TestFragment(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
mScreen = mock(PreferenceScreen.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -146,8 +152,22 @@ public class DashboardFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCategoriesChanged() {
|
protected DashboardCategory getDashboardTiles() {
|
||||||
|
return mCategory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void displayResourceTiles() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreferenceScreen getPreferenceScreen() {
|
||||||
|
return mScreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return "TEST_FRAG";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user