Search in sources :

Example 81 with WifiManager

use of android.net.wifi.WifiManager in project android_packages_apps_Settings by DirtyUnicorns.

the class DataPlanUsageSummaryTest method testUpdateNetworkRestrictionSummary_shouldSetSummary.

@Test
public void testUpdateNetworkRestrictionSummary_shouldSetSummary() {
    mDataUsageSummary = spy(new DataPlanUsageSummary());
    NetworkRestrictionsPreference preference = mock(NetworkRestrictionsPreference.class);
    mPolicyEditor = mock(NetworkPolicyEditor.class);
    WifiManager wifiManager = mock(WifiManager.class);
    ReflectionHelpers.setField(mDataUsageSummary, "mPolicyEditor", mPolicyEditor);
    ReflectionHelpers.setField(mDataUsageSummary, "mWifiManager", wifiManager);
    when(wifiManager.getConfiguredNetworks()).thenReturn(new ArrayList<>());
    doReturn(mContext.getResources()).when(mDataUsageSummary).getResources();
    mDataUsageSummary.updateNetworkRestrictionSummary(preference);
    verify(preference).setSummary(mContext.getResources().getQuantityString(R.plurals.network_restrictions_summary, 0, 0));
}
Also used : WifiManager(android.net.wifi.WifiManager) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) Test(org.junit.Test)

Example 82 with WifiManager

use of android.net.wifi.WifiManager in project android_packages_apps_Settings by DirtyUnicorns.

the class DataUsageSummaryTest method testUpdateNetworkRestrictionSummary_shouldSetSummary.

@Test
public void testUpdateNetworkRestrictionSummary_shouldSetSummary() {
    final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary());
    final NetworkRestrictionsPreference preference = mock(NetworkRestrictionsPreference.class);
    final NetworkPolicyEditor policyEditor = mock(NetworkPolicyEditor.class);
    final WifiManager wifiManager = mock(WifiManager.class);
    ReflectionHelpers.setField(dataUsageSummary, "mPolicyEditor", policyEditor);
    ReflectionHelpers.setField(dataUsageSummary, "mWifiManager", wifiManager);
    when(wifiManager.getConfiguredNetworks()).thenReturn(new ArrayList<WifiConfiguration>());
    doReturn(mContext.getResources()).when(dataUsageSummary).getResources();
    dataUsageSummary.updateNetworkRestrictionSummary(preference);
    verify(preference).setSummary(mContext.getResources().getQuantityString(R.plurals.network_restrictions_summary, 0, 0));
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiConfiguration(android.net.wifi.WifiConfiguration) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) Test(org.junit.Test)

Example 83 with WifiManager

use of android.net.wifi.WifiManager in project PhoneProfiles by henrichg.

the class WifiSSIDData method fillWifiConfigurationList.

static void fillWifiConfigurationList(Context context) {
    // if (wifiConfigurationList == null)
    // wifiConfigurationList = new ArrayList<WifiSSIDData>();
    List<WifiSSIDData> wifiConfigurationList = new ArrayList<>();
    WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifi == null)
        return;
    if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
        // wifi must be enabled for wifi.getConfiguredNetworks()
        return;
    List<WifiConfiguration> _wifiConfigurationList = wifi.getConfiguredNetworks();
    if (_wifiConfigurationList != null) {
        wifiConfigurationList.clear();
        for (WifiConfiguration device : _wifiConfigurationList) {
            boolean found = false;
            for (WifiSSIDData _device : wifiConfigurationList) {
                // if (_device.bssid.equals(device.BSSID))
                if ((_device.ssid != null) && (_device.ssid.equals(device.SSID))) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                wifiConfigurationList.add(new WifiSSIDData(device.SSID, device.BSSID));
            }
        }
    }
    SharedPreferences preferences = context.getSharedPreferences(PPApplication.WIFI_CONFIGURATION_LIST_PREFS_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.clear();
    editor.putInt(SCAN_RESULT_COUNT_PREF, wifiConfigurationList.size());
    Gson gson = new Gson();
    for (int i = 0; i < wifiConfigurationList.size(); i++) {
        String json = gson.toJson(wifiConfigurationList.get(i));
        editor.putString(SCAN_RESULT_DEVICE_PREF + i, json);
    }
    editor.apply();
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiConfiguration(android.net.wifi.WifiConfiguration) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson)

Example 84 with WifiManager

use of android.net.wifi.WifiManager in project wigle-wifi-wardriving by wiglenet.

the class MainActivity method finish.

