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