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:org.restcomm.app.qoslib.Utils.QosInfo.java

/**
 * Called Internally to initialize all of the current measurements for every type of connected newtork
 * This is like a snapshot of everything at this moment in time
 * The information comes from various sources and it mainly fetched from the database for this call, or from APIs such as WiFi
 *//* w ww. jav a  2  s  . co  m*/
private void updateFromDB() {
    Cursor sig_cursor = null;
    Cursor cell_cursor = null;

    try {
        Uri signalUri = UriMatch.SIGNAL_STRENGTHS.getContentUri();
        Uri limitSignalUri = signalUri.buildUpon().appendQueryParameter("limit", "1").build();
        // sig_cursor = managedQuery(

        Provider dbProvider = ReportManager.getInstance(context.getApplicationContext()).getDBProvider();
        if (dbProvider == null) {
            return;
        }

        sig_cursor = dbProvider.query(UriMatch.SIGNAL_STRENGTHS.getContentUri(), null, null, null,
                Tables.TIMESTAMP_COLUMN_NAME + " DESC");

        sig_cursor.moveToFirst();

        Uri baseStationTable = (UriMatch.BASE_STATIONS
                .getContentUri()/*telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA
                                ? UriMatchOld.BASE_STATIONS_CDMA.getContentUri()
                                : UriMatchOld.BASE_STATIONS_GSM.getContentUri()*/
        );
        Uri limitBSUri = baseStationTable.buildUpon().appendQueryParameter("limit", "1").build();

        // Cursor cell_cursor = managedQuery(
        cell_cursor = dbProvider.query(limitBSUri, null, null, null, Tables.TIMESTAMP_COLUMN_NAME + " DESC");

        cell_cursor.moveToFirst();

        int LowIndex = cell_cursor.getColumnIndex(Tables.BaseStations.BS_LOW);
        int MidIndex = cell_cursor.getColumnIndex(Tables.BaseStations.BS_MID);
        int HighIndex = cell_cursor.getColumnIndex(Tables.BaseStations.BS_HIGH);
        int CodeIndex = cell_cursor.getColumnIndex(Tables.BaseStations.BS_CODE);
        int BandIndex = cell_cursor.getColumnIndex(Tables.BaseStations.BS_BAND);
        int ChanIndex = cell_cursor.getColumnIndex(Tables.BaseStations.BS_CHAN);
        int netTypeIndex = cell_cursor.getColumnIndex(Tables.BaseStations.NET_TYPE);
        String netType = cell_cursor.getString(netTypeIndex);
        int bsLow = cell_cursor.getInt(LowIndex);
        int bsMid = cell_cursor.getInt(MidIndex);
        int bsHigh = cell_cursor.getInt(HighIndex);
        int bsCode = cell_cursor.getInt(CodeIndex);
        int bsBand = cell_cursor.getInt(BandIndex);
        int bsChan = cell_cursor.getInt(ChanIndex);
        if (netType.equals("cdma")) {
            if (LowIndex != -1)
                BID = bsLow;
            if (MidIndex != -1)
                NID = bsMid;
            if (HighIndex != -1)
                SID = bsHigh;
        } else if (netType.equals("gsm")) {
            if (LowIndex != -1) {
                RNC = bsMid;
                CellID = cell_cursor.getInt(LowIndex);
            }
            // the network Id is kept 0 for gsm phones
            if (HighIndex != -1)
                LAC = bsHigh;
            if (bsCode > 0 && bsCode < 1000)
                PSC = bsCode;
            else
                PSC = 0;
            if (bsBand > 0)
                Band = bsBand;
            else
                Band = 0;
            if (bsChan > 0)
                Channel = bsChan;
            else
                Channel = 0;
        }

        if (sig_cursor.getCount() != 0) {
            int signalIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.SIGNAL);
            int signal2GIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.SIGNAL2G);
            int rsrpIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.LTE_RSRP);
            int rsrqIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.LTE_RSRQ);
            int lteSnrIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.LTE_SNR);
            int lteSignalIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.LTE_SIGNAL);
            int lteCqiIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.LTE_CQI);
            int ecioIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.ECI0);
            int ecnoIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.ECN0);
            int snrIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.SNR);
            int berIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.BER);
            int rscpIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.RSCP);
            int tierIndex = sig_cursor.getColumnIndex(Tables.SignalStrengths.COVERAGE);
            Integer tier = sig_cursor.isNull(tierIndex) ? null : sig_cursor.getInt(tierIndex);
            Integer rssi = sig_cursor.isNull(signalIndex) ? null : sig_cursor.getInt(signalIndex);
            Integer rssi2G = sig_cursor.isNull(signal2GIndex) ? null : sig_cursor.getInt(signal2GIndex);
            Integer rsrp = sig_cursor.isNull(rsrpIndex) ? null : sig_cursor.getInt(rsrpIndex);
            Float lteSnr = (sig_cursor.isNull(lteSnrIndex) ? null : (float) sig_cursor.getInt(lteSnrIndex));
            Integer lteSignal = sig_cursor.isNull(lteSignalIndex) ? null : sig_cursor.getInt(lteSignalIndex);
            Integer lteCqi = sig_cursor.isNull(lteCqiIndex) ? null : sig_cursor.getInt(lteCqiIndex);
            Integer rsrq = sig_cursor.isNull(rsrqIndex) ? null : sig_cursor.getInt(rsrqIndex);
            Integer eci0 = sig_cursor.isNull(ecioIndex) ? null : sig_cursor.getInt(ecioIndex);
            Integer ecn0 = sig_cursor.isNull(ecnoIndex) ? null : sig_cursor.getInt(ecnoIndex);
            Integer snr = sig_cursor.isNull(snrIndex) ? null : sig_cursor.getInt(snrIndex);
            Integer ber = sig_cursor.isNull(berIndex) ? null : sig_cursor.getInt(berIndex);
            Integer rscp = sig_cursor.isNull(rscpIndex) ? null : sig_cursor.getInt(rscpIndex);

            if (eci0 != null && (netType.equals("cdma") || netType.equals("lte")) && eci0 <= -30)
                eci0 = (eci0 / 10);
            else if (ecn0 != null && ecn0 > 1 && ecn0 < 60 && netType.equals("gsm"))
                ecn0 = -(ecn0 / 2);
            else if (eci0 != null && eci0 > 1 && eci0 < 60 && netType.equals("gsm"))
                eci0 = -(eci0 / 2);

            // if (lteSnr != null && lteSnr > 1 && lteSnr < 500)
            // lteSnr = (lteSnr+5)/10;
            if (lteSignal != null && lteSignal > -120 && lteSignal < -20) // rssi == lteSignal)
            {
                LTE_RSSI = simpleValidate(lteSignal);
                RSSI = 0;
            } else if (rssi == null || rssi == -255)
                RSSI = 0;
            else if (rssi == -256)
                RSSI = -256;
            else {
                String name = "RSCP: ";
                int spacing = 15;
                if (netType.equals("gsm") && (tier == 3 || tier == 4)) {
                    RSCP = rssi;
                    RSSI = 0;
                } else {
                    RSSI = rssi;
                    RSCP = 0;
                }

                LTE_RSSI = 0;
            }
            if (netType.equals("cdma") && rssi2G != null && rssi2G < -30 && rssi2G >= -120)
                CDMA_RSSI = rssi2G;
            if (tier == 5) {
                if (lteSnr != null && lteSnr > -101)
                    lteSnr = lteSnr / 10;
                if (rsrq != null && rsrq > 0)
                    rsrq = -rsrq;
                LTE_RSRP = simpleValidate(rsrp);
                LTE_RSRQ = simpleValidate(rsrq);
                LTE_SNR = simpleValidate(lteSnr);
                LTE_CQI = simpleValidate(lteCqi);
                ECIO = simpleValidate(eci0);
                ECNO = simpleValidate(ecn0);
            }
            //nerdview.setValue(0, "RSCP", simpleValidate(rscp, "RSCP", "dBm"));
            //BER = simpleValidate(ber);
            SNR = simpleValidate(snr);

            if (rsrp != null && rsrp <= -10 && rsrp >= -140 && tier == 5) {
                LTEIdentity = ReportManager.getInstance(context.getApplicationContext()).getNeighbors();
            } else {
                LTEIdentity = null;
                Neighbors = ReportManager.getInstance(context.getApplicationContext()).getNeighbors();
            }
        }
        location = ReportManager.getInstance(context.getApplicationContext()).getLastKnownLocation();

        try {
            JSONObject serviceMode = PhoneState.getServiceMode();
            if (serviceMode != null && serviceMode.getLong("time") + 5000 > System.currentTimeMillis()) {
                if (serviceMode.has("rrc") && serviceMode.getString("rrc").length() > 1) {
                    RRC = serviceMode.getString("rrc");
                } else
                    RRC = null;
                if (serviceMode.has("band") && serviceMode.getString("band").length() > 0) {
                    Band = Integer.parseInt(serviceMode.getString("band"));
                }
                //else
                if (serviceMode.has("freq") && serviceMode.getString("freq").length() > 0) {
                    Band = Integer.parseInt(serviceMode.getString("freq"));
                } else
                    Band = 0;
                if (serviceMode.has("channel") && serviceMode.getString("channel").length() > 0) {
                    Channel = Integer.parseInt(serviceMode.getString("channel"));
                } else
                    Channel = 0;
            } else {
                RRC = null;
                Band = 0;
                Channel = 0;
            }

            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Service.TELEPHONY_SERVICE);
            String carrier = telephonyManager.getNetworkOperatorName();
            String mcc = "0", mnc = "0";
            if (telephonyManager.getNetworkOperator() != null
                    && telephonyManager.getNetworkOperator().length() >= 4) {
                mcc = telephonyManager.getNetworkOperator().substring(0, 3);
                mnc = telephonyManager.getNetworkOperator().substring(3);
            }
            int networkType = telephonyManager.getNetworkType();
            int networkTier = PhoneState.getNetworkGeneration(networkType);
            String nettype = PhoneState.getNetworkName(telephonyManager.getNetworkType());
            String data = PhoneState.getNetworkName(telephonyManager.getNetworkType()) + " ";
            int dataState = telephonyManager.getDataState();
            if (dataState == TelephonyManager.DATA_CONNECTED) {
                String activity = getActivityName(telephonyManager.getDataActivity());
                data += activity;
            } else if (telephonyManager.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                String state = getStateName(telephonyManager.getDataState());
                data += state;
            }

            Data = data;
            Carrier = carrier;

            Date date = new Date(System.currentTimeMillis());
            final String dateStr = DateFormat.getDateFormat(context).format(date);
            final String timeStr = dateStr + "  " + DateFormat.getTimeFormat(context).format(date);

            Time = timeStr;

            MCC = Integer.parseInt(mcc);
            MNC = Integer.parseInt(mnc);

            // Tell the service we're watching the signal, so keep it updated
            Intent intent = new Intent(CommonIntentActionsOld.VIEWING_SIGNAL);
            context.sendBroadcast(intent);

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

            // Instantiate only the relevant Network type
            if (netType.equals("cdma"))
                connectedNetwork = CDMAInfo = new CDMAInfo(this);
            else if (netType.equals("gsm") && networkTier < 3)
                connectedNetwork = GSMInfo = new GSMInfo(this);
            else if (netType.equals("gsm") && networkTier < 5)
                connectedNetwork = WCDMAInfo = new WCDMAInfo(this);
            if (networkTier == 5) // LTE
                connectedNetwork = LTEInfo = new LTEInfo(this);
            if (wifiConfig != null)
                connectedNetwork = WiFiInfo = new WIFIInfo(this); // The most relevant network ends up in NetworkInfo
        } catch (Exception e) {
        }

    } catch (Exception e) {
        LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "updateNerdViewFromDB",
                "exception querying signal data: " + e.getMessage());
    } finally {
        if (cell_cursor != null)
            cell_cursor.close();
        if (sig_cursor != null)
            sig_cursor.close();
    }
}

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

