Legacy foreground apps should continue getting scan results
Pre-M apps running in the foreground now get scan results. For legacy apps
running in the background, no results are returned and the error is logged.
Bug: 21852542
Change-Id: Ic7a91f34c6718c26f0dae30dade6a436fe1061af
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4673579..85910aa 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -60,6 +60,7 @@
<uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
+ <uses-permission android:name="android.permission.REAL_GET_TASKS" />
<!-- For PBAP Owner Vcard Info -->
<uses-permission android:name="android.permission.READ_PROFILE"/>
diff --git a/src/com/android/bluetooth/Utils.java b/src/com/android/bluetooth/Utils.java
index 6874438..1e82e7e 100644
--- a/src/com/android/bluetooth/Utils.java
+++ b/src/com/android/bluetooth/Utils.java
@@ -38,6 +38,7 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
+import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@@ -298,12 +299,27 @@
throw new SecurityException("Need ACCESS_COARSE_LOCATION or "
+ "ACCESS_FINE_LOCATION permission to get scan results");
} else {
+ // Pre-M apps running in the foreground should continue getting scan results
+ if (isForegroundApp(context, callingPackage)) {
+ return true;
+ }
Log.e(TAG, "Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION "
+ "permission to get scan results");
}
return false;
}
+ /**
+ * Return true if the specified package name is a foreground app.
+ *
+ * @param pkgName application package name.
+ */
+ private static boolean isForegroundApp(Context context, String pkgName) {
+ ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
+ List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
+ return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
+ }
+
private static boolean isAppOppAllowed(AppOpsManager appOps, int op, String callingPackage) {
return appOps.noteOp(op, Binder.getCallingUid(), callingPackage)
== AppOpsManager.MODE_ALLOWED;