OSDN Git Service

Legacy foreground apps should continue getting scan results
[android-x86/packages-apps-Bluetooth.git] / src / com / android / bluetooth / Utils.java
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;