public String getStateName(int state) {
    switch (state) {
    case TelephonyManager.DATA_CONNECTED:
        return "connected";
    case TelephonyManager.DATA_CONNECTING:
        return "connecting";
    case TelephonyManager.DATA_DISCONNECTED:
        return "disconnected";
    case TelephonyManager.DATA_SUSPENDED:
        return "suspended";
    }/*from   www.j a v  a2 s  . c om*/
    return "-";
}

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

private void stateChanged_2g(int state) {
    // No such thing as DATA outage event
    EventObj event = null;//from   ww w .j  a  va2 s .  com
    //if (state == TelephonyManager.DATA_CONNECTED && previousNetworkTier == 0 && owner.getNetworkGeneration() == 1){
    //   event = owner.stopPhoneEvent(EventType.COV_DATA_DISC, EventType.COV_DATA_CONN);
    //} 
    // DATA Outage defined as switching to and connecting to 1G (GPRS) from > 1G (EDGE or higher)
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier > 1
            && mPhoneState.getNetworkGeneration() == 1) {
        if (mPhoneState.isScreenOn() || mPhoneState.isOffHook() || owner.isTravelling())
            event = owner.getEventManager().startPhoneEvent(EventType.COV_DATA_NO, EventType.COV_DATA_YES);
    }
    // 3G Outage defined as switching to and connecting to 2G from >2G
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier > 2) {
        if (mPhoneState.isScreenOn() || mPhoneState.isOffHook() || owner.isTravelling())
            event = owner.getEventManager().startPhoneEvent(EventType.COV_3G_NO, EventType.COV_3G_YES);
    }
    // 4G Outage defined as switching to and connecting to 2G from 4G
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier > 4) {
        if (mPhoneState.isScreenOn() || mPhoneState.isOffHook() || owner.isTravelling())
            event = owner.getEventManager().startPhoneEvent(EventType.COV_4G_NO, EventType.COV_4G_YES);
    }

    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier < 2
            && mPhoneState.getNetworkGeneration() == 2)
        event = owner.getEventManager().stopPhoneEvent(EventType.COV_DATA_NO, EventType.COV_DATA_YES);

}

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

private void stateChanged_3g(int state) {
    EventObj event = null;//w  ww  .ja  va2  s  . c  om
    //if (state == TelephonyManager.DATA_CONNECTED && previousNetworkTier == 0 && owner.getNetworkGeneration() == 1){
    //   event = owner.stopPhoneEvent(EventType.COV_DATA_DISC, EventType.COV_DATA_CONN);
    //} 
    // 3G Regained defined as switching to and connecting to 3G+ from <3G
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier < 1)
        event = owner.getEventManager().stopPhoneEvent(EventType.COV_DATA_NO, EventType.COV_DATA_YES);
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier < 3) {
        if (mPhoneState.previousNetworkTier <= 1)
            event = owner.getEventManager().stopPhoneEvent(EventType.COV_DATA_NO, EventType.COV_DATA_YES);
        event = owner.getEventManager().stopPhoneEvent(EventType.COV_3G_NO, EventType.COV_3G_YES);
    }
    // 4G Outage defined as switching to and connecting to 3G from LTE 4G
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier > 4) {// && !mPhoneState.bOffHook){
        if (mPhoneState.isOffHook()) {
            EventCouple targetEventCouple = owner.getEventManager().getEventCouple(EventType.EVT_CONNECT,
                    EventType.EVT_DISCONNECT);
            if (targetEventCouple != null) {
                EventObj connectEvent = targetEventCouple.getStartEvent();
                connectEvent.setFlag(EventObj.CALL_CSFB, true);
            }
        } else if (mPhoneState.isScreenOn() || owner.isTravelling())
            event = owner.getEventManager().startPhoneEvent(EventType.COV_4G_NO, EventType.COV_4G_YES);
    }

}

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

