• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.fakeoemfeatures;
18 
19 import java.util.Random;
20 
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.Paint;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.view.View;
27 
28 /**
29  * Fake view to emulate stuff an OEM may want to do.
30  */
31 public class FakeView extends View {
32     static final long TICK_DELAY = 30*1000; // 30 seconds
33     static final int MSG_TICK = 1;
34 
35     final Handler mHandler = new Handler() {
36         @Override public void handleMessage(Message msg) {
37             switch (msg.what) {
38                 case MSG_TICK:
39                     invalidate();
40                     sendEmptyMessageDelayed(MSG_TICK, TICK_DELAY);
41                     break;
42                 default:
43                     super.handleMessage(msg);
44                     break;
45             }
46         }
47     };
48 
49     final Paint mPaint = new Paint();
50     final Random mRandom = new Random();
51 
FakeView(Context context)52     public FakeView(Context context) {
53         super(context);
54     }
55 
56     @Override
onAttachedToWindow()57     protected void onAttachedToWindow() {
58         super.onAttachedToWindow();
59         mHandler.sendEmptyMessageDelayed(MSG_TICK, TICK_DELAY);
60     }
61 
62     @Override
onDetachedFromWindow()63     protected void onDetachedFromWindow() {
64         super.onDetachedFromWindow();
65         mHandler.removeMessages(MSG_TICK);
66     }
67 
68     @Override
onDraw(Canvas canvas)69     protected void onDraw(Canvas canvas) {
70         super.onDraw(canvas);
71         canvas.drawColor(0xff000000);
72         mPaint.setTextSize(mRandom.nextInt(40) + 10);
73         mPaint.setColor(0xff000000 + mRandom.nextInt(0x1000000));
74         int x = mRandom.nextInt(getWidth()) - (getWidth()/2);
75         int y = mRandom.nextInt(getHeight());
76         canvas.drawText("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
77                 x, y, mPaint);
78     }
79 }
80