Example usage for android.telephony TelephonyManager DATA_CONNECTED

List of usage examples for android.telephony TelephonyManager DATA_CONNECTED

Introduction

In this page you can find the example usage for android.telephony TelephonyManager DATA_CONNECTED.

Prototype

int DATA_CONNECTED

To view the source code for android.telephony TelephonyManager DATA_CONNECTED.

Click Source Link

Document

Data connection state: Connected.

Usage

From source file:com.ultrafunk.network_info.service.MobileDataStateListener.java

@Override
public void onDataConnectionStateChanged(int state) {
    switch (state) {
    case TelephonyManager.DATA_CONNECTED:
    case TelephonyManager.DATA_DISCONNECTED:
        LocalBroadcastManager.getInstance(context)
                .sendBroadcast(new Intent(Constants.ACTION_DATA_CONNECTION_CHANGED));
        break;//from w w  w .  ja  v a2  s.  c  om
    }
}

From source file:com.ultrafunk.network_info.receiver.MobileDataStatusReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //   Log.e(this.getClass().getSimpleName(), "onReceive(): " + action);

    updateMobileDataViews = true;/*from  www  . j  a va  2 s .  c om*/

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    dataState = telephonyManager.getDataState();
    isMobileDataEnabled = MobileDataUtils.isMobileDataEnabled(context);
    isAirplaneModeOn = MobileDataUtils.isAirplaneModeOn(context);
    isMobileOutOfService = NetworkStateService.isMobileOutOfService();
    isDataRoaming = isDataRoaming(context);
    networkOperatorAndServiceProvider = getNetworkOperatorAndServiceProvider(context);
    dataUsageBytes = NetworkStateService
            .setGetDataUsageBytes(TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes());

    if (Constants.ACTION_DATA_CONNECTION_CHANGED.equals(action)
            || Constants.ACTION_DATA_STATE_CHANGED.equals(action)
            || Constants.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
        // Needed to get around a known bug in Android 5.x: https://code.google.com/p/android/issues/detail?id=78924
        if ((dataState == TelephonyManager.DATA_CONNECTED) && (dataUsageBytes == 0)
                && !NetworkStateService.isWaitingForDataUsage()) {
            NetworkStateService.setWaitingForDataUsage(true);
            Intent serviceIntent = new Intent(context, NetworkStateService.class);
            serviceIntent.setAction(Constants.ACTION_DATA_CONNECTED);
            context.startService(serviceIntent);
        }

        partiallyUpdateWidgets(context);
    } else if (Constants.ACTION_DATA_USAGE_UPDATE.equals(action) || Intent.ACTION_SCREEN_ON.equals(action)) {
        partiallyUpdateWidgets(context);
    } else if (Constants.ACTION_UPDATE_WIDGET.equals(action)) {
        partiallyUpdateWidget(context, AppWidgetManager.getInstance(context),
                intent.getIntExtra(Constants.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
    }
}

From source file:com.karpenstein.signalmon.SignalMonitorActivity.java

/** Called when the activity is first created. */
@Override// w w w. ja  va 2 s  . co m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get the UI
    textOut = (TextView) findViewById(R.id.textOut);

    // Get the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    // Create a new PhoneStateListener
    listener = new PhoneStateListener() {
        @Override
        public void onDataActivity(int direction) {
            String dirString = "N/A";
            switch (direction) {
            case TelephonyManager.DATA_ACTIVITY_NONE:
                dirString = "DATA_ACTIVITY_NONE";
                break;
            case TelephonyManager.DATA_ACTIVITY_IN:
                dirString = "DATA_ACTIVITY_IN";
                break;
            case TelephonyManager.DATA_ACTIVITY_OUT:
                dirString = "DATA_ACTIVITY_OUT";
                break;
            case TelephonyManager.DATA_ACTIVITY_INOUT:
                dirString = "DATA_ACTIVITY_INOUT";
                break;
            case TelephonyManager.DATA_ACTIVITY_DORMANT:
                dirString = "DATA_ACTIVITY_DORMANT";
                break;
            }
            textOut.append(dirString + "\n");
        }

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            textOut.append(signalStrength.toString() + "\n");
        }

        @Override
        public void onDataConnectionStateChanged(int state, int networkType) {
            String stateString = "N/A";
            String netTypString = "N/A";
            switch (state) {
            case TelephonyManager.DATA_CONNECTED:
                stateString = "DATA_CONNECTED";
                break;
            case TelephonyManager.DATA_CONNECTING:
                stateString = "DATA_CONNECTING";
                break;
            case TelephonyManager.DATA_DISCONNECTED:
                stateString = "DATA_DISCONNECTED";
                break;
            case TelephonyManager.DATA_SUSPENDED:
                stateString = "DATA_SUSPENDED";
                break;
            }
            switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                netTypString = "NETWORK_TYPE_1xRTT";
                break;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                netTypString = "NETWORK_TYPE_CDMA";
                break;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                netTypString = "NETWORK_TYPE_EDGE";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                netTypString = "NETWORK_TYPE_EVDO_0";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                netTypString = "NETWORK_TYPE_EVDO_A";
                break;
            case TelephonyManager.NETWORK_TYPE_GPRS:
                netTypString = "NETWORK_TYPE_GPRS";
                break;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                netTypString = "NETWORK_TYPE_HSDPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                netTypString = "NETWORK_TYPE_HSPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                netTypString = "NETWORK_TYPE_HSUPA";
                break;
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netTypString = "NETWORK_TYPE_IDE";
                break;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                netTypString = "NETWORK_TYPE_UMTS";
                break;
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                netTypString = "NETWORK_TYPE_UNKNOWN";
                break;
            }
            textOut.append(String.format("onDataConnectionStateChanged: %s; %s\n", stateString, netTypString));
        }
    };

    // Register the listener with the telephony manager
    telephonyManager.listen(listener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
            | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_DATA_ACTIVITY);

    // start the NetServerService
    startService(new Intent(this, NetServerService.class));
}

