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