// @Override
@Override
public void finish() {
    info("MAIN: finish. networks: " + state.wifiReceiver.getRunNetworkCount());
    final boolean wasFinishing = state.finishing.getAndSet(true);
    if (wasFinishing) {
        info("MAIN: finish called twice!");
    }
    // interrupt this just in case
    final ObservationUploader observationUploader = state.observationUploader;
    if (observationUploader != null) {
        observationUploader.setInterrupted();
    }
    if (state.gpsListener != null) {
        // save our location for later runs
        state.gpsListener.saveLocation();
    }
    // close the db. not in destroy, because it'll still write after that.
    state.dbHelper.close();
    final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (state.gpsListener != null) {
        locationManager.removeGpsStatusListener(state.gpsListener);
        try {
            locationManager.removeUpdates(state.gpsListener);
        } catch (final SecurityException ex) {
            error("SecurityException on finish: " + ex, ex);
        }
    }
    // stop the service, so when we die it's both stopped and unbound and will die
    final Intent serviceIntent = new Intent(this, WigleService.class);
    stopService(serviceIntent);
    try {
        // have to use the app context to bind to the service, cuz we're in tabs
        getApplicationContext().unbindService(state.serviceConnection);
    } catch (final IllegalArgumentException ex) {
        MainActivity.info("serviceConnection not registered: " + ex, ex);
    }
    // release the lock before turning wifi off
    if (state.wifiLock != null && state.wifiLock.isHeld()) {
        try {
            state.wifiLock.release();
        } catch (Exception ex) {
            MainActivity.error("exception releasing wifi lock: " + ex, ex);
        }
    }
    final SharedPreferences prefs = this.getSharedPreferences(ListFragment.SHARED_PREFS, 0);
    final boolean wifiWasOff = prefs.getBoolean(ListFragment.PREF_WIFI_WAS_OFF, false);
    // don't call on emulator, it crashes it
    if (wifiWasOff && !state.inEmulator) {
        // well turn it of now that we're done
        final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        MainActivity.info("turning back off wifi");
        try {
            wifiManager.setWifiEnabled(false);
        } catch (Exception ex) {
            MainActivity.error("exception turning wifi back off: " + ex, ex);
        }
    }
    TelephonyManager tele = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (tele != null && state.phoneState != null) {
        tele.listen(state.phoneState, PhoneStateListener.LISTEN_NONE);
    }
    if (state.tts != null) {
        if (!isMuted()) {
            // give time for the above "done" to be said
            sleep(250);
        }
        state.tts.shutdown();
    }
    // clean up.
    if (state.soundPop != null) {
        state.soundPop.release();
    }
    if (state.soundNewPop != null) {
        state.soundNewPop.release();
    }
    super.finish();
}
Also used : LocationManager(android.location.LocationManager) WifiManager(android.net.wifi.WifiManager) SharedPreferences(android.content.SharedPreferences) TelephonyManager(android.telephony.TelephonyManager) ObservationUploader(net.wigle.wigleandroid.background.ObservationUploader) Intent(android.content.Intent) IOException(java.io.IOException)

Example 85 with WifiManager

use of android.net.wifi.WifiManager in project wigle-wifi-wardriving by wiglenet.

the class MainActivity method setupWifi.

private void setupWifi() {
    // warn about turning off network notification
    final String notifOn = Settings.Secure.getString(getContentResolver(), Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON);
    if (notifOn != null && "1".equals(notifOn) && state.wifiReceiver == null && !isFinishing()) {
        WiGLEToast.showOverActivity(this, R.string.app_name, getString(R.string.best_results));
    }
    final WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    final SharedPreferences prefs = getSharedPreferences(ListFragment.SHARED_PREFS, 0);
    final Editor edit = prefs.edit();
    // keep track of for later
    boolean turnedWifiOn = false;
    if (!wifiManager.isWifiEnabled()) {
        // tell user, cuz this takes a little while
        if (!isFinishing()) {
            WiGLEToast.showOverActivity(this, R.string.app_name, getString(R.string.turn_on_wifi));
        }
        // save so we can turn it back off when we exit
        edit.putBoolean(ListFragment.PREF_WIFI_WAS_OFF, true);
        // just turn it on, but not in emulator cuz it crashes it
        if (!state.inEmulator) {
            MainActivity.info("turning on wifi");
            wifiManager.setWifiEnabled(true);
            MainActivity.info("wifi on");
            turnedWifiOn = true;
        }
    } else {
        edit.putBoolean(ListFragment.PREF_WIFI_WAS_OFF, false);
    }
    edit.apply();
    if (state.wifiReceiver == null) {
        MainActivity.info("new wifiReceiver");
        // wifi scan listener
        // this receiver is the main workhorse of the entire app
        state.wifiReceiver = new WifiReceiver(this, state.dbHelper);
        state.wifiReceiver.setupWifiTimer(turnedWifiOn);
    }
    // register wifi receiver
    setupWifiReceiverIntent();
    if (state.wifiLock == null) {
        MainActivity.info("lock wifi radio on");
        // lock the radio on
        state.wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, ListFragment.WIFI_LOCK_NAME);
        state.wifiLock.acquire();
    }
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiReceiver(net.wigle.wigleandroid.listener.WifiReceiver) SharedPreferences(android.content.SharedPreferences) Editor(android.content.SharedPreferences.Editor)

Aggregations

WifiManager (android.net.wifi.WifiManager)329 WifiInfo (android.net.wifi.WifiInfo)80 WifiConfiguration (android.net.wifi.WifiConfiguration)76 IOException (java.io.IOException)38 Intent (android.content.Intent)27 SuppressLint (android.annotation.SuppressLint)26 ConnectivityManager (android.net.ConnectivityManager)23 Context (android.content.Context)22 NetworkInfo (android.net.NetworkInfo)21 Test (org.junit.Test)20 AccessPoint (com.android.settingslib.wifi.AccessPoint)17 IntentFilter (android.content.IntentFilter)16 Bundle (android.os.Bundle)16 ArrayList (java.util.ArrayList)16 NetworkPolicyEditor (com.android.settingslib.NetworkPolicyEditor)12 PowerManager (android.os.PowerManager)11 WifiEnterpriseConfig (android.net.wifi.WifiEnterpriseConfig)10 SocketException (java.net.SocketException)10 SharedPreferences (android.content.SharedPreferences)9 Method (java.lang.reflect.Method)9