Example usage for android.net ConnectivityManager TYPE_WIFI

List of usage examples for android.net ConnectivityManager TYPE_WIFI

Introduction

In this page you can find the example usage for android.net ConnectivityManager TYPE_WIFI.

Prototype

int TYPE_WIFI

To view the source code for android.net ConnectivityManager TYPE_WIFI.

Click Source Link

Document

A WIFI data connection.

Usage

From source file:org.wso2.emm.agent.services.DeviceNetworkStatus.java

/**
 * Network data such as  connection type and signal details can be fetched with this method.
 *
 * @return String representing network details.
 * @throws AndroidAgentException//w w w .  j  av a 2  s.  c  om
 */
public String getNetworkStatus() throws AndroidAgentException {
    info = getNetworkInfo(this.context);
    String payload = null;
    if (info != null) {
        List<Device.Property> properties = new ArrayList<>();
        Device.Property property = new Device.Property();
        property.setName(Constants.Device.CONNECTION_TYPE);
        property.setValue(info.getTypeName());
        properties.add(property);

        if ((info.isConnected())) {
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                property = new Device.Property();
                property.setName(Constants.Device.MOBILE_CONNECTION_TYPE);
                property.setValue(info.getSubtypeName());
                properties.add(property);
            }
            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                property = new Device.Property();
                property.setName(Constants.Device.WIFI_SSID);
                // NetworkInfo API of Android seem to add extra "" to SSID, therefore escaping it.
                property.setValue(String.valueOf(getWifiSSID()).replaceAll("\"", ""));
                properties.add(property);

                property = new Device.Property();
                property.setName(Constants.Device.WIFI_SIGNAL_STRENGTH);
                property.setValue(String.valueOf(getWifiSignalStrength()));
                properties.add(property);

            }
        }

        property = new Device.Property();
        property.setName(Constants.Device.MOBILE_SIGNAL_STRENGTH);
        property.setValue(String.valueOf(getCellSignalStrength()));
        properties.add(property);

        try {
            payload = mapper.writeValueAsString(properties);
        } catch (JsonProcessingException e) {
            String errorMsg = "Error occurred while parsing " + "network property property object to json.";
            Log.e(TAG, errorMsg, e);
            throw new AndroidAgentException(errorMsg, e);
        }
    }
    return payload;
}

From source file:com.bangz.smartmute.WifiEditActivity.java

@Override
public void onSuccessUpdateDatabase(Uri uri) {

    if (bActivited == false)
        return;/*w w w  .  ja  v a  2  s .c o m*/

    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ninfo = cm.getActiveNetworkInfo();

    if (ninfo != null && ninfo.isConnected() && ninfo.getType() == ConnectivityManager.TYPE_WIFI) {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String currssid = wifiInfo.getSSID();
            if (currssid.equals(strSSID)) {
                WifiMuteService.wifiConnected(this, strSSID);
            }
        }
    }

}

From source file:org.dvbviewer.controller.ui.fragments.ChannelList.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getActivity().getApplicationContext();

    ConnectivityManager connManager = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    mNetworkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    prefs = new DVBViewerPreferences(getActivity());
    showFavs = prefs.getPrefs().getBoolean(DVBViewerPreferences.KEY_CHANNELS_USE_FAVS, false);
    showNowPlaying = prefs.getPrefs().getBoolean(DVBViewerPreferences.KEY_CHANNELS_SHOW_NOW_PLAYING, true);
    showNowPlayingWifi = prefs.getPrefs()
            .getBoolean(DVBViewerPreferences.KEY_CHANNELS_SHOW_NOW_PLAYING_WIFI_ONLY, true);
    mAdapter = new ChannelAdapter(getActivity());
    if (getArguments() != null) {
        if (getArguments().containsKey(ChannelList.KEY_HAS_OPTIONMENU)) {
            hasOptionsMenu = getArguments().getBoolean(KEY_HAS_OPTIONMENU);
        }//from w  ww.  j a  v  a  2s  . co  m
    }
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey(KEY_SELECTED_POSITION)) {
            selectedPosition = savedInstanceState.getInt(KEY_SELECTED_POSITION);
        }
    } else {
        selectedPosition = getActivity().getIntent().getIntExtra(KEY_SELECTED_POSITION, selectedPosition);
    }
    setHasOptionsMenu(hasOptionsMenu);
}

From source file:com.mobeelizer.mobile.android.MobeelizerRealConnectionManagerTest.java

