Fix popup dialog show wrong device name
- Current EditText value will set to preference text if we click OK in EditTextPreferenceDialogFragment. We will set preference text to default when click cancel in DeviceNameWarningDialog. Change-Id: Iab9561953b58276e98ee68d9196fa18e0dc3d78c Fixes: 115693838 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -39,10 +39,10 @@ import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
|
|||||||
|
|
||||||
public class DeviceNamePreferenceController extends BasePreferenceController
|
public class DeviceNamePreferenceController extends BasePreferenceController
|
||||||
implements ValidatedEditTextPreference.Validator,
|
implements ValidatedEditTextPreference.Validator,
|
||||||
Preference.OnPreferenceChangeListener,
|
Preference.OnPreferenceChangeListener,
|
||||||
LifecycleObserver,
|
LifecycleObserver,
|
||||||
OnSaveInstanceState,
|
OnSaveInstanceState,
|
||||||
OnCreate {
|
OnCreate {
|
||||||
private static final String PREF_KEY = "device_name";
|
private static final String PREF_KEY = "device_name";
|
||||||
public static final int DEVICE_NAME_SET_WARNING_ID = 1;
|
public static final int DEVICE_NAME_SET_WARNING_ID = 1;
|
||||||
private static final String KEY_PENDING_DEVICE_NAME = "key_pending_device_name";
|
private static final String KEY_PENDING_DEVICE_NAME = "key_pending_device_name";
|
||||||
@@ -116,9 +116,11 @@ public class DeviceNamePreferenceController extends BasePreferenceController
|
|||||||
return mWifiDeviceNameTextValidator.isTextValid(deviceName);
|
return mWifiDeviceNameTextValidator.isTextValid(deviceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void confirmDeviceName() {
|
public void updateDeviceName(boolean update) {
|
||||||
if (mPendingDeviceName != null) {
|
if (update && mPendingDeviceName != null) {
|
||||||
setDeviceName(mPendingDeviceName);
|
setDeviceName(mPendingDeviceName);
|
||||||
|
} else {
|
||||||
|
mPreference.setText(getSummary().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +155,8 @@ public class DeviceNamePreferenceController extends BasePreferenceController
|
|||||||
* For more information, see {@link com.android.settings.bluetooth.BluetoothNameDialogFragment}.
|
* For more information, see {@link com.android.settings.bluetooth.BluetoothNameDialogFragment}.
|
||||||
*/
|
*/
|
||||||
private static final String getFilteredBluetoothString(final String deviceName) {
|
private static final String getFilteredBluetoothString(final String deviceName) {
|
||||||
CharSequence filteredSequence = new BluetoothLengthDeviceNameFilter().filter(deviceName, 0, deviceName.length(),
|
CharSequence filteredSequence = new BluetoothLengthDeviceNameFilter().filter(deviceName, 0,
|
||||||
|
deviceName.length(),
|
||||||
new SpannedString(""),
|
new SpannedString(""),
|
||||||
0, 0);
|
0, 0);
|
||||||
// null -> use the original
|
// null -> use the original
|
||||||
|
@@ -66,7 +66,9 @@ public class DeviceNameWarningDialog extends InstrumentedDialogFragment
|
|||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
final MyDeviceInfoFragment host = (MyDeviceInfoFragment) getTargetFragment();
|
final MyDeviceInfoFragment host = (MyDeviceInfoFragment) getTargetFragment();
|
||||||
if (which == DialogInterface.BUTTON_POSITIVE) {
|
if (which == DialogInterface.BUTTON_POSITIVE) {
|
||||||
host.onSetDeviceNameConfirm();
|
host.onSetDeviceNameConfirm(true);
|
||||||
|
} else {
|
||||||
|
host.onSetDeviceNameConfirm(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -178,9 +178,9 @@ public class MyDeviceInfoFragment extends DashboardFragment
|
|||||||
DeviceNameWarningDialog.show(this);
|
DeviceNameWarningDialog.show(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSetDeviceNameConfirm() {
|
public void onSetDeviceNameConfirm(boolean confirm) {
|
||||||
final DeviceNamePreferenceController controller = use(DeviceNamePreferenceController.class);
|
final DeviceNamePreferenceController controller = use(DeviceNamePreferenceController.class);
|
||||||
controller.confirmDeviceName();
|
controller.updateDeviceName(confirm);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SummaryProvider implements SummaryLoader.SummaryProvider {
|
private static class SummaryProvider implements SummaryLoader.SummaryProvider {
|
||||||
|
@@ -114,7 +114,7 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setDeviceName_preferenceUpdatedWhenDeviceNameUpdated() {
|
public void setDeviceName_preferenceUpdatedWhenDeviceNameUpdated() {
|
||||||
forceAcceptDeviceName();
|
acceptDeviceName(true);
|
||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
@@ -123,7 +123,7 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setDeviceName_bluetoothNameUpdatedWhenDeviceNameUpdated() {
|
public void setDeviceName_bluetoothNameUpdatedWhenDeviceNameUpdated() {
|
||||||
forceAcceptDeviceName();
|
acceptDeviceName(true);
|
||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setDeviceName_wifiTetherNameUpdatedWhenDeviceNameUpdated() {
|
public void setDeviceName_wifiTetherNameUpdatedWhenDeviceNameUpdated() {
|
||||||
forceAcceptDeviceName();
|
acceptDeviceName(true);
|
||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
@@ -150,21 +150,39 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setDeviceName_ignoresIfCancelPressed() {
|
public void setDeviceName_ignoresIfCancelPressed() {
|
||||||
forceAcceptDeviceName();
|
acceptDeviceName(true);
|
||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
|
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void forceAcceptDeviceName() {
|
@Test
|
||||||
|
public void setDeviceName_okInDeviceNameWarningDialog_shouldChangePreferenceText() {
|
||||||
|
acceptDeviceName(true);
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
|
assertThat(mPreference.getSummary()).isEqualTo(TESTING_STRING);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setDeviceName_cancelInDeviceNameWarningDialog_shouldNotChangePreferenceText() {
|
||||||
|
acceptDeviceName(false);
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
|
assertThat(mPreference.getSummary()).isNotEqualTo(TESTING_STRING);
|
||||||
|
assertThat(mPreference.getText()).isEqualTo(mPreference.getSummary());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void acceptDeviceName(boolean accept) {
|
||||||
mController.setHost(
|
mController.setHost(
|
||||||
new DeviceNamePreferenceController.DeviceNamePreferenceHost() {
|
new DeviceNamePreferenceController.DeviceNamePreferenceHost() {
|
||||||
@Override
|
@Override
|
||||||
public void showDeviceNameWarningDialog(String deviceName) {
|
public void showDeviceNameWarningDialog(String deviceName) {
|
||||||
mController.confirmDeviceName();
|
mController.updateDeviceName(accept);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -30,7 +30,7 @@ public class DeviceNameWarningDialogTest {
|
|||||||
fragmentController.create().start().resume();
|
fragmentController.create().start().resume();
|
||||||
fragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
|
fragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
|
||||||
|
|
||||||
verify(deviceInfoFragment).onSetDeviceNameConfirm();
|
verify(deviceInfoFragment).onSetDeviceNameConfirm(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -43,6 +43,6 @@ public class DeviceNameWarningDialogTest {
|
|||||||
fragmentController.create().start().resume();
|
fragmentController.create().start().resume();
|
||||||
fragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
|
fragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
|
||||||
|
|
||||||
verify(deviceInfoFragment, never()).onSetDeviceNameConfirm();
|
verify(deviceInfoFragment).onSetDeviceNameConfirm(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user