下面列出了怎么用android.telephony.NeighboringCellInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Description: TODO: add more info
*
* Issues:
*
* [ ] Getting and comparing signal strengths between different RATs can be very
* tricky, since they all return different ranges of values. AOS doesn't
* specify very clearly what exactly is returned, even though people have
* a good idea, by trial and error.
*
* See note in : SignalStrengthTracker.java
*
* Notes:
*
*
*
*/
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// Update Signal Strength
if (signalStrength.isGsm()) {
int dbm;
if (signalStrength.getGsmSignalStrength() <= 2 ||
signalStrength.getGsmSignalStrength() == NeighboringCellInfo.UNKNOWN_RSSI) {
// Unknown signal strength, get it another way
String[] bits = signalStrength.toString().split(" ");
dbm = Integer.parseInt(bits[9]);
} else {
dbm = signalStrength.getGsmSignalStrength();
}
mDevice.setSignalDbm(dbm);
} else {
int evdoDbm = signalStrength.getEvdoDbm();
int cdmaDbm = signalStrength.getCdmaDbm();
// Use lowest signal to be conservative
mDevice.setSignalDbm((cdmaDbm < evdoDbm) ? cdmaDbm : evdoDbm);
}
// Send it to signal tracker
signalStrengthTracker.registerSignalStrength(mDevice.mCell.getCID(), mDevice.getSignalDBm());
//signalStrengthTracker.isMysterious(mDevice.mCell.getCID(), mDevice.getSignalDBm());
}
public final int[] d() {
if (this.c == 0) {
return new int[0];
}
List<NeighboringCellInfo> neighboringCellInfo = this.q.getNeighboringCellInfo();
if (neighboringCellInfo == null || neighboringCellInfo.size() == 0) {
return new int[]{this.c};
}
Object obj = new int[((neighboringCellInfo.size() * 2) + 2)];
obj[0] = this.c;
obj[1] = this.a;
int i = 2;
for (NeighboringCellInfo neighboringCellInfo2 : neighboringCellInfo) {
int cid = neighboringCellInfo2.getCid();
if (cid > 0 && cid != SupportMenu.USER_MASK) {
int i2 = i + 1;
obj[i] = cid;
i = i2 + 1;
obj[i2] = neighboringCellInfo2.getRssi();
}
}
Object obj2 = new int[i];
System.arraycopy(obj, 0, obj2, 0, i);
return obj2;
}
private void removeDuplicatedNeighbors(List<NeighboringCellInfo> neighboringCells, Cell mainCell) {
List<NeighboringCellInfo> cellsToRemove = new ArrayList<NeighboringCellInfo>();
Set<String> uniqueCellKeys = new HashSet<String>();
uniqueCellKeys.add(createCellKey(mainCell));
for (NeighboringCellInfo cell : neighboringCells) {
String key = createCellKey(cell, mainCell);
if (uniqueCellKeys.contains(key)) {
Timber.d("removeDuplicatedNeighbors(): Remove duplicated cell: %s", key);
cellsToRemove.add(cell);
} else {
uniqueCellKeys.add(key);
}
}
neighboringCells.removeAll(cellsToRemove);
}
public synchronized void setLastCellLocation(CellLocation cellLocation, NetworkGroup networkType,
String operatorCode, String operatorName, List<NeighboringCellInfo> neighboringCells) {
Timber.d("setLastCellLocation(): Cell location updated: %s, network type: %s, operator code: %s, operator name: %s", cellLocation, networkType, operatorCode, operatorName);
// check if any changes
boolean cellChanged = (!isCellLocationEqual(lastCellLocation, cellLocation)
|| lastNetworkType != networkType
|| !lastOperatorCode.equals(operatorCode)
|| !lastOperatorName.equals(operatorName));
// update last cell
this.lastCellLocation = cellLocation;
this.lastNetworkType = networkType;
this.lastOperatorCode = operatorCode;
this.lastOperatorName = operatorName;
this.neighboringCells = neighboringCells;
if (this.neighboringCells == null) {
this.neighboringCells = EMPTY_NEIGHBORING_CELL_LIST;
}
if (cellChanged) {
notifyIfReadyToProcess();
}
}
public Cell convert(NeighboringCellInfo neighboringCell, int mcc, int mnc, int mainCellLac, long mainCellCid) {
Cell cell = new Cell();
cell.setNeighboring(true);
// no type checking because only GSM and UMTS cells can have neighboring cells
NetworkGroup networkType = NetworkTypeUtils.getNetworkGroup(neighboringCell.getNetworkType());
if (networkType == NetworkGroup.Gsm) {
cell.setGsmCellLocation(mcc, mnc, neighboringCell.getLac(), neighboringCell.getCid(), NetworkGroup.Gsm);
} else {
int psc = neighboringCell.getPsc();
if (psc == NeighboringCellInfo.UNKNOWN_CID) {
psc = Cell.UNKNOWN_CID;
}
cell.setGsmCellLocation(mcc, mnc, mainCellLac, mainCellCid, psc, NetworkGroup.Wcdma);
}
return cell;
}
/**
* Add neighbouring cells as generated by the getNeighboringCells API.
* @param neighbours The list of neighbouring cells.
*/
public void addNeighbours(List<NeighboringCellInfo> neighbours) {
if (neighbours == null || neighbours.isEmpty()) return;
for (NeighboringCellInfo neighbour : neighbours) {
List<CellInfo> cellInfos = db.query(neighbour.getCid(), neighbour.getLac());
if (cellInfos != null && !cellInfos.isEmpty()) {
for (CellInfo cellInfo : cellInfos) {
pushRecentCells(cellInfo);
}
} else {
CellInfo ci = new CellInfo();
ci.lng = 0d;
ci.lat = 0d;
ci.CID = neighbour.getCid();
ci.LAC = neighbour.getLac();
ci.MCC = -1;
ci.MNC = -1;
pushUnusedCells(ci);
}
}
}
/**
* Handle a modem event by trying to pull all information. The parameter inc defines if the
* measurement counter should be increased on success.
* @param inc True if the measurement counter should be increased.
*/
private void handle(boolean inc) {
if (telephonyManager == null) return;
final List<android.telephony.CellInfo> cellInfos = telephonyManager.getAllCellInfo();
final List<NeighboringCellInfo> neighbours = telephonyManager.getNeighboringCellInfo();
final CellLocation cellLocation = telephonyManager.getCellLocation();
if (cellInfos == null || cellInfos.isEmpty()) {
if (neighbours == null || neighbours.isEmpty()) {
if (cellLocation == null || !(cellLocation instanceof GsmCellLocation)) return;
}
}
if (inc) measurement.getAndIncrement();
add(cellLocation);
addNeighbours(neighbours);
addCells(cellInfos);
synchronized (recentCells) {
cleanup();
}
}
/**
* Returns the information about cell towers in range. Returns null if the information is
* not available
*
* TODO(wenjiezeng): As folklore has it and Wenjie has confirmed, we cannot get cell info from
* Samsung phones.
*/
public String getCellInfo(boolean cidOnly) {
if(!(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED)){
return null;
}
initNetwork();
List<NeighboringCellInfo> infos = telephonyManager.getNeighboringCellInfo();
StringBuffer buf = new StringBuffer();
String tempResult = "";
if (infos.size() > 0) {
for (NeighboringCellInfo info : infos) {
tempResult = cidOnly ? info.getCid() + ";" : info.getLac() + ","
+ info.getCid() + "," + info.getRssi() + ";";
buf.append(tempResult);
}
// Removes the trailing semicolon
buf.deleteCharAt(buf.length() - 1);
return buf.toString();
} else {
return null;
}
}
/**
* Requeries neighboring cells
*/
protected void updateNeighboringCellInfo() {
try {
/*
* NeighboringCellInfo is not supported on some devices and will return no data. It lists
* only GSM and successors' cells, but not CDMA cells.
*/
List<NeighboringCellInfo> neighboringCells = mainActivity.telephonyManager.getNeighboringCellInfo();
String networkOperator = mainActivity.telephonyManager.getNetworkOperator();
mCellsGsm.updateAll(networkOperator, neighboringCells);
mCellsLte.updateAll(networkOperator, neighboringCells);
} catch (SecurityException e) {
// Permission not granted, can't retrieve cell data
Log.w(TAG, "Permission not granted, cannot get neighboring cell info");
}
}
private void handlePhoneStateChange() {
List<NeighboringCellInfo> neighboringCellInfo = tm.getNeighboringCellInfo();
if (neighboringCellInfo == null || neighboringCellInfo.size() == 0) {
return;
}
Log.i(TAG, mTAG + "NeighbouringCellInfo empty - event based polling succeeded!");
tm.listen(phoneStatelistener, PhoneStateListener.LISTEN_NONE);
if (neighboringCellInfo == null) {
neighboringCellInfo = new ArrayList<>();
}
neighboringCellBlockingQueue.addAll(neighboringCellInfo);
}
private void processCellLocation(TelephonyManager telephonyManager, CellLocation cellLocation, List<NeighboringCellInfo> neighboringCells) {
// get network type
int networkTypeInt = telephonyManager.getNetworkType();
NetworkGroup networkType = NetworkTypeUtils.getNetworkGroup(networkTypeInt);
// get network operator (may be unreliable for CDMA)
String networkOperatorCode = telephonyManager.getNetworkOperator();
String networkOperatorName = telephonyManager.getNetworkOperatorName();
Timber.d("processCellLocation(): Operator code = '%s', name = '%s'", networkOperatorCode, networkOperatorName);
Timber.d("processCellLocation(): Reported %s neighboring cells", (neighboringCells != null ? neighboringCells.size() : null));
measurementUpdater.setLastCellLocation(cellLocation, networkType, networkOperatorCode, networkOperatorName, neighboringCells);
}
public boolean isValid(NeighboringCellInfo neighboringCell, int mcc, int mnc) {
NetworkGroup netType = NetworkTypeUtils.getNetworkGroup(neighboringCell.getNetworkType());
if (netType == NetworkGroup.Gsm) {
return getGsmValidator().isValid(neighboringCell.getCid(), neighboringCell.getLac(), mnc, mcc, NeighboringCellInfo.UNKNOWN_CID);
} else if (netType == NetworkGroup.Wcdma) {
// NOTE: Maybe some phones return full set for WCDMA and then it should be valid
return getGsmValidator().isValid(neighboringCell.getCid(), neighboringCell.getLac(), mnc, mcc, neighboringCell.getPsc());
}
return false;
}
public Cell convert(CellLocation cellLocation, int mcc, int mnc, NetworkGroup networkType) {
Cell cell = new Cell();
if (cellLocation instanceof GsmCellLocation) {
GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;
if (gsmCellLocation.getCid() <= 65535 && gsmCellLocation.getPsc() == NeighboringCellInfo.UNKNOWN_CID) {
cell.setGsmCellLocation(mcc, mnc, gsmCellLocation.getLac(), gsmCellLocation.getCid(), NetworkGroup.Gsm);
} else {
// fix invalid network types (unfortunately not possible to distinguish between UMTS and LTE)
if (networkType == NetworkGroup.Gsm || networkType == NetworkGroup.Cdma)
networkType = NetworkGroup.Unknown;
int psc = gsmCellLocation.getPsc();
if (psc == NeighboringCellInfo.UNKNOWN_CID || psc == Cell.UNKNOWN_CID) {
psc = Cell.UNKNOWN_CID;
} else if (psc >= 504) {
// only UMTS networks support larger PSC
networkType = NetworkGroup.Wcdma;
}
cell.setGsmCellLocation(mcc, mnc, gsmCellLocation.getLac(), gsmCellLocation.getCid(), psc, networkType);
}
} else if (cellLocation instanceof CdmaCellLocation) {
CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation;
cell.setCdmaCellLocation(cdmaCellLocation.getSystemId(), cdmaCellLocation.getNetworkId(), cdmaCellLocation.getBaseStationId());
} else {
throw new UnsupportedOperationException("Cell location type not supported `" + cellLocation.getClass().getName() + "`");
}
return cell;
}
private void updateGsm(Cell cell, int asu) {
Timber.d("update(): Updating GSM signal strength = %s", asu);
if (asu == NeighboringCellInfo.UNKNOWN_RSSI)
asu = Cell.UNKNOWN_SIGNAL;
// NOTE: for GSM asu is always positive but RSSI negative
if (asu >= 0)
cell.setGsmLocationSignal(asu, UnitConverter.convertGsmAsuToDbm(asu));
else
cell.setGsmLocationSignal(UnitConverter.convertGsmDbmToAsu(asu), asu);
}
private void updateLte(Cell cell, int dbm) {
Timber.d("update(): Updating LTE signal strength = %s", dbm);
if (dbm == NeighboringCellInfo.UNKNOWN_RSSI)
dbm = Cell.UNKNOWN_SIGNAL;
// NOTE: for LTE asu is always positive but RSRP negative
if (dbm >= 0)
cell.setGsmLocationSignal(dbm, UnitConverter.convertLteAsuToDbm(dbm));
else
cell.setGsmLocationSignal(UnitConverter.convertLteDbmToAsu(dbm), dbm);
}
public static int convertGsmDbmToAsu(int dbm) {
if (dbm == Cell.UNKNOWN_SIGNAL || dbm == NeighboringCellInfo.UNKNOWN_RSSI)
return Cell.UNKNOWN_SIGNAL;
if (dbm <= -113)
return 0;
int asu = (dbm + 113) / 2;
if (asu > 31)
return 31;
return asu;
}
public static int convertLteAsuToDbm(int asu) {
if (asu == Cell.UNKNOWN_SIGNAL || asu == NeighboringCellInfo.UNKNOWN_RSSI)
return Cell.UNKNOWN_SIGNAL;
if (asu <= 0)
return -140;
if (asu >= 97)
return -43;
return asu - 140;
}
public static int convertLteDbmToAsu(int dbm) {
if (dbm == Cell.UNKNOWN_SIGNAL || dbm == NeighboringCellInfo.UNKNOWN_RSSI)
return Cell.UNKNOWN_SIGNAL;
if (dbm <= -140)
return 0;
if (dbm >= -43)
return 97;
return dbm + 140;
}
public LegacyMeasurementProcessingEvent(Location lastLocation, long lastLocationObtainedTime,
CellLocation lastCellLocation, SignalStrength lastSignalStrength, NetworkGroup lastNetworkType,
String lastOperatorCode, String lastOperatorName, List<NeighboringCellInfo> neighboringCells,
int minDistance) {
this.lastLocation = lastLocation;
this.lastLocationObtainedTime = lastLocationObtainedTime;
this.lastCellLocation = lastCellLocation;
this.lastSignalStrength = lastSignalStrength;
this.lastNetworkType = lastNetworkType;
this.neighboringCells = neighboringCells;
this.lastOperatorCode = lastOperatorCode;
this.lastOperatorName = lastOperatorName;
this.minDistance = minDistance;
}
private Cell parseCellInfo(NeighboringCellInfo info) {
try {
if (getCellType(info.getNetworkType()) != Cell.CellType.GSM) return null;
return new Cell(Cell.CellType.GSM, getMcc(), getMnc(), info.getLac(), info.getCid(),
info.getPsc(), info.getRssi());
} catch (Exception ignored) {
}
return null;
}
protected final List m()
{
ArrayList arraylist = new ArrayList();
if (b == null)
{
return arraylist;
}
if (!c())
{
return arraylist;
}
Iterator iterator = b.getNeighboringCellInfo().iterator();
int i1 = 0;
do
{
if (!iterator.hasNext())
{
break;
}
NeighboringCellInfo neighboringcellinfo = (NeighboringCellInfo)iterator.next();
if (i1 > 15)
{
break;
}
if (neighboringcellinfo.getLac() != 0 && neighboringcellinfo.getLac() != 65535 && neighboringcellinfo.getCid() != 65535 && neighboringcellinfo.getCid() != 0xfffffff)
{
arraylist.add(neighboringcellinfo);
i1++;
}
} while (true);
return arraylist;
}
/**
* Perform a (cached) DB query for a given cell tower. Note that MCC and MNC can be null.
* @param mcc
* @param mnc
* @param cid
* @param lac
* @return
*/
public List<CellInfo> query(final Integer mcc, final Integer mnc, final int cid, final int lac) {
if (this.reader == null) return null;
if (cid == NeighboringCellInfo.UNKNOWN_CID || cid == Integer.MAX_VALUE) return null;
if (mcc != null && mcc == Integer.MAX_VALUE) return query(null, mnc, cid, lac);
if (mnc != null && mnc == Integer.MAX_VALUE) return query(mcc, null, cid, lac);
QueryArgs args = new QueryArgs(mcc, mnc, cid, lac);
Boolean negative = queryResultNegativeCache.get(args);
if (negative != null && negative.booleanValue()) return null;
List<CellInfo> cached = queryResultCache.get(args);
if (cached != null) return cached;
List<CellInfo> result = _query(mcc, mnc, cid, lac);
if (result == null) {
queryResultNegativeCache.put(args, true);
return null;
}
result = Collections.unmodifiableList(result);
queryResultCache.put(args, result);
return result;
}
/**
* Internal db query to retrieve all cell tower candidates for a given cid/lac.
* @param mcc
* @param mnc
* @param cid
* @param lac
* @return
*/
private List<CellInfo> _query(Integer mcc, Integer mnc, int cid, int lac) {
if (this.reader == null) return null;
// we need at least CID/LAC
if (cid == NeighboringCellInfo.UNKNOWN_CID) return null;
android.util.Log.d("LNLP/Query", "(" + mcc + "," + mnc + "," + cid + "," + lac + ")");
List<CellInfo> cil = _queryDirect(mcc, mnc, cid, lac);
if (cil == null || cil.size() == 0) {
if (cid > 0xffff) {
_queryDirect(mcc, mnc, cid & 0xffff, lac);
}
}
if (cil != null && cil.size() > 0) {
return cil;
}
if (mcc != null && mnc != null) {
return query(mcc, null, cid, lac);
}
if (mcc != null || mnc != null) {
return query(null,null,cid,lac);
}
return null;
}
private Cell parseCellInfo(NeighboringCellInfo info) {
try {
if (getCellType(info.getNetworkType()) != Cell.CellType.GSM) return null;
return new Cell(Cell.CellType.GSM, getMcc(), getMnc(), info.getLac(), info.getCid(),
info.getPsc(), info.getRssi());
} catch (Exception ignored) {
}
return null;
}
/**
* Description: Updates Neighbouring Cell details
*
* TODO: add more details...
*
*
*/
public List<Cell> updateNeighbouringCells() {
List<Cell> neighboringCells = new ArrayList<>();
List<NeighboringCellInfo> neighboringCellInfo = tm.getNeighboringCellInfo();
if (neighboringCellInfo == null) {
neighboringCellInfo = new ArrayList<>();
}
Boolean nclp = tinydb.getBoolean("nc_list_present"); // Is NC list present? (default is false)
//if nclp = true then check for neighboringCellInfo
//if (neighboringCellInfo != null && neighboringCellInfo.size() == 0 && nclp) {
//if (!neighboringCellInfo.isEmpty() && neighboringCellInfo.size() == 0 && nclp) {
if (neighboringCellInfo.size() == 0 && nclp) {
Log.i(TAG, mTAG + "NeighbouringCellInfo is empty: start polling...");
// Try to poll the neighboring cells for a few seconds
neighboringCellBlockingQueue = new LinkedBlockingQueue<>(100); // Max 100 NC's before blocking (?)
// ToDo: rename... (We don't use API's <18 anymore.)
DeviceApi18.startListening(tm, phoneStatelistener);
// ToDo: Move or remove. This should now be done by getAllCellInfo() in new RadioAPI "collector" module.
for (int i = 0; i < 10 && neighboringCellInfo.size() == 0; i++) {
try {
Log.d(TAG, mTAG + "NeighbouringCellInfo empty: trying " + i);
NeighboringCellInfo info = neighboringCellBlockingQueue.poll(1, TimeUnit.SECONDS);
if (info == null) {
neighboringCellInfo = tm.getNeighboringCellInfo();
if (neighboringCellInfo != null) {
if (neighboringCellInfo.size() > 0) {
// Can we think of a better log message here?
Log.d(TAG, mTAG + "NeighbouringCellInfo found on " + i + " try. (time based)");
break;
} else {
continue;
}
}
}
List<NeighboringCellInfo> cellInfoList =
new ArrayList<>(neighboringCellBlockingQueue.size() + 1);
while (info != null) {
cellInfoList.add(info);
info = neighboringCellBlockingQueue.poll(1, TimeUnit.SECONDS);
}
neighboringCellInfo = cellInfoList;
} catch (InterruptedException e) {
Log.e(TAG, mTAG + "Interrupted BlockingQueue Exception: " + e);
}
}
}
//log.debug(mTAG + ": neighbouringCellInfo size: " + neighboringCellInfo.size());
// Add NC list to DBi_measure:nc_list
for (NeighboringCellInfo neighbourCell : neighboringCellInfo) {
Log.i(TAG, mTAG + "NeighbouringCellInfo -" +
" LAC:" + neighbourCell.getLac() +
" CID:" + neighbourCell.getCid() +
" PSC:" + neighbourCell.getPsc() +
" RSSI:" + neighbourCell.getRssi());
final Cell cell = new Cell(
neighbourCell.getCid(),
neighbourCell.getLac(),
neighbourCell.getRssi(),
neighbourCell.getPsc(),
neighbourCell.getNetworkType(), false);
neighboringCells.add(cell);
}
return neighboringCells;
}
public boolean isValid(int cid, int lac, int mnc, int mcc, int psc) {
boolean valid = (isCidOrCiInRange(cid) && isLacOrTacInRange(lac)
&& isMncInRange(mnc) && isMccInRange(mcc)
&& (psc == NeighboringCellInfo.UNKNOWN_CID || psc == Cell.UNKNOWN_CID || isPscOrPciInRange(psc)));
return valid;
}
private ParseResult parse(Location location, CellLocation cellLocation, SignalStrength signalStrength,
NetworkGroup networkType, String operatorCode, String operatorName, List<NeighboringCellInfo> neighboringCells,
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();
// operator name may be unreliable for CDMA
Timber.d("parse(): Operator name = '%s'", operatorName);
// get operator codes
int mcc = Cell.UNKNOWN_CID;
int mnc = Cell.UNKNOWN_CID;
if (cellLocation instanceof GsmCellLocation) {
int[] mccMncPair = MobileUtils.getMccMncPair(operatorCode);
if (mccMncPair != null) {
mcc = mccMncPair[0];
mnc = mccMncPair[1];
} else {
Timber.d("parseLocation(): Network operator unknown: %s", operatorCode);
}
}
// validate cell
if (!cellLocationValidator.isValid(cellLocation, mcc, mnc)) {
Timber.d("parse(): Cell invalid");
return ParseResult.NoNetworkSignal;
}
// create measurement with basic data
Measurement measurement = new Measurement();
measurement.setMeasuredAt(System.currentTimeMillis());
Cell mainCell = cellLocationConverter.convert(cellLocation, mcc, mnc, networkType);
measurement.addCell(mainCell);
// fix time if incorrect
fixMeasurementTimestamp(measurement, location);
// 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(createCellKey(lastCell));
}
boolean mainCellChanged = !lastMeasurementsCellKeys.contains(createCellKey(mainCell));
if (mainCellChanged) {
Timber.d("parse(): Distance condition not achieved but cell changed");
} 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);
// update measurement with signal strength
if (signalStrength != null) {
cellSignalConverter.update(mainCell, signalStrength);
}
if (collectNeighboringCells) {
// remove duplicated neighboring cells
removeDuplicatedNeighbors(neighboringCells, mainCell);
// process neighboring cells
for (NeighboringCellInfo neighboringCell : neighboringCells) {
// validate cell
if (cellLocationValidator.isValid(neighboringCell, mcc, mnc)) {
// set values
Cell tempCell = cellLocationConverter.convert(neighboringCell, mcc, mnc, mainCell.getLac(), mainCell.getCid());
// update measurement with signal strength
cellSignalConverter.update(tempCell, neighboringCell.getRssi());
// save
measurement.addCell(tempCell);
Timber.d("parse(): Neighboring cell valid: %s", neighboringCell);
} else {
Timber.d("parse(): Neighboring cell invalid: %s", neighboringCell);
}
}
}
// 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;
}
}
private String createCellKey(NeighboringCellInfo neighboringCell, Cell cell) {
return cell.getMcc() + "_" + cell.getMnc() + "_" + neighboringCell.getLac() + "_" + neighboringCell.getCid();
}
public static int convertGsmAsuToDbm(int asu) {
if (asu == Cell.UNKNOWN_SIGNAL || asu == NeighboringCellInfo.UNKNOWN_RSSI)
return Cell.UNKNOWN_SIGNAL;
return 2 * asu - 113;
}
public List<NeighboringCellInfo> getNeighboringCells() {
return neighboringCells;
}