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