From 213d63f4ee34c1671705f3e065016c354222b469 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Wed, 30 Mar 2016 12:58:27 +0200 Subject: [PATCH] Bug 1244722 - TabQueueHelper.canDrawOverlays(): Implement workaround for Android bug. r=ahunt a=ritu 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 --- .../gecko/tabqueue/TabQueueHelper.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/tabqueue/TabQueueHelper.java b/mobile/android/base/java/org/mozilla/gecko/tabqueue/TabQueueHelper.java index 18b6fe17d3c..9b50c20aae8 100644 --- a/mobile/android/base/java/org/mozilla/gecko/tabqueue/TabQueueHelper.java +++ b/mobile/android/base/java/org/mozilla/gecko/tabqueue/TabQueueHelper.java @@ -7,7 +7,6 @@ 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; @@ -23,11 +22,14 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Build; -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; @@ -68,7 +70,29 @@ public class TabQueueHelper { 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; + } } /**