I'm doing a bit of prototyping to inform my code and I run into an issue and I can't identify the source. I'm trying to draw rectangles (frames) around all accessibility-clickable items on the screen, but my frames end up with a horizontal shift. The shift seems constant, so I know I could offset it as a hack, but I'm after the source of it as I'm trying to understand this area better. Here's my code.
Function which is called on all views on the screen:
private void drawFrameIfClickable(AccessibilityNodeInfo accessibilityNodeInfo) {
boolean clickable;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
clickable = accessibilityNodeInfo.getActionList().contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK);
} else {
clickable = (accessibilityNodeInfo.getActions() & AccessibilityNodeInfo.ACTION_CLICK) > 0;
}
if (clickable) {
Rect rect = new Rect();
accessibilityNodeInfo.getBoundsInScreen(rect);
View view = new FrameView(mas, rect);
ViewGroup.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
rect.left, rect.top,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
cursorParams.gravity = Gravity.TOP | Gravity.LEFT;
windowManager.addView(view, params);
highlights.add(view);
}
}
private class FrameView extends View {
private final Paint paint = new Paint();
private Rect rect;
public FrameView(Context mas, Rect rect) {
super(mas);
this.rect = rect;
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, rect.width(), rect.height(), paint);
}
}
The values in 'rect' passed to the view are "correct", ie they agree with the position of the clickable items as checked by the dev tool "Pointer Location". But once drawn, the horizontal offset is there, as below: