Skip to content

Commit

Permalink
Only update backend helpers on actual changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Apr 24, 2022
1 parent 9c32fc7 commit 876181e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AbstractBackendHelper {
protected final Context context;
protected State state = State.DISABLED;
protected boolean currentDataUsed = true;
protected long lastUpdate = 0;

public AbstractBackendHelper(Context context) {
if (context == null)
Expand Down
128 changes: 69 additions & 59 deletions api/src/main/java/org/microg/nlp/api/CellBackendHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
Expand All @@ -57,13 +55,15 @@
public class CellBackendHelper extends AbstractBackendHelper {
private final Listener listener;
private final TelephonyManager telephonyManager;
private final Set<Cell> cells = new HashSet<>();
private Set<Cell> cells = new HashSet<>();
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 final static int MAX_AGE = 300000;
private long lastScan = 0;
private boolean forceNextUpdate = false;

/**
* Create a new instance of {@link CellBackendHelper}. Call this in
Expand Down Expand Up @@ -180,16 +180,17 @@ private void onCellsChanged(List<CellInfo> cellInfo) {
lastScan = System.currentTimeMillis();
if (loadCells(cellInfo)) {
listener.onCellsChanged(getCells());
} else {
Log.d(TAG, "No change in Cell networks");
}
}

/**
* This will fix empty MNC since Android 9 with 0-prefixed MNCs.
* Issue: https://issuetracker.google.com/issues/113560852
*/
@SuppressLint("SoonBlockedPrivateApi")
private void fixEmptyMnc(List<CellInfo> cellInfo) {
if (Build.VERSION.SDK_INT < 28 || cellInfo == null) {
private void fixEmptyMnc(Set<Cell> cells) {
if (Build.VERSION.SDK_INT < 28 || cells == null) {
return;
}

Expand All @@ -201,32 +202,36 @@ private void fixEmptyMnc(List<CellInfo> cellInfo) {

String mnc = networkOperator.substring(3);

for (CellInfo info : cellInfo) {
if (!info.isRegistered()) {
for (Cell cell : cells) {
if (!cell.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 (cell.info instanceof CellInfoGsm) {
identity = ((CellInfoGsm) cell.info).getCellIdentity();
} else if (cell.info instanceof CellInfoWcdma) {
identity = ((CellInfoWcdma) cell.info).getCellIdentity();
} else if (cell.info instanceof CellInfoLte) {
identity = ((CellInfoLte) cell.info).getCellIdentity();
}

if (identity == null) {
continue;
}

try {
Field mncField = CellIdentity.class.getDeclaredField("mMncStr");
mncField.setAccessible(true);
if (mncField.get(identity) == null) {
mncField.set(identity, mnc);
}
} catch (Exception ignored) {
String mncString = null;
if (identity instanceof CellIdentityGsm) {
mncString = ((CellIdentityGsm) identity).getMncString();
} else if (identity instanceof CellIdentityWcdma) {
mncString = ((CellIdentityWcdma) identity).getMncString();
} else if (identity instanceof CellIdentityLte) {
mncString = ((CellIdentityLte) identity).getMncString();
}

if (mncString == null) {
cell.mnc = Integer.parseInt(mnc);
}
}
}
Expand All @@ -235,45 +240,41 @@ private void fixEmptyMnc(List<CellInfo> cellInfo) {
* This will fix values returned by {@link TelephonyManager#getAllCellInfo()} as described
* here: https://github.com/mozilla/ichnaea/issues/340
*/
@SuppressWarnings({"ChainOfInstanceofChecks", "MagicNumber", "ConstantConditions"})
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void fixShortMncBug(List<CellInfo> cellInfo) {
if (cellInfo == null) return;
private void fixShortMncBug(Set<Cell> cells) {
if (cells == null) return;
String networkOperator = telephonyManager.getNetworkOperator();
if (networkOperator.length() != 5) return;
int realMnc = Integer.parseInt(networkOperator.substring(3));
boolean theBug = false;
for (CellInfo info : cellInfo) {
if (info instanceof CellInfoCdma) return;
if (info.isRegistered()) {
Cell cell = parseCellInfo(info);
if (cell == null) continue;
for (Cell cell : cells) {
if (cell.info instanceof CellInfoCdma) return;
if (cell.info.isRegistered()) {
int infoMnc = cell.getMnc();
if (infoMnc == (realMnc * 10 + 15)) {
theBug = true;
}
}
}
if (theBug) {
for (CellInfo info : cellInfo) {
for (Cell cell : cells) {
Object identity = null;
if (info instanceof CellInfoGsm)
identity = ((CellInfoGsm) info).getCellIdentity();
else if (info instanceof CellInfoLte)
identity = ((CellInfoLte) info).getCellIdentity();
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 &&
info instanceof CellInfoWcdma)
identity = ((CellInfoWcdma) info).getCellIdentity();
if (cell.info instanceof CellInfoGsm)
identity = ((CellInfoGsm) cell.info).getCellIdentity();
else if (cell.info instanceof CellInfoLte)
identity = ((CellInfoLte) cell.info).getCellIdentity();
else if (Build.VERSION.SDK_INT >= 18 && cell.info instanceof CellInfoWcdma)
identity = ((CellInfoWcdma) cell.info).getCellIdentity();
if (identity == null) continue;
try {
Field mncField = identity.getClass().getDeclaredField("mMnc");
mncField.setAccessible(true);
int mnc = (Integer) mncField.get(identity);
if (mnc >= 25 && mnc <= 1005) {
mnc = (mnc - 15) / 10;
mncField.setInt(identity, mnc);
}
} catch (Exception ignored) {
int mnc = -1;
if (identity instanceof CellIdentityGsm) {
mnc = ((CellIdentityGsm) identity).getMnc();
} else if (Build.VERSION.SDK_INT >= 18 && identity instanceof CellIdentityWcdma) {
mnc = ((CellIdentityWcdma) identity).getMnc();
} else if (identity instanceof CellIdentityLte) {
mnc = ((CellIdentityLte) identity).getMnc();
}
if (mnc >= 25 && mnc <= 1005) {
cell.mnc = (mnc - 15) / 10;
}
}
}
Expand Down Expand Up @@ -321,18 +322,16 @@ private CellInfo fromCellLocation(CellLocation cellLocation) {
@SuppressWarnings("unchecked")
@SuppressLint({"DiscouragedPrivateApi", "deprecation"})
private synchronized boolean loadCells(List<CellInfo> cellInfo) {
int oldHash = cells.hashCode();
cells.clear();
currentDataUsed = false;
Set<Cell> cells = new HashSet<>();
try {
if (cellInfo != null) {
fixEmptyMnc(cellInfo);
fixShortMncBug(cellInfo);
for (CellInfo info : cellInfo) {
Cell cell = parseCellInfo(info);
if (cell == null) continue;
cells.add(cell);
}
fixEmptyMnc(cells);
fixShortMncBug(cells);
}
Method getNeighboringCellInfo = TelephonyManager.class.getDeclaredMethod("getNeighboringCellInfo");
List<android.telephony.NeighboringCellInfo> neighboringCellInfo = (List<android.telephony.NeighboringCellInfo>) getNeighboringCellInfo.invoke(telephonyManager);
Expand All @@ -349,14 +348,17 @@ private synchronized boolean loadCells(List<CellInfo> cellInfo) {
}
if (state == State.DISABLING)
state = State.DISABLED;
switch (state) {
default:
case DISABLED:
return false;
case SCANNING:
if (!cells.equals(this.cells) || lastUpdate == 0 || forceNextUpdate) {
this.cells = cells;
lastUpdate = System.currentTimeMillis();
currentDataUsed = false;
forceNextUpdate = false;
if (state == State.SCANNING) {
state = State.WAITING;
return cells.hashCode() != oldHash;
}
return state != State.DISABLED;
}
return false;
}

public synchronized Set<Cell> getCells() {
Expand All @@ -379,6 +381,7 @@ public synchronized void onOpen() {
@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
if (cellInfo != null && !cellInfo.isEmpty()) {
forceNextUpdate = true;
onCellsChanged(cellInfo);
} else if (supportsCellInfoChanged) {
supportsCellInfoChanged = false;
Expand Down Expand Up @@ -409,6 +412,7 @@ private synchronized void doScan() {
}, new TelephonyManager.CellInfoCallback() {
@Override
public void onCellInfo(List<CellInfo> cellInfo) {
forceNextUpdate = true;
handleAllCellInfo(cellInfo);
}
});
Expand Down Expand Up @@ -450,7 +454,7 @@ public synchronized void onClose() {

@Override
public synchronized void onUpdate() {
if (!currentDataUsed) {
if (!currentDataUsed && System.currentTimeMillis() - lastUpdate < MAX_AGE) {
listener.onCellsChanged(getCells());
} else {
state = State.SCANNING;
Expand All @@ -470,6 +474,7 @@ public interface Listener {
}

public static class Cell {
private CellInfo info;
private CellType type;
private int mcc;
private int mnc;
Expand All @@ -478,6 +483,11 @@ public static class Cell {
private int psc;
private int signal;

private Cell(CellInfo info, CellType type, int mcc, int mnc, int lac, long cid, int psc, int signal) {
this(type, mcc, mnc, lac, cid, psc, signal);
this.info = info;
}

public Cell(CellType type, int mcc, int mnc, int lac, long cid, int psc, int signal) {
if (type == null)
throw new IllegalArgumentException("Each cell has an type!");
Expand Down
26 changes: 15 additions & 11 deletions api/src/main/java/org/microg/nlp/api/WiFiBackendHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import java.util.HashSet;
import java.util.List;
Expand All @@ -31,17 +32,18 @@
public class WiFiBackendHelper extends AbstractBackendHelper {
private final static IntentFilter wifiBroadcastFilter =
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
private final static int MAX_AGE = 60000;

private final Listener listener;
private final WifiManager wifiManager;
private final Set<WiFi> wiFis = new HashSet<>();
private final BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onWiFisChanged();
}
};

private Set<WiFi> wiFis = new HashSet<>();
private boolean ignoreNomap = true;

/**
Expand Down Expand Up @@ -90,7 +92,7 @@ public synchronized void onClose() {
* Call this in {@link LocationBackendService#update()}.
*/
public synchronized void onUpdate() {
if (!currentDataUsed) {
if (!currentDataUsed && System.currentTimeMillis() - lastUpdate < MAX_AGE) {
listener.onWiFisChanged(getWiFis());
} else {
scanWiFis();
Expand All @@ -105,6 +107,8 @@ public String[] getRequiredPermissions() {
private void onWiFisChanged() {
if (loadWiFis()) {
listener.onWiFisChanged(getWiFis());
} else {
Log.d(TAG, "No change in WiFi networks");
}
}

Expand All @@ -128,24 +132,24 @@ private boolean isScanAlwaysAvailable() {
}

private synchronized boolean loadWiFis() {
int oldHash = wiFis.hashCode();
wiFis.clear();
currentDataUsed = false;
Set<WiFi> wiFis = new HashSet<>();
List<ScanResult> scanResults = wifiManager.getScanResults();
for (ScanResult scanResult : scanResults) {
if (ignoreNomap && scanResult.SSID.toLowerCase(Locale.US).endsWith("_nomap")) continue;
wiFis.add(new WiFi(scanResult.BSSID, scanResult.level, frequencyToChannel(scanResult.frequency), scanResult.frequency));
}
if (state == State.DISABLING)
state = State.DISABLED;
switch (state) {
default:
case DISABLED:
return false;
case SCANNING:
if (!wiFis.equals(this.wiFis) || lastUpdate == 0) {
this.wiFis = wiFis;
lastUpdate = System.currentTimeMillis();
currentDataUsed = false;
if (state == State.SCANNING) {
state = State.WAITING;
return wiFis.hashCode() != oldHash;
}
return state != State.DISABLED;
}
return false;
}

@SuppressWarnings("MagicNumber")
Expand Down

0 comments on commit 876181e

Please sign in to comment.