From source file:com.ultrafunk.network_info.receiver.MobileDataStatusReceiver.java

private boolean isDataRoaming(Context context) {
    if ((dataState == TelephonyManager.DATA_CONNECTED) && telephonyManager.isNetworkRoaming()) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        return (networkInfo != null) ? networkInfo.isRoaming() : false;
    } else {// w  w  w  . j av a2  s . c  om
        return telephonyManager.isNetworkRoaming() && MobileDataUtils.isDataRoaming(context);
    }
}

From source file:com.ultrafunk.network_info.service.NetworkStateService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if ((intent != null) && (intent.getAction() != null)) {
        final String action = intent.getAction();

        //   Log.e(this.getClass().getSimpleName(), "onStartCommand(): " + action);

        if (Constants.ACTION_UPDATE_SERVICE_STATE.equals(action)) {
            initEnabledWidgets(new EnabledWidgets(
                    intent.getBooleanExtra(Constants.EXTRA_ENABLED_WIDGETS_MOBILE_DATA, false),
                    intent.getBooleanExtra(Constants.EXTRA_ENABLED_WIDGETS_WIFI, false)));
        } else {//  w w w . j  a va  2s .  c om
            final Handler handler = new Handler();

            if (Constants.ACTION_WIFI_CONNECTING.equals(action)) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                        if ((wifiInfo != null) && (wifiInfo.getSupplicantState() == SupplicantState.SCANNING))
                            localBroadcastManager.sendBroadcastSync(new Intent(Constants.ACTION_WIFI_SCANNING));
                    }
                }, 5 * 1000);
            } else if (Constants.ACTION_WIFI_CONNECTED.equals(action)) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                        if ((wifiInfo != null) && (wifiInfo.getLinkSpeed() != -1))
                            localBroadcastManager
                                    .sendBroadcastSync(new Intent(Constants.ACTION_WIFI_LINK_SPEED));
                    }
                }, 3 * 1000);
            } else if (Constants.ACTION_DATA_CONNECTED.equals(action)) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        isWaitingForDataUsage = false;

                        if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
                            localBroadcastManager
                                    .sendBroadcastSync(new Intent(Constants.ACTION_DATA_USAGE_UPDATE));
                    }
                }, 500);
            }
        }
    }

    return Service.START_STICKY;
}

From source file:com.fallahpoor.infocenter.fragments.SimFragment.java

