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.settings.wifi.utils.WifiDialogHelper; 32 import com.android.settingslib.RestrictedLockUtils; 33 import com.android.settingslib.RestrictedLockUtilsInternal; 34 import com.android.settingslib.wifi.AccessPoint; 35 36 /** 37 * Dialog for users to edit a Wi-Fi network. 38 * 39 * Migrating from Wi-Fi SettingsLib to to WifiTrackerLib, this object will be removed in the near 40 * future, please develop in {@link WifiDialog2}. 41 */ 42 public class WifiDialog extends AlertDialog implements WifiConfigUiBase, 43 DialogInterface.OnClickListener { 44 45 public interface WifiDialogListener { onForget(WifiDialog dialog)46 default void onForget(WifiDialog dialog) { 47 } 48 onSubmit(WifiDialog dialog)49 default void onSubmit(WifiDialog dialog) { 50 } 51 onScan(WifiDialog dialog, String ssid)52 default void onScan(WifiDialog dialog, String ssid) { 53 } 54 } 55 56 private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE; 57 private static final int BUTTON_FORGET = DialogInterface.BUTTON_NEUTRAL; 58 59 private final int mMode; 60 private final WifiDialogListener mListener; 61 private final AccessPoint mAccessPoint; 62 63 private View mView; 64 private WifiConfigController mController; 65 private boolean mHideSubmitButton; 66 private WifiDialogHelper mDialogHelper; 67 68 /** 69 * Creates a WifiDialog with no additional style. It displays as a dialog above the current 70 * view. 71 */ createModal(Context context, WifiDialogListener listener, AccessPoint accessPoint, int mode)72 public static WifiDialog createModal(Context context, WifiDialogListener listener, 73 AccessPoint accessPoint, int mode) { 74 return new WifiDialog(context, listener, accessPoint, mode, 0 /* style */, 75 mode == WifiConfigUiBase.MODE_VIEW /* hideSubmitButton */); 76 } 77 78 /** 79 * Creates a WifiDialog with customized style. It displays as a dialog above the current 80 * view. 81 */ createModal(Context context, WifiDialogListener listener, AccessPoint accessPoint, int mode, @StyleRes int style)82 public static WifiDialog createModal(Context context, WifiDialogListener listener, 83 AccessPoint accessPoint, int mode, @StyleRes int style) { 84 return new WifiDialog(context, listener, accessPoint, mode, style, 85 mode == WifiConfigUiBase.MODE_VIEW /* hideSubmitButton */); 86 } 87 WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint, int mode, @StyleRes int style, boolean hideSubmitButton)88 /* package */ WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint, 89 int mode, @StyleRes int style, boolean hideSubmitButton) { 90 super(context, style); 91 mMode = mode; 92 mListener = listener; 93 mAccessPoint = accessPoint; 94 mHideSubmitButton = hideSubmitButton; 95 } 96 97 @Override getController()98 public WifiConfigController getController() { 99 return mController; 100 } 101 102 @Override onCreate(Bundle savedInstanceState)103 protected void onCreate(Bundle savedInstanceState) { 104 mView = getLayoutInflater().inflate(R.layout.wifi_dialog, /* root */ null); 105 setView(mView); 106 mController = new WifiConfigController(this, mView, mAccessPoint, mMode); 107 super.onCreate(savedInstanceState); 108 109 if (mHideSubmitButton) { 110 mController.hideSubmitButton(); 111 } else { 112 /* During creation, the submit button can be unavailable to determine 113 * visibility. Right after creation, update button visibility */ 114 mController.enableSubmitIfAppropriate(); 115 } 116 117 if (mAccessPoint == null) { 118 mController.hideForgetButton(); 119 } 120 121 mDialogHelper = new WifiDialogHelper(this, this, mController.getValidator()); 122 } 123 124 @SuppressWarnings("MissingSuperCall") // TODO: Fix me 125 @Override onStart()126 protected void onStart() { 127 final ImageButton ssidScannerButton = findViewById(R.id.ssid_scanner_button); 128 if (mHideSubmitButton) { 129 ssidScannerButton.setVisibility(View.GONE); 130 return; 131 } 132 133 View.OnClickListener onClickScannerButtonListener = v -> { 134 if (mListener == null) { 135 return; 136 } 137 138 final TextView ssidEditText = findViewById(R.id.ssid); 139 final String ssid = ssidEditText.getText().toString(); 140 mListener.onScan(/* WifiDialog */ this, ssid); 141 }; 142 ssidScannerButton.setOnClickListener(onClickScannerButtonListener); 143 } 144 onRestoreInstanceState(Bundle savedInstanceState)145 public void onRestoreInstanceState(Bundle savedInstanceState) { 146 super.onRestoreInstanceState(savedInstanceState); 147 mController.updatePassword(); 148 } 149 150 @Override dispatchSubmit()151 public void dispatchSubmit() { 152 if (mListener != null) { 153 mListener.onSubmit(this); 154 } 155 dismiss(); 156 } 157 158 @Override onClick(DialogInterface dialogInterface, int id)159 public void onClick(DialogInterface dialogInterface, int id) { 160 if (mListener != null) { 161 switch (id) { 162 case BUTTON_SUBMIT: 163 mListener.onSubmit(this); 164 break; 165 case BUTTON_FORGET: 166 if (WifiUtils.isNetworkLockedDown(getContext(), mAccessPoint.getConfig())) { 167 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), 168 RestrictedLockUtilsInternal.getDeviceOwner(getContext())); 169 return; 170 } 171 mListener.onForget(this); 172 break; 173 } 174 } 175 } 176 177 @Override getMode()178 public int getMode() { 179 return mMode; 180 } 181 182 @Override getSubmitButton()183 public Button getSubmitButton() { 184 return getButton(BUTTON_SUBMIT); 185 } 186 187 @Override getForgetButton()188 public Button getForgetButton() { 189 return getButton(BUTTON_FORGET); 190 } 191 192 @Override getCancelButton()193 public Button getCancelButton() { 194 return getButton(BUTTON_NEGATIVE); 195 } 196 197 @Override setSubmitButton(CharSequence text)198 public void setSubmitButton(CharSequence text) { 199 setButton(BUTTON_SUBMIT, text, this); 200 } 201 202 @Override setForgetButton(CharSequence text)203 public void setForgetButton(CharSequence text) { 204 setButton(BUTTON_FORGET, text, this); 205 } 206 207 @Override setCancelButton(CharSequence text)208 public void setCancelButton(CharSequence text) { 209 setButton(BUTTON_NEGATIVE, text, this); 210 } 211 } 212