Example usage for android.net NetworkInfo isFailover

List of usage examples for android.net NetworkInfo isFailover

Introduction

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

Prototype

@Deprecated
public boolean isFailover() 

Source Link

Document

Indicates whether the current attempt to connect to the network resulted from the ConnectivityManager trying to fail over to this network following a disconnect from another network.

Usage

From source file:org.openchaos.android.fooping.service.PingService.java

@Override
protected void onHandleIntent(Intent intent) {
    String clientID = prefs.getString("ClientID", "unknown");
    long ts = System.currentTimeMillis();

    Log.d(tag, "onHandleIntent()");

    // always send ping
    if (true) {/*from  ww  w.j  a v a  2s.  com*/
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "ping");
            json.put("ts", ts);

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    // http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
    // http://developer.android.com/reference/android/os/BatteryManager.html
    if (prefs.getBoolean("UseBattery", false)) {
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "battery");
            json.put("ts", ts);

            Intent batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            if (batteryStatus != null) {
                JSONObject bat_data = new JSONObject();

                int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                if (level >= 0 && scale > 0) {
                    bat_data.put("pct", roundValue(((double) level / (double) scale) * 100, 2));
                } else {
                    Log.w(tag, "Battery level unknown");
                    bat_data.put("pct", -1);
                }
                bat_data.put("health", batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1));
                bat_data.put("status", batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1));
                bat_data.put("plug", batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1));
                bat_data.put("volt", batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1));
                bat_data.put("temp", batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1));
                bat_data.put("tech", batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY));
                // bat_data.put("present", batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false));

                json.put("battery", bat_data);
            }

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    // http://developer.android.com/guide/topics/location/strategies.html
    // http://developer.android.com/reference/android/location/LocationManager.html
    if (prefs.getBoolean("UseGPS", false)) {
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "loc_gps");
            json.put("ts", ts);

            if (lm == null) {
                lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            }

            Location last_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (last_loc != null) {
                JSONObject loc_data = new JSONObject();

                loc_data.put("ts", last_loc.getTime());
                loc_data.put("lat", last_loc.getLatitude());
                loc_data.put("lon", last_loc.getLongitude());
                if (last_loc.hasAltitude())
                    loc_data.put("alt", roundValue(last_loc.getAltitude(), 4));
                if (last_loc.hasAccuracy())
                    loc_data.put("acc", roundValue(last_loc.getAccuracy(), 4));
                if (last_loc.hasSpeed())
                    loc_data.put("speed", roundValue(last_loc.getSpeed(), 4));
                if (last_loc.hasBearing())
                    loc_data.put("bearing", roundValue(last_loc.getBearing(), 4));

                json.put("loc_gps", loc_data);
            }

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    if (prefs.getBoolean("UseNetwork", false)) {
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "loc_net");
            json.put("ts", ts);

            if (lm == null) {
                lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            }

            Location last_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (last_loc != null) {
                JSONObject loc_data = new JSONObject();

                loc_data.put("ts", last_loc.getTime());
                loc_data.put("lat", last_loc.getLatitude());
                loc_data.put("lon", last_loc.getLongitude());
                if (last_loc.hasAltitude())
                    loc_data.put("alt", roundValue(last_loc.getAltitude(), 4));
                if (last_loc.hasAccuracy())
                    loc_data.put("acc", roundValue(last_loc.getAccuracy(), 4));
                if (last_loc.hasSpeed())
                    loc_data.put("speed", roundValue(last_loc.getSpeed(), 4));
                if (last_loc.hasBearing())
                    loc_data.put("bearing", roundValue(last_loc.getBearing(), 4));

                json.put("loc_net", loc_data);
            }

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    // http://developer.android.com/reference/android/net/wifi/WifiManager.html
    if (prefs.getBoolean("UseWIFI", false)) {
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "wifi");
            json.put("ts", ts);

            if (wm == null) {
                wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            }

            List<ScanResult> wifiScan = wm.getScanResults();
            if (wifiScan != null) {
                JSONArray wifi_list = new JSONArray();

                for (ScanResult wifi : wifiScan) {
                    JSONObject wifi_data = new JSONObject();

                    wifi_data.put("BSSID", wifi.BSSID);
                    wifi_data.put("SSID", wifi.SSID);
                    wifi_data.put("freq", wifi.frequency);
                    wifi_data.put("level", wifi.level);
                    // wifi_data.put("cap", wifi.capabilities);
                    // wifi_data.put("ts", wifi.timestamp);

                    wifi_list.put(wifi_data);
                }

                json.put("wifi", wifi_list);
            }

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    // TODO: cannot poll sensors. register receiver to cache sensor data
    // http://developer.android.com/guide/topics/sensors/sensors_overview.html
    // http://developer.android.com/reference/android/hardware/SensorManager.html
    if (prefs.getBoolean("UseSensors", false)) {
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "sensors");
            json.put("ts", ts);

            if (sm == null) {
                sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            }

            List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
            if (sensors != null) {
                JSONArray sensor_list = new JSONArray();

                for (Sensor sensor : sensors) {
                    JSONObject sensor_info = new JSONObject();

                    sensor_info.put("name", sensor.getName());
                    sensor_info.put("type", sensor.getType());
                    sensor_info.put("vendor", sensor.getVendor());
                    sensor_info.put("version", sensor.getVersion());
                    sensor_info.put("power", roundValue(sensor.getPower(), 4));
                    sensor_info.put("resolution", roundValue(sensor.getResolution(), 4));
                    sensor_info.put("range", roundValue(sensor.getMaximumRange(), 4));

                    sensor_list.put(sensor_info);
                }

                json.put("sensors", sensor_list);
            }

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    // http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
    // http://developer.android.com/reference/android/net/ConnectivityManager.html
    if (prefs.getBoolean("UseConn", false)) {
        try {
            JSONObject json = new JSONObject();
            json.put("client", clientID);
            json.put("type", "conn");
            json.put("ts", ts);

            if (cm == null) {
                cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            }

            // TODO: add active/all preferences below UseConn
            if (prefs.getBoolean("UseConnActive", true)) {
                NetworkInfo net = cm.getActiveNetworkInfo();
                if (net != null) {
                    JSONObject net_data = new JSONObject();

                    net_data.put("type", net.getTypeName());
                    net_data.put("subtype", net.getSubtypeName());
                    net_data.put("connected", net.isConnected());
                    net_data.put("available", net.isAvailable());
                    net_data.put("roaming", net.isRoaming());
                    net_data.put("failover", net.isFailover());
                    if (net.getReason() != null)
                        net_data.put("reason", net.getReason());
                    if (net.getExtraInfo() != null)
                        net_data.put("extra", net.getExtraInfo());

                    json.put("conn_active", net_data);
                }
            }

            if (prefs.getBoolean("UseConnAll", false)) {
                NetworkInfo[] nets = cm.getAllNetworkInfo();
                if (nets != null) {
                    JSONArray net_list = new JSONArray();

                    for (NetworkInfo net : nets) {
                        JSONObject net_data = new JSONObject();

                        net_data.put("type", net.getTypeName());
                        net_data.put("subtype", net.getSubtypeName());
                        net_data.put("connected", net.isConnected());
                        net_data.put("available", net.isAvailable());
                        net_data.put("roaming", net.isRoaming());
                        net_data.put("failover", net.isFailover());
                        if (net.getReason() != null)
                            net_data.put("reason", net.getReason());
                        if (net.getExtraInfo() != null)
                            net_data.put("extra", net.getExtraInfo());

                        net_list.put(net_data);
                    }

                    json.put("conn_all", net_list);
                }
            }

            sendMessage(json);
        } catch (Exception e) {
            Log.e(tag, e.toString());
            e.printStackTrace();
        }
    }

    if (!PingServiceReceiver.completeWakefulIntent(intent)) {
        Log.w(tag, "completeWakefulIntent() failed. no active wake lock?");
    }
}