private String getDataConnectionState(TelephonyManager telephonyManager) {

    int dataStateInt = telephonyManager.getDataState();
    String dataStateStr;//from  w w  w  .jav  a2s  . c  o m

    switch (dataStateInt) {
    case TelephonyManager.DATA_CONNECTED:
        dataStateStr = getString(R.string.sim_sub_item_data_connected);
        break;
    case TelephonyManager.DATA_CONNECTING:
        dataStateStr = getString(R.string.sim_sub_item_data_connecting);
        break;
    case TelephonyManager.DATA_DISCONNECTED:
        dataStateStr = getString(R.string.sim_sub_item_data_disconnected);
        break;
    case TelephonyManager.DATA_SUSPENDED:
        dataStateStr = getString(R.string.sim_sub_item_data_suspended);
        break;
    default:
        dataStateStr = getString(R.string.unknown);
    }

    return dataStateStr;

}

From source file:riddimon.android.asianetautologin.HttpUtils.java

/**
 * Uses TelephonyManager and WifiManager to check for network connectivity.
 * Also incorporates CONNECTING state for retry scenarios.
 * @param context/*  w ww . j a v a  2  s .c  om*/
 * @return ConnectionStatus
 */
public ConnectionStatus isConnectedOLD(Context context) {
    boolean data = false, wifi = false;
    boolean data_connecting = false, wifi_connecting = false;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int ds = tm.getDataState();
    int ws = wm.getWifiState();
    switch (ds) {
    case TelephonyManager.DATA_CONNECTED:
        data = true;
        break;
    case TelephonyManager.DATA_CONNECTING:
        data_connecting = true;
    default:
        data = false;
        data_connecting = false;
    }

    switch (ws) {
    case WifiManager.WIFI_STATE_ENABLING:
        wifi_connecting = true;
    case WifiManager.WIFI_STATE_DISABLING:
    case WifiManager.WIFI_STATE_DISABLED:
    case WifiManager.WIFI_STATE_UNKNOWN:
        wifi = false;
        break;
    case WifiManager.WIFI_STATE_ENABLED:
        WifiInfo wi = wm.getConnectionInfo();
        if (wi != null)
            wifi = true;
        break;
    }

    if (wifi && data)
        return ConnectionStatus.BOTH_CONNECTED;
    else if (wifi && data_connecting)
        return ConnectionStatus.WIFI_CONNECTED;
    else if (data && wifi_connecting)
        return ConnectionStatus.DATA_CONNECTED;
    else if (wifi_connecting || data_connecting)
        return ConnectionStatus.CONNECTING;
    return ConnectionStatus.NO_CONNECTION;
}

From source file:org.restcomm.app.utillib.ContentProvider.ContentValuesGenerator.java

