DebugOverlayView.java revision dcfa7976fa836ae90bb4a579892a18a0abf35b3c
1/* 2 * Copyright (C) 2014 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.systemui.recents.views; 18 19import android.content.Context; 20import android.graphics.Canvas; 21import android.graphics.Paint; 22import android.graphics.Rect; 23import android.util.Pair; 24import android.view.View; 25import android.widget.FrameLayout; 26 27import java.util.ArrayList; 28 29/** 30 * A full screen overlay layer that allows us to draw views from throughout the system on the top 31 * most layer. 32 */ 33public class DebugOverlayView extends FrameLayout { 34 35 final static int sCornerRectSize = 50; 36 37 ArrayList<Pair<Rect, Integer>> mRects = new ArrayList<Pair<Rect, Integer>>(); 38 Paint mDebugOutline = new Paint(); 39 Paint mTmpPaint = new Paint(); 40 boolean mEnabled = true; 41 42 public DebugOverlayView(Context context) { 43 super(context); 44 mDebugOutline.setColor(0xFFff0000); 45 mDebugOutline.setStyle(Paint.Style.STROKE); 46 mDebugOutline.setStrokeWidth(8f); 47 setWillNotDraw(false); 48 } 49 50 /** Enables the debug overlay drawing. */ 51 public void enable() { 52 mEnabled = true; 53 invalidate(); 54 } 55 56 /** Disables the debug overlay drawing. */ 57 public void disable() { 58 mEnabled = false; 59 invalidate(); 60 } 61 62 /** Clears all debug rects. */ 63 public void clear() { 64 mRects.clear(); 65 } 66 67 /** Adds a rect to be drawn. */ 68 void addRect(Rect r, int color) { 69 mRects.add(new Pair<Rect, Integer>(r, color)); 70 invalidate(); 71 } 72 73 /** Adds a view's global rect to be drawn. */ 74 void addViewRect(View v, int color) { 75 Rect vr = new Rect(); 76 v.getGlobalVisibleRect(vr); 77 mRects.add(new Pair<Rect, Integer>(vr, color)); 78 invalidate(); 79 } 80 81 /** Adds a rect, relative to a given view to be drawn. */ 82 void addRectRelativeToView(View v, Rect r, int color) { 83 Rect vr = new Rect(); 84 v.getGlobalVisibleRect(vr); 85 r.offsetTo(vr.left, vr.top); 86 mRects.add(new Pair<Rect, Integer>(r, color)); 87 invalidate(); 88 } 89 90 @Override 91 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 92 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 93 addRect(new Rect(0, 0, sCornerRectSize, sCornerRectSize), 0xFFff0000); 94 addRect(new Rect(getMeasuredWidth() - sCornerRectSize, getMeasuredHeight() - sCornerRectSize, 95 getMeasuredWidth(), getMeasuredHeight()), 0xFFff0000); 96 } 97 98 @Override 99 protected void onDraw(Canvas canvas) { 100 if (mEnabled) { 101 // Draw the outline 102 canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mDebugOutline); 103 104 // Draw the rects 105 int numRects = mRects.size(); 106 for (int i = 0; i < numRects; i++) { 107 Pair<Rect, Integer> r = mRects.get(i); 108 mTmpPaint.setColor(r.second); 109 canvas.drawRect(r.first, mTmpPaint); 110 } 111 } 112 } 113} 114