From c9d74306858b45268f0a383c330cc71b197425ab Mon Sep 17 00:00:00 2001 From: Hall Liu Date: Thu, 28 Feb 2019 15:29:19 -0800 Subject: [PATCH] Allow requestNetworkScan to return sanitized info If an app doesn't have location permissions or if the system location switch is off, allow requestNetworkScan to still function, but sanitize the returned CellInfo objects first and disallow certain input arguments Bug: 126779616 Test: CTS Change-Id: Ic9c44043bd30d402f128bf0723f2b0bf6c420e3e --- .../java/android/telephony/CellIdentity.java | 16 ++++++++++++++++ .../java/android/telephony/CellIdentityCdma.java | 7 +++++++ .../java/android/telephony/CellIdentityGsm.java | 6 ++++++ .../java/android/telephony/CellIdentityLte.java | 7 +++++++ .../java/android/telephony/CellIdentityNr.java | 8 +++++++- .../android/telephony/CellIdentityTdscdma.java | 6 ++++++ .../android/telephony/CellIdentityWcdma.java | 7 +++++++ telephony/java/android/telephony/CellInfo.java | 5 +++++ .../java/android/telephony/CellInfoCdma.java | 9 +++++++++ .../java/android/telephony/CellInfoGsm.java | 9 +++++++++ .../java/android/telephony/CellInfoLte.java | 9 +++++++++ telephony/java/android/telephony/CellInfoNr.java | 13 +++++++++++++ .../java/android/telephony/CellInfoTdscdma.java | 8 ++++++++ .../java/android/telephony/CellInfoWcdma.java | 9 +++++++++ .../java/android/telephony/TelephonyManager.java | 11 +++++++++++ .../android/telephony/TelephonyScanManager.java | 14 ++++++++++++-- 16 files changed, 141 insertions(+), 3 deletions(-) diff --git a/telephony/java/android/telephony/CellIdentity.java b/telephony/java/android/telephony/CellIdentity.java index bda8812934342..3728de278c8d8 100644 --- a/telephony/java/android/telephony/CellIdentity.java +++ b/telephony/java/android/telephony/CellIdentity.java @@ -109,6 +109,22 @@ public abstract class CellIdentity implements Parcelable { return mType; } + /** + * @return MCC or null for CDMA + * @hide + */ + public String getMccString() { + return mMccStr; + } + + /** + * @return MNC or null for CDMA + * @hide + */ + public String getMncString() { + return mMncStr; + } + /** * Returns the channel number of the cell identity. * diff --git a/telephony/java/android/telephony/CellIdentityCdma.java b/telephony/java/android/telephony/CellIdentityCdma.java index fa198674c58ad..637f49dd665b9 100644 --- a/telephony/java/android/telephony/CellIdentityCdma.java +++ b/telephony/java/android/telephony/CellIdentityCdma.java @@ -109,6 +109,13 @@ public final class CellIdentityCdma extends CellIdentity { return new CellIdentityCdma(this); } + /** @hide */ + public CellIdentityCdma sanitizeLocationInfo() { + return new CellIdentityCdma(CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + mAlphaLong, mAlphaShort); + } + /** * Take the latitude and longitude in 1/4 seconds and see if * the reported location is on Null Island. diff --git a/telephony/java/android/telephony/CellIdentityGsm.java b/telephony/java/android/telephony/CellIdentityGsm.java index 9a24e47288c36..6c1048f78d288 100644 --- a/telephony/java/android/telephony/CellIdentityGsm.java +++ b/telephony/java/android/telephony/CellIdentityGsm.java @@ -97,6 +97,12 @@ public final class CellIdentityGsm extends CellIdentity { return new CellIdentityGsm(this); } + /** @hide */ + public CellIdentityGsm sanitizeLocationInfo() { + return new CellIdentityGsm(CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + CellInfo.UNAVAILABLE, mMccStr, mMncStr, mAlphaLong, mAlphaShort); + } + /** * @return 3-digit Mobile Country Code, 0..999, * {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable. diff --git a/telephony/java/android/telephony/CellIdentityLte.java b/telephony/java/android/telephony/CellIdentityLte.java index 51393b90e61e5..824fbc5aebbc7 100644 --- a/telephony/java/android/telephony/CellIdentityLte.java +++ b/telephony/java/android/telephony/CellIdentityLte.java @@ -113,6 +113,13 @@ public final class CellIdentityLte extends CellIdentity { cid.mMncStr, cid.mAlphaLong, cid.mAlphaShort); } + /** @hide */ + public CellIdentityLte sanitizeLocationInfo() { + return new CellIdentityLte(CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + mMccStr, mMncStr, mAlphaLong, mAlphaShort); + } + CellIdentityLte copy() { return new CellIdentityLte(this); } diff --git a/telephony/java/android/telephony/CellIdentityNr.java b/telephony/java/android/telephony/CellIdentityNr.java index 771e7b9a80766..44896e297e3c7 100644 --- a/telephony/java/android/telephony/CellIdentityNr.java +++ b/telephony/java/android/telephony/CellIdentityNr.java @@ -46,7 +46,7 @@ public final class CellIdentityNr extends CellIdentity { * * @hide */ - public CellIdentityNr(int pci, int tac, int nrArfcn, String mccStr, String mncStr, + public CellIdentityNr(int pci, int tac, int nrArfcn, String mccStr, String mncStr, long nci, String alphal, String alphas) { super(TAG, CellInfo.TYPE_NR, mccStr, mncStr, alphal, alphas); mPci = pci; @@ -55,6 +55,12 @@ public final class CellIdentityNr extends CellIdentity { mNci = nci; } + /** @hide */ + public CellIdentityNr sanitizeLocationInfo() { + return new CellIdentityNr(CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + mMccStr, mMncStr, CellInfo.UNAVAILABLE, mAlphaLong, mAlphaShort); + } + /** * @return a CellLocation object for this CellIdentity. * @hide diff --git a/telephony/java/android/telephony/CellIdentityTdscdma.java b/telephony/java/android/telephony/CellIdentityTdscdma.java index 19b11b666018c..a591bd15f95f7 100644 --- a/telephony/java/android/telephony/CellIdentityTdscdma.java +++ b/telephony/java/android/telephony/CellIdentityTdscdma.java @@ -90,6 +90,12 @@ public final class CellIdentityTdscdma extends CellIdentity { cid.uarfcn, cid.operatorNames.alphaLong, cid.operatorNames.alphaShort); } + /** @hide */ + public CellIdentityTdscdma sanitizeLocationInfo() { + return new CellIdentityTdscdma(mMccStr, mMncStr, CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, mAlphaLong, mAlphaShort); + } + CellIdentityTdscdma copy() { return new CellIdentityTdscdma(this); } diff --git a/telephony/java/android/telephony/CellIdentityWcdma.java b/telephony/java/android/telephony/CellIdentityWcdma.java index 6e0978434a577..3a1772f6068f6 100644 --- a/telephony/java/android/telephony/CellIdentityWcdma.java +++ b/telephony/java/android/telephony/CellIdentityWcdma.java @@ -91,6 +91,13 @@ public final class CellIdentityWcdma extends CellIdentity { cid.mMncStr, cid.mAlphaLong, cid.mAlphaShort); } + /** @hide */ + public CellIdentityWcdma sanitizeLocationInfo() { + return new CellIdentityWcdma(CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, + CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, mMccStr, mMncStr, + mAlphaLong, mAlphaShort); + } + CellIdentityWcdma copy() { return new CellIdentityWcdma(this); } diff --git a/telephony/java/android/telephony/CellInfo.java b/telephony/java/android/telephony/CellInfo.java index cdaf6ed7c6aab..2142febc82ae4 100644 --- a/telephony/java/android/telephony/CellInfo.java +++ b/telephony/java/android/telephony/CellInfo.java @@ -197,6 +197,11 @@ public abstract class CellInfo implements Parcelable { @NonNull public abstract CellSignalStrength getCellSignalStrength(); + /** @hide */ + public CellInfo sanitizeLocationInfo() { + return null; + } + /** * Gets the connection status of this cell. * diff --git a/telephony/java/android/telephony/CellInfoCdma.java b/telephony/java/android/telephony/CellInfoCdma.java index 359c8bee8b8d6..d9f44b83b0517 100644 --- a/telephony/java/android/telephony/CellInfoCdma.java +++ b/telephony/java/android/telephony/CellInfoCdma.java @@ -90,6 +90,15 @@ public final class CellInfoCdma extends CellInfo implements Parcelable { public CellSignalStrengthCdma getCellSignalStrength() { return mCellSignalStrengthCdma; } + + /** @hide */ + @Override + public CellInfo sanitizeLocationInfo() { + CellInfoCdma result = new CellInfoCdma(this); + result.mCellIdentityCdma = mCellIdentityCdma.sanitizeLocationInfo(); + return result; + } + /** @hide */ public void setCellSignalStrength(CellSignalStrengthCdma css) { mCellSignalStrengthCdma = css; diff --git a/telephony/java/android/telephony/CellInfoGsm.java b/telephony/java/android/telephony/CellInfoGsm.java index dc2779f5fb2ab..1cecbc6e5bca9 100644 --- a/telephony/java/android/telephony/CellInfoGsm.java +++ b/telephony/java/android/telephony/CellInfoGsm.java @@ -84,6 +84,15 @@ public final class CellInfoGsm extends CellInfo implements Parcelable { public CellSignalStrengthGsm getCellSignalStrength() { return mCellSignalStrengthGsm; } + + /** @hide */ + @Override + public CellInfo sanitizeLocationInfo() { + CellInfoGsm result = new CellInfoGsm(this); + result.mCellIdentityGsm = mCellIdentityGsm.sanitizeLocationInfo(); + return result; + } + /** @hide */ public void setCellSignalStrength(CellSignalStrengthGsm css) { mCellSignalStrengthGsm = css; diff --git a/telephony/java/android/telephony/CellInfoLte.java b/telephony/java/android/telephony/CellInfoLte.java index 0e9623dfab2b4..45f4b1d394634 100644 --- a/telephony/java/android/telephony/CellInfoLte.java +++ b/telephony/java/android/telephony/CellInfoLte.java @@ -96,6 +96,15 @@ public final class CellInfoLte extends CellInfo implements Parcelable { if (DBG) log("getCellSignalStrength: " + mCellSignalStrengthLte); return mCellSignalStrengthLte; } + + /** @hide */ + @Override + public CellInfo sanitizeLocationInfo() { + CellInfoLte result = new CellInfoLte(this); + result.mCellIdentityLte = mCellIdentityLte.sanitizeLocationInfo(); + return result; + } + /** @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public void setCellSignalStrength(CellSignalStrengthLte css) { diff --git a/telephony/java/android/telephony/CellInfoNr.java b/telephony/java/android/telephony/CellInfoNr.java index 7f7902c76095d..2a8b0673389c3 100644 --- a/telephony/java/android/telephony/CellInfoNr.java +++ b/telephony/java/android/telephony/CellInfoNr.java @@ -36,6 +36,13 @@ public final class CellInfoNr extends CellInfo { mCellSignalStrength = CellSignalStrengthNr.CREATOR.createFromParcel(in); } + private CellInfoNr(CellInfoNr other, boolean sanitizeLocationInfo) { + super(other); + mCellIdentity = sanitizeLocationInfo ? other.mCellIdentity.sanitizeLocationInfo() + : other.mCellIdentity; + mCellSignalStrength = other.mCellSignalStrength; + } + @Override @NonNull public CellIdentity getCellIdentity() { @@ -48,6 +55,12 @@ public final class CellInfoNr extends CellInfo { return mCellSignalStrength; } + /** @hide */ + @Override + public CellInfo sanitizeLocationInfo() { + return new CellInfoNr(this, true); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), mCellIdentity, mCellSignalStrength); diff --git a/telephony/java/android/telephony/CellInfoTdscdma.java b/telephony/java/android/telephony/CellInfoTdscdma.java index 1830086ba448b..ccafda61a1773 100644 --- a/telephony/java/android/telephony/CellInfoTdscdma.java +++ b/telephony/java/android/telephony/CellInfoTdscdma.java @@ -90,6 +90,14 @@ public final class CellInfoTdscdma extends CellInfo implements Parcelable { return mCellSignalStrengthTdscdma; } + /** @hide */ + @Override + public CellInfo sanitizeLocationInfo() { + CellInfoTdscdma result = new CellInfoTdscdma(this); + result.mCellIdentityTdscdma = mCellIdentityTdscdma.sanitizeLocationInfo(); + return result; + } + /** @hide */ public void setCellSignalStrength(CellSignalStrengthTdscdma css) { mCellSignalStrengthTdscdma = css; diff --git a/telephony/java/android/telephony/CellInfoWcdma.java b/telephony/java/android/telephony/CellInfoWcdma.java index fe06c78c3a913..81fc7bb3a1105 100644 --- a/telephony/java/android/telephony/CellInfoWcdma.java +++ b/telephony/java/android/telephony/CellInfoWcdma.java @@ -84,6 +84,15 @@ public final class CellInfoWcdma extends CellInfo implements Parcelable { public CellSignalStrengthWcdma getCellSignalStrength() { return mCellSignalStrengthWcdma; } + + /** @hide */ + @Override + public CellInfo sanitizeLocationInfo() { + CellInfoWcdma result = new CellInfoWcdma(this); + result.mCellIdentityWcdma = mCellIdentityWcdma.sanitizeLocationInfo(); + return result; + } + /** @hide */ public void setCellSignalStrength(CellSignalStrengthWcdma css) { mCellSignalStrengthWcdma = css; diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java index 9dcadf118c80f..a307c9abf86f5 100644 --- a/telephony/java/android/telephony/TelephonyManager.java +++ b/telephony/java/android/telephony/TelephonyManager.java @@ -6869,6 +6869,17 @@ public class TelephonyManager { * app has carrier privileges (see {@link #hasCarrierPrivileges}) * and {@link android.Manifest.permission#ACCESS_FINE_LOCATION}. * + * If the system-wide location switch is off, apps may still call this API, with the + * following constraints: + *
    + *
  1. The app must hold the {@code android.permission.NETWORK_SCAN} permission.
  2. + *
  3. The app must not supply any specific bands or channels to scan.
  4. + *
  5. The app must only specify MCC/MNC pairs that are + * associated to a SIM in the device.
  6. + *
  7. Returned results will have no meaningful info other than signal strength + * and MCC/MNC info.
  8. + *
