Example usage for android.net NetworkInfo getType

List of usage examples for android.net NetworkInfo getType

Introduction

In this page you can find the example usage for android.net NetworkInfo getType.

Prototype

@Deprecated
public int getType() 

Source Link

Document

Reports the type of network to which the info in this NetworkInfo pertains.

Usage

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

public boolean isMobileConnected() {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }/*from  w  ww  .j  a va 2 s  .c om*/
    return networkInfo.getState() == NetworkInfo.State.CONNECTED
            && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}

From source file:com.facebook.react.modules.netinfo.NetInfoModule.java

private String getCurrentConnectionType() {
    try {//  w ww.  java2s .c o  m
        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnected()) {
            return CONNECTION_TYPE_NONE;
        } else if (ConnectivityManager.isNetworkTypeValid(networkInfo.getType())) {
            return networkInfo.getTypeName().toUpperCase();
        } else {
            return CONNECTION_TYPE_UNKNOWN;
        }
    } catch (SecurityException e) {
        mNoNetworkPermission = true;
        return CONNECTION_TYPE_UNKNOWN;
    }
}

From source file:com.github.ignition.support.http.IgnitedHttpClient.java

public void updateProxySettings() {
    if (appContext == null) {
        return;/* www .j  a  v  a  2  s  . co m*/
    }
    HttpParams httpParams = httpClient.getParams();
    ConnectivityManager connectivity = (ConnectivityManager) appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nwInfo = connectivity.getActiveNetworkInfo();
    if (nwInfo == null) {
        return;
    }
    Log.i(LOG_TAG, nwInfo.toString());
    if (nwInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        String proxyHost = Proxy.getHost(appContext);
        if (proxyHost == null) {
            proxyHost = Proxy.getDefaultHost();
        }
        int proxyPort = Proxy.getPort(appContext);
        if (proxyPort == -1) {
            proxyPort = Proxy.getDefaultPort();
        }
        if (proxyHost != null && proxyPort > -1) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } else {
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
        }
    } else {
        httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}

From source file:com.commonsware.android.webserver.WebServerService.java

@Override
public void onCreate() {
    super.onCreate();

    ConnectivityManager mgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ni = mgr.getActiveNetworkInfo();

    if (ni == null || ni.getType() == ConnectivityManager.TYPE_MOBILE) {
        EventBus.getDefault().post(new ServerStartRejectedEvent());
        stopSelf();/* w  w  w .j  a  v a2 s  .  com*/
    } else {
        handlebars = new Handlebars(new AssetTemplateLoader(getAssets()));
        rootPath = "/" + new BigInteger(20, rng).toString(24).toUpperCase();

        server = new AsyncHttpServer();

        if (configureRoutes(server)) {
            server.get("/.*", new AssetRequestCallback());
        }

        server.listen(getPort());

        raiseReadyEvent();
        foregroundify();
        timeoutFuture = timer.schedule(onTimeout, getMaxIdleTimeSeconds(), TimeUnit.SECONDS);
    }
}

From source file:com.master.metehan.filtereagle.Util.java

public static String getNetworkInfo(Context context) {
    StringBuilder sb = new StringBuilder();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo ani = cm.getActiveNetworkInfo();
    List<NetworkInfo> listNI = new ArrayList<>();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        listNI.addAll(Arrays.asList(cm.getAllNetworkInfo()));
    else//from  w  w w.  j  av a2s . c o m
        for (Network network : cm.getAllNetworks()) {
            NetworkInfo ni = cm.getNetworkInfo(network);
            if (ni != null)
                listNI.add(ni);
        }

    for (NetworkInfo ni : listNI) {
        sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ')
                .append(ni.getDetailedState())
                .append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo())
                .append(ni.getType() == ConnectivityManager.TYPE_MOBILE
                        ? " " + Util.getNetworkGeneration(ni.getSubtype())
                        : "")
                .append(ni.isRoaming() ? " R" : "")
                .append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype()
                        ? " *"
                        : "")
                .append("\r\n");
    }

    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        if (nis != null) {
            sb.append("\r\n");
            while (nis.hasMoreElements()) {
                NetworkInterface ni = nis.nextElement();
                if (ni != null) {
                    List<InterfaceAddress> ias = ni.getInterfaceAddresses();
                    if (ias != null)
                        for (InterfaceAddress ia : ias)
                            sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress())
                                    .append('/').append(ia.getNetworkPrefixLength()).append(' ')
                                    .append(ni.getMTU()).append("\r\n");
                }
            }
        }
    } catch (Throwable ex) {
        sb.append(ex.toString()).append("\r\n");
    }

    if (sb.length() > 2)
        sb.setLength(sb.length() - 2);

    return sb.toString();
}

From source file:osu.crowd_ml.BackgroundDataSend.java

