Debian Codegraph @

templates/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??>
19 import ${applicationPackage}.R;
20 </#if>
21
22 /**
23 * This class is a sample implementation of a DreamService. When activated, a
24 * TextView will repeatedly, move from the left to the right of screen, at a
25 * random y-value.
26 <#if configurable>
27 * The generated {@link ${settingsClassName}} allows
28 * the user to change the text which is displayed.
29 </#if>
30 * <p />
31 * Daydreams are only available on devices running API v17+.
32 */
33 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
34 public class ${className} extends DreamService {
35
36 private static final TimeInterpolator sInterpolator = new LinearInterpolator();
37
38 private final AnimatorListener mAnimListener = new AnimatorListenerAdapter() {
39
40 @Override
41 public void onAnimationEnd(Animator animation) {
42 // Start animation again
43 startTextViewScrollAnimation();
44 }
45
46 };
47
48 private final Random mRandom = new Random();
49 private final Point mPointSize = new Point();
50
51 private TextView mDreamTextView;
52 private ViewPropertyAnimator mAnimator;
53
54 @Override
55 public void onAttachedToWindow() {
56 super.onAttachedToWindow();
57
58 // Exit dream upon user touch?
59 <#if isInteractive>
60 setInteractive(true);
61 <#else>
62 setInteractive(false);
63 </#if>
64
65 // Hide system UI?
66 <#if isFullscreen>
67 setFullscreen(true);
68 <#else>
69 setFullscreen(false);
70 </#if>
71
72 // Keep screen at full brightness?
73 <#if isScreenBright>
74 setScreenBright(true);
75 <#else>
76 setScreenBright(false);
77 </#if>
78
79 // Set the content view, just like you would with an Activity.
80 setContentView(R.layout.${class_name});
81
82 mDreamTextView = (TextView) findViewById(R.id.dream_text);
83 mDreamTextView.setText(getTextFromPreferences());
84 }
85
86 @Override
87 public void onDreamingStarted() {
88 super.onDreamingStarted();
89
90 // TODO: Begin animations or other behaviors here.
91
92 startTextViewScrollAnimation();
93 }
94
95 @Override
96 public void onDreamingStopped() {
97 super.onDreamingStopped();
98
99 // TODO: Stop anything that was started in onDreamingStarted()
100
101 mAnimator.cancel();
102 }
103
104 @Override
105 public void onDetachedFromWindow() {
106 super.onDetachedFromWindow();
107
108 // TODO: Dismantle resources
109 // (for example, detach from handlers and listeners).
110 }
111
112 private String getTextFromPreferences() {
113 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
114 return prefs.getString(getString(R.string.pref_dream_text_key),
115 getString(R.string.pref_dream_text_default));
116 }
117
118 private void startTextViewScrollAnimation() {
119 // Refresh Size of Window
120 getWindowManager().getDefaultDisplay().getSize(mPointSize);
121
122 final int windowWidth = mPointSize.x;
123 final int windowHeight = mPointSize.y;
124
125 // Move TextView so it's moved all the way to the left
126 mDreamTextView.setTranslationX(-mDreamTextView.getWidth());
127
128 // Move TextView to random y value
129 final int yRange = windowHeight - mDreamTextView.getHeight();
130 mDreamTextView.setTranslationY(mRandom.nextInt(yRange));
131
132 // Create an Animator and keep a reference to it
133 mAnimator = mDreamTextView.animate().translationX(windowWidth)
134 .setDuration(3000)
135 .setStartDelay(500)
136 .setListener(mAnimListener)
137 .setInterpolator(sInterpolator);
138
139 // Start the animation
140 mAnimator.start();
141 }
142
143 }