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
This commit is contained in:
Matthew Fritze
2018-01-02 16:39:39 -08:00
parent ce3633be80
commit c69f73f4d1
14 changed files with 94 additions and 62 deletions

View File

@@ -75,7 +75,8 @@ public class AppPermissionsPreferenceController extends AbstractPreferenceContro
The 3 permissions are the first three from the list which any app has granted: The 3 permissions are the first three from the list which any app has granted:
Location, Microphone, Camera, Sms, Contacts, and Phone Location, Microphone, Camera, Sms, Contacts, and Phone
*/ */
private String getSummary() { @Override
public String getSummary() {
final Set<String> permissions = getAllPermissionsInGroups(); final Set<String> permissions = getAllPermissionsInGroups();
Set<String> grantedPermissionGroups = getGrantedPermissionGroups(permissions); Set<String> grantedPermissionGroups = getGrantedPermissionGroups(permissions);
CharSequence summary = null; CharSequence summary = null;

View File

@@ -14,7 +14,6 @@
package com.android.settings.applications.appinfo; package com.android.settings.applications.appinfo;
import android.app.slice.Slice;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserManager; import android.os.UserManager;
@@ -52,7 +51,13 @@ public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePre
@Override @Override
public void updateState(Preference preference) { 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 @Override
@@ -69,12 +74,14 @@ public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePre
/** /**
* Check whether the app has the default app capability * Check whether the app has the default app capability
*
* @return true if the app has the default app capability * @return true if the app has the default app capability
*/ */
protected abstract boolean hasAppCapability(); protected abstract boolean hasAppCapability();
/** /**
* Check whether the app is the default app * Check whether the app is the default app
*
* @return true if the app is the default app * @return true if the app is the default app
*/ */
protected abstract boolean isDefaultApp(); protected abstract boolean isDefaultApp();

View File

@@ -21,7 +21,6 @@ import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.os.UserManager; import android.os.UserManager;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
@@ -61,9 +60,8 @@ public class DrawOverlayDetailPreferenceController extends AppInfoPreferenceCont
return DrawOverlayDetails.class; return DrawOverlayDetails.class;
} }
@VisibleForTesting @Override
CharSequence getSummary() { public String getSummary() {
return DrawOverlayDetails.getSummary(mContext, mParent.getAppEntry()); return DrawOverlayDetails.getSummary(mContext, mParent.getAppEntry()).toString();
} }
} }

View File

@@ -21,7 +21,6 @@ import static android.Manifest.permission.WRITE_SETTINGS;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.os.UserManager; import android.os.UserManager;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
@@ -62,9 +61,9 @@ public class WriteSystemSettingsPreferenceController extends AppInfoPreferenceCo
return WriteSettingsDetails.class; return WriteSettingsDetails.class;
} }
@VisibleForTesting @Override
CharSequence getSummary() { public String getSummary() {
return WriteSettingsDetails.getSummary(mContext, mParent.getAppEntry()); return WriteSettingsDetails.getSummary(mContext, mParent.getAppEntry()).toString();
}
}
} }

View File

@@ -23,8 +23,6 @@ import android.support.v7.preference.Preference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
public class BackupSettingsActivityPreferenceController extends BasePreferenceController { public class BackupSettingsActivityPreferenceController extends BasePreferenceController {
private static final String TAG = "BackupSettingActivityPC"; private static final String TAG = "BackupSettingActivityPC";
@@ -49,10 +47,15 @@ public class BackupSettingsActivityPreferenceController extends BasePreferenceCo
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
preference.setSummary(getSummary());
}
@Override
public String getSummary() {
final boolean backupEnabled = mBackupManager.isBackupEnabled(); final boolean backupEnabled = mBackupManager.isBackupEnabled();
preference.setSummary(backupEnabled return backupEnabled
? R.string.accessibility_feature_state_on ? mContext.getString(R.string.accessibility_feature_state_on)
: R.string.accessibility_feature_state_off); : mContext.getString(R.string.accessibility_feature_state_off);
} }
} }

View File

