ConnectivityManagerTestActivity.java revision 6026d52710d7a6195a33885020d29aa1330fa855
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 17package com.android.connectivitymanagertest; 18 19import com.android.connectivitymanagertest.R; 20import android.app.Activity; 21import android.content.Context; 22import android.content.res.Resources; 23import android.content.BroadcastReceiver; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.os.Bundle; 27import android.provider.Settings; 28import android.util.Log; 29import android.view.KeyEvent; 30 31import java.io.InputStream; 32import java.util.ArrayList; 33import java.util.List; 34import android.widget.LinearLayout; 35import android.net.ConnectivityManager; 36import android.net.NetworkInfo; 37import android.net.NetworkInfo.State; 38 39import android.net.wifi.SupplicantState; 40import android.net.wifi.WifiConfiguration; 41import android.net.wifi.WifiManager; 42import android.net.wifi.WifiInfo; 43import android.net.wifi.ScanResult; 44import android.net.wifi.WifiConfiguration.KeyMgmt; 45 46/** 47 * An activity registered with connectivity manager broadcast 48 * provides network connectivity information and 49 * can be used to set device states: Cellular, Wifi, Airplane mode. 50 */ 51public class ConnectivityManagerTestActivity extends Activity { 52 53 public static final String LOG_TAG = "ConnectivityManagerTestActivity"; 54 public static final int WAIT_FOR_SCAN_RESULT = 10 * 1000; //10 seconds 55 public static final int WIFI_SCAN_TIMEOUT = 20 * 1000; 56 public static final int SHORT_TIMEOUT = 5 * 1000; 57 public static final long LONG_TIMEOUT = 50 * 1000; 58 public static final int SUCCESS = 0; // for Wifi tethering state change 59 public static final int FAILURE = 1; 60 public static final int INIT = -1; 61 private static final String ACCESS_POINT_FILE = "accesspoints.xml"; 62 public ConnectivityReceiver mConnectivityReceiver = null; 63 public WifiReceiver mWifiReceiver = null; 64 /* 65 * Track network connectivity information 66 */ 67 public State mState; 68 public NetworkInfo mNetworkInfo; 69 public NetworkInfo mOtherNetworkInfo; 70 public boolean mIsFailOver; 71 public String mReason; 72 public boolean mScanResultIsAvailable = false; 73 public ConnectivityManager mCM; 74 public Object wifiObject = new Object(); 75 public Object connectivityObject = new Object(); 76 public int mWifiState; 77 public NetworkInfo mWifiNetworkInfo; 78 public String mBssid; 79 public String mPowerSsid = "GoogleGuest"; //Default power SSID 80 private Context mContext; 81 82 /* 83 * Control Wifi States 84 */ 85 public WifiManager mWifiManager; 86 87 /* 88 * Verify connectivity state 89 */ 90 public static final int NUM_NETWORK_TYPES = ConnectivityManager.MAX_NETWORK_TYPE + 1; 91 NetworkState[] connectivityState = new NetworkState[NUM_NETWORK_TYPES]; 92 93 // For wifi tethering tests 94 private String[] mWifiRegexs; 95 public int mWifiTetherResult = INIT; // -1 is initialization state 96 97 /** 98 * A wrapper of a broadcast receiver which provides network connectivity information 99 * for all kinds of network: wifi, mobile, etc. 100 */ 101 private class ConnectivityReceiver extends BroadcastReceiver { 102 @Override 103 public void onReceive(Context context, Intent intent) { 104 Log.v(LOG_TAG, "ConnectivityReceiver: onReceive() is called with " + intent); 105 String action = intent.getAction(); 106 if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 107 Log.v("ConnectivityReceiver", "onReceive() called with " + intent); 108 return; 109 } 110 111 boolean noConnectivity = 112 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 113 114 if (noConnectivity) { 115 mState = State.DISCONNECTED; 116 } else { 117 mState = State.CONNECTED; 118 } 119 120 mNetworkInfo = (NetworkInfo) 121 intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 122 123 mOtherNetworkInfo = (NetworkInfo) 124 intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO); 125 126 mReason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); 127 mIsFailOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); 128 129 Log.v(LOG_TAG, "mNetworkInfo: " + mNetworkInfo.toString()); 130 if (mOtherNetworkInfo != null) { 131 Log.v(LOG_TAG, "mOtherNetworkInfo: " + mOtherNetworkInfo.toString()); 132 } 133 recordNetworkState(mNetworkInfo.getType(), mNetworkInfo.getState()); 134 if (mOtherNetworkInfo != null) { 135 recordNetworkState(mOtherNetworkInfo.getType(), mOtherNetworkInfo.getState()); 136 } 137 notifyNetworkConnectivityChange(); 138 } 139 } 140 141 private class WifiReceiver extends BroadcastReceiver { 142 @Override 143 public void onReceive(Context context, Intent intent) { 144 String action = intent.getAction(); 145 Log.v("WifiReceiver", "onReceive() is calleld with " + intent); 146 if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { 147 notifyScanResult(); 148 } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 149 mWifiNetworkInfo = 150 (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 151 Log.v(LOG_TAG, "mWifiNetworkInfo: " + mWifiNetworkInfo.toString()); 152 if (mWifiNetworkInfo.getState() == State.CONNECTED) { 153 mBssid = intent.getStringExtra(WifiManager.EXTRA_BSSID); 154 } 155 notifyWifiState(); 156 } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { 157 mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 158 WifiManager.WIFI_STATE_UNKNOWN); 159 notifyWifiState(); 160 } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { 161 notifyWifiAPState(); 162 } else if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) { 163 ArrayList<String> available = intent.getStringArrayListExtra( 164 ConnectivityManager.EXTRA_AVAILABLE_TETHER); 165 ArrayList<String> active = intent.getStringArrayListExtra( 166 ConnectivityManager.EXTRA_ACTIVE_TETHER); 167 ArrayList<String> errored = intent.getStringArrayListExtra( 168 ConnectivityManager.EXTRA_ERRORED_TETHER); 169 updateTetherState(available.toArray(), active.toArray(), errored.toArray()); 170 } 171 else { 172 return; 173 } 174 } 175 } 176 177 public ConnectivityManagerTestActivity() { 178 mState = State.UNKNOWN; 179 } 180 181 @Override 182 protected void onCreate(Bundle savedInstanceState) { 183 super.onCreate(savedInstanceState); 184 Log.v(LOG_TAG, "onCreate, inst=" + Integer.toHexString(hashCode())); 185 186 // Create a simple layout 187 LinearLayout contentView = new LinearLayout(this); 188 contentView.setOrientation(LinearLayout.VERTICAL); 189 setContentView(contentView); 190 setTitle("ConnectivityManagerTestActivity"); 191 192 193 // register a connectivity receiver for CONNECTIVITY_ACTION; 194 mConnectivityReceiver = new ConnectivityReceiver(); 195 registerReceiver(mConnectivityReceiver, 196 new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); 197 198 mWifiReceiver = new WifiReceiver(); 199 IntentFilter mIntentFilter = new IntentFilter(); 200 mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 201 mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 202 mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 203 mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); 204 mIntentFilter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); 205 mIntentFilter.addAction(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); 206 registerReceiver(mWifiReceiver, mIntentFilter); 207 208 // Get an instance of ConnectivityManager 209 mCM = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 210 // Get an instance of WifiManager 211 mWifiManager =(WifiManager)getSystemService(Context.WIFI_SERVICE); 212 initializeNetworkStates(); 213 214 if (mWifiManager.isWifiEnabled()) { 215 Log.v(LOG_TAG, "Clear Wifi before we start the test."); 216 removeConfiguredNetworksAndDisableWifi(); 217 } 218 mWifiRegexs = mCM.getTetherableWifiRegexs(); 219 } 220 221 public List<WifiConfiguration> loadNetworkConfigurations() throws Exception { 222 InputStream in = getAssets().open(ACCESS_POINT_FILE); 223 AccessPointParserHelper parseHelper = new AccessPointParserHelper(); 224 return parseHelper.processAccessPoint(in); 225 } 226 227 private void printNetConfig(String[] configuration) { 228 for (int i = 0; i < configuration.length; i++) { 229 if (i == 0) { 230 Log.v(LOG_TAG, "SSID: " + configuration[0]); 231 } else { 232 Log.v(LOG_TAG, " " + configuration[i]); 233 } 234 } 235 } 236 237 // for each network type, initialize network states to UNKNOWN, and no verification flag is set 238 public void initializeNetworkStates() { 239 for (int networkType = NUM_NETWORK_TYPES - 1; networkType >=0; networkType--) { 240 connectivityState[networkType] = new NetworkState(); 241 Log.v(LOG_TAG, "Initialize network state for " + networkType + ": " + 242 connectivityState[networkType].toString()); 243 } 244 } 245 246 // deposit a network state 247 public void recordNetworkState(int networkType, State networkState) { 248 Log.v(LOG_TAG, "record network state for network " + networkType + 249 ", state is " + networkState); 250 connectivityState[networkType].recordState(networkState); 251 } 252 253 // set the state transition criteria 254 public void setStateTransitionCriteria(int networkType, State initState, 255 int transitionDir, State targetState) { 256 connectivityState[networkType].setStateTransitionCriteria( 257 initState, transitionDir, targetState); 258 } 259 260 // Validate the states recorded 261 public boolean validateNetworkStates(int networkType) { 262 Log.v(LOG_TAG, "validate network state for " + networkType + ": "); 263 return connectivityState[networkType].validateStateTransition(); 264 } 265 266 // return result from network state validation 267 public String getTransitionFailureReason(int networkType) { 268 Log.v(LOG_TAG, "get network state transition failure reason for " + networkType + ": " + 269 connectivityState[networkType].toString()); 270 return connectivityState[networkType].getReason(); 271 } 272 273 private void notifyNetworkConnectivityChange() { 274 synchronized(connectivityObject) { 275 Log.v(LOG_TAG, "notify network connectivity changed"); 276 connectivityObject.notifyAll(); 277 } 278 } 279 private void notifyScanResult() { 280 synchronized (this) { 281 Log.v(LOG_TAG, "notify that scan results are available"); 282 this.notify(); 283 } 284 } 285 286 private void notifyWifiState() { 287 synchronized (wifiObject) { 288 Log.v(LOG_TAG, "notify wifi state changed"); 289 wifiObject.notify(); 290 } 291 } 292 293 private void notifyWifiAPState() { 294 synchronized (this) { 295 Log.v(LOG_TAG, "notify wifi AP state changed"); 296 this.notify(); 297 } 298 } 299 300 // Update wifi tethering state 301 private void updateTetherState(Object[] available, Object[] tethered, Object[] errored) { 302 boolean wifiTethered = false; 303 boolean wifiErrored = false; 304 305 synchronized (this) { 306 for (Object obj: tethered) { 307 String str = (String)obj; 308 for (String tethRex: mWifiRegexs) { 309 Log.v(LOG_TAG, "str: " + str +"tethRex: " + tethRex); 310 if (str.matches(tethRex)) { 311 wifiTethered = true; 312 } 313 } 314 } 315 316 for (Object obj: errored) { 317 String str = (String)obj; 318 for (String tethRex: mWifiRegexs) { 319 Log.v(LOG_TAG, "error: str: " + str +"tethRex: " + tethRex); 320 if (str.matches(tethRex)) { 321 wifiErrored = true; 322 } 323 } 324 } 325 326 if (wifiTethered) { 327 mWifiTetherResult = SUCCESS; // wifi tethering is successful 328 } else if (wifiErrored) { 329 mWifiTetherResult = FAILURE; // wifi tethering failed 330 } 331 Log.v(LOG_TAG, "mWifiTetherResult: " + mWifiTetherResult); 332 this.notify(); 333 } 334 } 335 336 337 // Wait for network connectivity state: CONNECTING, CONNECTED, SUSPENDED, 338 // DISCONNECTING, DISCONNECTED, UNKNOWN 339 public boolean waitForNetworkState(int networkType, State expectedState, long timeout) { 340 long startTime = System.currentTimeMillis(); 341 while (true) { 342 if ((System.currentTimeMillis() - startTime) > timeout) { 343 if (mCM.getNetworkInfo(networkType).getState() != expectedState) { 344 return false; 345 } else { 346 // the broadcast has been sent out. the state has been changed. 347 Log.v(LOG_TAG, "networktype: " + networkType + " state: " + 348 mCM.getNetworkInfo(networkType)); 349 return true; 350 } 351 } 352 Log.v(LOG_TAG, "Wait for the connectivity state for network: " + networkType + 353 " to be " + expectedState.toString()); 354 synchronized (connectivityObject) { 355 try { 356 connectivityObject.wait(SHORT_TIMEOUT); 357 } catch (InterruptedException e) { 358 e.printStackTrace(); 359 } 360 if ((mNetworkInfo.getType() != networkType) || 361 (mNetworkInfo.getState() != expectedState)) { 362 Log.v(LOG_TAG, "network state for " + mNetworkInfo.getType() + 363 "is: " + mNetworkInfo.getState()); 364 continue; 365 } 366 return true; 367 } 368 } 369 } 370 371 // Wait for Wifi state: WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED, 372 // WIFI_STATE_ENALBING, WIFI_STATE_UNKNOWN 373 public boolean waitForWifiState(int expectedState, long timeout) { 374 long startTime = System.currentTimeMillis(); 375 while (true) { 376 if ((System.currentTimeMillis() - startTime) > timeout) { 377 if (mWifiState != expectedState) { 378 return false; 379 } else { 380 return true; 381 } 382 } 383 Log.v(LOG_TAG, "Wait for wifi state to be: " + expectedState); 384 synchronized (wifiObject) { 385 try { 386 wifiObject.wait(SHORT_TIMEOUT); 387 } catch (InterruptedException e) { 388 e.printStackTrace(); 389 } 390 if (mWifiState != expectedState) { 391 Log.v(LOG_TAG, "Wifi state is: " + mWifiNetworkInfo.getState()); 392 continue; 393 } 394 return true; 395 } 396 } 397 } 398 399 // Wait for Wifi AP state: WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_DISABLING, 400 // WIFI_AP_STATE_ENABLED, WIFI_STATE_ENALBING, WIFI_STATE_UNKNOWN 401 public boolean waitForWifiAPState(int expectedState, long timeout) { 402 long startTime = System.currentTimeMillis(); 403 while (true) { 404 if ((System.currentTimeMillis() - startTime) > timeout) { 405 if (mWifiManager.getWifiApState() != expectedState) { 406 return false; 407 } else { 408 return true; 409 } 410 } 411 Log.v(LOG_TAG, "Wait for wifi AP state to be: " + expectedState); 412 synchronized (wifiObject) { 413 try { 414 wifiObject.wait(SHORT_TIMEOUT); 415 } catch (InterruptedException e) { 416 e.printStackTrace(); 417 } 418 if (mWifiManager.getWifiApState() != expectedState) { 419 Log.v(LOG_TAG, "Wifi state is: " + mWifiManager.getWifiApState()); 420 continue; 421 } 422 return true; 423 } 424 } 425 } 426 427 /** 428 * Wait for the wifi tethering result: 429 * @param timeout is the maximum waiting time 430 * @return SUCCESS if tethering result is successful 431 * FAILURE if tethering result returns error. 432 */ 433 public int waitForTetherStateChange(long timeout) { 434 long startTime = System.currentTimeMillis(); 435 while (true) { 436 if ((System.currentTimeMillis() - startTime) > timeout) { 437 return mWifiTetherResult; 438 } 439 Log.v(LOG_TAG, "Wait for wifi tethering result."); 440 synchronized (this) { 441 try { 442 this.wait(SHORT_TIMEOUT); 443 } catch (InterruptedException e) { 444 e.printStackTrace(); 445 } 446 if (mWifiTetherResult == INIT ) { 447 continue; 448 } else { 449 return mWifiTetherResult; 450 } 451 } 452 } 453 } 454 455 // Return true if device is currently connected to mobile network 456 public boolean isConnectedToMobile() { 457 return (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE); 458 } 459 460 // Return true if device is currently connected to Wifi 461 public boolean isConnectedToWifi() { 462 return (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI); 463 } 464 465 public boolean enableWifi() { 466 return mWifiManager.setWifiEnabled(true); 467 } 468 469 /** 470 * Associate the device to given SSID 471 * If the device is already associated with a WiFi, disconnect and forget it, 472 * We don't verify whether the connection is successful or not, leave this to the test 473 */ 474 public boolean connectToWifi(String knownSSID) { 475 WifiConfiguration config = new WifiConfiguration(); 476 config.SSID = knownSSID; 477 config.allowedKeyManagement.set(KeyMgmt.NONE); 478 return connectToWifiWithConfiguration(config); 479 } 480 481 /** 482 * Connect to Wi-Fi with the given configuration. Note the SSID in the configuration 483 * is pure string, we need to convert it to quoted string. 484 * @param config 485 * @return 486 */ 487 public boolean connectToWifiWithConfiguration(WifiConfiguration config) { 488 String ssid = config.SSID; 489 config.SSID = convertToQuotedString(ssid); 490 491 //If Wifi is not enabled, enable it 492 if (!mWifiManager.isWifiEnabled()) { 493 Log.v(LOG_TAG, "Wifi is not enabled, enable it"); 494 mWifiManager.setWifiEnabled(true); 495 } 496 497 List<ScanResult> netList = mWifiManager.getScanResults(); 498 if (netList == null) { 499 Log.v(LOG_TAG, "scan results are null"); 500 // if no scan results are available, start active scan 501 mWifiManager.startScanActive(); 502 mScanResultIsAvailable = false; 503 long startTime = System.currentTimeMillis(); 504 while (!mScanResultIsAvailable) { 505 if ((System.currentTimeMillis() - startTime) > WIFI_SCAN_TIMEOUT) { 506 return false; 507 } 508 // wait for the scan results to be available 509 synchronized (this) { 510 // wait for the scan result to be available 511 try { 512 this.wait(WAIT_FOR_SCAN_RESULT); 513 } catch (InterruptedException e) { 514 e.printStackTrace(); 515 } 516 if ((mWifiManager.getScanResults() == null) || 517 (mWifiManager.getScanResults().size() <= 0)) { 518 continue; 519 } 520 mScanResultIsAvailable = true; 521 } 522 } 523 } 524 525 netList = mWifiManager.getScanResults(); 526 527 for (int i = 0; i < netList.size(); i++) { 528 ScanResult sr= netList.get(i); 529 if (sr.SSID.equals(ssid)) { 530 Log.v(LOG_TAG, "found " + ssid + " in the scan result list"); 531 int networkId = mWifiManager.addNetwork(config); 532 // Connect to network by disabling others. 533 mWifiManager.enableNetwork(networkId, true); 534 mWifiManager.saveConfiguration(); 535 List<WifiConfiguration> wifiNetworks = mWifiManager.getConfiguredNetworks(); 536 for (WifiConfiguration netConfig : wifiNetworks) { 537 Log.v(LOG_TAG, netConfig.toString()); 538 } 539 540 mWifiManager.reconnect(); 541 break; 542 } 543 } 544 545 List<WifiConfiguration> netConfList = mWifiManager.getConfiguredNetworks(); 546 if (netConfList.size() <= 0) { 547 Log.v(LOG_TAG, ssid + " is not available"); 548 return false; 549 } 550 return true; 551 } 552 553 /* 554 * Disconnect from the current AP and remove configured networks. 555 */ 556 public boolean disconnectAP() { 557 if (mWifiManager.isWifiEnabled()) { 558 //remove the current network Id 559 WifiInfo curWifi = mWifiManager.getConnectionInfo(); 560 if (curWifi == null) { 561 return false; 562 } 563 int curNetworkId = curWifi.getNetworkId(); 564 mWifiManager.removeNetwork(curNetworkId); 565 mWifiManager.saveConfiguration(); 566 567 // remove other saved networks 568 List<WifiConfiguration> netConfList = mWifiManager.getConfiguredNetworks(); 569 if (netConfList != null) { 570 Log.v(LOG_TAG, "remove configured network ids"); 571 for (int i = 0; i < netConfList.size(); i++) { 572 WifiConfiguration conf = new WifiConfiguration(); 573 conf = netConfList.get(i); 574 mWifiManager.removeNetwork(conf.networkId); 575 } 576 } 577 } 578 mWifiManager.saveConfiguration(); 579 return true; 580 } 581 /** 582 * Disable Wifi 583 * @return true if Wifi is disabled successfully 584 */ 585 public boolean disableWifi() { 586 return mWifiManager.setWifiEnabled(false); 587 } 588 589 /** 590 * Remove configured networks and disable wifi 591 */ 592 public boolean removeConfiguredNetworksAndDisableWifi() { 593 if (!disconnectAP()) { 594 return false; 595 } 596 // Disable Wifi 597 if (!mWifiManager.setWifiEnabled(false)) { 598 return false; 599 } 600 // Wait for the actions to be completed 601 try { 602 Thread.sleep(5*1000); 603 } catch (InterruptedException e) {} 604 return true; 605 } 606 607 /** 608 * Set airplane mode 609 */ 610 public void setAirplaneMode(Context context, boolean enableAM) { 611 //set the airplane mode 612 Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 613 enableAM ? 1 : 0); 614 // Post the intent 615 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 616 intent.putExtra("state", enableAM); 617 context.sendBroadcast(intent); 618 } 619 620 protected static String convertToQuotedString(String string) { 621 return "\"" + string + "\""; 622 } 623 624 @Override 625 protected void onDestroy() { 626 super.onDestroy(); 627 628 //Unregister receiver 629 if (mConnectivityReceiver != null) { 630 unregisterReceiver(mConnectivityReceiver); 631 } 632 if (mWifiReceiver != null) { 633 unregisterReceiver(mWifiReceiver); 634 } 635 Log.v(LOG_TAG, "onDestroy, inst=" + Integer.toHexString(hashCode())); 636 } 637 638 @Override 639 public void onStart() { 640 super.onStart(); 641 mContext = this; 642 Bundle bundle = this.getIntent().getExtras(); 643 if (bundle != null){ 644 mPowerSsid = bundle.getString("power_ssid"); 645 } 646 } 647 //A thread to set the device into airplane mode then turn on wifi. 648 Thread setDeviceWifiAndAirplaneThread = new Thread(new Runnable() { 649 public void run() { 650 setAirplaneMode(mContext, true); 651 connectToWifi(mPowerSsid); 652 } 653 }); 654 655 //A thread to set the device into wifi 656 Thread setDeviceInWifiOnlyThread = new Thread(new Runnable() { 657 public void run() { 658 connectToWifi(mPowerSsid); 659 } 660 }); 661 662 @Override 663 public boolean onKeyDown(int keyCode, KeyEvent event) { 664 switch (keyCode) { 665 //This is a tricky way for the scripted monkey to 666 //set the device in wifi and wifi in airplane mode. 667 case KeyEvent.KEYCODE_1: 668 setDeviceWifiAndAirplaneThread.start(); 669 break; 670 671 case KeyEvent.KEYCODE_2: 672 setDeviceInWifiOnlyThread.start(); 673 break; 674 } 675 return super.onKeyDown(keyCode, event); 676 } 677} 678