Example usage for android.telephony ServiceState STATE_OUT_OF_SERVICE

List of usage examples for android.telephony ServiceState STATE_OUT_OF_SERVICE

Introduction

In this page you can find the example usage for android.telephony ServiceState STATE_OUT_OF_SERVICE.

Prototype

int STATE_OUT_OF_SERVICE

To view the source code for android.telephony ServiceState STATE_OUT_OF_SERVICE.

Click Source Link

Document

Phone is not registered with any operator, the phone can be currently searching a new operator to register to, or not searching to registration at all, or registration is denied, or radio signal is not available.

Usage

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

@Override
public void onServiceStateChanged(ServiceState serviceState) {
    switch (serviceState.getState()) {
    case ServiceState.STATE_EMERGENCY_ONLY:
    case ServiceState.STATE_OUT_OF_SERVICE:
        NetworkStateService.setMobileOutOfService(true);
        LocalBroadcastManager.getInstance(context)
                .sendBroadcast(new Intent(Constants.ACTION_SERVICE_STATE_CHANGED));
        break;//from   w w w. j a v  a2s .c  om

    default: {
        NetworkStateService.setMobileOutOfService(false);

        // If the device is network roaming but mobile data roaming is disabled, this
        // broadcast is necessary to properly update the widget on service state changes.
        if ((serviceState.getState() == ServiceState.STATE_IN_SERVICE) && serviceState.getRoaming())
            LocalBroadcastManager.getInstance(context)
                    .sendBroadcast(new Intent(Constants.ACTION_SERVICE_STATE_CHANGED));
    }
        break;
    }
}

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./*from w ww. j av a  2  s .  c  o m*/
 * @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 om

    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//  w  w w.j a va2 s  . c  o  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);
    }
}

From source file:org.restcomm.app.qoslib.Utils.QosInfo.java

public String getServiceStateName(int state) {
    String name = "";
    if (state >= 10) {
        name = "roam ";
        if (state == 10)
            return name;
        else/*  www. j  av  a  2 s  .c o  m*/
            state = state - 10;
    }
    switch (state) {
    case ServiceState.STATE_OUT_OF_SERVICE:
        name += "no service";
        break;
    case ServiceState.STATE_EMERGENCY_ONLY:
        name += "911 only";
        break;
    case PhoneState.SERVICE_STATE_AIRPLANE:
        name += "airplane";
        break;
    case ServiceState.STATE_IN_SERVICE:
        name += "in service";
        break;
    case ServiceState.STATE_POWER_OFF:
        name += "power off";
        break;
    }
    return name;
}

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

@Override
public void onServiceStateChanged(ServiceState serviceState) {
    super.onServiceStateChanged(serviceState);
    if (serviceState == null)
        return;/*ww w.j  a v a 2 s.c o  m*/

    boolean isRoaming = serviceState.getRoaming();
    String operator = serviceState.getOperatorAlphaLong();
    String mccmnc = serviceState.getOperatorNumeric();

    //owner.getConnectionHistory().updateConnectionHistory(cellnettype, state, activity, networkInfo)
    try {
        String activity = owner.getConnectionHistory().updateConnectionHistory(
                telephonyManager.getNetworkType(), telephonyManager.getDataState(),
                telephonyManager.getDataActivity(), serviceState,
                owner.getConnectivityManager().getActiveNetworkInfo());
        if (activity != null)
            owner.getIntentDispatcher().updateConnection(activity, false);
    } catch (Exception e) {
        LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "onServiceStateChanged",
                "exception with updateConnectionHistory:", e);
    }

    //MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "onServiceStateChanged", String.format("State: %s, roaming: %s, operator: %s, mccmnc: %s",
    //         serviceState, isRoaming, operator, mccmnc));
    //MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "onServiceStateChanged", "Reflected: " + listServiceStateFields(serviceState));

    boolean wasRoaming = PreferenceManager.getDefaultSharedPreferences(owner)
            .getBoolean(PreferenceKeys.Miscellaneous.WAS_ROAMING, false);

    //If roaming, track time spend doing this         
    if (mPhoneState.isRoaming() != wasRoaming) {
        int roamValue = 2; //off
        String status = "off";
        if (mPhoneState.isRoaming()) {
            roamValue = 1; //on
            status = "on";
            //For DataMonitor tracking
            Intent intent = new Intent(IntentHandler.ROAMING_ON);
            owner.sendBroadcast(intent);
        } else {
            Intent intent = new Intent(IntentHandler.ROAMING_OFF);
            owner.sendBroadcast(intent);
        }
        LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "onServiceStateChanged", "roaming status: " + status);
        owner.trackAccessPoints(roamValue);
        if (!EventObj.isDisabledEvent(owner, EventObj.DISABLE_ROAMUPDATE))
            owner.getEventManager().triggerUpdateEvent(false, false);
        PreferenceManager.getDefaultSharedPreferences(owner).edit()
                .putBoolean(PreferenceKeys.Miscellaneous.WAS_ROAMING, mPhoneState.isRoaming()).commit();
    }

    //If wimax, track time spend doing this
    if (PhoneState.ActiveConnection(owner) == 12) {
        Intent intent = new Intent(IntentHandler.WIMAX_STATE_CHANGE);
        owner.sendBroadcast(intent);
    }

    //in airplane mode
    if (isAirplaneModeOn(owner.getApplicationContext()) == true) {
        LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "onServiceStateChanged", "airplane mode on");
        mPhoneState.previousServiceState = mPhoneState.SERVICE_STATE_AIRPLANE;
        try {
            SignalEx mmcSignal = new SignalEx();
            processNewMMCSignal(mmcSignal);
        } catch (Exception e) {
        }
        return;
    }

    if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
        if (mPhoneState.previousServiceState != ServiceState.STATE_IN_SERVICE) {

            //state changed from OUT_OF_SERVICE to IN_SERVICE
            LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "onServiceStateChanged",
                    "trigger regain service");
            owner.getEventManager().stopPhoneEvent(EventType.COV_VOD_NO, EventType.COV_VOD_YES);
            mPhoneState.mLastServiceStateChangeTimeStamp = System.currentTimeMillis();

        }
    } else if (serviceState.getState() == ServiceState.STATE_OUT_OF_SERVICE) {// || serviceState.getState() == ServiceState.STATE_EMERGENCY_ONLY) {
        if (mPhoneState.previousServiceState == mPhoneState.SERVICE_STATE_AIRPLANE)
            return; // discard 'no-service' occurring after exiting airplane mode

        if (mPhoneState.previousServiceState == ServiceState.STATE_IN_SERVICE) { // && previousServiceState != SERVICE_STATE_AIRPLANE) {

            mPhoneState.previousServiceState = serviceState.getState();
            SignalEx signal = mPhoneState.getLastMMCSignal();
            processNewMMCSignal(signal);

            // Outage needs to last longer than 5 seconds to actually trigger
            int delay = 5000;
            if (mPhoneState.isCallConnected()
                    || mPhoneState.disconnectTime + 12000 > System.currentTimeMillis())
                delay = 1; // If phone call is connected (or recently disconnected), no delay, dont ignore short outages
            owner.handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // If longer outage after 2 seconds, do nothing
                    if (mPhoneState.previousServiceState != ServiceState.STATE_OUT_OF_SERVICE) //  && previousServiceState != ServiceState.STATE_EMERGENCY_ONLY)
                    {
                        LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "onServiceStateChanged",
                                "Outage lasted < 5 sec, ignoring");
                        return;
                    }
                    // Officially an outage now
                    // If service dropped straight from 3G to nothing, trigger a 3G outage as well
                    // If was connected to wifi when service was lost, does not count as a 3G outage
                    if (PhoneState.ActiveConnection(owner) <= 1 && mPhoneState.previousNetworkTier >= 3) // 10=Wifi, 11=Wimax, 12=Ethernet, 0=other
                    {
                        owner.getEventManager().startPhoneEvent(EventType.COV_3G_NO, EventType.COV_3G_YES);
                        if (mPhoneState.previousNetworkTier >= 5)
                            owner.getEventManager().startPhoneEvent(EventType.COV_4G_NO, EventType.COV_4G_YES);
                    }
                    mPhoneState.mLastServiceStateChangeTimeStamp = System.currentTimeMillis();

                    LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "onServiceStateChanged",
                            "trigger lost service");
                    //state changed from IN_SERVICE to OUT_OF_SERVICE 
                    owner.getEventManager().startPhoneEvent(EventType.COV_VOD_NO, EventType.COV_VOD_YES);
                    if (mPhoneState.previousNetworkTier >= 2)
                        owner.getEventManager().startPhoneEvent(EventType.COV_DATA_NO, EventType.COV_DATA_YES);
                    SignalEx signal = mPhoneState.getLastMMCSignal();
                    processNewMMCSignal(signal);
                    mPhoneState.previousNetworkTier = 0;
                    //previousNetworkState = 0;
                }
            }, delay);

        }
    }
    mPhoneState.previousServiceState = serviceState.getState();
    mPhoneState.previousServiceStateObj = serviceState;

}

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

