AssistVisualizer.java revision 16036f26847f3f1a88a093fb776bf081008ff8d8
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.test.voiceinteraction; 18 19import android.annotation.Nullable; 20import android.app.assist.AssistStructure; 21import android.content.Context; 22import android.graphics.Canvas; 23import android.graphics.Paint; 24import android.graphics.Rect; 25import android.util.AttributeSet; 26import android.util.Log; 27import android.view.View; 28 29import java.util.ArrayList; 30 31public class AssistVisualizer extends View { 32 static final String TAG = "AssistVisualizer"; 33 34 AssistStructure mAssistStructure; 35 final Paint mFramePaint = new Paint(); 36 final ArrayList<Rect> mTextRects = new ArrayList<>(); 37 final int[] mTmpLocation = new int[2]; 38 39 public AssistVisualizer(Context context, @Nullable AttributeSet attrs) { 40 super(context, attrs); 41 setWillNotDraw(false); 42 mFramePaint.setColor(0xffff0000); 43 mFramePaint.setStyle(Paint.Style.STROKE); 44 mFramePaint.setStrokeWidth(0); 45 } 46 47 public void setAssistStructure(AssistStructure as) { 48 mAssistStructure = as; 49 mAssistStructure.dump(); 50 mTextRects.clear(); 51 final int N = as.getWindowNodeCount(); 52 if (N > 0) { 53 for (int i=0; i<N; i++) { 54 AssistStructure.WindowNode windowNode = as.getWindowNodeAt(i); 55 buildTextRects(windowNode.getRootViewNode(), windowNode.getLeft(), 56 windowNode.getTop()); 57 } 58 } 59 Log.d(TAG, "Building text rects in " + this + ": found " + mTextRects.size()); 60 invalidate(); 61 } 62 63 public void clearAssistData() { 64 mAssistStructure = null; 65 mTextRects.clear(); 66 } 67 68 void buildTextRects(AssistStructure.ViewNode root, int parentLeft, int parentTop) { 69 if (root.getVisibility() != View.VISIBLE) { 70 return; 71 } 72 int left = parentLeft+root.getLeft(); 73 int top = parentTop+root.getTop(); 74 if (root.getText() != null || root.getContentDescription() != null) { 75 Rect r = new Rect(left, top, left+root.getWidth(), top+root.getHeight()); 76 Log.d(TAG, "View " + root.getClassName() + " " + left + "," + top + " tr " 77 + r.toShortString() + ": " 78 + (root.getText() != null ? root.getText() : root.getContentDescription())); 79 mTextRects.add(r); 80 } 81 final int N = root.getChildCount(); 82 if (N > 0) { 83 left -= root.getScrollX(); 84 top -= root.getScrollY(); 85 for (int i=0; i<N; i++) { 86 AssistStructure.ViewNode child = root.getChildAt(i); 87 buildTextRects(child, left, top); 88 } 89 } 90 } 91 92 @Override 93 protected void onDraw(Canvas canvas) { 94 super.onDraw(canvas); 95 getLocationOnScreen(mTmpLocation); 96 final int N = mTextRects.size(); 97 Log.d(TAG, "Drawing text rects in " + this + ": found " + mTextRects.size()); 98 for (int i=0; i<N; i++) { 99 Rect r = mTextRects.get(i); 100 canvas.drawRect(r.left-mTmpLocation[0], r.top-mTmpLocation[1], 101 r.right-mTmpLocation[0], r.bottom-mTmpLocation[1], mFramePaint); 102 } 103 } 104} 105