diff --git a/unifiednlp-api/src/main/java/org/microg/nlp/api/CellBackendHelper.java b/unifiednlp-api/src/main/java/org/microg/nlp/api/CellBackendHelper.java index 93e177c..d96e5cb 100644 --- a/unifiednlp-api/src/main/java/org/microg/nlp/api/CellBackendHelper.java +++ b/unifiednlp-api/src/main/java/org/microg/nlp/api/CellBackendHelper.java @@ -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()}. @@ -174,6 +178,7 @@ private Cell parseCellInfo(NeighboringCellInfo info) { } private void onCellsChanged(List cellInfo) { + lastScan = System.currentTimeMillis(); if (loadCells(cellInfo)) { listener.onCellsChanged(getCells()); } @@ -323,7 +328,7 @@ public void run() { @Override public void onCellInfoChanged(List cellInfo) { - if (cellInfo != null) { + if (cellInfo != null && !cellInfo.isEmpty()) { onCellsChanged(cellInfo); } else if (supportsCellInfoChanged) { supportsCellInfoChanged = false; @@ -334,16 +339,7 @@ public void onCellInfoChanged(List cellInfo) { @Override public void onSignalStrengthsChanged(SignalStrength signalStrength) { if (!supportsCellInfoChanged) { - List allCellInfo = telephonyManager.getAllCellInfo(); - if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) { - allCellInfo = new ArrayList(); - CellLocation cellLocation = telephonyManager.getCellLocation(); - if (cellLocation != null) { - CellInfo cellInfo = fromCellLocation(cellLocation); - if (cellInfo != null) allCellInfo.add(cellInfo); - } - } - onCellInfoChanged(allCellInfo); + fallbackScan(); } } }; @@ -355,6 +351,18 @@ public void onSignalStrengthsChanged(SignalStrength signalStrength) { } } + private synchronized void fallbackScan() { + if (lastScan + MIN_UPDATE_INTERVAL > System.currentTimeMillis()) return; + List allCellInfo = telephonyManager.getAllCellInfo(); + if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) { + allCellInfo = new ArrayList(); + CellLocation cellLocation = telephonyManager.getCellLocation(); + CellInfo cellInfo = fromCellLocation(cellLocation); + if (cellInfo != null) allCellInfo.add(cellInfo); + } + onCellsChanged(allCellInfo); + } + private synchronized void registerPhoneStateListener() { try { telephonyManager.listen(phoneStateListener, @@ -382,6 +390,9 @@ public synchronized void onUpdate() { listener.onCellsChanged(getCells()); } else { state = State.SCANNING; + if (lastScan + FALLBACK_UPDATE_INTERVAL < System.currentTimeMillis()) { + fallbackScan(); + } } } @@ -391,7 +402,7 @@ public String[] getRequiredPermissions() { } public interface Listener { - public void onCellsChanged(Set cells); + void onCellsChanged(Set cells); } public static class Cell {