Java Code Examples for android.telephony.CellInfo#isRegistered()

The following examples show how to use android.telephony.CellInfo#isRegistered() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CellLocationScannerDialogFragment.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Void doInBackground(Void... voids) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
        if (cellInfoList != null) {
            for (CellInfo cellInfo : cellInfoList) {
                if (cellInfo.isRegistered()) {
                    publishProgress(CellLocationSingleData.fromCellInfo(cellInfo));
                }
            }
        }
    } else {
        CellLocation cellLocation = telephonyManager.getCellLocation();
        publishProgress(CellLocationSingleData.fromCellLocation(cellLocation));
    }
    return null;
}
 
Example 2
Source File: RadioInfo.java    From CellularSignal with GNU General Public License v3.0 6 votes vote down vote up
private void getWcdmaSignalStrength() {
    List<CellInfo> cellInfoList = mTM.getAllCellInfo();

    if (cellInfoList == null) {
        //Log.e(Tag,"getAllCellInfo is null");
        return;
    }
    //Log.e(Tag,"getAllCellInfo size "+cellInfoList.size());

    for (CellInfo cellInfo : cellInfoList) {

        if (!cellInfo.isRegistered())
            continue;

        if (cellInfo instanceof CellInfoWcdma) {
            CellInfoWcdma wcdmaInfo = (CellInfoWcdma) cellInfo;
            wcdma_RSSI = wcdmaInfo.getCellSignalStrength().getDbm();
        }
    }
}
 
Example 3
Source File: PlatformNetworksManager.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a CellInfo object representing the currently registered base stations, containing
 * its identity fields and signal strength. Null if no base station is active.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Nullable
private static CellInfo getActiveCellInfo(TelephonyManager telephonyManager) {
    int numRegisteredCellInfo = 0;
    List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();

    if (cellInfos == null) {
        return null;
    }
    CellInfo result = null;

    for (int i = 0; i < cellInfos.size(); i++) {
        CellInfo cellInfo = cellInfos.get(i);
        if (cellInfo.isRegistered()) {
            numRegisteredCellInfo++;
            if (numRegisteredCellInfo > 1) {
                return null;
            }
            result = cellInfo;
        }
    }
    // Only found one registered cellinfo, so we know which base station was used to measure
    // network quality
    return result;
}
 
Example 4
Source File: Api17PlusMeasurementParser.java    From TowerCollector with Mozilla Public License 2.0 4 votes vote down vote up
private ParseResult parse(Location location, List<CellInfo> cells,
                          long timestamp, int minDistance) {
    // if required accuracy was achieved
    if (!locationValidator.isValid(location)) {
        Timber.d("parse(): Required accuracy not achieved: %s", location.getAccuracy());
        return ParseResult.AccuracyNotAchieved;
    }
    Timber.d("parse(): Required accuracy achieved: %s", location.getAccuracy());
    // get last location
    getAndSetLastLocation();
    // create measurement
    Measurement measurement = new Measurement();
    measurement.setMeasuredAt(System.currentTimeMillis());
    // fix time if incorrect
    fixMeasurementTimestamp(measurement, location);
    // remove duplicated cells
    removeDuplicatedCells(cells);
    // if the same cell check distance condition, otherwise accept
    if (lastSavedMeasurement != null && lastSavedLocation != null && !conditionsValidator.isMinDistanceSatisfied(lastSavedLocation, location, minDistance)) {
        List<String> lastMeasurementsCellKeys = new ArrayList<>();
        for (Cell lastCell : lastSavedMeasurement.getCells()) {
            lastMeasurementsCellKeys.add(cellIdentityConverter.createCellKey(lastCell));
        }
        int mainCellsChanged = 0;
        for (CellInfo cell : cells) {
            if (cell.isRegistered() && !lastMeasurementsCellKeys.contains(cellIdentityConverter.createCellKey(cell))) {
                mainCellsChanged++;
            }
        }
        if (mainCellsChanged > 0) {
            Timber.d("parse(): Distance condition not achieved but %s main cells changed", mainCellsChanged);
        } else {
            Timber.d("parse(): Distance condition not achieved");
            return ParseResult.DistanceNotAchieved;
        }
    }
    // check if location has been obtained recently
    if (!locationValidator.isUpToDate(timestamp, System.currentTimeMillis())) {
        Timber.d("parse(): Location too old");
        return ParseResult.LocationTooOld;
    }
    Timber.d("parse(): Destination and time conditions achieved");
    // update measurement with location
    updateMeasurementWithLocation(measurement, location);
    // loop through all cells
    for (CellInfo cellInfo : cells) {
        if (!cellValidator.isValid(cellInfo)) {
            // don't try to create neighboring cells because this may be even more unreliable than on older API
            Timber.d("parse(): Cell invalid: %s", cellInfo);
            continue;
        }
        if (!collectNeighboringCells && !cellInfo.isRegistered()) {
            // skip neighboring cells
            Timber.d("parse(): Neighboring cell skipped: %s", cellInfo);
            continue;
        }
        // update with cell data
        Cell tempCell = cellIdentityConverter.convert(cellInfo);
        // update measurement with signal strength
        cellSignalConverter.update(tempCell, cellInfo);
        // write to database
        Timber.d("parse(): Cell valid: %s", cellInfo);
        measurement.addCell(tempCell);
    }
    // none of cells are valid
    if (measurement.getCells().isEmpty()) {
        Timber.d("parse(): All cells invalid or skipped");
        return ParseResult.NoNetworkSignal;
    }
    // write to database
    Timber.d("parse(): Measurement: %s", measurement);
    boolean inserted = MeasurementsDatabase.getInstance(MyApplication.getApplication()).insertMeasurement(measurement);
    if (inserted) {
        lastSavedLocation = location;
        lastSavedMeasurement = measurement;
        Timber.d("parse(): Measurement saved");
        // broadcast information to main activity
        Statistics stats = MeasurementsDatabase.getInstance(MyApplication.getApplication()).getMeasurementsStatistics();
        EventBus.getDefault().post(new MeasurementSavedEvent(measurement, stats));
        EventBus.getDefault().post(new MeasurementsCollectedEvent(measurement));
        Timber.d("parse(): Notification updated and measurement broadcasted");
        return ParseResult.Saved;
    } else {
        return ParseResult.SaveFailed;
    }
}
 