private void stateChanged_4g(int state) {
    EventObj event = null;//from   www  .  j  a v  a2s  . co  m
    //if (state == TelephonyManager.DATA_CONNECTED && previousNetworkTier == 0 && owner.getNetworkGeneration() == 1){
    //   event = owner.stopPhoneEvent(EventType.COV_DATA_DISC, EventType.COV_DATA_CONN);
    //} 
    // 4G Regained defined as switching to and connecting to 4G+ from <3G
    // If it switches to and from LTE too often, this could result in excessive events, but it appears to hold LTE steady
    if (state == TelephonyManager.DATA_CONNECTED && mPhoneState.previousNetworkTier < 5) {
        if (mPhoneState.previousNetworkTier <= 1)
            event = owner.getEventManager().stopPhoneEvent(EventType.COV_DATA_NO, EventType.COV_DATA_YES);
        if (mPhoneState.previousNetworkTier < 3)
            event = owner.getEventManager().stopPhoneEvent(EventType.COV_3G_NO, EventType.COV_3G_YES);

        String pref = mPhoneState.getNetworkTypesAndroidPreference();
        if (pref.indexOf("LTE") >= 0)
            return;
        // disregard and undo an LTE outage if it regains just after a phone call disconnects
        EventCouple targetEventCouple = owner.getEventManager().getEventCouple(EventType.COV_4G_NO,
                EventType.COV_4G_YES);

        if (mPhoneState.disconnectTime + 120000 > System.currentTimeMillis() && targetEventCouple != null
                && targetEventCouple.getStartEvent() != null
                && targetEventCouple.getStartEvent().getEventTimestamp() + 30000 > mPhoneState.offhookTime
                && mPhoneState.offhookTime > 0) {
            LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "stateChanged_4g", "Undo LTE outage event");
            // 4G outage wont be staged anymore, but it still needs to remove COV_4G_NO from 'eventCache'
            owner.getEventManager().unstageEvent(targetEventCouple.getStartEvent());
            owner.getEventManager().cancelCouple(targetEventCouple);
            //owner.getEventManager().deleteEventDB(targetEventCouple.getStartEvent().getUri(), null, null);
            int eventId = ReportManager.getInstance(owner).getEventId(
                    targetEventCouple.getStartEvent().getEventTimestamp(), EventType.COV_4G_NO.getIntValue());
            if (eventId != 0)
                ReportManager.getInstance(owner).deleteEvent(eventId);
        } else {
            event = owner.getEventManager().stopPhoneEvent(EventType.COV_4G_NO, EventType.COV_4G_YES);
            //if (event != null)
            //   owner.queueActiveTest(EventType.LATENCY_TEST, 1);
        }
    }
}