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     @Override
onStart()141     protected void onStart() {
142         final ImageButton ssidScannerButton = findViewById(R.id.ssid_scanner_button);
143         if (mHideSubmitButton) {
144             ssidScannerButton.setVisibility(View.GONE);
145             return;
146         }
147 
148         View.OnClickListener onClickScannerButtonListener = v -> {
149             if (mListener == null) {
150                 return;
151             }
152 
153             final TextView ssidEditText = findViewById(R.id.ssid);
154             final String ssid = ssidEditText.getText().toString();
155             mListener.onScan(/* WifiDialog2 */ this, ssid);
156         };
157         ssidScannerButton.setOnClickListener(onClickScannerButtonListener);
158     }
159 
160     @Override
onRestoreInstanceState(Bundle savedInstanceState)161     public void onRestoreInstanceState(Bundle savedInstanceState) {
162         super.onRestoreInstanceState(savedInstanceState);
163         mController.updatePassword();
164     }
165 
166     @Override
dispatchSubmit()167     public void dispatchSubmit() {
168         if (mListener != null) {
169             mListener.onSubmit(this);
170         }
171         dismiss();
172     }
173 
174     @Override
onClick(DialogInterface dialogInterface, int id)175     public void onClick(DialogInterface dialogInterface, int id) {
176         if (mListener != null) {
177             switch (id) {
178                 case BUTTON_SUBMIT:
179                     mListener.onSubmit(this);
180                     break;
181                 case BUTTON_FORGET:
182                     if (WifiUtils.isNetworkLockedDown(getContext(),
183                             mWifiEntry.getWifiConfiguration())) {
184                         RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
185                                 RestrictedLockUtilsInternal.getDeviceOwner(getContext()));
186                         return;
187                     }
188                     mListener.onForget(this);
189                     break;
190             }
191         }
192     }
193 
194     @Override
getMode()195     public int getMode() {
196         return mMode;
197     }
198 
199     @Override
getSubmitButton()200     public Button getSubmitButton() {
201         return getButton(BUTTON_SUBMIT);
202     }
203 
204     @Override
getForgetButton()205     public Button getForgetButton() {
206         return getButton(BUTTON_FORGET);
207     }
208 
209     @Override
getCancelButton()210     public Button getCancelButton() {
211         return getButton(BUTTON_NEGATIVE);
212     }
213 
214     @Override
setSubmitButton(CharSequence text)215     public void setSubmitButton(CharSequence text) {
216         setButton(BUTTON_SUBMIT, text, this);
217     }
218 
219     @Override
setForgetButton(CharSequence text)220     public void setForgetButton(CharSequence text) {
221         setButton(BUTTON_FORGET, text, this);
222     }
223 
224     @Override
setCancelButton(CharSequence text)225     public void setCancelButton(CharSequence text) {
226         setButton(BUTTON_NEGATIVE, text, this);
227     }
228 
getWifiEntry()229     public WifiEntry getWifiEntry() {
230         return mWifiEntry;
231     }
232 }
233