/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.test.voiceinteraction; import android.annotation.Nullable; import android.app.assist.AssistStructure; import android.content.Context; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.view.View; import java.util.ArrayList; public class AssistVisualizer extends View { static final String TAG = "AssistVisualizer"; static class TextEntry { final Rect bounds; final int parentLeft, parentTop; final Matrix matrix; final String className; final float textSize; final int textColor; final CharSequence text; final int scrollY; final int[] lineCharOffsets; final int[] lineBaselines; TextEntry(AssistStructure.ViewNode node, int parentLeft, int parentTop, Matrix matrix) { int left = parentLeft+node.getLeft(); int top = parentTop+node.getTop(); bounds = new Rect(left, top, left+node.getWidth(), top+node.getHeight()); this.parentLeft = parentLeft; this.parentTop = parentTop; this.matrix = new Matrix(matrix); this.className = node.getClassName(); this.textSize = node.getTextSize(); this.textColor = node.getTextColor(); this.text = node.getText() != null ? node.getText() : node.getContentDescription(); this.scrollY = node.getScrollY(); this.lineCharOffsets = node.getTextLineCharOffsets(); this.lineBaselines = node.getTextLineBaselines(); } } AssistStructure mAssistStructure; final Paint mFramePaint = new Paint(); final Paint mFrameBaselinePaint = new Paint(); final Paint mFrameNoTransformPaint = new Paint(); final ArrayList mMatrixStack = new ArrayList<>(); final ArrayList mTextRects = new ArrayList<>(); final int[] mTmpLocation = new int[2]; public AssistVisualizer(Context context, @Nullable AttributeSet attrs) { super(context, attrs); setWillNotDraw(false); mFramePaint.setColor(0xffff0000); mFramePaint.setStyle(Paint.Style.STROKE); mFramePaint.setStrokeWidth(0); mFrameBaselinePaint.setColor(0xa0b0b000); mFrameBaselinePaint.setStyle(Paint.Style.STROKE); mFrameBaselinePaint.setStrokeWidth(0); float density = getResources().getDisplayMetrics().density; mFramePaint.setShadowLayer(density, density, density, 0xff000000); mFrameNoTransformPaint.setColor(0xff0000ff); mFrameNoTransformPaint.setStyle(Paint.Style.STROKE); mFrameNoTransformPaint.setStrokeWidth(0); mFrameNoTransformPaint.setShadowLayer(density, density, density, 0xff000000); } public void setAssistStructure(AssistStructure as) { mAssistStructure = as; mTextRects.clear(); final int N = as.getWindowNodeCount(); if (N > 0) { for (int i=0; i