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