Insert dynamic dashboard tiles into optional placeholders
Insteads of inserting tile onto screen using absolute priority values, now each page can have a placeholder preference, and at run time we will add dynamic dashboard tiles to placeholder's place. Bug: 32827787 Test: RunSettingsRoboTests Change-Id: I1fe9e11dce4eb6fb4a9b56af05a2b8e5cdae00d2
This commit is contained in:
@@ -27,4 +27,8 @@
|
||||
<intent android:action="android.intent.action.MANAGE_PERMISSIONS"/>
|
||||
</Preference>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="dashboard_tile_placeholder"
|
||||
android:order="10"/>
|
||||
|
||||
</PreferenceScreen>
|
@@ -20,21 +20,25 @@
|
||||
android:key="toggle_nfc"
|
||||
android:title="@string/nfc_quick_toggle_title"
|
||||
android:summary="@string/nfc_quick_toggle_summary"
|
||||
android:order="-5" />
|
||||
android:order="-5"/>
|
||||
|
||||
<com.android.settingslib.RestrictedPreference
|
||||
android:fragment="com.android.settings.nfc.AndroidBeam"
|
||||
android:key="android_beam_settings"
|
||||
android:title="@string/android_beam_settings_title"
|
||||
android:order="-4" />
|
||||
android:order="-4"/>
|
||||
|
||||
<Preference
|
||||
android:key="usb_mode"
|
||||
android:title="@string/usb_pref"
|
||||
android:order="-3" >
|
||||
android:order="-3">
|
||||
<intent android:action="android.intent.action.MAIN"
|
||||
android:targetPackage="com.android.settings"
|
||||
android:targetClass="com.android.settings.deviceinfo.UsbModeChooserActivity"/>
|
||||
android:targetPackage="com.android.settings"
|
||||
android:targetClass="com.android.settings.deviceinfo.UsbModeChooserActivity"/>
|
||||
</Preference>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="dashboard_tile_placeholder"
|
||||
android:order="50"/>
|
||||
|
||||
</PreferenceScreen>
|
@@ -781,8 +781,8 @@ public class SecuritySettings extends SettingsPreferenceFragment
|
||||
final List<Preference> preferences = new ArrayList<>();
|
||||
for (Tile tile : tiles) {
|
||||
final Preference pref = new Preference(getPrefContext());
|
||||
mDashboardFeatureProvider
|
||||
.bindPreferenceToTile(getActivity(), pref, tile, null /* key */);
|
||||
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), pref, tile,
|
||||
null /* key */, Preference.DEFAULT_ORDER/* baseOrder */);
|
||||
preferences.add(pref);
|
||||
}
|
||||
return preferences;
|
||||
|
@@ -63,12 +63,16 @@ public interface DashboardFeatureProvider {
|
||||
* @param pref The preference to bind data
|
||||
* @param tile The binding data
|
||||
* @param key They key for preference. If null, we will generate one from tile data
|
||||
* @param baseOrder The order offset value. When binding, pref's order is determined by
|
||||
* both this value and tile's own priority.
|
||||
*/
|
||||
void bindPreferenceToTile(Activity activity, Preference pref, Tile tile, String key);
|
||||
void bindPreferenceToTile(Activity activity, Preference pref, Tile tile, String key,
|
||||
int baseOrder);
|
||||
|
||||
/**
|
||||
* Returns a {@link ProgressiveDisclosureMixin} for specified fragment.
|
||||
*/
|
||||
ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
|
||||
DashboardFragment fragment);
|
||||
|
||||
}
|
||||
|
@@ -80,7 +80,8 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindPreferenceToTile(Activity activity, Preference pref, Tile tile, String key) {
|
||||
public void bindPreferenceToTile(Activity activity, Preference pref, Tile tile, String key,
|
||||
int baseOrder) {
|
||||
pref.setTitle(tile.title);
|
||||
if (!TextUtils.isEmpty(key)) {
|
||||
pref.setKey(key);
|
||||
@@ -112,11 +113,21 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
final String skipOffsetPackageName = activity.getPackageName();
|
||||
// Use negated priority for order, because tile priority is based on intent-filter
|
||||
// (larger value has higher priority). However pref order defines smaller value has
|
||||
// higher priority.
|
||||
if (tile.priority != 0) {
|
||||
pref.setOrder(-tile.priority);
|
||||
boolean shouldSkipBaseOrderOffset = false;
|
||||
if (tile.intent != null) {
|
||||
shouldSkipBaseOrderOffset = TextUtils.equals(
|
||||
skipOffsetPackageName, tile.intent.getComponent().getPackageName());
|
||||
}
|
||||
if (shouldSkipBaseOrderOffset || baseOrder == Preference.DEFAULT_ORDER) {
|
||||
pref.setOrder(-tile.priority);
|
||||
} else {
|
||||
pref.setOrder(-tile.priority + baseOrder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -62,6 +62,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
|
||||
protected ProgressiveDisclosureMixin mProgressiveDisclosureMixin;
|
||||
protected DashboardFeatureProvider mDashboardFeatureProvider;
|
||||
private DashboardTilePlaceholderPreferenceController mPlaceholderPreferenceController;
|
||||
private boolean mListeningToCategoryChange;
|
||||
private SummaryLoader mSummaryLoader;
|
||||
|
||||
@@ -74,10 +75,13 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
.getProgressiveDisclosureMixin(context, this);
|
||||
getLifecycle().addObserver(mProgressiveDisclosureMixin);
|
||||
|
||||
final List<PreferenceController> controllers = getPreferenceControllers(context);
|
||||
List<PreferenceController> controllers = getPreferenceControllers(context);
|
||||
if (controllers == null) {
|
||||
return;
|
||||
controllers = new ArrayList<>();
|
||||
}
|
||||
mPlaceholderPreferenceController =
|
||||
new DashboardTilePlaceholderPreferenceController(context);
|
||||
controllers.add(mPlaceholderPreferenceController);
|
||||
for (PreferenceController controller : controllers) {
|
||||
addPreferenceController(controller);
|
||||
}
|
||||
@@ -319,12 +323,13 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
// Have the key already, will rebind.
|
||||
final Preference preference = mProgressiveDisclosureMixin.findPreference(
|
||||
screen, key);
|
||||
mDashboardFeatureProvider.bindPreferenceToTile(
|
||||
getActivity(), preference, tile, key);
|
||||
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), preference, tile, key,
|
||||
mPlaceholderPreferenceController.getOrder());
|
||||
} else {
|
||||
// Don't have this key, add it.
|
||||
final Preference pref = new Preference(getPrefContext());
|
||||
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), pref, tile, key);
|
||||
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), pref, tile, key,
|
||||
mPlaceholderPreferenceController.getOrder());
|
||||
mProgressiveDisclosureMixin.addPreference(screen, pref);
|
||||
mDashboardTilePrefKeys.add(key);
|
||||
}
|
||||
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.dashboard;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.PreferenceController;
|
||||
|
||||
/**
|
||||
* PreferenceController for a dashboard_tile_placeholder, a special preference marking where
|
||||
* dynamic dashboard tiles should be injected in a screen. It is optional when building
|
||||
* preference screen in xml. If not present, all dynamic dashboard tiles will be added to the
|
||||
* bottom of page.
|
||||
*/
|
||||
class DashboardTilePlaceholderPreferenceController extends PreferenceController {
|
||||
|
||||
private static final String KEY_PLACEHOLDER = "dashboard_tile_placeholder";
|
||||
|
||||
private int mOrder = Preference.DEFAULT_ORDER;
|
||||
|
||||
public DashboardTilePlaceholderPreferenceController(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
final Preference pref = screen.findPreference(getPreferenceKey());
|
||||
if (pref != null) {
|
||||
mOrder = pref.getOrder();
|
||||
screen.removePreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_PLACEHOLDER;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return mOrder;
|
||||
}
|
||||
}
|
@@ -77,7 +77,7 @@ public class DashboardFeatureProviderImplTest {
|
||||
tile.metaData = new Bundle();
|
||||
tile.metaData.putString(SettingsActivity.META_DATA_KEY_FRAGMENT_CLASS, "HI");
|
||||
tile.priority = 10;
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123");
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", Preference.DEFAULT_ORDER);
|
||||
|
||||
assertThat(preference.getTitle()).isEqualTo(tile.title);
|
||||
assertThat(preference.getSummary()).isEqualTo(tile.summary);
|
||||
@@ -95,7 +95,9 @@ public class DashboardFeatureProviderImplTest {
|
||||
tile.metaData = new Bundle();
|
||||
tile.priority = 10;
|
||||
tile.intent = new Intent();
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123");
|
||||
tile.intent.setComponent(new ComponentName("pkg", "class"));
|
||||
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", Preference.DEFAULT_ORDER);
|
||||
|
||||
assertThat(preference.getFragment()).isNull();
|
||||
assertThat(preference.getOnPreferenceClickListener()).isNotNull();
|
||||
@@ -112,11 +114,12 @@ public class DashboardFeatureProviderImplTest {
|
||||
tile.userHandle.add(mock(UserHandle.class));
|
||||
tile.userHandle.add(mock(UserHandle.class));
|
||||
tile.intent = new Intent();
|
||||
tile.intent.setComponent(new ComponentName("pkg", "class"));
|
||||
|
||||
when(mActivity.getApplicationContext().getSystemService(Context.USER_SERVICE))
|
||||
.thenReturn(mUserManager);
|
||||
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123");
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", Preference.DEFAULT_ORDER);
|
||||
preference.getOnPreferenceClickListener().onPreferenceClick(null);
|
||||
|
||||
verify(mActivity).getFragmentManager();
|
||||
@@ -129,9 +132,23 @@ public class DashboardFeatureProviderImplTest {
|
||||
final Tile tile = new Tile();
|
||||
tile.intent = new Intent();
|
||||
tile.intent.setComponent(new ComponentName("pkg", "class"));
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, null /* key */);
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, null /* key */
|
||||
, Preference.DEFAULT_ORDER);
|
||||
|
||||
assertThat(preference.getKey()).isNotNull();
|
||||
assertThat(preference.getOrder()).isEqualTo(Preference.DEFAULT_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindPreference_withBaseOrder_shouldOffsetPriority() {
|
||||
final int baseOrder = 100;
|
||||
final Preference preference = new Preference(
|
||||
ShadowApplication.getInstance().getApplicationContext());
|
||||
final Tile tile = new Tile();
|
||||
tile.metaData = new Bundle();
|
||||
tile.priority = 10;
|
||||
mImpl.bindPreferenceToTile(mActivity, preference, tile, "123", baseOrder);
|
||||
|
||||
assertThat(preference.getOrder()).isEqualTo(-tile.priority + baseOrder);
|
||||
}
|
||||
}
|
||||
|
@@ -42,9 +42,6 @@ import org.robolectric.shadows.ShadowApplication;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
@@ -122,19 +119,29 @@ public class DashboardFragmentTest {
|
||||
verify(mTestFragment.mScreen, never()).addPreference(any(Preference.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAttach_shouldCreatePlaceholderPreferenceController() {
|
||||
final PreferenceController controller = mTestFragment.getPreferenceController(
|
||||
DashboardTilePlaceholderPreferenceController.class);
|
||||
|
||||
assertThat(controller).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_skipUnavailablePrefs() {
|
||||
List<PreferenceController> preferenceControllers = mTestFragment.mControllers;
|
||||
preferenceControllers.add(mock(PreferenceController.class));
|
||||
preferenceControllers.add(mock(PreferenceController.class));
|
||||
when(preferenceControllers.get(0).isAvailable()).thenReturn(false);
|
||||
when(preferenceControllers.get(1).isAvailable()).thenReturn(true);
|
||||
final List<PreferenceController> preferenceControllers = mTestFragment.mControllers;
|
||||
final PreferenceController mockController1 = mock(PreferenceController.class);
|
||||
final PreferenceController mockController2 = mock(PreferenceController.class);
|
||||
preferenceControllers.add(mockController1);
|
||||
preferenceControllers.add(mockController2);
|
||||
when(mockController1.isAvailable()).thenReturn(false);
|
||||
when(mockController2.isAvailable()).thenReturn(true);
|
||||
|
||||
mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
|
||||
mTestFragment.onResume();
|
||||
|
||||
verify(mTestFragment.mControllers.get(0), never()).getPreferenceKey();
|
||||
verify(mTestFragment.mControllers.get(1)).getPreferenceKey();
|
||||
verify(mockController1, never()).getPreferenceKey();
|
||||
verify(mockController2).getPreferenceKey();
|
||||
}
|
||||
|
||||
public static class TestPreferenceController extends PreferenceController {
|
||||
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.dashboard;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class DashboardTilePlaceholderPreferenceControllerTest {
|
||||
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private PreferenceScreen mScreen;
|
||||
private DashboardTilePlaceholderPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mController = new DashboardTilePlaceholderPreferenceController(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void display_hasPlaceholderPref_shouldUseOrderFromPlaceholder() {
|
||||
final int baseOrder = 15;
|
||||
when(mScreen.findPreference(anyString()).getOrder()).thenReturn(baseOrder);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
assertThat(mController.getOrder()).isEqualTo(baseOrder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void display_noPlaceholderPref_shouldUseDefaultOrder() {
|
||||
when(mScreen.findPreference(anyString())).thenReturn(null);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
assertThat(mController.getOrder()).isEqualTo(Preference.DEFAULT_ORDER);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user