@Before
public void init() throws Exception {
    PowerMockito.mockStatic(Log.class);
    PowerMockito.when(Log.class, "i", anyString(), anyString()).thenReturn(0);
    PowerMockito.when(Log.class, "e", anyString(), anyString()).thenReturn(0);
    PowerMockito.when(Log.class, "e", anyString(), anyString(), any(Throwable.class)).thenReturn(0);

    PowerMockito.mockStatic(Proxy.class);
    PowerMockito.when(Proxy.class, "getHost", any(Context.class)).thenReturn(null);
    PowerMockito.when(Proxy.class, "getPort", any(Context.class)).thenReturn(-1);

    httpClient = PowerMockito.mock(DefaultHttpClient.class);
    whenNew(DefaultHttpClient.class)
            .withArguments(any(ClientConnectionManager.class), any(BasicHttpParams.class))
            .thenReturn(httpClient);/*from  w  w  w . j  av a  2s .  c o  m*/

    httpConnectionManager = PowerMockito.mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(httpConnectionManager);

    httpGet = PowerMockito.mock(HttpGet.class);
    whenNew(HttpGet.class).withArguments(anyString()).thenReturn(httpGet);

    httpPost = PowerMockito.mock(HttpPost.class);
    whenNew(HttpPost.class).withArguments(anyString()).thenReturn(httpPost);

    httpResponse = PowerMockito.mock(HttpResponse.class);
    when(httpClient.execute(httpGet)).thenReturn(httpResponse);
    when(httpClient.execute(httpPost)).thenReturn(httpResponse);

    httpEntity = PowerMockito.mock(HttpEntity.class);
    when(httpResponse.getEntity()).thenReturn(httpEntity);

    httpStatusLine = PowerMockito.mock(StatusLine.class);
    when(httpResponse.getStatusLine()).thenReturn(httpStatusLine);
    when(httpStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);

    application = mock(MobeelizerApplication.class);
    when(application.getVendor()).thenReturn("vendor");
    when(application.getApplication()).thenReturn("application");
    when(application.getInstance()).thenReturn("instance");
    when(application.getDevice()).thenReturn("device");
    when(application.getDeviceIdentifier()).thenReturn("deviceIdentifier");
    when(application.getUser()).thenReturn("user");
    when(application.getPassword()).thenReturn("password");
    when(application.getUrl()).thenReturn("http://url/app");

    Context context = PowerMockito.mock(Context.class);
    when(application.getContext()).thenReturn(context);

    ConnectivityManager connectivityService = PowerMockito.mock(ConnectivityManager.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityService);

    networkWifiInfo = PowerMockito.mock(NetworkInfo.class);
    when(connectivityService.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkWifiInfo);
    when(networkWifiInfo.isConnected()).thenReturn(true);

    NetworkInfo networkMobileInfo = PowerMockito.mock(NetworkInfo.class);
    when(connectivityService.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)).thenReturn(networkMobileInfo);

    database = mock(MobeelizerInternalDatabase.class);
    when(application.getInternalDatabase()).thenReturn(database);

    connectionManager = new MobeelizerRealConnectionManager(application);
}

From source file:de.eidottermihi.rpicheck.widget.OverclockingWidget.java

private static boolean isWiFiAvailable(Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return wifiInfo != null && wifiInfo.isConnected();
}

From source file:dev.ukanth.ufirewall.InterfaceTracker.java

private static InterfaceDetails getInterfaceDetails(Context context) {
    InterfaceDetails ret = new InterfaceDetails();

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();

    if (info == null || info.isConnected() == false) {
        return ret;
    }//w ww.j  a va  2  s .  c  om

    switch (info.getType()) {
    case ConnectivityManager.TYPE_MOBILE:
    case ConnectivityManager.TYPE_MOBILE_DUN:
    case ConnectivityManager.TYPE_MOBILE_HIPRI:
    case ConnectivityManager.TYPE_MOBILE_MMS:
    case ConnectivityManager.TYPE_MOBILE_SUPL:
    case ConnectivityManager.TYPE_WIMAX:
        ret.isRoaming = info.isRoaming();
        ret.netType = ConnectivityManager.TYPE_MOBILE;
        ret.netEnabled = true;
        break;
    case ConnectivityManager.TYPE_WIFI:
    case ConnectivityManager.TYPE_BLUETOOTH:
    case ConnectivityManager.TYPE_ETHERNET:
        ret.netType = ConnectivityManager.TYPE_WIFI;
        ret.netEnabled = true;
        break;
    }
    getTetherStatus(context, ret);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
        OldInterfaceScanner.populateLanMasks(context, ITFS_WIFI, ret);
    } else {
        NewInterfaceScanner.populateLanMasks(context, ITFS_WIFI, ret);
    }

    return ret;
}

From source file:de.stadtrallye.rallyesoft.services.UploadService.java

private void updateNetworkStatus() {
    NetworkInfo activeNetwork = connection.getActiveNetworkInfo();
    conn_available = activeNetwork.isConnected();

    switch (activeNetwork.getType()) {
    case (ConnectivityManager.TYPE_WIFI):
        conn_metered = false;//www  .  j a va 2s .c o m
        conn_slow = false;
        break;
    case (ConnectivityManager.TYPE_MOBILE): {
        conn_metered = true;
        switch (telephony.getNetworkType()) {
        case (TelephonyManager.NETWORK_TYPE_LTE | TelephonyManager.NETWORK_TYPE_HSPAP
                | TelephonyManager.NETWORK_TYPE_HSPA)://TODO more + check
            conn_slow = false;
            break;
        case (TelephonyManager.NETWORK_TYPE_EDGE | TelephonyManager.NETWORK_TYPE_GPRS):
            conn_slow = true;
            break;
        default:
            conn_slow = false;
            break;
        }
        break;
    }
    default:
        conn_metered = false;
        conn_slow = false;
        break;
    }
    Log.d(THIS,
            "Network: available: " + conn_available + ", metered: " + conn_metered + ", slow: " + conn_slow);
}