@@ -109,7 +109,19 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
@Override @Override
public void updateState(Preference preference) { 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 * Update device summary with {@code deviceName}, where {@code deviceName} has accent color
* *
* @param preference to set the summary for * @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) { protected void updateDeviceName(final Preference preference) {
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));
preference.setSelectable(false); 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 (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
if (mPreference != null && mLocalAdapter != null && mLocalAdapter.isEnabled()) { if (mPreference != null && mLocalAdapter != null && mLocalAdapter.isEnabled()) {
updateDeviceName(mPreference, mLocalAdapter.getName()); updateDeviceName(mPreference);
} }
} }
} }

View File

@@ -67,8 +67,13 @@ public class BluetoothDeviceRenamePreferenceController extends
} }
@Override @Override
protected void updateDeviceName(final Preference preference, final String deviceName) { protected void updateDeviceName(final Preference preference) {
preference.setSummary(deviceName); preference.setSummary(getSummary());
}
@Override
public String getSummary() {
return getDeviceName();
} }
@Override @Override

View File

@@ -78,8 +78,12 @@ public class SystemUpdatePreferenceController extends BasePreferenceController {
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
preference.setSummary(mContext.getString(R.string.about_summary, preference.setSummary(getSummary());
Build.VERSION.RELEASE)); }
@Override
public String getSummary() {
return mContext.getString(R.string.about_summary, Build.VERSION.RELEASE);
} }
/** /**

View File

@@ -24,7 +24,6 @@ import android.support.v7.preference.Preference;
import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
@@ -58,25 +57,28 @@ public class GesturesSettingPreferenceController extends BasePreferenceControlle
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
preference.setSummary(getSummary());
}
@Override
public String getSummary() {
if (!mFeatureProvider.isSensorAvailable(mContext)) { if (!mFeatureProvider.isSensorAvailable(mContext)) {
preference.setSummary(""); return "";
return;
} }
final ContentResolver contentResolver = mContext.getContentResolver(); final ContentResolver contentResolver = mContext.getContentResolver();
final boolean assistGestureEnabled = Settings.Secure.getInt( final boolean assistGestureEnabled = Settings.Secure.getInt(
contentResolver, Settings.Secure.ASSIST_GESTURE_ENABLED, 1) != 0; contentResolver, Settings.Secure.ASSIST_GESTURE_ENABLED, 1) != 0;
final boolean assistGestureSilenceEnabled = Settings.Secure.getInt( final boolean assistGestureSilenceEnabled = Settings.Secure.getInt(
contentResolver, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 1) != 0; contentResolver, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 1) != 0;
final String summary;
if (mFeatureProvider.isSupported(mContext) && assistGestureEnabled) { if (mFeatureProvider.isSupported(mContext) && assistGestureEnabled) {
summary = mContext.getString( return mContext.getString(
R.string.language_input_gesture_summary_on_with_assist); R.string.language_input_gesture_summary_on_with_assist);
} else if (assistGestureSilenceEnabled) {
summary = 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); if (assistGestureSilenceEnabled) {
return mContext.getString(
R.string.language_input_gesture_summary_on_non_assist);
}
return mContext.getString(R.string.language_input_gesture_summary_off);
} }
} }

View File

@@ -77,7 +77,8 @@ public class HeaderPreferenceController extends NotificationPreferenceController
: mAppRow.label; : mAppRow.label;
} }
CharSequence getSummary() { @Override
public String getSummary() {
if (mChannel != null) { if (mChannel != null) {
if (mChannelGroup != null && mChannelGroup.getGroup() != null if (mChannelGroup != null && mChannelGroup.getGroup() != null
&& !TextUtils.isEmpty(mChannelGroup.getGroup().getName())) { && !TextUtils.isEmpty(mChannelGroup.getGroup().getName())) {
@@ -87,12 +88,12 @@ public class HeaderPreferenceController extends NotificationPreferenceController
summary.append(bidi.unicodeWrap(mContext.getText( summary.append(bidi.unicodeWrap(mContext.getText(
R.string.notification_header_divider_symbol_with_spaces))); R.string.notification_header_divider_symbol_with_spaces)));
summary.append(bidi.unicodeWrap(mChannelGroup.getGroup().getName().toString())); summary.append(bidi.unicodeWrap(mChannelGroup.getGroup().getName().toString()));
return summary; return summary.toString();
} else { } else {
return mAppRow.label; return mAppRow.label.toString();
} }
} else if (mChannelGroup != null && mChannelGroup.getGroup() != null) { } else if (mChannelGroup != null && mChannelGroup.getGroup() != null) {
return mAppRow.label; return mAppRow.label.toString();
} else { } else {
return ""; return "";
} }

View File

@@ -96,8 +96,8 @@ public class DefaultAppShortcutPreferenceControllerBaseTest {
mController.isDefault = true; mController.isDefault = true;
mController.updateState(mPreference); mController.updateState(mPreference);
String yesString = mContext.getString(R.string.yes);
verify(mPreference).setSummary(R.string.yes); verify(mPreference).setSummary(yesString);
} }
@Test @Test
@@ -106,7 +106,8 @@ public class DefaultAppShortcutPreferenceControllerBaseTest {
mController.updateState(mPreference); mController.updateState(mPreference);
verify(mPreference).setSummary(R.string.no); String noString = mContext.getString(R.string.no);
verify(mPreference).setSummary(noString);
} }
@Test @Test

View File

@@ -77,8 +77,8 @@ public class BackupSettingsActivityPreferenceControllerTest {
mBackupEnabled = true; mBackupEnabled = true;
mController.updateState(mBackupPreference); mController.updateState(mBackupPreference);
String summaryString = mContext.getString(R.string.accessibility_feature_state_on);
verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_on); verify(mBackupPreference).setSummary(summaryString);
} }
@Test @Test
@@ -86,8 +86,8 @@ public class BackupSettingsActivityPreferenceControllerTest {
mBackupEnabled = false; mBackupEnabled = false;
mController.updateState(mBackupPreference); mController.updateState(mBackupPreference);
String summaryString = mContext.getString(R.string.accessibility_feature_state_off);
verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_off); verify(mBackupPreference).setSummary(summaryString);
} }
@Test @Test

View File

@@ -63,13 +63,14 @@ public class BluetoothDeviceNamePreferenceControllerTest {
doReturn(mContext).when(mPreferenceScreen).getContext(); doReturn(mContext).when(mPreferenceScreen).getContext();
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
mPreference.setKey(BluetoothDeviceNamePreferenceController.KEY_DEVICE_NAME); mPreference.setKey(BluetoothDeviceNamePreferenceController.KEY_DEVICE_NAME);
mController = new BluetoothDeviceNamePreferenceController( mController = spy(new BluetoothDeviceNamePreferenceController(
mContext, mLocalAdapter); mContext, mLocalAdapter));
doReturn(DEVICE_NAME).when(mController).getDeviceName();
} }
@Test @Test
public void testUpdateDeviceName_showSummaryWithDeviceName() { public void testUpdateDeviceName_showSummaryWithDeviceName() {
mController.updateDeviceName(mPreference, DEVICE_NAME); mController.updateDeviceName(mPreference);
final CharSequence summary = mPreference.getSummary(); final CharSequence summary = mPreference.getSummary();

View File

@@ -19,6 +19,7 @@ package com.android.settings.bluetooth;
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.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy; 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;
@@ -69,13 +70,14 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
mPreference.setKey(PREF_KEY); mPreference.setKey(PREF_KEY);
mController = new BluetoothDeviceRenamePreferenceController( mController = spy(new BluetoothDeviceRenamePreferenceController(
mContext, PREF_KEY, mFragment, mLocalAdapter); mContext, PREF_KEY, mFragment, mLocalAdapter));
doReturn(DEVICE_NAME).when(mController).getDeviceName();
} }
@Test @Test
public void testUpdateDeviceName_showSummaryWithDeviceName() { public void testUpdateDeviceName_showSummaryWithDeviceName() {
mController.updateDeviceName(mPreference, DEVICE_NAME); mController.updateDeviceName(mPreference);
final CharSequence summary = mPreference.getSummary(); final CharSequence summary = mPreference.getSummary();