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