AssistVisualizer.java revision 27eac1d58fe0b7ca3a2e27f5ed64eff232745f45
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.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 mTextRects.clear(); 50 final int N = as.getWindowCount(); 51 if (N > 0) { 52 AssistStructure.ViewNode window = new AssistStructure.ViewNode(); 53 for (int i=0; i<N; i++) { 54 as.getWindowAt(i, window); 55 buildTextRects(window, 0, 0); 56 } 57 } 58 invalidate(); 59 } 60 61 public void clearAssistData() { 62 mAssistStructure = null; 63 mTextRects.clear(); 64 } 65 66 void buildTextRects(AssistStructure.ViewNode root, int parentLeft, int parentTop) { 67 if (root.getVisibility() != View.VISIBLE) { 68 return; 69 } 70 int left = parentLeft+root.getLeft(); 71 int top = parentTop+root.getTop(); 72 Log.d(TAG, "View " + root.getClassName() + ": " + left + ", " + top); 73 if (root.getText() != null) { 74 Rect r = new Rect(left, top, left+root.getWidth(), top+root.getHeight()); 75 Log.d(TAG, "Text Rect " + r.toShortString() + ": " + root.getText()); 76 mTextRects.add(r); 77 } 78 final int N = root.getChildCount(); 79 if (N > 0) { 80 left -= root.getScrollX(); 81 top -= root.getScrollY(); 82 AssistStructure.ViewNode child = new AssistStructure.ViewNode(); 83 for (int i=0; i<N; i++) { 84 root.getChildAt(i, child); 85 buildTextRects(child, left, top); 86 } 87 } 88 } 89 90 @Override 91 protected void onDraw(Canvas canvas) { 92 super.onDraw(canvas); 93 getLocationOnScreen(mTmpLocation); 94 final int N = mTextRects.size(); 95 for (int i=0; i<N; i++) { 96 Rect r = mTextRects.get(i); 97 canvas.drawRect(r.left-mTmpLocation[0], r.top-mTmpLocation[1], 98 r.right-mTmpLocation[0], r.bottom-mTmpLocation[1], mFramePaint); 99 } 100 } 101} 102