Debian Codegraph @

templates/eclipse/other/Daydream/root/src/app_package/DreamService.java.ftl

raw
1 package ${packageName};
2
3 import java.util.Random;
4
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>
19
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 {
33
34 private static final TimeInterpolator sInterpolator = new LinearInterpolator();
35
36 private final AnimatorListener mAnimListener = new AnimatorListenerAdapter() {
37
38 @Override
39 public void onAnimationEnd(Animator animation) {
40 // Start animation again
41 startTextViewScrollAnimation();
42 }
43
44 };
45
46 private final Random mRandom = new Random();
47 private final Point mPointSize = new Point();
48
49 private TextView mDreamTextView;
50 private ViewPropertyAnimator mAnimator;
51
52 @Override
53 public void onAttachedToWindow() {
54 super.onAttachedToWindow();
55
56 // Exit dream upon user touch?
57 <#if isInteractive>
58 setInteractive(true);
59 <#else>
60 setInteractive(false);
61 </#if>
62
63 // Hide system UI?
64 <#if isFullscreen>
65 setFullscreen(true);
66 <#else>
67 setFullscreen(false);
68 </#if>
69
70 // Keep screen at full brightness?
71 <#if isScreenBright>
72 setScreenBright(true);
73 <#else>
74 setScreenBright(false);
75 </#if>
76
77 // Set the content view, just like you would with an Activity.
78 setContentView(R.layout.${class_name});
79
80 mDreamTextView = (TextView) findViewById(R.id.dream_text);
81 mDreamTextView.setText(getTextFromPreferences());
82 }
83
84 @Override
85 public void onDreamingStarted() {
86 super.onDreamingStarted();
87
88 // TODO: Begin animations or other behaviors here.
89
90 startTextViewScrollAnimation();
91 }
92
93 @Override
94 public void onDreamingStopped() {
95 super.onDreamingStopped();
96
97 // TODO: Stop anything that was started in onDreamingStarted()
98
99 mAnimator.cancel();
100 }
101
102 @Override
103 public void onDetachedFromWindow() {
104 super.onDetachedFromWindow();
105
106 // TODO: Dismantle resources
107 // (for example, detach from handlers and listeners).
108 }
109
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 }
115
116 private void startTextViewScrollAnimation() {
117 // Refresh Size of Window
118 getWindowManager().getDefaultDisplay().getSize(mPointSize);
119
120 final int windowWidth = mPointSize.x;
121 final int windowHeight = mPointSize.y;
122
123 // Move TextView so it's moved all the way to the left
124 mDreamTextView.setTranslationX(-mDreamTextView.getWidth());
125
126 // Move TextView to random y value
127 final int yRange = windowHeight - mDreamTextView.getHeight();
128 mDreamTextView.setTranslationY(mRandom.nextInt(yRange));
129
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);
136
137 // Start the animation
138 mAnimator.start();
139 }
140
141 }