Move ad-hoc dynamic tile injection into FeatureProvider.
Ideally a page should use DashboardFragment and it doesn't need to handle adding dynamic tiles manually. This method is only designed for page that are not fully migrated to DashboardFragment yet. Bug: 32623105 Test: RunSettingsRoboTests Change-Id: I0cafcddf9a43b164daea500bade869fada5b3f4e
This commit is contained in:
@@ -66,8 +66,6 @@ import com.android.settingslib.RestrictedLockUtils;
|
|||||||
import com.android.settingslib.RestrictedPreference;
|
import com.android.settingslib.RestrictedPreference;
|
||||||
import com.android.settingslib.RestrictedSwitchPreference;
|
import com.android.settingslib.RestrictedSwitchPreference;
|
||||||
import com.android.settingslib.drawer.CategoryKey;
|
import com.android.settingslib.drawer.CategoryKey;
|
||||||
import com.android.settingslib.drawer.DashboardCategory;
|
|
||||||
import com.android.settingslib.drawer.Tile;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -410,7 +408,8 @@ public class SecuritySettings extends SettingsPreferenceFragment
|
|||||||
Index.getInstance(getActivity())
|
Index.getInstance(getActivity())
|
||||||
.updateFromClassNameResource(SecuritySettings.class.getName(), true, true);
|
.updateFromClassNameResource(SecuritySettings.class.getName(), true, true);
|
||||||
|
|
||||||
final List<Preference> tilePrefs = getDynamicTilesForSecurity();
|
final List<Preference> tilePrefs = mDashboardFeatureProvider.getPreferencesForCategory(
|
||||||
|
getActivity(), getPrefContext(), CategoryKey.CATEGORY_SECURITY);
|
||||||
if (tilePrefs != null && !tilePrefs.isEmpty()) {
|
if (tilePrefs != null && !tilePrefs.isEmpty()) {
|
||||||
for (Preference preference : tilePrefs) {
|
for (Preference preference : tilePrefs) {
|
||||||
root.addPreference(preference);
|
root.addPreference(preference);
|
||||||
@@ -763,31 +762,6 @@ public class SecuritySettings extends SettingsPreferenceFragment
|
|||||||
SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
|
SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Preference> getDynamicTilesForSecurity() {
|
|
||||||
if (!mDashboardFeatureProvider.isEnabled()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final DashboardCategory category =
|
|
||||||
mDashboardFeatureProvider.getTilesForCategory(CategoryKey.CATEGORY_SECURITY);
|
|
||||||
if (category == null) {
|
|
||||||
Log.d(TAG, "NO dashboard tiles for " + TAG);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final List<Tile> tiles = category.tiles;
|
|
||||||
if (tiles == null) {
|
|
||||||
Log.d(TAG, "tile list is empty, skipping category " + category.title);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final List<Preference> preferences = new ArrayList<>();
|
|
||||||
for (Tile tile : tiles) {
|
|
||||||
final Preference pref = new Preference(getPrefContext());
|
|
||||||
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), pref, tile,
|
|
||||||
null /* key */, Preference.DEFAULT_ORDER/* baseOrder */);
|
|
||||||
preferences.add(pref);
|
|
||||||
}
|
|
||||||
return preferences;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object value) {
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
||||||
boolean result = true;
|
boolean result = true;
|
||||||
|
@@ -39,6 +39,19 @@ public interface DashboardFeatureProvider {
|
|||||||
*/
|
*/
|
||||||
DashboardCategory getTilesForCategory(String key);
|
DashboardCategory getTilesForCategory(String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get tiles (wrapped as a list of Preference) for key defined in CategoryKey.
|
||||||
|
*
|
||||||
|
* @param activity Activity hosting the preference
|
||||||
|
* @param context UI context to inflate preference
|
||||||
|
* @param key Value from CategoryKey
|
||||||
|
* @deprecated Pages implementing {@code DashboardFragment} should use
|
||||||
|
* {@link #getTilesForCategory(String)} instead. Using this method will not get the benefit
|
||||||
|
* of auto-ordering, progressive disclosure, auto-refreshing summary text etc.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
List<Preference> getPreferencesForCategory(Activity activity, Context context, String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all tiles, grouped by category.
|
* Get all tiles, grouped by category.
|
||||||
*/
|
*/
|
||||||
@@ -75,4 +88,5 @@ public interface DashboardFeatureProvider {
|
|||||||
ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
|
ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
|
||||||
DashboardFragment fragment);
|
DashboardFragment fragment);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -21,8 +21,10 @@ import android.content.ComponentName;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settingslib.drawer.CategoryManager;
|
import com.android.settingslib.drawer.CategoryManager;
|
||||||
@@ -30,6 +32,7 @@ import com.android.settingslib.drawer.DashboardCategory;
|
|||||||
import com.android.settingslib.drawer.ProfileSelectDialog;
|
import com.android.settingslib.drawer.ProfileSelectDialog;
|
||||||
import com.android.settingslib.drawer.Tile;
|
import com.android.settingslib.drawer.Tile;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,6 +40,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
||||||
|
|
||||||
|
private static final String TAG = "DashboardFeatureImpl";
|
||||||
|
|
||||||
private static final String DASHBOARD_TILE_PREF_KEY_PREFIX = "dashboard_tile_pref_";
|
private static final String DASHBOARD_TILE_PREF_KEY_PREFIX = "dashboard_tile_pref_";
|
||||||
|
|
||||||
protected final Context mContext;
|
protected final Context mContext;
|
||||||
@@ -44,8 +49,13 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
|||||||
private final CategoryManager mCategoryManager;
|
private final CategoryManager mCategoryManager;
|
||||||
|
|
||||||
public DashboardFeatureProviderImpl(Context context) {
|
public DashboardFeatureProviderImpl(Context context) {
|
||||||
mContext = context.getApplicationContext();
|
this(context.getApplicationContext(), CategoryManager.get(context));
|
||||||
mCategoryManager = CategoryManager.get(mContext);
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
DashboardFeatureProviderImpl(Context context, CategoryManager categoryManager) {
|
||||||
|
mContext = context;
|
||||||
|
mCategoryManager = categoryManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -58,6 +68,32 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
|||||||
return mCategoryManager.getTilesByCategory(mContext, key);
|
return mCategoryManager.getTilesByCategory(mContext, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Preference> getPreferencesForCategory(Activity activity, Context context,
|
||||||
|
String key) {
|
||||||
|
if (!isEnabled()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final DashboardCategory category = getTilesForCategory(key);
|
||||||
|
if (category == null) {
|
||||||
|
Log.d(TAG, "NO dashboard tiles for " + TAG);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final List<Tile> tiles = category.tiles;
|
||||||
|
if (tiles == null || tiles.isEmpty()) {
|
||||||
|
Log.d(TAG, "tile list is empty, skipping category " + category.title);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final List<Preference> preferences = new ArrayList<>();
|
||||||
|
for (Tile tile : tiles) {
|
||||||
|
final Preference pref = new Preference(context);
|
||||||
|
bindPreferenceToTile(activity, pref, tile, null /* key */,
|
||||||
|
Preference.DEFAULT_ORDER /* baseOrder */);
|
||||||
|
preferences.add(pref);
|
||||||
|
}
|
||||||
|
return preferences;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<DashboardCategory> getAllCategories() {
|
public List<DashboardCategory> getAllCategories() {
|
||||||
return mCategoryManager.getCategories(mContext);
|
return mCategoryManager.getCategories(mContext);
|
||||||
|
@@ -30,6 +30,9 @@ import android.support.v7.preference.Preference;
|
|||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settingslib.drawer.CategoryKey;
|
||||||
|
import com.android.settingslib.drawer.CategoryManager;
|
||||||
|
import com.android.settingslib.drawer.DashboardCategory;
|
||||||
import com.android.settingslib.drawer.Tile;
|
import com.android.settingslib.drawer.Tile;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -45,6 +48,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -54,9 +58,10 @@ public class DashboardFeatureProviderImplTest {
|
|||||||
|
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private Activity mActivity;
|
private Activity mActivity;
|
||||||
|
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
|
@Mock
|
||||||
|
private CategoryManager mCategoryManager;
|
||||||
|
|
||||||
private DashboardFeatureProviderImpl mImpl;
|
private DashboardFeatureProviderImpl mImpl;
|
||||||
|
|
||||||
@@ -151,4 +156,53 @@ public class DashboardFeatureProviderImplTest {
|
|||||||
|
|
||||||
assertThat(preference.getOrder()).isEqualTo(-tile.priority + baseOrder);
|
assertThat(preference.getOrder()).isEqualTo(-tile.priority + baseOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getPreferences_notEnabled_shouldReturnNull() {
|
||||||
|
final DashboardFeatureProviderImpl mSpy = spy(mImpl);
|
||||||
|
when(mSpy.isEnabled()).thenReturn(false);
|
||||||
|
|
||||||
|
assertThat(mSpy.getPreferencesForCategory(null, null, CategoryKey.CATEGORY_HOMEPAGE))
|
||||||
|
.isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getPreferences_noCategory_shouldReturnNull() {
|
||||||
|
mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
|
||||||
|
final DashboardFeatureProviderImpl mSpy = spy(mImpl);
|
||||||
|
when(mSpy.isEnabled()).thenReturn(true);
|
||||||
|
when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
|
||||||
|
.thenReturn(null);
|
||||||
|
|
||||||
|
assertThat(mSpy.getPreferencesForCategory(null, null, CategoryKey.CATEGORY_HOMEPAGE))
|
||||||
|
.isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getPreferences_noTileForCategory_shouldReturnNull() {
|
||||||
|
mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
|
||||||
|
final DashboardFeatureProviderImpl mSpy = spy(mImpl);
|
||||||
|
when(mSpy.isEnabled()).thenReturn(true);
|
||||||
|
when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
|
||||||
|
.thenReturn(new DashboardCategory());
|
||||||
|
|
||||||
|
assertThat(mSpy.getPreferencesForCategory(null, null, CategoryKey.CATEGORY_HOMEPAGE))
|
||||||
|
.isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getPreferences_hasTileForCategory_shouldReturnPrefList() {
|
||||||
|
mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
|
||||||
|
final DashboardFeatureProviderImpl mSpy = spy(mImpl);
|
||||||
|
when(mSpy.isEnabled()).thenReturn(true);
|
||||||
|
final DashboardCategory category = new DashboardCategory();
|
||||||
|
category.tiles.add(new Tile());
|
||||||
|
when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
|
||||||
|
.thenReturn(category);
|
||||||
|
|
||||||
|
assertThat(mSpy.getPreferencesForCategory(mActivity,
|
||||||
|
ShadowApplication.getInstance().getApplicationContext(),
|
||||||
|
CategoryKey.CATEGORY_HOMEPAGE).isEmpty())
|
||||||
|
.isFalse();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user