public void processNewMMCSignal(SignalEx signal) {
    ContentValues values = null;/* www.  j  av a2 s  .c o  m*/
    // if in a service outage, store a null signal
    // (I've seen cases where phone was out of service yet it was still returning a signal level)
    try {
        if (mPhoneState.getLastServiceState() == ServiceState.STATE_OUT_OF_SERVICE)
            signal = null;

        // avoid storing repeating identical signals
        if (mPhoneState.lastKnownMMCSignal != null && mPhoneState.lastKnownMMCSignal.getSignalStrength() != null
                && signal != null && signal.getSignalStrength() != null)
            if (mPhoneState.lastKnownMMCSignal.getSignalStrength().toString().equals(
                    signal.getSignalStrength().toString()) && tmlastSig + 3000 > System.currentTimeMillis())
                return;
        tmlastSig = System.currentTimeMillis();
        Integer dbmValue = 0;
        boolean isLTE = false;
        if (signal == null)
            dbmValue = -256;
        else if (signal.getSignalStrength() == null)
            dbmValue = 0;

        //store the last known signal
        if (signal != null && signal.getSignalStrength() != null) {
            prevMMCSignal = mPhoneState.lastKnownMMCSignal; // used for looking at signal just before a call ended
            mPhoneState.lastKnownMMCSignal = signal;
        } else if (signal == null)
            mPhoneState.lastKnownMMCSignal = null;

        //push the new signal level into the sqlite database
        long stagedEventId = owner.getEventManager().getStagedEventId();
        int serviceState = mPhoneState.getLastServiceState();
        int wifiSignal = -1;
        WifiManager wifiManager = (WifiManager) owner.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiinfo = wifiManager.getConnectionInfo();
        if (wifiinfo != null && wifiinfo.getBSSID() != null)
            wifiSignal = wifiManager.getConnectionInfo().getRssi();

        //if (signal != null) //  disabled because we do want the no-signal to be written to the signals table
        {
            values = ContentValuesGenerator.generateFromSignal(signal, telephonyManager.getPhoneType(),
                    telephonyManager.getNetworkType(), serviceState, telephonyManager.getDataState(),
                    stagedEventId, wifiSignal, mPhoneState.mServicemode);
            Integer valSignal = (Integer) values.get("signal");
            if (mPhoneState.getNetworkType() == PhoneState.NETWORK_NEWTYPE_LTE) // && phoneStateListener.previousNetworkState == TelephonyManager.DATA_CONNECTED)
                valSignal = (Integer) values.get("lteRsrp");
            if (valSignal != null && dbmValue != null && valSignal > -130 && valSignal < -30) //  && (dbmValue <= -120 || dbmValue >= -1))
                dbmValue = valSignal;
            if ((dbmValue > -120 || mPhoneState.getNetworkType() == PhoneState.NETWORK_NEWTYPE_LTE)
                    && dbmValue < -40)
                this.validSignal = true;
            if (this.validSignal) // make sure phone has at least one valid signal before recording
                owner.getDBProvider(owner).insert(TablesEnum.SIGNAL_STRENGTHS.getContentUri(), values);

        }
        //update the signal strength percentometer, chart, and look for low/high signal event
        if (dbmValue != null) {
            if (dbmValue < -120) // might be -256 if no service, but want to display as -120 on livestatus chart
                dbmValue = -120;

            owner.getIntentDispatcher().updateSignalStrength(dbmValue, mPhoneState.getNetworkType(),
                    owner.bWifiConnected, wifiSignal);

            // Store signal in a sharedPreference for tools such as Indoor/Transit sample mapper, which dont have reference to service
            if (isLTE == true) // improve the value of the signal for LTE, so that Indoor samples don't look redder in LTE
                PreferenceManager.getDefaultSharedPreferences(owner).edit()
                        .putInt(PreferenceKeys.Miscellaneous.SIGNAL_STRENGTH, (dbmValue + 15)).commit();
            else
                PreferenceManager.getDefaultSharedPreferences(owner).edit()
                        .putInt(PreferenceKeys.Miscellaneous.SIGNAL_STRENGTH, dbmValue).commit();

        }
    } catch (Exception e) {
        LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "processNewMMCSignal", "exception", e);
        LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "processNewMMCSignal", "values: " + values);
    } catch (Error err) {
        LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "processNewMMCSignal", "error" + err.getMessage());
    }
}

From source file:com.android.mms.ui.MessageUtils.java

public static int getCellularState(int selectedSubId, Context context) {
    MmsLog.d(TAG, "getCellularState() subid: " + selectedSubId);
    ITelephonyEx telephonyEx = ITelephonyEx.Stub
            .asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE_EX));
    Bundle bundle = null;//from  w  ww.j  a  v  a 2  s .c  o  m
    try {
        bundle = telephonyEx.getServiceState(selectedSubId);
    } catch (RemoteException e) {
        e.printStackTrace();
        return ServiceState.STATE_OUT_OF_SERVICE;
    }
    if (bundle != null) {
        return ServiceState.newFromBundle(bundle).getState();
    } else {
        return ServiceState.STATE_OUT_OF_SERVICE;
    }
}