Example usage for android.telephony CellInfo isRegistered

List of usage examples for android.telephony CellInfo isRegistered

Introduction

In this page you can find the example usage for android.telephony CellInfo isRegistered.

Prototype

public boolean isRegistered() 

Source Link

Document

True if the phone is registered to a mobile network that provides service on this cell and this cell is being used or would be used for network signaling.

Usage

From source file:ch.ethz.coss.nervousnet.vm.sensors.ConnectivitySensor.java

public void runConnectivitySensor() {
    Log.d(LOG_TAG, "Inside runConnectivitySensor");
    if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(context,
            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;//from www. j  a va  2s  .  c  o  m
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    int networkType = -1;
    boolean isRoaming = false;
    if (isConnected) {
        networkType = activeNetwork.getType();
        isRoaming = activeNetwork.isRoaming();
    }

    String wifiHashId = "";
    int wifiStrength = Integer.MIN_VALUE;

    if (networkType == ConnectivityManager.TYPE_WIFI) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wi = wm.getConnectionInfo();
        StringBuilder wifiInfoBuilder = new StringBuilder();
        wifiInfoBuilder.append(wi.getBSSID());
        wifiInfoBuilder.append(wi.getSSID());
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(wifiInfoBuilder.toString().getBytes());
            wifiHashId = new String(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
        }
        wifiStrength = wi.getRssi();
    }

    byte[] cdmaHashId = new byte[32];
    byte[] lteHashId = new byte[32];
    byte[] gsmHashId = new byte[32];
    byte[] wcdmaHashId = new byte[32];

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    List<CellInfo> cis = tm.getAllCellInfo();
    if (cis != null) {
        // New method
        for (CellInfo ci : cis) {
            if (ci.isRegistered()) {
                if (ci instanceof CellInfoCdma) {
                    CellInfoCdma cic = (CellInfoCdma) ci;
                    cdmaHashId = generateMobileDigestId(cic.getCellIdentity().getSystemId(),
                            cic.getCellIdentity().getNetworkId(), cic.getCellIdentity().getBasestationId());
                }
                if (ci instanceof CellInfoGsm) {
                    CellInfoGsm cic = (CellInfoGsm) ci;
                    gsmHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(),
                            cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCid());
                }
                if (ci instanceof CellInfoLte) {
                    CellInfoLte cic = (CellInfoLte) ci;
                    lteHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(),
                            cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCi());
                }
                if (ci instanceof CellInfoWcdma) {
                    CellInfoWcdma cic = (CellInfoWcdma) ci;
                    wcdmaHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(),
                            cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCid());
                }
            }
        }
    } else {
        // Legacy method
        CellLocation cl = tm.getCellLocation();
        if (cl instanceof CdmaCellLocation) {
            CdmaCellLocation cic = (CdmaCellLocation) cl;
            cdmaHashId = generateMobileDigestId(cic.getSystemId(), cic.getNetworkId(), cic.getBaseStationId());
        }
        if (cl instanceof GsmCellLocation) {
            GsmCellLocation cic = (GsmCellLocation) cl;
            gsmHashId = generateMobileDigestId(cic.getLac(), 0, cic.getCid());
        }
    }

    StringBuilder mobileHashBuilder = new StringBuilder();
    mobileHashBuilder.append(new String(cdmaHashId));
    mobileHashBuilder.append(new String(lteHashId));
    mobileHashBuilder.append(new String(gsmHashId));
    mobileHashBuilder.append(new String(wcdmaHashId));

    dataReady(new ConnectivityReading(System.currentTimeMillis(), isConnected, networkType, isRoaming,
            wifiHashId, wifiStrength, mobileHashBuilder.toString()));

}

From source file:org.pixmob.freemobile.netstat.MonitorService.java