Example 5
Source File: RadioInfo.java    From CellularSignal with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCellInfoChanged(List<CellInfo> cellInfoList) {
    super.onCellInfoChanged(cellInfoList);


    if (cellInfoList == null) {
        //Log.e(Tag,"onCellInfoChanged is null");
        return;
    }

    //Log.e(Tag,"onCellInfoChanged size "+cellInfoList.size());

    for (CellInfo cellInfo : cellInfoList) {

        if (!cellInfo.isRegistered())
            continue;

        if (cellInfo instanceof CellInfoLte) {

            CellInfoLte lteinfo = (CellInfoLte) cellInfo;

            lte_MCC = lteinfo.getCellIdentity().getMcc();
            lte_MNC = lteinfo.getCellIdentity().getMnc();
            lte_CI = lteinfo.getCellIdentity().getCi();
            lte_PCI = lteinfo.getCellIdentity().getPci();
            lte_TAC = lteinfo.getCellIdentity().getTac();
            //Log.e(Tag,lteinfo.toString());

        } else if (cellInfo instanceof CellInfoCdma) {

            CellInfoCdma cdmainfo = (CellInfoCdma) cellInfo;

            cdma_SID = cdmainfo.getCellIdentity().getSystemId();
            cdma_NID = cdmainfo.getCellIdentity().getNetworkId();
            cdma_BSID = cdmainfo.getCellIdentity().getBasestationId();

            //Log.e(Tag,cdmainfo.toString());
        } else if (cellInfo instanceof CellInfoGsm) {
            CellInfoGsm gsmInfo = (CellInfoGsm) cellInfo;

            gsm_MCC = gsmInfo.getCellIdentity().getMcc();
            gsm_MNC = gsmInfo.getCellIdentity().getMnc();
            gsm_CID = gsmInfo.getCellIdentity().getCid();
            gsm_LAC = gsmInfo.getCellIdentity().getLac();

        } else if (cellInfo instanceof CellInfoWcdma) {
            CellInfoWcdma wcdmaInfo = (CellInfoWcdma) cellInfo;

            wcdma_MCC = wcdmaInfo.getCellIdentity().getMcc();
            wcdma_MNC = wcdmaInfo.getCellIdentity().getMnc();
            wcdma_CID = wcdmaInfo.getCellIdentity().getCid();
            wcdma_LAC = wcdmaInfo.getCellIdentity().getLac();
            wcdma_PSC = wcdmaInfo.getCellIdentity().getPsc();
        }
    }

    ((MainActivity)mcontext).mSectionsPagerAdapter.notifyDataSetChanged();
}
 
