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