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
This commit is contained in:
Sebastian Kaspari 2016-03-30 12:58:27 +02:00
parent 4a2532926b
commit 213d63f4ee

View File

@ -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;
}
}
/**