/**
 * Check if we are connected on a Free Mobile femtocell
 *//*from  w w w  .  ja  v  a2  s.co m*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void updateFemtocellStatus() {
    // No need to check LAC if current operator is not free mobile
    // And no need to check if network type is not femtocell supported network
    if ((!MobileOperator.FREE_MOBILE.equals(MobileOperator.fromString(mobileOperatorId)))
            || ((MobileOperator.FREE_MOBILE.equals(MobileOperator.fromString(mobileOperatorId)))
                    && (!FEMTOCELL_AVAILABLE_NETWORK_TYPE.contains(mobileNetworkType)))) {
        isFemtocell = false;
        return;
    }

    Integer lac = null;
    if (tm != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

            //get the cell list
            List<CellInfo> cellInfos = tm.getAllCellInfo();
            if (cellInfos != null) {
                for (CellInfo cellInfo : cellInfos) {

                    if (cellInfo.isRegistered()) { //we use only registered cells
                        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
                                && (cellInfo instanceof CellInfoWcdma)) { //manage the wcdma cell case
                            Log.d(TAG, "We got a WCDMA cell");
                            CellIdentityWcdma ci = ((CellInfoWcdma) cellInfo).getCellIdentity();
                            if (ci != null) { //save the LAC and exit loop
                                lac = ci.getLac();
                                Log.d(TAG, "We got the LAC - exit loop");
                                break;
                            }

                        } else if (cellInfo instanceof CellInfoGsm) { //test the gsm case
                            CellIdentityGsm ci = ((CellInfoGsm) cellInfo).getCellIdentity();
                            Log.d(TAG, "We got a CDMA cell");
                            if (ci != null) { //save the LAC and exit loop
                                lac = ci.getLac();
                                Log.d(TAG, "We got the LAC - exit loop");
                                break;
                            }
                        }

                    } else
                        Log.d(TAG, "Unregistered cell - skipping");
                }
            } else
                Log.d(TAG, "No cell infos available");

        }
        if (lac == null) { //use old API if LAC was not found with the new method (useful for buggy devices such as Samsung Galaxy S5) or if SDK is too old
            CellLocation cellLocation = tm.getCellLocation(); //cell location might be null... handle with care
            if ((cellLocation != null) && (cellLocation instanceof GsmCellLocation)) {
                Log.d(TAG, "We got a old GSM cell with LAC");
                lac = ((GsmCellLocation) cellLocation).getLac();
            }
        }
    }
    if (DEBUG)
        Log.d(TAG, "LAC value : " + lac);

    Log.i(TAG, "Femtocell value : " + isFemtocell);

    if (lac != null) {
        String lacAsString = String.valueOf(lac);
        isFemtocell = (lacAsString.length() == 4)
                && (lacAsString.subSequence(1, 3).equals(FREE_MOBILE_FEMTOCELL_LAC_CODE));
    }
}

From source file:org.restcomm.app.qoslib.UtilsOld.CellHistory.java

public String updateNeighborHistory(int[] _list, int[] _list_rssi) {
    int i;// w w w  . ja v  a 2s .  co  m
    //if (tmLastNeighborUpdate + 2000 > System.currentTimeMillis())
    //   return "";

    //if (!_isTravelling)
    {
        String txt = "";
        String stringNeighboring = "";
        String[] _type = null;
        try {
            int netType = telephonyManager.getNetworkType();
            int gen = PhoneState.getNetworkGeneration(netType);
            int n = 0, len = 0;
            boolean bValid = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                try {
                    List<CellInfo> cells = null;
                    cells = telephonyManager.getAllCellInfo();
                    if (cells != null && cells.size() > 0 && cells.get(0) instanceof CellInfoLte
                            && telephonyManager.getNetworkType() == PhoneState.NETWORK_NEWTYPE_LTE) {
                        return updateLteNeighborHistory(cells);
                    }
                    //if (cells == null)
                    //   LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "updateNeighborHistory", "cells = null");

                    if (cells != null && cells.size() > 1)//1)
                    {

                        len = cells.size();
                        if (len > 8)
                            len = 8;
                        _list = new int[len];
                        _list_rssi = new int[len];
                        _type = new String[len];
                        for (i = 0; i < len; i++) {
                            bValid = true;
                            CellInfo neighbor = cells.get(i);
                            String msg = "cells[" + i + "]=" + neighbor.toString();
                            String classname = neighbor.getClass().toString();
                            //LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "updateNeighborHistory", "cells[" + i + "]=" + neighbor.toString());
                            //Log.d(TAG, "cells[" + c + "]=" + cells.get(c).toString());
                            if (gen < 3)
                                _type[i] = "2G";
                            else
                                _type[i] = "3G";
                            if (neighbor.isRegistered())
                                _type[i] += "*";
                            if (neighbor instanceof CellInfoCdma) {
                                CellIdentityCdma cdmacell = ((CellInfoCdma) neighbor).getCellIdentity();
                                CellSignalStrengthCdma cdmasig = ((CellInfoCdma) neighbor)
                                        .getCellSignalStrength();
                                if (cdmacell.getBasestationId() > 0) {
                                    _list[n] = cdmacell.getBasestationId();
                                    _list_rssi[n] = cdmasig.getEvdoDbm();
                                    n++;
                                    //else if (neighbor.getSystemId() > 0)
                                    //   _list[n] = (neighbor.getLac()<<16) + neighbor.getCid();
                                } else
                                    bValid = false;
                            } else if (neighbor instanceof CellInfoGsm) {
                                CellIdentityGsm gsmcell = ((CellInfoGsm) neighbor).getCellIdentity();
                                CellSignalStrengthGsm gsmsig = ((CellInfoGsm) neighbor).getCellSignalStrength();
                                if (gsmcell.getPsc() > 0 && gsmcell.getPsc() < 1000) {
                                    _list[n] = gsmcell.getPsc();
                                    _list_rssi[n] = gsmsig.getDbm();
                                    n++;
                                } else if (gsmcell.getCid() > 0) {
                                    _list[n] = (gsmcell.getLac() << 16) + gsmcell.getCid();
                                    _list_rssi[n] = gsmsig.getDbm();
                                } else
                                    bValid = false;
                            } else if (neighbor.getClass().toString()
                                    .equals("class android.telephony.CellInfoWcdma")) {
                                try {
                                    Object gsmcell = neighbor.getClass().getDeclaredMethod("getCellIdentity")
                                            .invoke(neighbor, (Object[]) null);
                                    Object gsmsig = neighbor.getClass()
                                            .getDeclaredMethod("getCellSignalStrength")
                                            .invoke(neighbor, (Object[]) null);
                                    Integer psc = (Integer) gsmcell.getClass().getDeclaredMethod("getPsc")
                                            .invoke(gsmcell, (Object[]) null);
                                    Integer cid = (Integer) gsmcell.getClass().getDeclaredMethod("getCid")
                                            .invoke(gsmcell, (Object[]) null);
                                    Integer dbm = (Integer) gsmsig.getClass().getDeclaredMethod("getDbm")
                                            .invoke(gsmsig, (Object[]) null);
                                    if (psc > 0 && psc < 1000) {
                                        _list[n] = psc;
                                        //if (neighbor.isRegistered())
                                        //   _list[n] += 10000;
                                        _list_rssi[n] = dbm;
                                        n++;
                                    } else if (cid > 0 && cid < 65536) {
                                        _list[n] = cid;
                                        //if (neighbor.isRegistered())
                                        //   _list[n] += 10000;
                                        _list_rssi[n] = dbm;
                                        n++;
                                    } else
                                        bValid = false;
                                } catch (Exception e) {
                                    //Log.d(TAG, "Field does not exist - " + fieldname);
                                }

                                //                     CellIdentityWcdma gsmcell =((CellInfoWcdma) neighbor).getCellIdentity();
                                //                     CellSignalStrengthWcdma gsmsig = ((CellInfoWcdma) neighbor).getCellSignalStrength();
                                //                     if (gsmcell.getPsc() > 0)
                                //                     {   _list[n] = gsmcell.getPsc();
                                //                        if (neighbor.isRegistered())
                                //                           _list[n] += 0;
                                //                        _list_rssi[n] = gsmsig.getDbm();
                                //                        n++;
                                //                     }
                                //                     else if (gsmcell.getCid() > 0)
                                //                     {
                                //                        _list[n] = (gsmcell.getLac()<<16) + gsmcell.getCid();
                                //                     }
                                //                     else
                                //                        bValid = false;
                            }
                        }

                    }

                } catch (Exception e) {

                }
            }
            if (_list == null) {
                // Try to obtain the Neighbor list from the API
                // If the API neighbor list has more active entries than what the log provided, use it instead
                List<NeighboringCellInfo> neighboringList = telephonyManager.getNeighboringCellInfo();
                len = neighboringList.size();
                if (neighboringList != null && len > 0) {
                    int activeN = 0; // how many of the API neighbors are active?

                    for (i = 0; i < neighboringList.size(); i++) {
                        NeighboringCellInfo neighbor = neighboringList.get(i);
                        if (neighbor.getPsc() > 0 && neighbor.getRssi() > -120 && neighbor.getRssi() < -10)
                            activeN++;
                        else if (neighbor.getCid() > 0 && neighbor.getRssi() > 0 && neighbor.getRssi() < 32)
                            activeN++;
                    }
                    _list = new int[len];
                    _list_rssi = new int[len];
                    _type = new String[len];

                    for (i = 0; i < len; i++) {
                        bValid = true;
                        NeighboringCellInfo neighbor = neighboringList.get(i);
                        if (gen < 3)
                            _type[n] = "2G";
                        else
                            _type[n] = "3G";
                        if (neighbor.getPsc() > 0)
                            _list[n] = neighbor.getPsc();
                        else if (neighbor.getLac() > 0)
                            _list[n] = (neighbor.getLac() << 16) + neighbor.getCid();
                        else
                            bValid = false;

                        if (bValid) {
                            _list_rssi[n] = neighbor.getRssi();
                            if (_list_rssi[n] >= 0)
                                _list_rssi[n] = getDbmValue(_list_rssi[n]);
                            n++;
                        }
                    }
                }
            }

            if (_list == null)
                return null;
            tmLastNeighborUpdate = System.currentTimeMillis();
            // create and add a neighbor cell item for each neighbor
            for (i = 0; i < _list.length; i++) {
                if (_list[i] != 0) {
                    CellidSample smp = new CellidSample(_type[i], _list[i], _list_rssi[i]);
                    // Update Neighbor list history a maximum of once per 2 seconds
                    neighbor_history.add(smp);
                    stringNeighboring = stringNeighboring + String.valueOf(_list[i] & 0xFFFF) + "@"
                            + _list_rssi[i] + ",";

                }
            }
            // Report the API neighbor list in the Log, build a string of neighbors
            if (lastCellString == stringNeighboring)
                return null;
            lastCellString = stringNeighboring;
            //if (neighboringList.size() > 0)
            //MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "updateNeighborHistory", stringNeighboring);

            return stringNeighboring;
        } catch (Exception e) {
            return "";
        } finally {
            //if (txt.length() > 0 && (txt.indexOf("might") > 0 || txt.indexOf("detect") > 0))
            //   MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "detectTravellingFromNeighbors", "\n"+txt.substring(0,txt.length()));
        }
    }
}