/**
 * This method generates a ContentValues object from the signal object so that it may
 * be stored in the database./*  w  w  w. j  a  v a2s  .c om*/
 * @param signal
 * @param phoneType This is the phone type and must be one of {@link TelephonyManager#PHONE_TYPE_CDMA}
 * or {@link TelephonyManager#PHONE_TYPE_GSM}.
 * @param stagedEventId This is the id of the event that this signal has to be related to
 * @return
 */
public static ContentValues generateFromSignal(SignalEx signal, int phoneType, int networkType,
        int serviceState, int dataState, long stagedEventId, int wifiSignal, JSONObject serviceMode) {
    ContentValues values = new ContentValues();
    Integer dBm = 0;
    Integer signalDB = null;
    try {
        if (serviceMode != null && serviceMode.getLong("time") + 5000 < System.currentTimeMillis())
            serviceMode = null;
        if (signal == null) // as a result of a service outage
        {
            LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, "ContentValues", "generateFromSignal",
                    "signal == null");
            values.put(Tables.SignalStrengths.SIGNAL, -256);
            //now do the common parameters
            values.put(Tables.SignalStrengths.TIMESTAMP, System.currentTimeMillis());
            values.put(Tables.SignalStrengths.EVENT_ID, stagedEventId);
            values.put(Tables.SignalStrengths.COVERAGE, 0);
            return values;
        }
        if (signal.getSignalStrength() == null) // as a result of a screen off (signal unknown)
            values.put(Tables.SignalStrengths.SIGNAL, (Integer) null);
        //do phone type specific actions first
        else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) {
            boolean isEvdo = true;

            if (networkType == TelephonyManager.NETWORK_TYPE_1xRTT
                    || networkType == TelephonyManager.NETWORK_TYPE_CDMA) {
                isEvdo = false;
                dBm = signal.getCdmaDbm();
            } else {
                dBm = signal.getEvdoDbm();
                int evdoDbm = signal.getEvdoDbm();
                // If there is no EVDO signal but there is CDMA signal, then use CDMA signal
                if (evdoDbm <= -120 || evdoDbm >= -1) {
                    int cdmaDbm = signal.getCdmaDbm();
                    if (cdmaDbm <= -120 || cdmaDbm >= -1)
                        dBm = evdoDbm; // no cdma signal either, so send the evdo signal afterall
                    else {
                        dBm = cdmaDbm;
                        isEvdo = false; // display and report the CDMA signal if CDMA has signal and EVDO does not
                    }
                }
            }

            //if (dBm == -1) // When Scott had a network outage on CDMA, he got -1, we want -256
            //   dBm = -256;
            if (dBm == -120 && networkType == TelephonyManager.NETWORK_TYPE_LTE)
                dBm = null; // signal not known, this seems to happen with LTE advanced
            values.put(Tables.SignalStrengths.SIGNAL, dBm
            //isEvdo ? signal.getSignalStrength().getEvdoDbm() : signal.getSignalStrength().getCdmaDbm()
            );
            values.put(Tables.SignalStrengths.ECI0,
                    isEvdo ? signal.getEvdoEcio() / 10.0 : signal.getCdmaEcio() / 10.0);
            values.put(Tables.SignalStrengths.SNR, isEvdo ? signal.getEvdoSnr() : null);
            //if (isEvdo)
            values.put(Tables.SignalStrengths.SIGNAL2G, signal.getCdmaDbm()
            //isEvdo ? signal.getSignalStrength().getCdmaDbm() : null
            );
            signalDB = dBm;
        } else if (phoneType == TelephonyManager.PHONE_TYPE_GSM) {

            if (getPlatform() == 1) //On Android device
                signalDB = signal.getGsmSignalStrength();
            else if (getPlatform() == 3) {//On Blackberry device
                signalDB = PreferenceManager.getDefaultSharedPreferences(service.getApplicationContext())
                        .getInt(PreferenceKeys.Miscellaneous.BB_SIGNAL, 99);

            }
            if (signalDB == 99 || signalDB == -1 || signalDB == null) {
                signalDB = null;
                //            Integer signalLte = signal.getLayer3("mLteSignalStrength");
                //            if (signalLte != null && signalLte < 99)
                //            {
                //               if (signalLte == 0)
                //                  signalDB = -120;
                //               else
                //                  signalDB = -113 + signalLte*2;
                //            }

                // If signal is unknown but signal bars are known, send bars
                Integer signalBar = signal.getLayer3("mGsmSignalBar");
                if (signalBar != null && signalBar != -1) {
                    signalDB = getSignalDBFromBars(signalBar);
                    values.put(Tables.SignalStrengths.SIGNALBARS, signalBar);
                }

            } else if (getPlatform() == 1)
                signalDB = signal.getDbmValue(networkType, phoneType);

            Integer ecio = signal.getLayer3("mUmtsEcio");
            if (ecio == null)
                ecio = signal.getLayer3("mgw_ecio");
            if (ecio == null)
                ecio = signal.getLayer3("mGsmEcio");
            //         if (ecio == null)
            //         {
            //            ecio = signal.getLayer3("lastEcIoIndex");
            //            if (ecio != null)
            //               ecio = 2*signal.getLayer3Array("lastEcIoValues", ecio);
            //         }

            Integer ecno = signal.getLayer3("mUmtsEcno");
            if (ecno == null)
                ecno = signal.getLayer3("mGsmEcno");
            Integer rscp = signal.getLayer3("mUmtsRscp");
            if (rscp == null)
                rscp = signal.getLayer3("mGsmRscp");
            if (rscp == null)
                rscp = signal.getLayer3("mWcdmaRscp");
            if ((signalDB == null || signalDB <= -120) && rscp != null && rscp > -120 && rscp < -20)
                signalDB = rscp;

            values.put(Tables.SignalStrengths.ECI0, ecio);
            values.put(Tables.SignalStrengths.RSCP, rscp);
            values.put(Tables.SignalStrengths.ECN0, ecno);
            values.put(Tables.SignalStrengths.SIGNAL, signalDB);
            values.put(Tables.SignalStrengths.BER,
                    signal.getGsmBitErrorRate() == 99 ? null : signal.getGsmBitErrorRate());
        }
        values.put(Tables.SignalStrengths.WIFISIGNAL, wifiSignal);
        // check for LTE signal signal quality parameters only if connected to LTE
        if (networkType == TelephonyManager.NETWORK_TYPE_LTE) {
            Integer lteRsrp = -1, lteRsrq, lteSnr, lteCqi;

            lteRsrp = signal.getLayer3("mLteRsrp");
            lteRsrq = signal.getLayer3("mLteRsrq");
            lteSnr = signal.getLayer3("mLteRssnr");
            if (lteRsrp != null && lteRsrp >= 40 && lteRsrp < 140)
                lteRsrp = -lteRsrp;
            else if (lteRsrp != null && lteRsrp > 0 && lteRsrp <= 32)
                lteRsrp = (lteRsrp - 2) * 2 + -109;
            if (lteSnr == null || lteSnr > 1000)
                lteSnr = signal.getLayer3("mLteSnr");
            if (lteSnr == null || lteSnr < -200 || lteSnr > 1000)
                lteSnr = null;
            if (lteRsrp != null && lteRsrp > 1000)
                lteRsrp = lteRsrq = null;

            lteCqi = signal.getLayer3("mLteCqi");
            values.put(Tables.SignalStrengths.LTE_RSRP, lteRsrp);
            values.put(Tables.SignalStrengths.LTE_RSRQ, lteRsrq);
            values.put(Tables.SignalStrengths.LTE_SNR, lteSnr);
            values.put(Tables.SignalStrengths.LTE_CQI, lteCqi);

        }
        // check for the LTE signal regardless, at least it will indicate of device supports LTE
        Integer lteRssi = signal.getLayer3("mLteRssi");
        if (lteRssi == null)
            lteRssi = signal.getLayer3("mLteSignalStrength");
        if (lteRssi != null) {
            if (lteRssi >= 0 && lteRssi < 32) {
                if (lteRssi == 0)
                    lteRssi = -120; // officially 0 means -113dB or less, but since lowest possible signal on Blackberry = -120, call it -120 for consistency
                else if (lteRssi == 1)
                    lteRssi = -111; // officially 1 = -111 dB
                else if (lteRssi > 1 && lteRssi <= 31)
                    lteRssi = (lteRssi - 2) * 2 + -109;
            }

            // allow for the possibility of sending a 3G signal and LTE signal at the same time
            // but if LTE signal is present, and 3G signal says -120 or worse, ignore regular signal
            if (lteRssi > -120 && (dBm == null || dBm <= -120))
                values.put(Tables.SignalStrengths.SIGNAL, (Integer) lteRssi);

        }
        values.put(Tables.SignalStrengths.LTE_SIGNAL, lteRssi);

        if (serviceMode != null && serviceMode.getLong("time") + 20000 > System.currentTimeMillis()) {
            if (serviceMode.has("ecio") && serviceMode.getString("ecio").length() > 1) {
                int svc_ecio = Integer.parseInt(serviceMode.getString("ecio"), 10);
                if (svc_ecio <= -2 && svc_ecio >= -30) {
                    values.put(Tables.SignalStrengths.ECI0, svc_ecio);
                }
            }
            if (serviceMode.has("rscp") && serviceMode.getString("rscp").length() > 1) {
                int svc_rscp = Integer.parseInt(serviceMode.getString("rscp"), 10);
                if (svc_rscp <= -20 && svc_rscp >= -120) //  && (signalDB == null || signalDB <= -120))
                    values.put(Tables.SignalStrengths.SIGNAL, svc_rscp);
            }

            if (serviceMode.has("snr") && serviceMode.getString("snr").length() > 1) {
                float svc_fsnr = Float.parseFloat(serviceMode.getString("snr"));
                int svc_snr = (int) (svc_fsnr * 10);
                if (svc_snr > -200 && svc_snr < 2000)
                    values.put(Tables.SignalStrengths.LTE_SNR, svc_snr);
            }

            if (serviceMode.has("rsrp") && serviceMode.getString("rsrp").length() > 1) {
                int svc_rsrp = Integer.parseInt(serviceMode.getString("rsrp"), 10);
                if (svc_rsrp <= -20 && svc_rsrp >= -140)
                    values.put(Tables.SignalStrengths.LTE_RSRP, svc_rsrp);
            }

            if (serviceMode.has("rsrq") && serviceMode.getString("rsrq").length() > 1) {
                int svc_rsrq = Integer.parseInt(serviceMode.getString("rsrp"), 10);
                if (svc_rsrq <= -1 && svc_rsrq >= -30)
                    values.put(Tables.SignalStrengths.LTE_RSRQ, svc_rsrq);
            }

        }

        //now do the common parameters
        values.put(Tables.SignalStrengths.TIMESTAMP, signal.getTimestamp());
        values.put(Tables.SignalStrengths.EVENT_ID, stagedEventId);
        int coverage = 0;
        if (networkType == 0) {
            if (serviceState == ServiceState.STATE_IN_SERVICE)
                networkType = 1;
            //else if (serviceState == ServiceState.STATE_POWER_OFF)
            //   networkType = -1;
        }
        int networkTier = PhoneState.getNetworkGeneration(networkType);
        if (networkTier == 0) // dont make it 0 unless truly out of service
            networkTier = 1;
        if (serviceState == ServiceState.STATE_OUT_OF_SERVICE && (dataState != TelephonyManager.DATA_CONNECTED
                || networkType != TelephonyManager.NETWORK_TYPE_LTE)) // Sprint can be connected to LTE and say outofservice
            networkTier = 0;
        else if (serviceState == ServiceState.STATE_POWER_OFF
                || serviceState == ServiceState.STATE_EMERGENCY_ONLY
                || serviceState == ServiceState.STATE_POWER_OFF || serviceState == 9) // 9 = MMCPhoneStateListenerOld.SERVICE_STATE_AIRPLANE)
            networkTier = -1;

        // tier 5 becomes 11111, tier 1 = 00001
        coverage = networkTier; // (1 << networkTier) - 1;

        //String reflect = listSignalFields (signal);

        values.put(Tables.SignalStrengths.COVERAGE, coverage);
        //MMCLogger.logToFile(MMCLogger.Level.DEBUG, "", "onSignal.listSignalFields", reflect);
        //MMCLogger.logToFile(MMCLogger.Level.DEBUG, "", "onSignal.values", values.toString());
    } catch (Exception e) {
        LoggerUtil.logToFile(LoggerUtil.Level.ERROR, "ContentValuesGenerator", "generateFromSignal",
                "exception", e);
    }
    return values;
}