From source file:org.cirdles.chroni.HomeScreenActivity.java

/**
 * Creates the necessary application directories: CIRDLES, Aliquot and Report Settings folders
 *///www.  j a  v a  2 s.  co  m
protected void createDirectories() throws FileNotFoundException {

    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
    }

    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        // establishes the CIRDLES directories
        File chroniDirectory = new File(Environment.getExternalStorageDirectory() + "/CHRONI/");
        File aliquotDirectory = new File(Environment.getExternalStorageDirectory() + "/CHRONI/Aliquot");
        File reportSettingsDirectory = new File(
                Environment.getExternalStorageDirectory() + "/CHRONI/Report Settings");

        // gives default aliquot a path
        File defaultAliquotDirectory = new File(reportSettingsDirectory, "Default Aliquot");

        defaultReportSettingsPresent = false; // determines whether the default report settings is present or not
        defaultReportSettings2Present = false; // determines whether the default report settings 2 is present or not
        defaultAliquotPresent = false; // determines whether the aliquot is present or not

        // creates the directories if they are not there
        chroniDirectory.mkdirs();
        aliquotDirectory.mkdirs();
        reportSettingsDirectory.mkdirs();

        // checks internet connection before downloading files
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        // checks to see if the default Report Settings files are present
        File[] reportSettingsFiles = reportSettingsDirectory.listFiles(); // Lists files in CHRONI directory
        for (File f : reportSettingsFiles) {
            if (f.getName().contentEquals("Default Report Settings.xml"))
                defaultReportSettingsPresent = true;

            if (f.getName().contentEquals("Default Report Settings 2.xml"))
                defaultReportSettings2Present = true;
        }

        // checks to see if the default Aliquot file is present
        File[] aliquotFiles = aliquotDirectory.listFiles();
        for (File f : aliquotFiles)
            if (f.getName().contentEquals("Default Aliquot.xml"))
                defaultAliquotPresent = true;

        if (mobileWifi.isConnected()) {
            // Downloads default report settings 1 if not present
            if (!defaultReportSettingsPresent) {
                // Downloads the default report settings file if absent
                URLFileReader downloader = new URLFileReader(HomeScreenActivity.this, "HomeScreen",
                        "https://raw.githubusercontent.com/CIRDLES/cirdles.github.com/master/assets/Default%20Report%20Settings%20XML/Default%20Report%20Settings.xml",
                        "url");
                downloader.startFileDownload(); // begins download
                defaultReportSettingsPresent = true;
                saveInitialLaunch();
                saveCurrentReportSettings(); // Notes that files have been downloaded and application has been properly initialized
            }

            if (!defaultReportSettings2Present) {
                // Downloads the second default report settings file if absent
                URLFileReader downloader2 = new URLFileReader(HomeScreenActivity.this, "HomeScreen",
                        "https://raw.githubusercontent.com/CIRDLES/cirdles.github.com/master/assets/Default%20Report%20Settings%20XML/Default%20Report%20Settings%202.xml",
                        "url");
                downloader2.startFileDownload(); // begins download
                defaultReportSettings2Present = true;
                saveInitialLaunch();
                saveCurrentReportSettings(); // Notes that files have been downloaded and application has been properly initialized
            }

            if (!defaultAliquotPresent) {
                URLFileReader downloader3 = new URLFileReader(HomeScreenActivity.this, "HomeScreenAliquot",
                        "https://raw.githubusercontent.com/CIRDLES/cirdles.github.com/master/assets/Default-Aliquot-XML/Default%20Aliquot.xml",
                        "url");
                downloader3.startFileDownload();
                defaultAliquotPresent = true;
                saveInitialLaunch();
                saveCurrentAliquot();
            }

        }
    }

}

From source file:com.nextgis.metroaccess.MetaDownloader.java

static boolean IsNetworkAvailible(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null) {
        int netType = info.getType();
        if (netType == ConnectivityManager.TYPE_WIFI) {
            return info.isConnected();
        } else if (netType == ConnectivityManager.TYPE_MOBILE && !tm.isNetworkRoaming()) {
            return info.isConnected();
        }//www.ja v  a2s .  c  o m
    }
    return false;
}

From source file:com.lemi.mario.download.utils.Proxy.java

static final private boolean isNetworkWifi(Context context) {
    if (context == null) {
        return false;
    }//w w w .ja va 2 s .c o  m

    final ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        final NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
    }

    return false;
}