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 com.android.settings.R; 20 21 import android.app.AlertDialog; 22 import android.content.DialogInterface; 23 import android.content.Intent; 24 import android.net.wifi.WifiManager; 25 26 import android.os.Bundle; 27 import android.preference.Preference; 28 import android.preference.PreferenceActivity; 29 import android.preference.PreferenceScreen; 30 import android.text.Editable; 31 import android.widget.EditText; 32 33 34 /** 35 * Provide an interface for testing out the Wifi API 36 */ 37 public class WifiAPITest extends PreferenceActivity implements 38 Preference.OnPreferenceClickListener { 39 40 private static final String TAG = "WifiAPITest"; 41 private int netid; 42 43 //============================ 44 // Preference/activity member variables 45 //============================ 46 47 private static final String KEY_DISCONNECT = "disconnect"; 48 private static final String KEY_DISABLE_NETWORK = "disable_network"; 49 private static final String KEY_ENABLE_NETWORK = "enable_network"; 50 51 private Preference mWifiDisconnect; 52 private Preference mWifiDisableNetwork; 53 private Preference mWifiEnableNetwork; 54 55 private WifiManager mWifiManager; 56 57 58 //============================ 59 // Activity lifecycle 60 //============================ 61 62 @Override onCreate(Bundle savedInstanceState)63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 66 onCreatePreferences(); 67 mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 68 } 69 70 onCreatePreferences()71 private void onCreatePreferences() { 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 //============================ 88 // Preference callbacks 89 //============================ 90 91 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)92 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 93 super.onPreferenceTreeClick(preferenceScreen, preference); 94 return false; 95 } 96 97 /** 98 * Implements OnPreferenceClickListener interface 99 */ onPreferenceClick(Preference pref)100 public boolean onPreferenceClick(Preference pref) { 101 if (pref == mWifiDisconnect) { 102 mWifiManager.disconnect(); 103 } else if (pref == mWifiDisableNetwork) { 104 AlertDialog.Builder alert = new AlertDialog.Builder(this); 105 alert.setTitle("Input"); 106 alert.setMessage("Enter Network ID"); 107 // Set an EditText view to get user input 108 final EditText input = new EditText(this); 109 alert.setView(input); 110 alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 111 public void onClick(DialogInterface dialog, int whichButton) { 112 Editable value = input.getText(); 113 netid = Integer.parseInt(value.toString()); 114 mWifiManager.disableNetwork(netid); 115 } 116 }); 117 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 118 public void onClick(DialogInterface dialog, int whichButton) { 119 // Canceled. 120 } 121 }); 122 alert.show(); 123 } else if (pref == mWifiEnableNetwork) { 124 AlertDialog.Builder alert = new AlertDialog.Builder(this); 125 alert.setTitle("Input"); 126 alert.setMessage("Enter Network ID"); 127 // Set an EditText view to get user input 128 final EditText input = new EditText(this); 129 alert.setView(input); 130 alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 131 public void onClick(DialogInterface dialog, int whichButton) { 132 Editable value = input.getText(); 133 netid = Integer.parseInt(value.toString()); 134 mWifiManager.enableNetwork(netid, false); 135 } 136 }); 137 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 138 public void onClick(DialogInterface dialog, int whichButton) { 139 // Canceled. 140 } 141 }); 142 alert.show(); 143 } 144 return true; 145 } 146 } 147