private boolean isDataConnected() {
    try {/*  w  w  w .  j  a va  2s.  c  o  m*/
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();

        //TODO: See if making sure it's not a metered connection would be better? Consult: https://developer.android.com/reference/android/net/ConnectivityManager.html#isActiveNetworkMetered
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            return cm.getActiveNetworkInfo().isConnectedOrConnecting();
        } else {
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:org.telegram.messenger.GcmPushListenerService.java

@Override
public void onMessageReceived(String from, final Bundle bundle) {
    FileLog.d("GCM received bundle: " + bundle + " from: " + from);
    AndroidUtilities.runOnUIThread(new Runnable() {
        @Override/*from   ww  w. java 2 s.c om*/
        public void run() {
            ApplicationLoader.postInitApplication();

            try {
                String key = bundle.getString("loc_key");
                if ("DC_UPDATE".equals(key)) {
                    String data = bundle.getString("custom");
                    JSONObject object = new JSONObject(data);
                    int dc = object.getInt("dc");
                    String addr = object.getString("addr");
                    String[] parts = addr.split(":");
                    if (parts.length != 2) {
                        return;
                    }
                    String ip = parts[0];
                    int port = Integer.parseInt(parts[1]);
                    ConnectionsManager.getInstance().applyDatacenterAddress(dc, ip, port);
                } else if ("MESSAGE_ANNOUNCEMENT".equals(key)) {
                    Object obj = bundle.get("google.sent_time");
                    long time;
                    try {
                        if (obj instanceof String) {
                            time = Utilities.parseLong((String) obj);
                        } else if (obj instanceof Long) {
                            time = (Long) obj;
                        } else {
                            time = System.currentTimeMillis();
                        }
                    } catch (Exception ignore) {
                        time = System.currentTimeMillis();
                    }

                    TLRPC.TL_updateServiceNotification update = new TLRPC.TL_updateServiceNotification();
                    update.popup = false;
                    update.flags = 2;
                    update.inbox_date = (int) (time / 1000);
                    update.message = bundle.getString("message");
                    update.type = "announcement";
                    update.media = new TLRPC.TL_messageMediaEmpty();
                    final TLRPC.TL_updates updates = new TLRPC.TL_updates();
                    updates.updates.add(update);
                    Utilities.stageQueue.postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            MessagesController.getInstance().processUpdates(updates, false);
                        }
                    });
                } else if (Build.VERSION.SDK_INT >= 24 && ApplicationLoader.mainInterfacePaused
                        && UserConfig.isClientActivated()) {
                    Object value = bundle.get("badge");
                    if (value == null) {
                        Object obj = bundle.get("google.sent_time");
                        long time;
                        if (obj instanceof String) {
                            time = Utilities.parseLong((String) obj);
                        } else if (obj instanceof Long) {
                            time = (Long) obj;
                        } else {
                            time = -1;
                        }
                        if (time == -1 || UserConfig.lastAppPauseTime < time) {
                            ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext
                                    .getSystemService(Context.CONNECTIVITY_SERVICE);
                            NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
                            if (connectivityManager
                                    .getRestrictBackgroundStatus() == RESTRICT_BACKGROUND_STATUS_ENABLED
                                    && netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                                NotificationsController.getInstance().showSingleBackgroundNotification();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            ConnectionsManager.onInternalPushReceived();
            ConnectionsManager.getInstance().resumeNetworkMaybe();
        }
    });
}

From source file:net.mm2d.dmsexplorer.ServerListActivity.java

private boolean hasAvailableNetwork() {
    final NetworkInfo ni = mConnectivityManager.getActiveNetworkInfo();
    return ni != null && ni.isConnected() && (ni.getType() == ConnectivityManager.TYPE_WIFI
            || ni.getType() == ConnectivityManager.TYPE_ETHERNET);
}

From source file:com.inovex.zabbixmobile.push.NotificationService.java

private void logNotification(String status, String message, Long triggerid, String source) {
    // Logging incoming notifications for Debuggin
    // timestamp status message triggerid network
    try {// w w  w .  j a  v  a 2 s .c  o m
        File folder = new File(Environment.getExternalStorageDirectory() + "/zax");
        boolean var = false;
        if (!folder.exists()) {
            var = folder.mkdir();
        }
        final String filename = folder.toString() + "/" + "push_logs.csv";
        File csv = new File(filename);
        if (!csv.exists() || !csv.isFile()) {
            csv.createNewFile();
        }
        FileWriter fw = new FileWriter(csv, true);
        String date = Calendar.getInstance().getTime().toString();
        fw.append(date);
        fw.append('\t');
        fw.append(source);
        fw.append('\t');
        fw.append(Long.toString(triggerid));
        fw.append('\t');
        fw.append(status);
        fw.append('\t');
        fw.append(message);
        fw.append('\t');

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

        String net = null;
        switch (activeNetwork.getType()) {
        case ConnectivityManager.TYPE_WIFI:
            String ssid = ((WifiManager) getSystemService(WIFI_SERVICE)).getConnectionInfo().getSSID();
            net = "wifi - ssid: " + ssid;
            break;
        case ConnectivityManager.TYPE_MOBILE:
            net = "mobile";
            break;
        default:
            net = "other network type";
        }
        fw.append(net);
        fw.append("\t\n");
        fw.flush();
        fw.close();
        Log.d(TAG, "writing to logfile " + date + "" + source + " " + status + " " + message + " " + net);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.openhab.habdroid.ui.OpenHABStartupActivity.java

private void initPage() {

    if (!tryManualUrl()) {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
                Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            Log.i(TAG, "Network is connected");
            if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    || activeNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                Log.i(TAG, "Network is WiFi or Ethernet");
                AsyncServiceResolver serviceResolver = new AsyncServiceResolver(this, openHABServiceType);
                if (!this.isFinishing())
                    progressDialog = ProgressDialog.show(OpenHABStartupActivity.this, "",
                            "Discovering openHAB. Please wait...", true);
                serviceResolver.start();
            } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                Log.i(TAG, "Network is Mobile (" + activeNetworkInfo.getSubtypeName() + ")");
                onAlternativeUrl();/* w w w  .j av a 2 s  . c  o  m*/
            } else {
                Log.i(TAG, "Network type (" + activeNetworkInfo.getTypeName() + ") is unsupported");
            }
        } else {
            Log.i(TAG, "Network is not available");
            Toast.makeText(getApplicationContext(), "@string/error_network_not_available", Toast.LENGTH_LONG)
                    .show();
        }
    }

}