From source file:org.restcomm.app.qoslib.Services.LibPhoneStateListener.java

@Override
public void onDataConnectionStateChanged(int state, int networkType) {
    super.onDataConnectionStateChanged(state, networkType);
    //MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "onDataConnectionStateChanged", String.format("Network type: %d, State: %d", networkType, state));

    //notify MainService of the new network type
    mPhoneState.updateNetworkType(networkType);

    int datastate = telephonyManager.getDataState();
    // disregard network change events if data is disabled or in airplane mode
    if (datastate == TelephonyManager.DATA_SUSPENDED
            || mPhoneState.previousServiceState == mPhoneState.SERVICE_STATE_AIRPLANE)
        return;//  www. ja  v a  2  s. c o  m

    if (PhoneState.ActiveConnection(owner) > 1) {// 10=Wifi, 11=Wimax, 12=Ethernet, 0=other
        mPhoneState.previousNetworkTier = -1;
        return;
    }

    // Ignore any data outages that occur just after turning screen off, these are probably not to be blamed on the carrier
    if (mPhoneState.getScreenOnTime(false) + 30000 > System.currentTimeMillis())
        return;

    try {

        String conn = owner.getConnectionHistory().updateConnectionHistory(networkType, state,
                telephonyManager.getDataActivity(), mPhoneState.previousServiceStateObj,
                owner.getConnectivityManager().getActiveNetworkInfo());
        if (conn != null)
            owner.getIntentDispatcher().updateConnection(conn, false);

    } catch (Exception e) {
    }

    int networkGeneration = mPhoneState.getNetworkGeneration(networkType);

    // The 3G outage will be handled by the Service state outage
    if (mPhoneState.previousServiceState == ServiceState.STATE_OUT_OF_SERVICE
            || mPhoneState.previousServiceState == ServiceState.STATE_EMERGENCY_ONLY)
        return;
    //if the network generation hasn't changed, then don't cause an event
    if (mPhoneState.previousNetworkTier == networkGeneration && mPhoneState.previousNetworkState == state) {
        return;
    }

    SignalEx signal = mPhoneState.getLastMMCSignal();
    if (signal != null) {
        signal.setTimestamp(System.currentTimeMillis());
        mPhoneState.clearLastMMCSignal(); // to force a duplicate signal to be added
        processNewMMCSignal(signal);
    }
    //this was falsely reporting outages when screen turned off, and not coupling them to regained
    //if (datastate == TelephonyManager.DATA_DISCONNECTED)
    //   networkGeneration = 0;
    // First network state
    if (mPhoneState.previousNetworkType == -1)
        mPhoneState.previousNetworkType = networkType;
    else {
        switch (networkGeneration) {
        case 3: //3g
        case 4: //3g
            stateChanged_3g(state);
            break;
        case 5: //3g
            stateChanged_4g(state);
            break;

        case 1:
        case 2: //2g
            stateChanged_2g(state);
            break;

        // disconnected data without disconnecting service?
        case 0:
            stateChanged_0g(state);
            break;

        }
    }
    //update the previous network generation and state
    if (state == TelephonyManager.DATA_CONNECTED && networkGeneration != 0)
        mPhoneState.previousNetworkTier = networkGeneration;
    // If there is truly an outage, the service state listener will update the previousNetworkTier to 0
    mPhoneState.previousNetworkState = state;
    mPhoneState.previousNetworkType = networkType;

}