Example 6
Source File: RadioInfo.java    From CellularSignal with GNU General Public License v3.0 4 votes vote down vote up
private void getCellIdentity() {
    List<CellInfo> cellInfoList = mTM.getAllCellInfo();

    if (cellInfoList == null) {
        //Log.e(Tag,"getAllCellInfo is null");
        return;
    }
    //Log.e(Tag,"getAllCellInfo size "+cellInfoList.size());

    for (CellInfo cellInfo : cellInfoList) {

        if (!cellInfo.isRegistered())
            continue;

        if (cellInfo instanceof CellInfoLte) {

            CellInfoLte lteinfo = (CellInfoLte) cellInfo;

            lte_MCC = lteinfo.getCellIdentity().getMcc();
            lte_MNC = lteinfo.getCellIdentity().getMnc();
            lte_CI = lteinfo.getCellIdentity().getCi();
            lte_PCI = lteinfo.getCellIdentity().getPci();
            lte_TAC = lteinfo.getCellIdentity().getTac();
            //Log.e(Tag,lteinfo.toString());

        } else if (cellInfo instanceof CellInfoCdma) {

            CellInfoCdma cdmainfo = (CellInfoCdma) cellInfo;

            cdma_SID = cdmainfo.getCellIdentity().getSystemId();
            cdma_NID = cdmainfo.getCellIdentity().getNetworkId();
            cdma_BSID = cdmainfo.getCellIdentity().getBasestationId();

            //Log.e(Tag,cdmainfo.toString());
        } else if (cellInfo instanceof CellInfoGsm) {
            CellInfoGsm gsmInfo = (CellInfoGsm) cellInfo;

            gsm_MCC = gsmInfo.getCellIdentity().getMcc();
            gsm_MNC = gsmInfo.getCellIdentity().getMnc();
            gsm_CID = gsmInfo.getCellIdentity().getCid();
            gsm_LAC = gsmInfo.getCellIdentity().getLac();

        } else if (cellInfo instanceof CellInfoWcdma) {
            CellInfoWcdma wcdmaInfo = (CellInfoWcdma) cellInfo;

            wcdma_MCC = wcdmaInfo.getCellIdentity().getMcc();
            wcdma_MNC = wcdmaInfo.getCellIdentity().getMnc();
            wcdma_CID = wcdmaInfo.getCellIdentity().getCid();
            wcdma_LAC = wcdmaInfo.getCellIdentity().getLac();
            wcdma_PSC = wcdmaInfo.getCellIdentity().getPsc();
        }
    }
}
 
Example 7
Source File: CellBackendHelper.java    From android_external_UnifiedNlpApi with Apache License 2.0 4 votes vote down vote up
/**
 * This will fix empty MNC since Android 9 with 0-prefixed MNCs.
 * Issue: https://issuetracker.google.com/issues/113560852
 */
private void fixEmptyMnc(List<CellInfo> cellInfo) {
    if (Build.VERSION.SDK_INT < 28 || cellInfo == null) {
        return;
    }

    String networkOperator = telephonyManager.getNetworkOperator();

    if (networkOperator.length() < 5 || networkOperator.charAt(3) != '0') {
        return;
    }

    String mnc = networkOperator.substring(3);

    for (CellInfo info : cellInfo) {
        if (!info.isRegistered()) {
            continue;
        }

        Object identity = null;

        if (info instanceof CellInfoGsm) {
            identity = ((CellInfoGsm) info).getCellIdentity();
        } else if (info instanceof CellInfoWcdma) {
            identity = ((CellInfoWcdma) info).getCellIdentity();
        } else if (info instanceof CellInfoLte) {
            identity = ((CellInfoLte) info).getCellIdentity();
        }

        if (identity == null) {
            continue;
        }

        try {
            Field mncField = identity.getClass().getSuperclass().getDeclaredField("mMncStr");
            mncField.setAccessible(true);
            if (mncField.get(identity) == null) {
                mncField.set(identity, mnc);
            }
        } catch (NoSuchFieldException | IllegalAccessException ignored) {

        }
    }
}
 
