use of android.telephony.CellInfo in project robolectric by robolectric.
the class ShadowTelephonyManagerTest method shouldGiveCellInfoUpdate.
@Test
@Config(minSdk = Q)
public void shouldGiveCellInfoUpdate() throws Exception {
List<CellInfo> callbackCellInfo = Collections.singletonList(mock(CellInfo.class));
shadowOf(telephonyManager).setCallbackCellInfos(callbackCellInfo);
assertNotEquals(callbackCellInfo, telephonyManager.getAllCellInfo());
CountDownLatch callbackLatch = new CountDownLatch(1);
shadowOf(telephonyManager).requestCellInfoUpdate(new Executor() {
@Override
public void execute(Runnable r) {
r.run();
}
}, new CellInfoCallback() {
@Override
public void onCellInfo(List<CellInfo> list) {
assertEquals(callbackCellInfo, list);
callbackLatch.countDown();
}
});
assertTrue(callbackLatch.await(5000, TimeUnit.MILLISECONDS));
}
use of android.telephony.CellInfo in project network-monitor by caarmen.
the class NetMonSignalStrength method getLteRsrq.
@TargetApi(17)
public int getLteRsrq(SignalStrength signalStrength) {
// First hacky way: reflection on the signalStrength object
try {
// Yes, I know it's a hack, thanks.
@SuppressLint("PrivateApi") Method method = SignalStrength.class.getDeclaredMethod("getLteRsrq");
int rsrq = (Integer) method.invoke(signalStrength);
Log.v(TAG, "getLteRsrq: found " + rsrq + " using SignalStrength.getLteRsrq()");
if (rsrq < 0)
return rsrq;
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Log.e(TAG, "getLteRsrq Could not get rsrq", e);
}
// Second hacky way: reflection on the CellInfo object.
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
List<CellInfo> cellInfos = mTelephonyManager.getAllCellInfo();
if (cellInfos != null) {
for (CellInfo cellInfo : cellInfos) {
if (cellInfo.isRegistered()) {
if (cellInfo instanceof CellInfoLte) {
CellSignalStrengthLte signalStrengthLte = ((CellInfoLte) cellInfo).getCellSignalStrength();
try {
Field fieldRsrq = CellSignalStrength.class.getDeclaredField("mRsrq");
fieldRsrq.setAccessible(true);
int rsrq = (Integer) fieldRsrq.get(signalStrengthLte);
Log.v(TAG, "getLteRsrq: found " + rsrq + " using CellInfoLte.mRsrq");
if (rsrq < 0)
return rsrq;
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
Log.e(TAG, "getRsrq Could not get Rsrq", e);
}
}
}
}
}
}
return SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
}
use of android.telephony.CellInfo in project network-monitor by caarmen.
the class CellLocationDataSource method fillCellLocationV17.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void fillCellLocationV17(ContentValues values) {
if (PermissionUtil.hasLocationPermission(mContext)) {
@SuppressLint("MissingPermission") List<CellInfo> cellInfos = mTelephonyManager.getAllCellInfo();
if (cellInfos == null || cellInfos.isEmpty()) {
fillCellLocation(values);
} else {
StreamSupport.stream(cellInfos).filter(CellInfo::isRegistered).forEach(cellInfo -> {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm gsmCellInfo = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentityGsm = gsmCellInfo.getCellIdentity();
setGsmCellInfo(values, cellIdentityGsm.getCid(), cellIdentityGsm.getLac());
} else if (cellInfo instanceof CellInfoCdma) {
CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
CellIdentityCdma cellIdentityCdma = cellInfoCdma.getCellIdentity();
setCdmaCellInfo(values, cellIdentityCdma.getBasestationId(), cellIdentityCdma.getLatitude(), cellIdentityCdma.getLongitude(), cellIdentityCdma.getNetworkId(), cellIdentityCdma.getSystemId());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && cellInfo instanceof CellInfoWcdma) {
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();
setGsmCellInfo(values, cellIdentityWcdma.getCid(), cellIdentityWcdma.getLac());
values.put(NetMonColumns.GSM_CELL_PSC, cellIdentityWcdma.getPsc());
} else if (cellInfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();
values.put(NetMonColumns.LTE_CELL_CI, cellIdentityLte.getCi());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
values.put(NetMonColumns.LTE_CELL_EARFCN, cellIdentityLte.getEarfcn());
}
values.put(NetMonColumns.LTE_CELL_TAC, cellIdentityLte.getTac());
values.put(NetMonColumns.LTE_CELL_PCI, cellIdentityLte.getPci());
}
});
}
}
}
use of android.telephony.CellInfo in project Android-IMSI-Catcher-Detector by CellularPrivacy.
the class DeviceApi18 method loadCellInfo.
public static void loadCellInfo(TelephonyManager tm, Device pDevice) {
int lCurrentApiVersion = Build.VERSION.SDK_INT;
try {
if (pDevice.cell == null) {
pDevice.cell = new Cell();
}
List<CellInfo> cellInfoList = tm.getAllCellInfo();
if (cellInfoList != null) {
for (final CellInfo info : cellInfoList) {
// Network Type
pDevice.cell.setNetType(tm.getNetworkType());
if (info instanceof CellInfoGsm) {
final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
final CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
// Signal Strength
// [dBm]
pDevice.cell.setDbm(gsm.getDbm());
// Cell Identity
pDevice.cell.setCellId(identityGsm.getCid());
pDevice.cell.setMobileCountryCode(identityGsm.getMcc());
pDevice.cell.setMobileNetworkCode(identityGsm.getMnc());
pDevice.cell.setLocationAreaCode(identityGsm.getLac());
} else if (info instanceof CellInfoCdma) {
final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
final CellIdentityCdma identityCdma = ((CellInfoCdma) info).getCellIdentity();
// Signal Strength
pDevice.cell.setDbm(cdma.getDbm());
// Cell Identity
pDevice.cell.setCellId(identityCdma.getBasestationId());
pDevice.cell.setMobileNetworkCode(identityCdma.getSystemId());
pDevice.cell.setLocationAreaCode(identityCdma.getNetworkId());
pDevice.cell.setSid(identityCdma.getSystemId());
} else if (info instanceof CellInfoLte) {
final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
final CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
// Signal Strength
pDevice.cell.setDbm(lte.getDbm());
pDevice.cell.setTimingAdvance(lte.getTimingAdvance());
// Cell Identity
pDevice.cell.setMobileCountryCode(identityLte.getMcc());
pDevice.cell.setMobileNetworkCode(identityLte.getMnc());
pDevice.cell.setCellId(identityLte.getCi());
} else if (lCurrentApiVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2 && info instanceof CellInfoWcdma) {
final CellSignalStrengthWcdma wcdma = ((CellInfoWcdma) info).getCellSignalStrength();
final CellIdentityWcdma identityWcdma = ((CellInfoWcdma) info).getCellIdentity();
// Signal Strength
pDevice.cell.setDbm(wcdma.getDbm());
// Cell Identity
pDevice.cell.setLocationAreaCode(identityWcdma.getLac());
pDevice.cell.setMobileCountryCode(identityWcdma.getMcc());
pDevice.cell.setMobileNetworkCode(identityWcdma.getMnc());
pDevice.cell.setCellId(identityWcdma.getCid());
pDevice.cell.setPrimaryScramblingCode(identityWcdma.getPsc());
} else {
log.info("Unknown type of cell signal!" + "\n ClassName: " + info.getClass().getSimpleName() + "\n ToString: " + info.toString());
}
if (pDevice.cell.isValid()) {
break;
}
}
}
} catch (NullPointerException npe) {
log.error("loadCellInfo: Unable to obtain cell signal information: ", npe);
}
}
use of android.telephony.CellInfo in project Android-IMSI-Catcher-Detector by CellularPrivacy.
the class DeviceApi17 method loadCellInfo.
public static void loadCellInfo(TelephonyManager tm, Device pDevice) {
int lCurrentApiVersion = Build.VERSION.SDK_INT;
try {
if (pDevice.cell == null) {
pDevice.cell = new Cell();
}
List<CellInfo> cellInfoList = tm.getAllCellInfo();
if (cellInfoList != null) {
for (final CellInfo info : cellInfoList) {
// Network Type
pDevice.cell.setNetType(tm.getNetworkType());
if (info instanceof CellInfoGsm) {
final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
final CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
// Signal Strength
// [dBm]
pDevice.cell.setDbm(gsm.getDbm());
// Cell Identity
pDevice.cell.setCellId(identityGsm.getCid());
pDevice.cell.setMobileCountryCode(identityGsm.getMcc());
pDevice.cell.setMobileNetworkCode(identityGsm.getMnc());
pDevice.cell.setLocationAreaCode(identityGsm.getLac());
} else if (info instanceof CellInfoCdma) {
final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
final CellIdentityCdma identityCdma = ((CellInfoCdma) info).getCellIdentity();
// Signal Strength
pDevice.cell.setDbm(cdma.getDbm());
// Cell Identity
pDevice.cell.setCellId(identityCdma.getBasestationId());
pDevice.cell.setMobileNetworkCode(identityCdma.getSystemId());
pDevice.cell.setLocationAreaCode(identityCdma.getNetworkId());
pDevice.cell.setSid(identityCdma.getSystemId());
} else if (info instanceof CellInfoLte) {
final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
final CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
// Signal Strength
pDevice.cell.setDbm(lte.getDbm());
pDevice.cell.setTimingAdvance(lte.getTimingAdvance());
// Cell Identity
pDevice.cell.setMobileCountryCode(identityLte.getMcc());
pDevice.cell.setMobileNetworkCode(identityLte.getMnc());
pDevice.cell.setCellId(identityLte.getCi());
} else if (lCurrentApiVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2 && info instanceof CellInfoWcdma) {
final CellSignalStrengthWcdma wcdma = ((CellInfoWcdma) info).getCellSignalStrength();
final CellIdentityWcdma identityWcdma = ((CellInfoWcdma) info).getCellIdentity();
// Signal Strength
pDevice.cell.setDbm(wcdma.getDbm());
// Cell Identity
pDevice.cell.setLocationAreaCode(identityWcdma.getLac());
pDevice.cell.setMobileCountryCode(identityWcdma.getMcc());
pDevice.cell.setMobileNetworkCode(identityWcdma.getMnc());
pDevice.cell.setCellId(identityWcdma.getCid());
pDevice.cell.setPrimaryScramblingCode(identityWcdma.getPsc());
} else {
log.info("Unknown type of cell signal! " + "ClassName: " + info.getClass().getSimpleName() + " ToString: " + info.toString());
}
if (pDevice.cell.isValid()) {
break;
}
}
}
} catch (NullPointerException npe) {
log.error("loadCellInfo: Unable to obtain cell signal information: ", npe);
}
}
Aggregations