OSDN Git Service

Legacy foreground apps should continue getting scan results
authorFyodor Kupolov <fkupolov@google.com>
Wed, 29 Jul 2015 21:32:54 +0000 (14:32 -0700)
committerFyodor Kupolov <fkupolov@google.com>
Wed, 29 Jul 2015 21:32:54 +0000 (14:32 -0700)
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

AndroidManifest.xml
src/com/android/bluetooth/Utils.java

index 4673579..85910aa 100644 (file)
@@ -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"/>
index 6874438..1e82e7e 100644 (file)
@@ -38,6 +38,7 @@ import java.io.InputStream;
 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 @@ final public class Utils {
             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;