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     @SuppressWarnings("MissingSuperCall") // TODO: Fix me
121     @Override
onStart()122     protected void onStart() {
123         final ImageButton ssidScannerButton = findViewById(R.id.ssid_scanner_button);
124         if (mHideSubmitButton) {
125             ssidScannerButton.setVisibility(View.GONE);
126             return;
127         }
128 
129         View.OnClickListener onClickScannerButtonListener = v -> {
130             if (mListener == null) {
131                 return;
132             }
133 
134             final TextView ssidEditText = findViewById(R.id.ssid);
135             final String ssid = ssidEditText.getText().toString();
136             mListener.onScan(/* WifiDialog */ this, ssid);
137         };
138         ssidScannerButton.setOnClickListener(onClickScannerButtonListener);
139     }
140 
onRestoreInstanceState(Bundle savedInstanceState)141     public void onRestoreInstanceState(Bundle savedInstanceState) {
142         super.onRestoreInstanceState(savedInstanceState);
143         mController.updatePassword();
144     }
145 
146     @Override
dispatchSubmit()147     public void dispatchSubmit() {
148         if (mListener != null) {
149             mListener.onSubmit(this);
150         }
151         dismiss();
152     }
153 
154     @Override
onClick(DialogInterface dialogInterface, int id)155     public void onClick(DialogInterface dialogInterface, int id) {
156         if (mListener != null) {
157             switch (id) {
158                 case BUTTON_SUBMIT:
159                     mListener.onSubmit(this);
160                     break;
161                 case BUTTON_FORGET:
162                     if (WifiUtils.isNetworkLockedDown(getContext(), mAccessPoint.getConfig())) {
163                         RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
164                                 RestrictedLockUtilsInternal.getDeviceOwner(getContext()));
165                         return;
166                     }
167                     mListener.onForget(this);
168                     break;
169             }
170         }
171     }
172 
173     @Override
getMode()174     public int getMode() {
175         return mMode;
176     }
177 
178     @Override
getSubmitButton()179     public Button getSubmitButton() {
180         return getButton(BUTTON_SUBMIT);
181     }
182 
183     @Override
getForgetButton()184     public Button getForgetButton() {
185         return getButton(BUTTON_FORGET);
186     }
187 
188     @Override
getCancelButton()189     public Button getCancelButton() {
190         return getButton(BUTTON_NEGATIVE);
191     }
192 
193     @Override
setSubmitButton(CharSequence text)194     public void setSubmitButton(CharSequence text) {
195         setButton(BUTTON_SUBMIT, text, this);
196     }
197 
198     @Override
setForgetButton(CharSequence text)199     public void setForgetButton(CharSequence text) {
200         setButton(BUTTON_FORGET, text, this);
201     }
202 
203     @Override
setCancelButton(CharSequence text)204     public void setCancelButton(CharSequence text) {
205         setButton(BUTTON_NEGATIVE, text, this);
206     }
207 }
208