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.
This commit is contained in:
Sebastian Kaspari 2016-04-22 16:30:57 +02:00
parent 1553422a0e
commit 7756a55f6b

View File

@ -22,12 +22,16 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Build;
import android.provider.Settings;
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 +72,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;
}
}
/**