1 /*
2  * Copyright (C) 2018 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 
17 package com.android.server.cts.device.statsdatom;
18 
19 import android.view.View;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.Paint;
25 import android.graphics.Paint.FontMetrics;
26 import android.os.SystemClock;
27 import android.util.AttributeSet;
28 import android.util.Log;
29 
30 
31 public class DaveyView extends View {
32 
33     private static final String TAG = "statsdDaveyView";
34 
35     private static final long DAVEY_TIME_MS = 750; // A bit more than 700ms to be safe.
36     private boolean mCauseDavey;
37     private Paint mPaint;
38     private int mTexty;
39 
DaveyView(Context context, AttributeSet attrs)40     public DaveyView(Context context, AttributeSet attrs) {
41         super(context, attrs);
42         TypedArray a = context.getTheme().obtainStyledAttributes(
43                 attrs,
44                 R.styleable.DaveyView,
45                 0, 0);
46 
47         try {
48             mCauseDavey = a.getBoolean(R.styleable.DaveyView_causeDavey, false);
49         } finally {
50             a.recycle();
51         }
52 
53         mPaint = new Paint();
54         mPaint.setColor(Color.BLACK);
55         mPaint.setTextSize(20);
56         FontMetrics metric = mPaint.getFontMetrics();
57         int textHeight = (int) Math.ceil(metric.descent - metric.ascent);
58         mTexty = textHeight - (int) metric.descent;
59     }
60 
causeDavey(boolean cause)61     public void causeDavey(boolean cause) {
62         mCauseDavey = cause;
63         invalidate();
64     }
65 
66     @Override
onDraw(Canvas canvas)67     protected void onDraw(Canvas canvas) {
68         super.onDraw(canvas);
69         if (mCauseDavey) {
70             canvas.drawText("Davey!", 0, mTexty, mPaint);
71             SystemClock.sleep(DAVEY_TIME_MS);
72             mCauseDavey = false;
73         } else {
74             canvas.drawText("No Davey", 0, mTexty, mPaint);
75         }
76     }
77 }