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