Skip to content

Commit

Permalink
CellBackendHelper: Add fallback scanning for devices not supporting P…
Browse files Browse the repository at this point in the history
…honeStateListener
  • Loading branch information
mar-v-in committed Apr 3, 2016
1 parent 19d9ede commit e07b203
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class CellBackendHelper extends AbstractBackendHelper {
private PhoneStateListener phoneStateListener;
private boolean supportsCellInfoChanged = true;

public static final int MIN_UPDATE_INTERVAL = 30 * 1000;
public static final int FALLBACK_UPDATE_INTERVAL = 5 * 60 * 1000;
private long lastScan = 0;

/**
* Create a new instance of {@link CellBackendHelper}. Call this in
* {@link LocationBackendService#onCreate()}.
Expand Down Expand Up @@ -174,6 +178,7 @@ private Cell parseCellInfo(NeighboringCellInfo info) {
}

private void onCellsChanged(List<CellInfo> cellInfo) {
lastScan = System.currentTimeMillis();
if (loadCells(cellInfo)) {
listener.onCellsChanged(getCells());
}
Expand Down Expand Up @@ -323,7 +328,7 @@ public void run() {

@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
if (cellInfo != null) {
if (cellInfo != null && !cellInfo.isEmpty()) {
onCellsChanged(cellInfo);
} else if (supportsCellInfoChanged) {
supportsCellInfoChanged = false;
Expand All @@ -334,16 +339,7 @@ public void onCellInfoChanged(List<CellInfo> cellInfo) {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (!supportsCellInfoChanged) {
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) {
allCellInfo = new ArrayList<CellInfo>();
CellLocation cellLocation = telephonyManager.getCellLocation();
if (cellLocation != null) {
CellInfo cellInfo = fromCellLocation(cellLocation);
if (cellInfo != null) allCellInfo.add(cellInfo);
}
}
onCellInfoChanged(allCellInfo);
fallbackScan();
}
}
};
Expand All @@ -355,6 +351,18 @@ public void onSignalStrengthsChanged(SignalStrength signalStrength) {
}
}

private synchronized void fallbackScan() {
if (lastScan + MIN_UPDATE_INTERVAL > System.currentTimeMillis()) return;
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) {
allCellInfo = new ArrayList<CellInfo>();
CellLocation cellLocation = telephonyManager.getCellLocation();
CellInfo cellInfo = fromCellLocation(cellLocation);
if (cellInfo != null) allCellInfo.add(cellInfo);
}
onCellsChanged(allCellInfo);
}

private synchronized void registerPhoneStateListener() {
try {
telephonyManager.listen(phoneStateListener,
Expand Down Expand Up @@ -382,6 +390,9 @@ public synchronized void onUpdate() {
listener.onCellsChanged(getCells());
} else {
state = State.SCANNING;
if (lastScan + FALLBACK_UPDATE_INTERVAL < System.currentTimeMillis()) {
fallbackScan();
}
}
}

Expand All @@ -391,7 +402,7 @@ public String[] getRequiredPermissions() {
}

public interface Listener {
public void onCellsChanged(Set<Cell> cells);
void onCellsChanged(Set<Cell> cells);
}

public static class Cell {
Expand Down

0 comments on commit e07b203

Please sign in to comment.