1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.wifi; 18 19 import android.annotation.StyleRes; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.widget.Button; 25 import android.widget.ImageButton; 26 import android.widget.TextView; 27 28 import androidx.appcompat.app.AlertDialog; 29 30 import com.android.settings.R; 31 import com.android.settingslib.RestrictedLockUtils; 32 import com.android.settingslib.RestrictedLockUtilsInternal; 33 import com.android.settingslib.wifi.AccessPoint; 34 35 /** 36 * Dialog for users to edit a Wi-Fi network. 37 * 38 * Migrating from Wi-Fi SettingsLib to to WifiTrackerLib, this object will be removed in the near 39 * future, please develop in {@link WifiDialog2}. 40 */ 41 public class WifiDialog extends AlertDialog implements WifiConfigUiBase, 42 DialogInterface.OnClickListener { 43 44 public interface WifiDialogListener { onForget(WifiDialog dialog)45 default void onForget(WifiDialog dialog) { 46 } 47 onSubmit(WifiDialog dialog)48 default void onSubmit(WifiDialog dialog) { 49 } 50 onScan(WifiDialog dialog, String ssid)51 default void onScan(WifiDialog dialog, String ssid) { 52 } 53 } 54 55 private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE; 56 private static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL; 57 58 private final int mMode; 59 private final WifiDialogListener mListener; 60 private final AccessPoint mAccessPoint; 61 62 private View mView; 63 private WifiConfigController mController; 64 private boolean mHideSubmitButton; 65 66 /** 67 * Creates a WifiDialog with no additional style. It displays as a dialog above the current 68 * view. 69 */ createModal(Context context, WifiDialogListener listener, AccessPoint accessPoint, int mode)70 public static WifiDialog createModal(Context context, WifiDialogListener listener, 71 AccessPoint accessPoint, int mode) { 72 return new WifiDialog(context, listener, accessPoint, mode, 0 /* style */, 73 mode == WifiConfigUiBase.MODE_VIEW /* hideSubmitButton */); 74 } 75 76 /** 77 * Creates a WifiDialog with customized style. It displays as a dialog above the current 78 * view. 79 */ createModal(Context context, WifiDialogListener listener, AccessPoint accessPoint, int mode, @StyleRes int style)80 public static WifiDialog createModal(Context context, WifiDialogListener listener, 81 AccessPoint accessPoint, int mode, @StyleRes int style) { 82 return new WifiDialog(context, listener, accessPoint, mode, style, 83 mode == WifiConfigUiBase.MODE_VIEW /* hideSubmitButton */); 84 } 85 WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint, int mode, @StyleRes int style, boolean hideSubmitButton)86 /* package */ WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint, 87 int mode, @StyleRes int style, boolean hideSubmitButton) { 88 super(context, style); 89 mMode = mode; 90 mListener = listener; 91 mAccessPoint = accessPoint; 92 mHideSubmitButton = hideSubmitButton; 93 } 94 95 @Override getController()96 public WifiConfigController getController() { 97 return mController; 98 } 99 100 @Override onCreate(Bundle savedInstanceState)101 protected void onCreate(Bundle savedInstanceState) { 102 mView = getLayoutInflater().inflate(R.layout.wifi_dialog, /* root */ null); 103 setView(mView); 104 mController = new WifiConfigController(this, mView, mAccessPoint, mMode); 105 super.onCreate(savedInstanceState); 106 107 if (mHideSubmitButton) { 108 mController.hideSubmitButton(); 109 } else { 110 /* During creation, the submit button can be unavailable to determine 111 * visibility. Right after creation, update button visibility */ 112 mController.enableSubmitIfAppropriate(); 113 } 114 115 if (mAccessPoint == null) { 116 mController.hideForgetButton(); 117 } 118 } 119 120 @Override onStart()121 protected void onStart() { 122 final ImageButton ssidScannerButton = findViewById(R.id.ssid_scanner_button); 123 if (mHideSubmitButton) { 124 ssidScannerButton.setVisibility(View.GONE); 125 return; 126 } 127 128 View.OnClickListener onClickScannerButtonListener = v -> { 129 if (mListener == null) { 130 return; 131 } 132 133 final TextView ssidEditText = findViewById(R.id.ssid); 134 final String ssid = ssidEditText.getText().toString(); 135 mListener.onScan(/* WifiDialog */ this, ssid); 136 }; 137 ssidScannerButton.setOnClickListener(onClickScannerButtonListener); 138 } 139 onRestoreInstanceState(Bundle savedInstanceState)140 public void onRestoreInstanceState(Bundle savedInstanceState) { 141 super.onRestoreInstanceState(savedInstanceState); 142 mController.updatePassword(); 143 } 144 145 @Override dispatchSubmit()146 public void dispatchSubmit() { 147 if (mListener != null) { 148 mListener.onSubmit(this); 149 } 150 dismiss(); 151 } 152 153 @Override onClick(DialogInterface dialogInterface, int id)154 public void onClick(DialogInterface dialogInterface, int id) { 155 if (mListener != null) { 156 switch (id) { 157 case BUTTON_SUBMIT: 158 mListener.onSubmit(this); 159 break; 160 case BUTTON_FORGET: 161 if (WifiUtils.isNetworkLockedDown(getContext(), mAccessPoint.getConfig())) { 162 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), 163 RestrictedLockUtilsInternal.getDeviceOwner(getContext())); 164 return; 165 } 166 mListener.onForget(this); 167 break; 168 } 169 } 170 } 171 172 @Override getMode()173 public int getMode() { 174 return mMode; 175 } 176 177 @Override getSubmitButton()178 public Button getSubmitButton() { 179 return getButton(BUTTON_SUBMIT); 180 } 181 182 @Override getForgetButton()183 public Button getForgetButton() { 184 return getButton(BUTTON_FORGET); 185 } 186 187 @Override getCancelButton()188 public Button getCancelButton() { 189 return getButton(BUTTON_NEGATIVE); 190 } 191 192 @Override setSubmitButton(CharSequence text)193 public void setSubmitButton(CharSequence text) { 194 setButton(BUTTON_SUBMIT, text, this); 195 } 196 197 @Override setForgetButton(CharSequence text)198 public void setForgetButton(CharSequence text) { 199 setButton(BUTTON_FORGET, text, this); 200 } 201 202 @Override setCancelButton(CharSequence text)203 public void setCancelButton(CharSequence text) { 204 setButton(BUTTON_NEGATIVE, text, this); 205 } 206 } 207