Bug 1244722 - TabQueueHelper.canDrawOverlays(): Implement workaround for Android bug. r=ahunt
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 30 Mar 2016 12:58:27 +0200
changeset 329188 a70b50e2830f3316d5a1307bb9f8231ca88efc52
parent 329187 38fa3ece64fbd82cd88a0dc91c820b786cbcee4c
child 329189 2b594859ee8084df81a0e8506eed1313a612878e
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1244722 - TabQueueHelper.canDrawOverlays(): Implement workaround for Android bug. r=ahunt Android's Settings.canDrawOverlays() returns true/false for one of the packages with the same sharedUserId. There's no guarantee that this is actually the current package. Instead of relying on Settings.canDrawOverlays() we just try to add and remove an invisible view through WindowManger. If this succeeds then we obviously can draw overlays and if this fails then we seem to not have the permission. MozReview-Commit-ID: 1jdrQ7iV3ek
--- a/mobile/android/base/java/org/mozilla/gecko/tabqueue/TabQueueHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabqueue/TabQueueHelper.java
@@ -2,34 +2,36 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 package org.mozilla.gecko.tabqueue;
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.GeckoAppShell;
-import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.util.ThreadUtils;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Resources;
-import android.provider.Settings;
+import android.graphics.PixelFormat;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.content.ContextCompat;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.View;
+import android.view.WindowManager;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import java.util.ArrayList;
 import java.util.List;
 public class TabQueueHelper {
@@ -60,17 +62,39 @@ public class TabQueueHelper {
      * @return true if the specified context can draw on top of other apps, false otherwise.
     public static boolean canDrawOverlays(Context context) {
         if (AppConstants.Versions.preMarshmallow) {
             return true; // We got the permission at install time.
-        return Settings.canDrawOverlays(context);
+        // It would be nice to just use Settings.canDrawOverlays() - but this helper is buggy for
+        // apps using sharedUserId (See bug 1244722).
+        // Instead we'll add and remove an invisible view. If this is successful then we seem to
+        // have permission to draw overlays.
+        View view = new View(context);
+        view.setVisibility(View.INVISIBLE);
+        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
+                1, 1,
+                WindowManager.LayoutParams.TYPE_PHONE,
+                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
+                PixelFormat.TRANSLUCENT);
+        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
+        try {
+            windowManager.addView(view, layoutParams);
+            windowManager.removeView(view);
+            return true;
+        } catch (final SecurityException | WindowManager.BadTokenException e) {
+            return false;
+        }
      * Check if we should show the tab queue prompt
      * @param context
      * @return true if we should display the prompt, false if not.