diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java index 39c77a17924..713dfb2458a 100644 --- a/src/com/android/settings/wifi/WifiConfigController.java +++ b/src/com/android/settings/wifi/WifiConfigController.java @@ -545,13 +545,13 @@ public class WifiConfigController implements TextWatcher, && !isValidSaePassword(mPasswordView.getText().toString())))) { passwordInvalid = true; } - if ((mSsidView != null && mSsidView.length() == 0) - // If Accesspoint is not saved, apply passwordInvalid check - || ((mAccessPoint == null || !mAccessPoint.isSaved()) && passwordInvalid - // If AccessPoint is saved (modifying network) and password is changed, apply - // Invalid password check - || mAccessPoint != null && mAccessPoint.isSaved() && passwordInvalid - && mPasswordView.length() > 0)) { + if ((mAccessPoint == null || !mAccessPoint.isSaved()) && passwordInvalid) { + // If Accesspoint is not saved, apply passwordInvalid check + enabled = false; + } else if (mAccessPoint != null && mAccessPoint.isSaved() && passwordInvalid + && mPasswordView.length() > 0) { + // If AccessPoint is saved (modifying network) and password is changed, apply + // Invalid password check enabled = false; } else { enabled = ipAndProxyFieldsAreValid(); diff --git a/src/com/android/settings/wifi/WifiDialog.java b/src/com/android/settings/wifi/WifiDialog.java index a1ff1ac9a45..40d22e60fa2 100644 --- a/src/com/android/settings/wifi/WifiDialog.java +++ b/src/com/android/settings/wifi/WifiDialog.java @@ -28,6 +28,8 @@ import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import com.android.settings.R; +import com.android.settings.wifi.utils.SsidInputGroup; +import com.android.settings.wifi.utils.WifiDialogHelper; import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.wifi.AccessPoint; @@ -62,6 +64,7 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase, private View mView; private WifiConfigController mController; private boolean mHideSubmitButton; + private WifiDialogHelper mDialogHelper; /** * Creates a WifiDialog with no additional style. It displays as a dialog above the current @@ -115,6 +118,9 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase, if (mAccessPoint == null) { mController.hideForgetButton(); } + + mDialogHelper = new WifiDialogHelper(this, + new SsidInputGroup(getContext(), mView, R.id.ssid_layout, R.id.ssid)); } @SuppressWarnings("MissingSuperCall") // TODO: Fix me @@ -155,9 +161,6 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase, public void onClick(DialogInterface dialogInterface, int id) { if (mListener != null) { switch (id) { - case BUTTON_SUBMIT: - mListener.onSubmit(this); - break; case BUTTON_FORGET: if (WifiUtils.isNetworkLockedDown(getContext(), mAccessPoint.getConfig())) { RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), @@ -170,6 +173,11 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase, } } + /** Return true to tell the parent activity to call onSubmit before onDismiss. */ + public boolean shouldSubmitBeforeFinish() { + return mDialogHelper.isPositive(); + } + @Override public int getMode() { return mMode; diff --git a/src/com/android/settings/wifi/WifiDialogActivity.java b/src/com/android/settings/wifi/WifiDialogActivity.java index 951277aa9cf..1d431753f32 100644 --- a/src/com/android/settings/wifi/WifiDialogActivity.java +++ b/src/com/android/settings/wifi/WifiDialogActivity.java @@ -346,6 +346,9 @@ public class WifiDialogActivity extends ObservableActivity implements WifiDialog @Override public void onDismiss(DialogInterface dialogInterface) { mDialog2 = null; + if (mDialog != null && mDialog.shouldSubmitBeforeFinish()) { + onSubmit(mDialog); + } mDialog = null; finish(); } diff --git a/src/com/android/settings/wifi/utils/AlertDialogHelper.kt b/src/com/android/settings/wifi/utils/AlertDialogHelper.kt new file mode 100644 index 00000000000..c50323220b7 --- /dev/null +++ b/src/com/android/settings/wifi/utils/AlertDialogHelper.kt @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2025 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.wifi.utils + +import android.content.DialogInterface +import android.util.Log +import androidx.appcompat.app.AlertDialog + +open class AlertDialogHelper(val alertDialog: AlertDialog) { + + var isPositive = false + + init { + alertDialog.setOnShowListener { + alertDialog.getButton(DialogInterface.BUTTON_POSITIVE)?.setOnClickListener { + onPositiveButtonClicked() + } ?: Log.e(TAG, "Can't get the positive button!") + } + } + + open fun onPositiveButtonClicked() { + if (!canDismiss()) { + Log.w(TAG, "Can't dismiss dialog!") + return + } + isPositive = true + alertDialog.dismiss() + } + + open fun canDismiss() = true + + companion object { + const val TAG = "AlertDialogHelper" + } +} diff --git a/src/com/android/settings/wifi/utils/WifiDialogHelper.kt b/src/com/android/settings/wifi/utils/WifiDialogHelper.kt new file mode 100644 index 00000000000..3b23b1a7e50 --- /dev/null +++ b/src/com/android/settings/wifi/utils/WifiDialogHelper.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2025 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.wifi.utils + +import android.util.Log +import androidx.appcompat.app.AlertDialog + +class WifiDialogHelper( + alertDialog: AlertDialog, + private val ssidInputGroup: SsidInputGroup? = null, +) : AlertDialogHelper(alertDialog) { + + override fun canDismiss(): Boolean { + val isValid = ssidInputGroup?.validate() ?: true + if (!isValid) Log.w(TAG, "SSID is invalid!") + return isValid + } + + companion object { + const val TAG = "WifiDialogHelper" + } +} diff --git a/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java index d80464d5467..d5d395e4452 100644 --- a/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java @@ -145,14 +145,6 @@ public class WifiConfigControllerTest { .isEqualTo(View.GONE); } - @Test - public void isSubmittable_noSSID_shouldReturnFalse() { - final TextView ssid = mView.findViewById(R.id.ssid); - assertThat(ssid).isNotNull(); - ssid.setText(""); - assertThat(mController.isSubmittable()).isFalse(); - } - @Test public void isSubmittable_longPsk_shouldReturnFalse() { final TextView password = mView.findViewById(R.id.password); diff --git a/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java b/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java index d1cbd0ee1b7..159d97acd15 100644 --- a/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java +++ b/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java @@ -349,4 +349,14 @@ public class WifiDialogActivityTest { verify(mActivity).dismissDialog(); } + + @Test + public void onDismiss_shouldSubmitBeforeFinish_callOnSubmit() { + mActivity.mDialog = mWifiDialog; + when(mWifiDialog.shouldSubmitBeforeFinish()).thenReturn(true); + + mActivity.onDismiss(mWifiDialog); + + verify(mActivity).onSubmit(mWifiDialog); + } }