From c69f73f4d156fad10b5a30731e46b08e7ae0114d Mon Sep 17 00:00:00 2001 From: Matthew Fritze Date: Tue, 2 Jan 2018 16:39:39 -0800 Subject: [PATCH] Support getDynamicSummary in BasePreferenceController Adds dynamic summary getter in relevant BasePreferenceControllers. Preferece controllers that don't have dynamic summaries or which are not yet BasePreferenceControllers are not changed right now. Change-Id: I435ccab7758d90515583fd8ca10a9b1ef0c858b9 Fixes: 71514936 Test: robotests --- .../AppPermissionsPreferenceController.java | 3 +- ...DefaultAppShortcutPreferenceControllerBase.java | 11 ++++++-- .../DrawOverlayDetailPreferenceController.java | 8 ++---- .../WriteSystemSettingsPreferenceController.java | 9 +++--- ...BackupSettingsActivityPreferenceController.java | 13 +++++---- .../BluetoothDeviceNamePreferenceController.java | 32 ++++++++++++++-------- .../BluetoothDeviceRenamePreferenceController.java | 9 ++++-- .../SystemUpdatePreferenceController.java | 8 ++++-- .../GesturesSettingPreferenceController.java | 22 ++++++++------- .../notification/HeaderPreferenceController.java | 9 +++--- ...ultAppShortcutPreferenceControllerBaseTest.java | 7 +++-- ...upSettingsActivityPreferenceControllerTest.java | 8 +++--- ...luetoothDeviceNamePreferenceControllerTest.java | 7 +++-- ...etoothDeviceRenamePreferenceControllerTest.java | 8 ++++-- 14 files changed, 93 insertions(+), 61 deletions(-) diff --git a/src/com/android/settings/applications/AppPermissionsPreferenceController.java b/src/com/android/settings/applications/AppPermissionsPreferenceController.java index c8ff381165..610012355b 100644 --- a/src/com/android/settings/applications/AppPermissionsPreferenceController.java +++ b/src/com/android/settings/applications/AppPermissionsPreferenceController.java @@ -75,7 +75,8 @@ public class AppPermissionsPreferenceController extends AbstractPreferenceContro The 3 permissions are the first three from the list which any app has granted: Location, Microphone, Camera, Sms, Contacts, and Phone */ - private String getSummary() { + @Override + public String getSummary() { final Set permissions = getAllPermissionsInGroups(); Set grantedPermissionGroups = getGrantedPermissionGroups(permissions); CharSequence summary = null; diff --git a/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java b/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java index fa67ec8216..873c98ce34 100644 --- a/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java +++ b/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java @@ -14,7 +14,6 @@ package com.android.settings.applications.appinfo; -import android.app.slice.Slice; import android.content.Context; import android.os.Bundle; import android.os.UserManager; @@ -52,7 +51,13 @@ public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePre @Override public void updateState(Preference preference) { - preference.setSummary(isDefaultApp() ? R.string.yes : R.string.no); + preference.setSummary(getSummary()); + } + + @Override + public String getSummary() { + int summaryResId = isDefaultApp() ? R.string.yes : R.string.no; + return mContext.getString(summaryResId); } @Override @@ -69,12 +74,14 @@ public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePre /** * Check whether the app has the default app capability + * * @return true if the app has the default app capability */ protected abstract boolean hasAppCapability(); /** * Check whether the app is the default app + * * @return true if the app is the default app */ protected abstract boolean isDefaultApp(); diff --git a/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java b/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java index 37a9edfff0..02f52b60e3 100644 --- a/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java +++ b/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java @@ -21,7 +21,6 @@ import static android.Manifest.permission.SYSTEM_ALERT_WINDOW; import android.content.Context; import android.content.pm.PackageInfo; import android.os.UserManager; -import android.support.annotation.VisibleForTesting; import android.support.v7.preference.Preference; import com.android.settings.SettingsPreferenceFragment; @@ -61,9 +60,8 @@ public class DrawOverlayDetailPreferenceController extends AppInfoPreferenceCont return DrawOverlayDetails.class; } - @VisibleForTesting - CharSequence getSummary() { - return DrawOverlayDetails.getSummary(mContext, mParent.getAppEntry()); + @Override + public String getSummary() { + return DrawOverlayDetails.getSummary(mContext, mParent.getAppEntry()).toString(); } - } diff --git a/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java b/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java index 2a88d2f6c9..73e7675520 100644 --- a/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java +++ b/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java @@ -21,7 +21,6 @@ import static android.Manifest.permission.WRITE_SETTINGS; import android.content.Context; import android.content.pm.PackageInfo; import android.os.UserManager; -import android.support.annotation.VisibleForTesting; import android.support.v7.preference.Preference; import com.android.settings.SettingsPreferenceFragment; @@ -62,9 +61,9 @@ public class WriteSystemSettingsPreferenceController extends AppInfoPreferenceCo return WriteSettingsDetails.class; } - @VisibleForTesting - CharSequence getSummary() { - return WriteSettingsDetails.getSummary(mContext, mParent.getAppEntry()); - } + @Override + public String getSummary() { + return WriteSettingsDetails.getSummary(mContext, mParent.getAppEntry()).toString(); + } } diff --git a/src/com/android/settings/backup/BackupSettingsActivityPreferenceController.java b/src/com/android/settings/backup/BackupSettingsActivityPreferenceController.java index 7a7530cfc8..dccc310156 100644 --- a/src/com/android/settings/backup/BackupSettingsActivityPreferenceController.java +++ b/src/com/android/settings/backup/BackupSettingsActivityPreferenceController.java @@ -23,8 +23,6 @@ import android.support.v7.preference.Preference; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; -import com.android.settings.core.PreferenceControllerMixin; -import com.android.settingslib.core.AbstractPreferenceController; public class BackupSettingsActivityPreferenceController extends BasePreferenceController { private static final String TAG = "BackupSettingActivityPC"; @@ -49,10 +47,15 @@ public class BackupSettingsActivityPreferenceController extends BasePreferenceCo @Override public void updateState(Preference preference) { + preference.setSummary(getSummary()); + } + + @Override + public String getSummary() { final boolean backupEnabled = mBackupManager.isBackupEnabled(); - preference.setSummary(backupEnabled - ? R.string.accessibility_feature_state_on - : R.string.accessibility_feature_state_off); + return backupEnabled + ? mContext.getString(R.string.accessibility_feature_state_on) + : mContext.getString(R.string.accessibility_feature_state_off); } } \ No newline at end of file diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java b/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java index 2d0ce6021a..bf13e07b06 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java @@ -109,7 +109,19 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr @Override public void updateState(Preference preference) { - updateDeviceName(preference, mLocalAdapter.getName()); + updateDeviceName(preference); + } + + @Override + public String getSummary() { + String deviceName = getDeviceName(); + if (TextUtils.isEmpty(deviceName)) { + return super.getSummary(); + } + + return TextUtils.expandTemplate( + mContext.getText(R.string.bluetooth_device_name_summary), + BidiFormatter.getInstance().unicodeWrap(deviceName)).toString(); } /** @@ -132,18 +144,14 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr * Update device summary with {@code deviceName}, where {@code deviceName} has accent color * * @param preference to set the summary for - * @param deviceName bluetooth device name to show in the summary */ - protected void updateDeviceName(final Preference preference, final String deviceName) { - if (deviceName == null) { - // TODO: show error message in preference subtitle - return; - } - final CharSequence summary = TextUtils.expandTemplate( - mContext.getText(R.string.bluetooth_device_name_summary), - BidiFormatter.getInstance().unicodeWrap(deviceName)); + protected void updateDeviceName(final Preference preference) { preference.setSelectable(false); - preference.setSummary(summary); + preference.setSummary(getSummary()); + } + + protected String getDeviceName() { + return mLocalAdapter.getName(); } /** @@ -158,7 +166,7 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr if (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) { if (mPreference != null && mLocalAdapter != null && mLocalAdapter.isEnabled()) { - updateDeviceName(mPreference, mLocalAdapter.getName()); + updateDeviceName(mPreference); } } } diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java b/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java index 69eefcf2d2..a12d1a8ecb 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java @@ -67,8 +67,13 @@ public class BluetoothDeviceRenamePreferenceController extends } @Override - protected void updateDeviceName(final Preference preference, final String deviceName) { - preference.setSummary(deviceName); + protected void updateDeviceName(final Preference preference) { + preference.setSummary(getSummary()); + } + + @Override + public String getSummary() { + return getDeviceName(); } @Override diff --git a/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java b/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java index 92c33d86e4..a061f82e6e 100644 --- a/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java +++ b/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java @@ -78,8 +78,12 @@ public class SystemUpdatePreferenceController extends BasePreferenceController { @Override public void updateState(Preference preference) { - preference.setSummary(mContext.getString(R.string.about_summary, - Build.VERSION.RELEASE)); + preference.setSummary(getSummary()); + } + + @Override + public String getSummary() { + return mContext.getString(R.string.about_summary, Build.VERSION.RELEASE); } /** diff --git a/src/com/android/settings/gestures/GesturesSettingPreferenceController.java b/src/com/android/settings/gestures/GesturesSettingPreferenceController.java index 819b12861c..8b2fcc084a 100644 --- a/src/com/android/settings/gestures/GesturesSettingPreferenceController.java +++ b/src/com/android/settings/gestures/GesturesSettingPreferenceController.java @@ -24,7 +24,6 @@ import android.support.v7.preference.Preference; import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; -import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.overlay.FeatureFactory; import com.android.settingslib.core.AbstractPreferenceController; @@ -58,25 +57,28 @@ public class GesturesSettingPreferenceController extends BasePreferenceControlle @Override public void updateState(Preference preference) { + preference.setSummary(getSummary()); + } + + @Override + public String getSummary() { if (!mFeatureProvider.isSensorAvailable(mContext)) { - preference.setSummary(""); - return; + return ""; } final ContentResolver contentResolver = mContext.getContentResolver(); final boolean assistGestureEnabled = Settings.Secure.getInt( contentResolver, Settings.Secure.ASSIST_GESTURE_ENABLED, 1) != 0; final boolean assistGestureSilenceEnabled = Settings.Secure.getInt( contentResolver, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 1) != 0; - final String summary; + if (mFeatureProvider.isSupported(mContext) && assistGestureEnabled) { - summary = mContext.getString( + return mContext.getString( R.string.language_input_gesture_summary_on_with_assist); - } else if (assistGestureSilenceEnabled) { - summary = mContext.getString( + } + if (assistGestureSilenceEnabled) { + return mContext.getString( R.string.language_input_gesture_summary_on_non_assist); - } else { - summary = mContext.getString(R.string.language_input_gesture_summary_off); } - preference.setSummary(summary); + return mContext.getString(R.string.language_input_gesture_summary_off); } } \ No newline at end of file diff --git a/src/com/android/settings/notification/HeaderPreferenceController.java b/src/com/android/settings/notification/HeaderPreferenceController.java index 3d51b25ea7..5ec60c2d93 100644 --- a/src/com/android/settings/notification/HeaderPreferenceController.java +++ b/src/com/android/settings/notification/HeaderPreferenceController.java @@ -77,7 +77,8 @@ public class HeaderPreferenceController extends NotificationPreferenceController : mAppRow.label; } - CharSequence getSummary() { + @Override + public String getSummary() { if (mChannel != null) { if (mChannelGroup != null && mChannelGroup.getGroup() != null && !TextUtils.isEmpty(mChannelGroup.getGroup().getName())) { @@ -87,12 +88,12 @@ public class HeaderPreferenceController extends NotificationPreferenceController summary.append(bidi.unicodeWrap(mContext.getText( R.string.notification_header_divider_symbol_with_spaces))); summary.append(bidi.unicodeWrap(mChannelGroup.getGroup().getName().toString())); - return summary; + return summary.toString(); } else { - return mAppRow.label; + return mAppRow.label.toString(); } } else if (mChannelGroup != null && mChannelGroup.getGroup() != null) { - return mAppRow.label; + return mAppRow.label.toString(); } else { return ""; } diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBaseTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBaseTest.java index e44fdfb207..8667f742a8 100644 --- a/tests/robotests/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBaseTest.java +++ b/tests/robotests/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBaseTest.java @@ -96,8 +96,8 @@ public class DefaultAppShortcutPreferenceControllerBaseTest { mController.isDefault = true; mController.updateState(mPreference); - - verify(mPreference).setSummary(R.string.yes); + String yesString = mContext.getString(R.string.yes); + verify(mPreference).setSummary(yesString); } @Test @@ -106,7 +106,8 @@ public class DefaultAppShortcutPreferenceControllerBaseTest { mController.updateState(mPreference); - verify(mPreference).setSummary(R.string.no); + String noString = mContext.getString(R.string.no); + verify(mPreference).setSummary(noString); } @Test diff --git a/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java index 5fc3ebcae8..0c98ddc986 100644 --- a/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java @@ -77,8 +77,8 @@ public class BackupSettingsActivityPreferenceControllerTest { mBackupEnabled = true; mController.updateState(mBackupPreference); - - verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_on); + String summaryString = mContext.getString(R.string.accessibility_feature_state_on); + verify(mBackupPreference).setSummary(summaryString); } @Test @@ -86,8 +86,8 @@ public class BackupSettingsActivityPreferenceControllerTest { mBackupEnabled = false; mController.updateState(mBackupPreference); - - verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_off); + String summaryString = mContext.getString(R.string.accessibility_feature_state_off); + verify(mBackupPreference).setSummary(summaryString); } @Test diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceControllerTest.java index 2e094e2407..c3515df56c 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceControllerTest.java @@ -63,13 +63,14 @@ public class BluetoothDeviceNamePreferenceControllerTest { doReturn(mContext).when(mPreferenceScreen).getContext(); mPreference = new Preference(mContext); mPreference.setKey(BluetoothDeviceNamePreferenceController.KEY_DEVICE_NAME); - mController = new BluetoothDeviceNamePreferenceController( - mContext, mLocalAdapter); + mController = spy(new BluetoothDeviceNamePreferenceController( + mContext, mLocalAdapter)); + doReturn(DEVICE_NAME).when(mController).getDeviceName(); } @Test public void testUpdateDeviceName_showSummaryWithDeviceName() { - mController.updateDeviceName(mPreference, DEVICE_NAME); + mController.updateDeviceName(mPreference); final CharSequence summary = mPreference.getSummary(); diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java index 62a0d42177..faf906930b 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java @@ -19,6 +19,7 @@ package com.android.settings.bluetooth; 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.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -69,13 +70,14 @@ public class BluetoothDeviceRenamePreferenceControllerTest { mPreference = new Preference(mContext); mPreference.setKey(PREF_KEY); - mController = new BluetoothDeviceRenamePreferenceController( - mContext, PREF_KEY, mFragment, mLocalAdapter); + mController = spy(new BluetoothDeviceRenamePreferenceController( + mContext, PREF_KEY, mFragment, mLocalAdapter)); + doReturn(DEVICE_NAME).when(mController).getDeviceName(); } @Test public void testUpdateDeviceName_showSummaryWithDeviceName() { - mController.updateDeviceName(mPreference, DEVICE_NAME); + mController.updateDeviceName(mPreference); final CharSequence summary = mPreference.getSummary(); -- 2.11.0