Screensaver.java revision 1b3cc5b1442bae15439aef3c7d1929ebba417561
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.deskclock; 18 19import android.animation.Animator; 20import android.animation.AnimatorSet; 21import android.animation.ObjectAnimator; 22import android.animation.TimeInterpolator; 23import android.content.SharedPreferences; 24import android.content.res.Configuration; 25import android.graphics.Color; 26import android.graphics.Paint; 27import android.graphics.PorterDuff; 28import android.graphics.PorterDuffColorFilter; 29import android.os.Handler; 30import android.preference.PreferenceManager; 31import android.service.dreams.DreamService; 32import android.util.Log; 33import android.view.View; 34import android.view.animation.AccelerateInterpolator; 35import android.view.animation.DecelerateInterpolator; 36 37public class Screensaver extends DreamService { 38 static final boolean DEBUG = false; 39 static final String TAG = "DeskClock/Screensaver"; 40 41 static final long MOVE_DELAY = 60000; // DeskClock.SCREEN_SAVER_MOVE_DELAY; 42 static final long SLIDE_TIME = 10000; 43 static final long FADE_TIME = 3000; 44 45 static final boolean SLIDE = false; 46 47 private View mContentView, mSaverView; 48 private View mAnalogClock, mDigitalClock; 49 50 private static TimeInterpolator mSlowStartWithBrakes = 51 new TimeInterpolator() { 52 @Override 53 public float getInterpolation(float x) { 54 return (float)(Math.cos((Math.pow(x,3) + 1) * Math.PI) / 2.0f) + 0.5f; 55 } 56 }; 57 58 private final Handler mHandler = new Handler(); 59 60 61 private final Runnable mMoveSaverRunnable = new Runnable() { 62 @Override 63 public void run() { 64 long delay = MOVE_DELAY; 65 66 if (DEBUG) Log.d(TAG, 67 String.format("mContentView=(%d x %d) container=(%d x %d)", 68 mContentView.getWidth(), mContentView.getHeight(), 69 mSaverView.getWidth(), mSaverView.getHeight() 70 )); 71 final float xrange = mContentView.getWidth() - mSaverView.getWidth(); 72 final float yrange = mContentView.getHeight() - mSaverView.getHeight(); 73 74 if (xrange == 0 && yrange == 0) { 75 delay = 500; // back in a split second 76 } else { 77 final int nextx = (int) (Math.random() * xrange); 78 final int nexty = (int) (Math.random() * yrange); 79 80 if (mSaverView.getAlpha() == 0f) { 81 // jump right there 82 mSaverView.setX(nextx); 83 mSaverView.setY(nexty); 84 ObjectAnimator.ofFloat(mSaverView, "alpha", 0f, 1f) 85 .setDuration(FADE_TIME) 86 .start(); 87 } else { 88 AnimatorSet s = new AnimatorSet(); 89 Animator xMove = ObjectAnimator.ofFloat(mSaverView, 90 "x", mSaverView.getX(), nextx); 91 Animator yMove = ObjectAnimator.ofFloat(mSaverView, 92 "y", mSaverView.getY(), nexty); 93 94 Animator xShrink = ObjectAnimator.ofFloat(mSaverView, "scaleX", 1f, 0.85f); 95 Animator xGrow = ObjectAnimator.ofFloat(mSaverView, "scaleX", 0.85f, 1f); 96 97 Animator yShrink = ObjectAnimator.ofFloat(mSaverView, "scaleY", 1f, 0.85f); 98 Animator yGrow = ObjectAnimator.ofFloat(mSaverView, "scaleY", 0.85f, 1f); 99 AnimatorSet shrink = new AnimatorSet(); shrink.play(xShrink).with(yShrink); 100 AnimatorSet grow = new AnimatorSet(); grow.play(xGrow).with(yGrow); 101 102 Animator fadeout = ObjectAnimator.ofFloat(mSaverView, "alpha", 1f, 0f); 103 Animator fadein = ObjectAnimator.ofFloat(mSaverView, "alpha", 0f, 1f); 104 105 106 if (SLIDE) { 107 s.play(xMove).with(yMove); 108 s.setDuration(SLIDE_TIME); 109 110 s.play(shrink.setDuration(SLIDE_TIME/2)); 111 s.play(grow.setDuration(SLIDE_TIME/2)).after(shrink); 112 s.setInterpolator(mSlowStartWithBrakes); 113 } else { 114 AccelerateInterpolator accel = new AccelerateInterpolator(); 115 DecelerateInterpolator decel = new DecelerateInterpolator(); 116 117 shrink.setDuration(FADE_TIME).setInterpolator(accel); 118 fadeout.setDuration(FADE_TIME).setInterpolator(accel); 119 grow.setDuration(FADE_TIME).setInterpolator(decel); 120 fadein.setDuration(FADE_TIME).setInterpolator(decel); 121 s.play(shrink); 122 s.play(fadeout); 123 s.play(xMove.setDuration(0)).after(FADE_TIME); 124 s.play(yMove.setDuration(0)).after(FADE_TIME); 125 s.play(fadein).after(FADE_TIME); 126 s.play(grow).after(FADE_TIME); 127 } 128 s.start(); 129 } 130 131 long now = System.currentTimeMillis(); 132 long adjust = (now % 60000); 133 delay = delay 134 + (MOVE_DELAY - adjust) // minute aligned 135 - (SLIDE ? 0 : FADE_TIME) // start moving before the fade 136 ; 137 if (DEBUG) Log.d(TAG, 138 "will move again in " + delay + " now=" + now + " adjusted by " + adjust); 139 } 140 141 mHandler.removeCallbacks(this); 142 mHandler.postDelayed(this, delay); 143 } 144 }; 145 146 public Screensaver() { 147 if (DEBUG) Log.d(TAG, "Screensaver allocated"); 148 } 149 150 @Override 151 public void onCreate() { 152 if (DEBUG) Log.d(TAG, "Screensaver created"); 153 super.onCreate(); 154 } 155 156 @Override 157 public void onConfigurationChanged(Configuration newConfig) { 158 if (DEBUG) Log.d(TAG, "Screensaver configuration changed"); 159 super.onConfigurationChanged(newConfig); 160 mHandler.removeCallbacks(mMoveSaverRunnable); 161 layoutClockSaver(); 162 mHandler.post(mMoveSaverRunnable); 163 } 164 165 @Override 166 public void onAttachedToWindow() { 167 if (DEBUG) Log.d(TAG, "Screensaver attached to window"); 168 super.onAttachedToWindow(); 169 170 // We want the screen saver to exit upon user interaction. 171 setInteractive(false); 172 173 setFullscreen(true); 174 175 layoutClockSaver(); 176 177 mHandler.post(mMoveSaverRunnable); 178 } 179 180 @Override 181 public void onDetachedFromWindow() { 182 if (DEBUG) Log.d(TAG, "Screensaver detached from window"); 183 super.onDetachedFromWindow(); 184 185 mHandler.removeCallbacks(mMoveSaverRunnable); 186 } 187 188 private void setClockStyle() { 189 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); 190 String style = sharedPref.getString(ScreensaverSettingsActivity.KEY_CLOCK_STYLE, "analog"); 191 if (style.equals("analog")) { 192 mDigitalClock.setVisibility(View.GONE); 193 mAnalogClock.setVisibility(View.VISIBLE); 194 mSaverView = mAnalogClock; 195 } else { 196 mDigitalClock.setVisibility(View.VISIBLE); 197 mAnalogClock.setVisibility(View.GONE); 198 mSaverView = mDigitalClock; 199 } 200 boolean night = sharedPref.getBoolean(ScreensaverSettingsActivity.KEY_NIGHT_MODE, false); 201 202 Paint paint = new Paint(); 203 paint.setColor(Color.WHITE); 204 paint.setColorFilter(new PorterDuffColorFilter( 205 (night ? 0x60FFFFFF : 0xC0FFFFFF), 206 PorterDuff.Mode.MULTIPLY)); 207 mSaverView.setLayerType(View.LAYER_TYPE_HARDWARE, paint); 208 209 setScreenBright(!night); 210 } 211 212 private void layoutClockSaver() { 213 setContentView(R.layout.desk_clock_saver); 214 mDigitalClock = findViewById(R.id.digital_clock); 215 mAnalogClock =findViewById(R.id.analog_clock); 216 setClockStyle(); 217 mContentView = (View) mSaverView.getParent(); 218 mSaverView.setAlpha(0); 219 } 220} 221