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