@Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { synchronized (mMySync) { mMySync.expectedState = STATE_WIFI_CHANGED; mMySync.notify(); } } else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { synchronized (mMySync) { mMySync.expectedState = STATE_SCAN_RESULTS_AVAILABLE; mMySync.notify(); } } }
@Override protected void setUp() throws Exception { super.setUp(); if (!WifiFeature.isWifiSupported(getContext())) { // skip the test if WiFi is not supported return; } mMySync = new MySync(); mIntentFilter = new IntentFilter(); mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.ACTION_PICK_WIFI_NETWORK); mContext.registerReceiver(mReceiver, mIntentFilter); mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); assertNotNull(mWifiManager); mWifiLock = mWifiManager.createWifiLock(TAG); mWifiLock.acquire(); if (!mWifiManager.isWifiEnabled()) setWifiEnabled(true); Thread.sleep(ENABLE_WAIT_MSEC); assertTrue(mWifiManager.isWifiEnabled()); mMySync.expectedState = STATE_NULL; }
private void scanAndWait() throws Exception { synchronized (mMySync) { mMySync.expectedState = STATE_START_SCAN; mWifiManager.startScan(); waitForBroadcast(SCAN_WAIT_MSEC, STATE_SCAN_RESULTS_AVAILABLE); } }
private void setWifiEnabled(boolean enable) throws Exception { synchronized (mMySync) { mMySync.expectedState = STATE_WIFI_CHANGING; assertTrue(mWifiManager.setWifiEnabled(enable)); waitForBroadcast(TIMEOUT_MSEC, STATE_WIFI_CHANGED); } }
private void waitForBroadcast(long timeout, int expectedState) throws Exception { long waitTime = System.currentTimeMillis() + timeout; while (System.currentTimeMillis() < waitTime && mMySync.expectedState != expectedState) mMySync.wait(WAIT_MSEC); }