From source file:org.restcomm.app.qoslib.Services.Events.EventManager.java

/**
 * At the start of the event, stores like-timestamped entries in the tables for signals, cells and locations
 * This is so that querying the timespan of the event recording will easily return records for the beginning of the event
 * among other things/*  ww w. j a v  a 2s . co  m*/
 * @param event
 */
public void signalSnapshot(EventObj event) {
    // In case the signal hasn't changed recently (last 30 sec or so), 
    // the signal won't be retrieved by the cursor unless we timestamp the last known signal now
    try {

        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        // Update the database and intents with a snapshot of the last known signal, cell, location
        SignalEx signal = context.getPhoneState().getLastMMCSignal();
        long timestamp = System.currentTimeMillis();
        if (event != null)
            timestamp = event.getEventTimestamp();
        if (signal != null) {
            signal.setTimestamp(timestamp);
            context.getPhoneState().clearLastMMCSignal(); // to force a duplicate signal to be added
            context.getPhoneStateListener().processNewMMCSignal(signal);
        }

        CellLocationEx cell = context.getPhoneStateListener().getLastCellLocation();
        if (cell != null) {
            cell.setCellIdTimestamp(timestamp);
            context.getPhoneStateListener().clearLastCellLocation();
            context.getPhoneStateListener().processNewCellLocation(cell);
        }

        if (event == null)
            return;

        ongoingEvents.add(event);
        Location location = context.getLastLocation();
        int satellites = context.getLastNumSatellites();
        if (location != null) {
            location.setTime(timestamp);
            context.setLastLocation(null); // to force a duplicate location to be added
            context.processNewFilteredLocation(location, satellites);
        }

        // set an initial location on the event if it is recent, but update it if a better location becomes available
        location = context.getLastLocation();
        event.setSignal(context.getPhoneState().getLastMMCSignal());
        event.setCell(context.getPhoneStateListener().getLastCellLocation());
        event.setServiceState(context.getPhoneState().getLastServiceStateObj());

        if (location != null && location.getTime() - 10000 > System.currentTimeMillis())
            event.setLocation(location, context.getLastNumSatellites());

        // Also, set the coverage flags at the time of the event
        int tier = context.getPhoneState().getNetworkGeneration();
        int servicestate = context.getPhoneState().getLastServiceState();
        int datastate = telephonyManager.getDataState();
        int activeConnection = PhoneState.ActiveConnection(context);
        if (activeConnection == 10)
            event.setFlag(EventObj.SERVICE_WIFI, true);
        else if (activeConnection == 11)
            event.setFlag(EventObj.SERVICE_WIMAX, true);
        ReportManager reportManager = ReportManager.getInstance(context);
        if (reportManager.manualPlottingEvent != null || event.getEventType() == EventType.MAN_PLOTTING)
            event.setFlag(EventObj.MANUAL_SAMPLES, true);

        if (datastate == TelephonyManager.DATA_CONNECTED || activeConnection > 1)
            event.setFlag(EventObj.SERVICE_DATA, true);
        else if (datastate == TelephonyManager.DATA_SUSPENDED && servicestate == ServiceState.STATE_IN_SERVICE) // not counted as data outage if data turned off
            event.setFlag(EventObj.SERVICE_DATA, true);
        else
            event.setFlag(EventObj.SERVICE_DATA, false);
        if (servicestate == ServiceState.STATE_OUT_OF_SERVICE
                || servicestate == ServiceState.STATE_EMERGENCY_ONLY)
            event.setFlag(EventObj.SERVICE_VOICE, false);
        else
            event.setFlag(EventObj.SERVICE_VOICE, true);
        if (tier > 2)
            event.setFlag(EventObj.SERVICE_3G, true);
        if (tier > 4)
            event.setFlag(EventObj.SERVICE_4G, true);
        if (context.getPhoneState().isCallConnected() || context.getPhoneState().isCallDialing()
                || event.getEventType().getIntValue() <= 6)
            event.setFlag(EventObj.PHONE_INUSE, true);

        // dropped call detection support using logcat or precise call state?
        if (event.getEventType().getIntValue() <= 7) {
            String pname = context.getPackageName();
            int permissionForReadLogs = context.getPackageManager()
                    .checkPermission(android.Manifest.permission.READ_LOGS, pname); //0 means allowed
            int permissionForPrecise = context.getPackageManager()
                    .checkPermission("android.permission.READ_PRECISE_PHONE_STATE", pname); // 0 means allowed
            if (permissionForPrecise == 0)
                event.setFlag(EventObj.CALL_PRECISE, true);
            else if (permissionForReadLogs == 0)
                event.setFlag(EventObj.CALL_LOGCAT, true);
        }

        event.setBattery(DeviceInfoOld.battery);
        if (event.getEventType() == EventType.COV_VOD_NO || event.getEventType() == EventType.COV_VOD_YES
                || event.getEventType() == EventType.COV_UPDATE
                || event.getEventType() == EventType.TRAVEL_CHECK) {
            long duration = 0;
            if (mLastServiceStateChangeTimeStamp != 0) {
                duration = System.currentTimeMillis() - mLastServiceStateChangeTimeStamp;
                mLastServiceStateChangeTimeStamp = System.currentTimeMillis();
                event.setDuration(duration);
            } else
                duration = mLastScreenOffDuration;

        }
        if (event.getEventType() == EventType.COV_3G_NO || event.getEventType() == EventType.COV_3G_YES
                || event.getEventType() == EventType.COV_UPDATE
                || event.getEventType() == EventType.TRAVEL_CHECK) {
            long duration = 0;
            if (mLast3GStateChangeTimeStamp != 0) {
                duration = System.currentTimeMillis() - mLast3GStateChangeTimeStamp;
                if (event.getEventType() == EventType.COV_UPDATE
                        || event.getEventType() == EventType.TRAVEL_CHECK)
                    event.setEventIndex(duration);
                else
                    event.setDuration(duration);
                mLast3GStateChangeTimeStamp = System.currentTimeMillis();
            }
        }
        //context.getCellHistory ().snapshotHistory();
        String conn = context.getConnectionHistory().updateConnectionHistory(telephonyManager.getNetworkType(),
                telephonyManager.getDataState(), telephonyManager.getDataActivity(),
                context.getPhoneState().getLastServiceStateObj(),
                context.getConnectivityManager().getActiveNetworkInfo());
        context.getIntentDispatcher().updateConnection(conn, true);

        WifiInfo wifiinfo = getWifiInfo();
        WifiConfiguration wifiConfig = getWifiConfig();
        event.setWifi(wifiinfo, wifiConfig);

        localReportEvent(event);
    } catch (Exception e) {
        LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "signalSnapshot", "error", e);
    }
}