+ * * @param request Contains all the RAT with bands/channels that need to be scanned. * @param executor The executor through which the callback should be invoked. Since the scan * request may trigger multiple callbacks and they must be invoked in the same order as diff --git a/telephony/java/android/telephony/TelephonyScanManager.java b/telephony/java/android/telephony/TelephonyScanManager.java index 91f74b867fa01..28747dab38dbb 100644 --- a/telephony/java/android/telephony/TelephonyScanManager.java +++ b/telephony/java/android/telephony/TelephonyScanManager.java @@ -53,6 +53,11 @@ public final class TelephonyScanManager { public static final int CALLBACK_SCAN_ERROR = 2; /** @hide */ public static final int CALLBACK_SCAN_COMPLETE = 3; + /** @hide */ + public static final int CALLBACK_RESTRICTED_SCAN_RESULTS = 4; + + /** @hide */ + public static final int INVALID_SCAN_ID = -1; /** * The caller of @@ -129,6 +134,7 @@ public final class TelephonyScanManager { } switch (message.what) { + case CALLBACK_RESTRICTED_SCAN_RESULTS: case CALLBACK_SCAN_RESULTS: try { final Bundle b = message.getData(); @@ -137,9 +143,9 @@ public final class TelephonyScanManager { for (int i = 0; i < parcelables.length; i++) { ci[i] = (CellInfo) parcelables[i]; } - executor.execute(() ->{ + executor.execute(() -> { Rlog.d(TAG, "onResults: " + ci.toString()); - callback.onResults((List) Arrays.asList(ci)); + callback.onResults(Arrays.asList(ci)); }); } catch (Exception e) { Rlog.e(TAG, "Exception in networkscan callback onResults", e); @@ -200,6 +206,10 @@ public final class TelephonyScanManager { if (telephony != null) { int scanId = telephony.requestNetworkScan( subId, request, mMessenger, new Binder(), callingPackage); + if (scanId == INVALID_SCAN_ID) { + Rlog.e(TAG, "Failed to initiate network scan"); + return null; + } saveScanInfo(scanId, request, executor, callback); return new NetworkScan(scanId, subId); }