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>
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
25 * The generated {@link BlahDreamServiceSettingsActivity} allows
26 * the user to change the text which is displayed.
29 * Daydreams are only available on devices running API v17+.
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() {
39 public void onAnimationEnd(Animator animation) {
40 // Start animation again
41 startTextViewScrollAnimation();
46 private final Random mRandom = new Random();
47 private final Point mPointSize = new Point();
49 private TextView mDreamTextView;
50 private ViewPropertyAnimator mAnimator;
53 public void onAttachedToWindow() {
54 super.onAttachedToWindow();
56 // Exit dream upon user touch?
60 setInteractive(false);
70 // Keep screen at full brightness?
72 setScreenBright(true);
74 setScreenBright(false);
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());
85 public void onDreamingStarted() {
86 super.onDreamingStarted();
88 // TODO: Begin animations or other behaviors here.
90 startTextViewScrollAnimation();
94 public void onDreamingStopped() {
95 super.onDreamingStopped();
97 // TODO: Stop anything that was started in onDreamingStarted()
103 public void onDetachedFromWindow() {
104 super.onDetachedFromWindow();
106 // TODO: Dismantle resources
107 // (for example, detach from handlers and listeners).
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));
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)
134 .setListener(mAnimListener)
135 .setInterpolator(sInterpolator);
137 // Start the animation