Add control to save button.
If the mode is PRIVATE_DNS_MODE_PROVIDER_HOSTNAME and hostname is invalid, we should disable the save button. Otherwise enable the save button. Bug: 68030013 Test: RunSettingsRoboTests Change-Id: If9149c119f1b396a7057590d515b62e7c19b9f99
This commit is contained in:
@@ -32,6 +32,7 @@ import android.text.Editable;
|
|||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.RadioGroup;
|
import android.widget.RadioGroup;
|
||||||
|
|
||||||
@@ -68,6 +69,8 @@ public class PrivateDnsModeDialogFragment extends InstrumentedDialogFragment imp
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
RadioGroup mRadioGroup;
|
RadioGroup mRadioGroup;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
Button mSaveButton;
|
||||||
|
@VisibleForTesting
|
||||||
String mMode;
|
String mMode;
|
||||||
|
|
||||||
public static void show(FragmentManager fragmentManager) {
|
public static void show(FragmentManager fragmentManager) {
|
||||||
@@ -81,17 +84,23 @@ public class PrivateDnsModeDialogFragment extends InstrumentedDialogFragment imp
|
|||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
|
|
||||||
return new AlertDialog.Builder(context)
|
final AlertDialog dialog = new AlertDialog.Builder(context)
|
||||||
.setTitle(R.string.select_private_dns_configuration_title)
|
.setTitle(R.string.select_private_dns_configuration_title)
|
||||||
.setView(buildPrivateDnsView(context))
|
.setView(buildPrivateDnsView(context))
|
||||||
.setPositiveButton(R.string.save, this)
|
.setPositiveButton(R.string.save, this)
|
||||||
.setNegativeButton(R.string.dlg_cancel, null)
|
.setNegativeButton(R.string.dlg_cancel, null)
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
|
dialog.setOnShowListener(dialogInterface -> {
|
||||||
|
mSaveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
||||||
|
updateDialogInfo();
|
||||||
|
});
|
||||||
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
private View buildPrivateDnsView(final Context context) {
|
private View buildPrivateDnsView(final Context context) {
|
||||||
final ContentResolver contentResolver = context.getContentResolver();
|
final ContentResolver contentResolver = context.getContentResolver();
|
||||||
final String mode = Settings.Global.getString(contentResolver, MODE_KEY);
|
mMode = Settings.Global.getString(contentResolver, MODE_KEY);
|
||||||
final View view = LayoutInflater.from(context).inflate(R.layout.private_dns_mode_dialog,
|
final View view = LayoutInflater.from(context).inflate(R.layout.private_dns_mode_dialog,
|
||||||
null);
|
null);
|
||||||
|
|
||||||
@@ -101,7 +110,7 @@ public class PrivateDnsModeDialogFragment extends InstrumentedDialogFragment imp
|
|||||||
|
|
||||||
mRadioGroup = view.findViewById(R.id.private_dns_radio_group);
|
mRadioGroup = view.findViewById(R.id.private_dns_radio_group);
|
||||||
mRadioGroup.setOnCheckedChangeListener(this);
|
mRadioGroup.setOnCheckedChangeListener(this);
|
||||||
mRadioGroup.check(PRIVATE_DNS_MAP.getOrDefault(mode, R.id.private_dns_mode_opportunistic));
|
mRadioGroup.check(PRIVATE_DNS_MAP.getOrDefault(mMode, R.id.private_dns_mode_opportunistic));
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@@ -129,17 +138,15 @@ public class PrivateDnsModeDialogFragment extends InstrumentedDialogFragment imp
|
|||||||
switch (checkedId) {
|
switch (checkedId) {
|
||||||
case R.id.private_dns_mode_off:
|
case R.id.private_dns_mode_off:
|
||||||
mMode = PRIVATE_DNS_MODE_OFF;
|
mMode = PRIVATE_DNS_MODE_OFF;
|
||||||
mEditText.setEnabled(false);
|
|
||||||
break;
|
break;
|
||||||
case R.id.private_dns_mode_opportunistic:
|
case R.id.private_dns_mode_opportunistic:
|
||||||
mMode = PRIVATE_DNS_MODE_OPPORTUNISTIC;
|
mMode = PRIVATE_DNS_MODE_OPPORTUNISTIC;
|
||||||
mEditText.setEnabled(false);
|
|
||||||
break;
|
break;
|
||||||
case R.id.private_dns_mode_provider:
|
case R.id.private_dns_mode_provider:
|
||||||
mMode = PRIVATE_DNS_MODE_PROVIDER_HOSTNAME;
|
mMode = PRIVATE_DNS_MODE_PROVIDER_HOSTNAME;
|
||||||
mEditText.setEnabled(true);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
updateDialogInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -152,8 +159,9 @@ public class PrivateDnsModeDialogFragment extends InstrumentedDialogFragment imp
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterTextChanged(Editable s) {
|
public void afterTextChanged(Editable s) {
|
||||||
// TODO(b/68030013): Disable the "positive button" ("Save") when appearsValid is false.
|
if (mSaveButton != null) {
|
||||||
final boolean valid = isWeaklyValidatedHostname(s.toString());
|
mSaveButton.setEnabled(isWeaklyValidatedHostname(mEditText.getText().toString()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isWeaklyValidatedHostname(String hostname) {
|
private boolean isWeaklyValidatedHostname(String hostname) {
|
||||||
@@ -165,4 +173,17 @@ public class PrivateDnsModeDialogFragment extends InstrumentedDialogFragment imp
|
|||||||
return hostname.matches(WEAK_HOSTNAME_REGEX);
|
return hostname.matches(WEAK_HOSTNAME_REGEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateDialogInfo() {
|
||||||
|
final boolean modeProvider = PRIVATE_DNS_MODE_PROVIDER_HOSTNAME.equals(mMode);
|
||||||
|
if (mEditText != null) {
|
||||||
|
mEditText.setEnabled(modeProvider);
|
||||||
|
}
|
||||||
|
if (mSaveButton != null) {
|
||||||
|
mSaveButton.setEnabled(
|
||||||
|
modeProvider
|
||||||
|
? isWeaklyValidatedHostname(mEditText.getText().toString())
|
||||||
|
: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.spy;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
import android.widget.Button;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
@@ -43,9 +44,11 @@ import org.robolectric.annotation.Config;
|
|||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class PrivateDnsModeDialogFragmentTest {
|
public class PrivateDnsModeDialogFragmentTest {
|
||||||
private static final String HOST_NAME = "192.168.1.1";
|
private static final String HOST_NAME = "192.168.1.1";
|
||||||
|
private static final String INVALID_HOST_NAME = "...,";
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private PrivateDnsModeDialogFragment mFragment;
|
private PrivateDnsModeDialogFragment mFragment;
|
||||||
|
private Button mSaveButton;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -53,9 +56,12 @@ public class PrivateDnsModeDialogFragmentTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mSaveButton = new Button(mContext);
|
||||||
|
|
||||||
mFragment = spy(new PrivateDnsModeDialogFragment());
|
mFragment = spy(new PrivateDnsModeDialogFragment());
|
||||||
doReturn(mContext).when(mFragment).getContext();
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
mFragment.onCreateDialog(null);
|
mFragment.onCreateDialog(null);
|
||||||
|
mFragment.mSaveButton = mSaveButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -96,4 +102,19 @@ public class PrivateDnsModeDialogFragmentTest {
|
|||||||
R.id.private_dns_mode_opportunistic);
|
R.id.private_dns_mode_opportunistic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnCheckedChanged_switchMode_saveButtonHasCorrectState() {
|
||||||
|
// Set invalid hostname
|
||||||
|
mFragment.mEditText.setText(INVALID_HOST_NAME);
|
||||||
|
|
||||||
|
mFragment.onCheckedChanged(null, R.id.private_dns_mode_opportunistic);
|
||||||
|
assertThat(mSaveButton.isEnabled()).isTrue();
|
||||||
|
|
||||||
|
mFragment.onCheckedChanged(null, R.id.private_dns_mode_provider);
|
||||||
|
assertThat(mSaveButton.isEnabled()).isFalse();
|
||||||
|
|
||||||
|
mFragment.onCheckedChanged(null, R.id.private_dns_mode_off);
|
||||||
|
assertThat(mSaveButton.isEnabled()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user