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.dreams.phototable;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 public class BummerView extends TextView {
27     public static final int START = 1;
28     public static final int MOVE = 2;
29 
30     private int mDelay = 10000; // ms
31     private int mAnimTime = 2000; // ms
32     private boolean mAnimate = false;
33 
34     private Handler mHandler = new Handler() {
35         @Override
36         public void handleMessage(Message m) {
37             boolean animate = false;
38             switch (m.what) {
39                 case MOVE:
40                     animate = mAnimate;
41                     // fall through
42                 case START:
43                     final View parent = (View) BummerView.this.getParent();
44                     if (parent == null)
45                         return;
46 
47                     final float framew = parent.getMeasuredWidth();
48                     final float frameh = parent.getMeasuredHeight();
49                     final float textw = getMeasuredWidth();
50                     final float texth = getMeasuredHeight();
51 
52                     final float newx = (float) (Math.random() * (framew - textw));
53                     final float newy = (float) (Math.random() * (frameh - texth));
54                     if (animate) {
55                         animate().x(newx)
56                                 .y(newy)
57                                 .setDuration(mAnimTime)
58                                 .start();
59                     } else {
60                         setX(newx);
61                         setY(newy);
62                     }
63                     setVisibility(View.VISIBLE);
64 
65                     removeMessages(MOVE);
66                     sendEmptyMessageDelayed(MOVE, mDelay);
67                     break;
68             }
69         }
70     };
71 
BummerView(Context context)72     public BummerView(Context context) {
73         super(context);
74     }
75 
BummerView(Context context, AttributeSet attrs)76     public BummerView(Context context, AttributeSet attrs) {
77         this(context, attrs, 0);
78     }
79 
BummerView(Context context, AttributeSet attrs, int flags)80     public BummerView(Context context, AttributeSet attrs, int flags) {
81         super(context, attrs, flags);
82     }
83 
setAnimationParams(boolean animate, int delay, int animTime)84     public void setAnimationParams(boolean animate, int delay, int animTime) {
85         mAnimate = animate;
86         mDelay = delay;
87         mAnimTime = animTime;
88     }
89 
90     @Override
onAttachedToWindow()91     public void onAttachedToWindow() {
92         final View parent = (View) this.getParent();
93         parent.addOnLayoutChangeListener(new OnLayoutChangeListener() {
94             public void onLayoutChange(View v, int left, int top, int right, int bottom,
95                     int oldLeft, int oldTop, int oldRight, int oldBottom) {
96                 if (v == parent && right != oldRight) {
97                     mHandler.removeMessages(MOVE);
98                     mHandler.sendEmptyMessage(START);
99                 }
100             }
101         });
102 
103         mHandler.sendEmptyMessage(START);
104     }
105 
106 }
107