1 /*
2  * Copyright (C) 2009 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 static android.content.Context.WIFI_SERVICE;
20 
21 import static com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed;
22 
23 import android.app.settings.SettingsEnums;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.net.wifi.WifiManager;
27 import android.os.Bundle;
28 import android.text.Editable;
29 import android.widget.EditText;
30 
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceScreen;
34 
35 import com.android.settings.R;
36 import com.android.settings.SettingsPreferenceFragment;
37 
38 /**
39  * Provide an interface for testing out the Wifi API
40  */
41 public class WifiAPITest extends SettingsPreferenceFragment implements
42         Preference.OnPreferenceClickListener {
43 
44     private static final String TAG = "WifiAPITest+++";
45     private int netid;
46 
47     //============================
48     // Preference/activity member variables
49     //============================
50 
51     private static final String KEY_DISCONNECT = "disconnect";
52     private static final String KEY_DISABLE_NETWORK = "disable_network";
53     private static final String KEY_ENABLE_NETWORK = "enable_network";
54 
55     private Preference mWifiDisconnect;
56     private Preference mWifiDisableNetwork;
57     private Preference mWifiEnableNetwork;
58 
59     private WifiManager mWifiManager;
60 
61 
62     //============================
63     // Activity lifecycle
64     //============================
65 
66     @Override
onCreate(Bundle savedInstanceState)67     public void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69 
70         mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
71     }
72 
73     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)74     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
75         Context context = getContext();
76         if (context == null) {
77             return;
78         }
79         addPreferencesFromResource(R.xml.wifi_api_test);
80 
81         boolean isChangeWifiStateAllowed = isChangeWifiStateAllowed(context);
82         final PreferenceScreen preferenceScreen = getPreferenceScreen();
83 
84         mWifiDisconnect = (Preference) preferenceScreen.findPreference(KEY_DISCONNECT);
85         if (mWifiDisconnect != null) {
86             mWifiDisconnect.setEnabled(isChangeWifiStateAllowed);
87             if (isChangeWifiStateAllowed) {
88                 mWifiDisconnect.setOnPreferenceClickListener(this);
89             }
90         }
91 
92         mWifiDisableNetwork = (Preference) preferenceScreen.findPreference(KEY_DISABLE_NETWORK);
93         if (mWifiDisableNetwork != null) {
94             mWifiDisableNetwork.setEnabled(isChangeWifiStateAllowed);
95             if (isChangeWifiStateAllowed) {
96                 mWifiDisableNetwork.setOnPreferenceClickListener(this);
97             }
98         }
99 
100         mWifiEnableNetwork = (Preference) preferenceScreen.findPreference(KEY_ENABLE_NETWORK);
101         if (mWifiEnableNetwork != null) {
102             mWifiEnableNetwork.setEnabled(isChangeWifiStateAllowed);
103             if (isChangeWifiStateAllowed) {
104                 mWifiEnableNetwork.setOnPreferenceClickListener(this);
105             }
106         }
107 
108     }
109 
110     @Override
getMetricsCategory()111     public int getMetricsCategory() {
112         return SettingsEnums.TESTING;
113     }
114 
115     //============================
116     // Preference callbacks
117     //============================
118 
119     @Override
onPreferenceTreeClick(Preference preference)120     public boolean onPreferenceTreeClick(Preference preference) {
121         super.onPreferenceTreeClick(preference);
122         return false;
123     }
124 
125     /**
126      *  Implements OnPreferenceClickListener interface
127      */
onPreferenceClick(Preference pref)128     public boolean onPreferenceClick(Preference pref) {
129         if (pref == mWifiDisconnect) {
130             mWifiManager.disconnect();
131         } else if (pref == mWifiDisableNetwork) {
132             AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
133             alert.setTitle("Input");
134             alert.setMessage("Enter Network ID");
135             // Set an EditText view to get user input
136             final EditText input = new EditText(getPrefContext());
137             alert.setView(input);
138             alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
139                     public void onClick(DialogInterface dialog, int whichButton) {
140                     Editable value = input.getText();
141                     try {
142                         netid = Integer.parseInt(value.toString());
143                     } catch (NumberFormatException e) {
144                         // Invalid netid
145                         e.printStackTrace();
146                         return;
147                     }
148                     mWifiManager.disableNetwork(netid);
149                     }
150                     });
151             alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
152                     public void onClick(DialogInterface dialog, int whichButton) {
153                     // Canceled.
154                     }
155                     });
156             alert.show();
157         } else if (pref == mWifiEnableNetwork) {
158             AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
159             alert.setTitle("Input");
160             alert.setMessage("Enter Network ID");
161             // Set an EditText view to get user input
162             final EditText input = new EditText(getPrefContext());
163             alert.setView(input);
164             alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
165                     public void onClick(DialogInterface dialog, int whichButton) {
166                     Editable value = input.getText();
167                     netid =  Integer.parseInt(value.toString());
168                     mWifiManager.enableNetwork(netid, false);
169                     }
170                     });
171             alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
172                     public void onClick(DialogInterface dialog, int whichButton) {
173                     // Canceled.
174                     }
175                     });
176             alert.show();
177         }
178         return true;
179     }
180 }
181