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