Example 8
Source File: PhoneStateScanner.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
private void getAllCellInfo(List<CellInfo> cellInfo) {
    // only for registered cells is returned identify
    // SlimKat in Galaxy Nexus - returns null :-/
    // Honor 7 - returns empty list (not null), Dual SIM?

    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "cellInfo="+cellInfo);

    if (cellInfo!=null) {

        if (Permissions.checkLocation(context.getApplicationContext())) {

            //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "---- start ----------------------------");

            boolean anyRegistered = false;

            for (CellInfo _cellInfo : cellInfo) {
                //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "registered="+_cellInfo.isRegistered());

                boolean isRegistered = false;

                if (_cellInfo instanceof CellInfoGsm) {
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "gsm info="+_cellInfo);
                    CellIdentityGsm identityGsm = ((CellInfoGsm) _cellInfo).getCellIdentity();
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "gsm cid="+identityGsm.getCid());
                    if (isValidCellId(identityGsm.getCid())) {
                        if (_cellInfo.isRegistered()) {
                            registeredCell = identityGsm.getCid();
                            //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "gsm registeredCell="+registeredCell);
                            lastConnectedTime = Calendar.getInstance().getTimeInMillis();
                            isRegistered = true;
                        }
                    }
                } else if (_cellInfo instanceof CellInfoLte) {
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "lte info="+_cellInfo);
                    CellIdentityLte identityLte = ((CellInfoLte) _cellInfo).getCellIdentity();
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "lte cid="+identityLte.getCi());
                    if (isValidCellId(identityLte.getCi())) {
                        if (_cellInfo.isRegistered()) {
                            registeredCell = identityLte.getCi();
                            //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "lte registeredCell="+registeredCell);
                            lastConnectedTime = Calendar.getInstance().getTimeInMillis();
                            isRegistered = true;
                        }
                    }
                } else if (_cellInfo instanceof CellInfoWcdma) {
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "wcdma info="+_cellInfo);
                    CellIdentityWcdma identityWcdma = ((CellInfoWcdma) _cellInfo).getCellIdentity();
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "wcdma cid=" + identityWcdma.getCid());
                    if (isValidCellId(identityWcdma.getCid())) {
                        if (_cellInfo.isRegistered()) {
                            registeredCell = identityWcdma.getCid();
                            //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "wcdma registeredCell="+registeredCell);
                            lastConnectedTime = Calendar.getInstance().getTimeInMillis();
                            isRegistered = true;
                        }
                    }
                } else if (_cellInfo instanceof CellInfoCdma) {
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "cdma info="+_cellInfo);
                    CellIdentityCdma identityCdma = ((CellInfoCdma) _cellInfo).getCellIdentity();
                    //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "wcdma cid="+identityCdma.getBasestationId());
                    if (isValidCellId(identityCdma.getBasestationId())) {
                        if (_cellInfo.isRegistered()) {
                            registeredCell = identityCdma.getBasestationId();
                            //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "cdma registeredCell="+registeredCell);
                            lastConnectedTime = Calendar.getInstance().getTimeInMillis();
                            isRegistered = true;
                        }
                    }
                }
                /*else {
                    PPApplication.logE("PhoneStateScanner.getAllCellInfo", "unknown info="+_cellInfo);
                }*/

                if (isRegistered) {
                    anyRegistered = true;
                    /*if (PPApplication.logEnabled()) {
                        PPApplication.logE("PhoneStateScanner.getAllCellInfo", "registeredCell=" + registeredCell);
                        PPApplication.logE("PhoneStateScanner.getAllCellInfo", "is registered, save it");
                    }*/
                    DatabaseHandler db = DatabaseHandler.getInstance(context);
                    db.updateMobileCellLastConnectedTime(registeredCell, lastConnectedTime);
                    doAutoRegistration(registeredCell);
                }
            }

            if (!anyRegistered) {
                //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "empty cellInfo");
                registeredCell = Integer.MAX_VALUE;
                doAutoRegistration(registeredCell);
            }

            //PPApplication.logE("PhoneStateScanner.getAllCellInfo", "---- end ----------------------------");
        }

    }
    //else
    //    PPApplication.logE("PhoneStateScanner.getAllCellInfo", "cell info is null");
}
 
Example 9
Source File: CellBackendHelper.java    From android_external_UnifiedNlpApi with Apache License 2.0 4 votes vote down vote up
/**
 * This will fix empty MNC since Android 9 with 0-prefixed MNCs.
 * Issue: https://issuetracker.google.com/issues/113560852
 */
private void fixEmptyMnc(List<CellInfo> cellInfo) {
    if (Build.VERSION.SDK_INT < 28 || cellInfo == null) {
        return;
    }

    String networkOperator = telephonyManager.getNetworkOperator();

    if (networkOperator.length() < 5 || networkOperator.charAt(3) != '0') {
        return;
    }

    String mnc = networkOperator.substring(3);

    for (CellInfo info : cellInfo) {
        if (!info.isRegistered()) {
            continue;
        }

        Object identity = null;

        if (info instanceof CellInfoGsm) {
            identity = ((CellInfoGsm) info).getCellIdentity();
        } else if (info instanceof CellInfoWcdma) {
            identity = ((CellInfoWcdma) info).getCellIdentity();
        } else if (info instanceof CellInfoLte) {
            identity = ((CellInfoLte) info).getCellIdentity();
        }

        if (identity == null) {
            continue;
        }

        try {
            Field mncField = identity.getClass().getSuperclass().getDeclaredField("mMncStr");
            mncField.setAccessible(true);
            if (mncField.get(identity) == null) {
                mncField.set(identity, mnc);
            }
        } catch (NoSuchFieldException | IllegalAccessException ignored) {

        }
    }
}