From source file:com.lgallardo.youtorrentcontroller.RefreshListener.java

private void refresh(String state) {

    // If Contextual Action Bar is open, don't refresh
    if (firstFragment != null && firstFragment.mActionMode != null) {
        return;//ww w.java 2s  .c  om
    }

    // Gett all torrents
    params[0] = "gui/?list=1&token=";

    // Set state
    params[1] = state;

    // Set token
    params[2] = token;

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected() && !networkInfo.isFailover()) {

        if (hostname.equals("")) {
            qBittorrentNoSettingsFoundDialog(R.string.info, R.string.about_help1);
        } else {

            //                if (token == null || token.equals("") || cookie ==  null || cookie.equals("")) {
            //                    // Request new token and execute task in background, and Execute task in background
            //                    new torrentTokenTask().execute();
            //
            //                }else{
            // Execute the  in background
            //                    params[2] = token;
            new torrentTask().execute(params);
            //                }
        }

    } else {

        // Connection Error message
        if (connection400ErrorCounter > 1) {
            Toast.makeText(getApplicationContext(), R.string.connection_error, Toast.LENGTH_SHORT).show();
        }

    }

}

From source file:com.lgallardo.qbittorrentclient.RefreshListener.java

private void refresh(String state, String label) {

    // If Contextual Action Bar is open, don't refresh
    if (firstFragment != null && firstFragment.mActionMode != null) {
        return;//  w  w  w .  ja  v a 2  s.  c  o  m
    }

    if (qb_version.equals("2.x")) {
        qbQueryString = "json";
        params[0] = qbQueryString + "/events";
    }

    if (qb_version.equals("3.1.x")) {
        qbQueryString = "json";
        params[0] = qbQueryString + "/torrents";
    }

    if (qb_version.equals("3.2.x")) {
        qbQueryString = "query";
        params[0] = qbQueryString + "/torrents?filter=" + state;

        // Get API version in case it hasn't been gotten before
        if (qb_api == null || qb_api.equals("") || qb_api.equals("0")) {
            new qBittorrentApiTask().execute(new Intent());
        }

        // Label
        if (label != null && !label.equals(getResources().getString(R.string.drawer_label_all))) {

            if (label.equals(getResources().getString(R.string.drawer_label_unlabeled))) {
                label = "";
            }

            if (!labels.contains(label)) {
                label = getResources().getString(R.string.drawer_label_all);
            }

            saveLastLabel(label);

            //                Log.d("Debug", "Label filter: " + label);

            try {

                if (!label.equals(getResources().getString(R.string.drawer_label_all))) {

                    // I used a dummy URL to encode label
                    String labelEncoded = Uri.encode("http://www.dummy.org?label=" + label);

                    // then I got the the encoded label
                    labelEncoded = labelEncoded.substring(labelEncoded.indexOf("%3D") + 3);

                    // to build the url and pass it to params[0]
                    if (Integer.parseInt(MainActivity.qb_api) < 10) {
                        params[0] = params[0] + "&label=" + labelEncoded;
                    } else {
                        params[0] = params[0] + "&category=" + labelEncoded;
                    }
                }

            } catch (Exception e) {
                Log.d("Debug", "[Main] Label Exception: " + e.toString());
            }
        } else {
            //                Log.d("Debug", "Label filter2: " + label);

        }

    }

    params[1] = state;

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected() && !networkInfo.isFailover()) {

        // Logs for reporting
        if (CustomLogger.isMainActivityReporting()) {
            generateSettingsReport();
        }

        if (hostname.equals("")) {
            qBittorrentNoSettingsFoundDialog(R.string.info, R.string.about_help1);
        } else {

            //                Log.d("Report", "Report: " + CustomLogger.getReport());

            if (qb_version.equals("3.2.x") && (cookie == null || cookie.equals(""))) {
                // Request new cookie and execute task in background

                if (connection403ErrorCounter > 1) {

                    toastText(R.string.error403);

                    httpStatusCode = 0;
                    disableRefreshSwipeLayout();
                } else {
                    new qBittorrentCookieTask().execute(params);
                }

            } else {

                if (connection403ErrorCounter > 1) {

                    if (cookie != null && !cookie.equals("")) {
                        // Only toasts the message if there is not a cookie set before
                        toastText(R.string.error403);

                        cookie = null;
                    }

                    httpStatusCode = 0;
                    disableRefreshSwipeLayout();
                } else {

                    // Execute the task in background
                    qbTask = new qBittorrentTask().execute(params);

                    // Check if  alternative speed limit is set
                    new qBittorrentCommand().execute(new String[] { "alternativeSpeedLimitsEnabled", "" });
                }
            }

        }

    } else {

        // Connection Error message
        toastText(R.string.connection_error);

        disableRefreshSwipeLayout();
    }

}