Roll NDK to pick std::deque patch.
[android_tools.git] / sdk / tools / templates / other / Daydream / root / src / app_package / DreamService.java.ftl
blob0dc51ca362c63d6406850e63ab5d843bd0ca077e
1 package ${packageName};
3 import java.util.Random;
5 import android.animation.Animator;
6 import android.animation.Animator.AnimatorListener;
7 import android.animation.AnimatorListenerAdapter;
8 import android.animation.TimeInterpolator;
9 import android.annotation.TargetApi;
10 import android.content.SharedPreferences;
11 import android.graphics.Point;
12 import android.os.Build;
13 import android.preference.PreferenceManager;
14 import android.service.dreams.DreamService;
15 import android.view.ViewPropertyAnimator;
16 import android.view.animation.LinearInterpolator;
17 import android.widget.TextView;
18 <#if applicationPackage??>import ${applicationPackage}.R;</#if>
20 /**
21  * This class is a sample implementation of a DreamService. When activated, a
22  * TextView will repeatedly, move from the left to the right of screen, at a
23  * random y-value.
24 <#if configurable>
25  * The generated {@link BlahDreamServiceSettingsActivity} allows
26  * the user to change the text which is displayed.
27 </#if>
28  * <p />
29  * Daydreams are only available on devices running API v17+.
30  */
31 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
32 public class ${className} extends DreamService {
34     private static final TimeInterpolator sInterpolator = new LinearInterpolator();
36     private final AnimatorListener mAnimListener = new AnimatorListenerAdapter() {
38         @Override
39         public void onAnimationEnd(Animator animation) {
40             // Start animation again
41             startTextViewScrollAnimation();
42         }
44     };
46     private final Random mRandom = new Random();
47     private final Point mPointSize = new Point();
49     private TextView mDreamTextView;
50     private ViewPropertyAnimator mAnimator;
52     @Override
53     public void onAttachedToWindow() {
54         super.onAttachedToWindow();
56         // Exit dream upon user touch?
57 <#if isInteractive>
58         setInteractive(true);
59 <#else>
60         setInteractive(false);
61 </#if>
63         // Hide system UI?
64 <#if isFullscreen>
65         setFullscreen(true);
66 <#else>
67         setFullscreen(false);
68 </#if>
70         // Keep screen at full brightness?
71 <#if isScreenBright>
72         setScreenBright(true);
73 <#else>
74         setScreenBright(false);
75 </#if>
77         // Set the content view, just like you would with an Activity.
78         setContentView(R.layout.${class_name});
80         mDreamTextView = (TextView) findViewById(R.id.dream_text);
81         mDreamTextView.setText(getTextFromPreferences());
82     }
84     @Override
85     public void onDreamingStarted() {
86         super.onDreamingStarted();
88         // TODO: Begin animations or other behaviors here.
90         startTextViewScrollAnimation();
91     }
93     @Override
94     public void onDreamingStopped() {
95         super.onDreamingStopped();
97         // TODO: Stop anything that was started in onDreamingStarted()
99         mAnimator.cancel();
100     }
102     @Override
103     public void onDetachedFromWindow() {
104         super.onDetachedFromWindow();
106         // TODO: Dismantle resources
107         // (for example, detach from handlers and listeners).
108     }
110     private String getTextFromPreferences() {
111         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
112         return prefs.getString(getString(R.string.pref_dream_text_key),
113                 getString(R.string.pref_dream_text_default));
114     }
116     private void startTextViewScrollAnimation() {
117         // Refresh Size of Window
118         getWindowManager().getDefaultDisplay().getSize(mPointSize);
120         final int windowWidth = mPointSize.x;
121         final int windowHeight = mPointSize.y;
123         // Move TextView so it's moved all the way to the left
124         mDreamTextView.setTranslationX(-mDreamTextView.getWidth());
126         // Move TextView to random y value
127         final int yRange = windowHeight - mDreamTextView.getHeight();
128         mDreamTextView.setTranslationY(mRandom.nextInt(yRange));
130         // Create an Animator and keep a reference to it
131         mAnimator = mDreamTextView.animate().translationX(windowWidth)
132             .setDuration(3000)
133             .setStartDelay(500)
134             .setListener(mAnimListener)
135             .setInterpolator(sInterpolator);
137         // Start the animation
138         mAnimator.start();
139     }