Home | History | Annotate | Download | only in deskclock
      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 
     17 package com.android.deskclock;
     18 
     19 import android.animation.Animator;
     20 import android.animation.AnimatorSet;
     21 import android.animation.ObjectAnimator;
     22 import android.animation.TimeInterpolator;
     23 import android.content.SharedPreferences;
     24 import android.content.res.Configuration;
     25 import android.graphics.Color;
     26 import android.graphics.Paint;
     27 import android.graphics.PorterDuff;
     28 import android.graphics.PorterDuffColorFilter;
     29 import android.os.Handler;
     30 import android.preference.PreferenceManager;
     31 import android.service.dreams.DreamService;
     32 import android.util.Log;
     33 import android.view.View;
     34 import android.view.animation.AccelerateInterpolator;
     35 import android.view.animation.DecelerateInterpolator;
     36 
     37 import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable;
     38 
     39 public class Screensaver extends DreamService {
     40     static final boolean DEBUG = false;
     41     static final String TAG = "DeskClock/Screensaver";
     42 
     43     private View mContentView, mSaverView;
     44     private View mAnalogClock, mDigitalClock;
     45 
     46     private final Handler mHandler = new Handler();
     47 
     48     private final ScreensaverMoveSaverRunnable mMoveSaverRunnable;
     49 
     50     public Screensaver() {
     51         if (DEBUG) Log.d(TAG, "Screensaver allocated");
     52         mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler);
     53     }
     54 
     55     @Override
     56     public void onCreate() {
     57         if (DEBUG) Log.d(TAG, "Screensaver created");
     58         super.onCreate();
     59     }
     60 
     61     @Override
     62     public void onConfigurationChanged(Configuration newConfig) {
     63         if (DEBUG) Log.d(TAG, "Screensaver configuration changed");
     64         super.onConfigurationChanged(newConfig);
     65         mHandler.removeCallbacks(mMoveSaverRunnable);
     66         layoutClockSaver();
     67         mHandler.post(mMoveSaverRunnable);
     68     }
     69 
     70     @Override
     71     public void onAttachedToWindow() {
     72         if (DEBUG) Log.d(TAG, "Screensaver attached to window");
     73         super.onAttachedToWindow();
     74 
     75         // We want the screen saver to exit upon user interaction.
     76         setInteractive(false);
     77 
     78         setFullscreen(true);
     79 
     80         layoutClockSaver();
     81 
     82         mHandler.post(mMoveSaverRunnable);
     83     }
     84 
     85     @Override
     86     public void onDetachedFromWindow() {
     87         if (DEBUG) Log.d(TAG, "Screensaver detached from window");
     88         super.onDetachedFromWindow();
     89 
     90         mHandler.removeCallbacks(mMoveSaverRunnable);
     91     }
     92 
     93     private void setClockStyle() {
     94         Utils.setClockStyle(this, mDigitalClock, mAnalogClock,
     95                 ScreensaverSettingsActivity.KEY_CLOCK_STYLE);
     96         mSaverView = findViewById(R.id.main_clock);
     97         boolean dimNightMode = PreferenceManager.getDefaultSharedPreferences(this)
     98                 .getBoolean(ScreensaverSettingsActivity.KEY_NIGHT_MODE, false);
     99         Utils.dimClockView(dimNightMode, mSaverView);
    100         setScreenBright(!dimNightMode);
    101     }
    102 
    103     private void layoutClockSaver() {
    104         setContentView(R.layout.desk_clock_saver);
    105         mDigitalClock = findViewById(R.id.digital_clock);
    106         mAnalogClock =findViewById(R.id.analog_clock);
    107         setClockStyle();
    108         mContentView = (View) mSaverView.getParent();
    109         mSaverView.setAlpha(0);
    110 
    111         mMoveSaverRunnable.registerViews(mContentView, mSaverView);
    112     }
    113 }
    114