Screensaver.java revision c917c8aab554b21f9fc9fb701a0bcfe27f99573e
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
37import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable;
38
39public 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    private String mDateFormat;
46    private String mDateFormatForAccessibility;
47
48    private final Handler mHandler = new Handler();
49
50    private final ScreensaverMoveSaverRunnable mMoveSaverRunnable;
51
52    public Screensaver() {
53        if (DEBUG) Log.d(TAG, "Screensaver allocated");
54        mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler);
55    }
56
57    @Override
58    public void onCreate() {
59        if (DEBUG) Log.d(TAG, "Screensaver created");
60        super.onCreate();
61
62        mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
63        mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year);
64    }
65
66    @Override
67    public void onConfigurationChanged(Configuration newConfig) {
68        if (DEBUG) Log.d(TAG, "Screensaver configuration changed");
69        super.onConfigurationChanged(newConfig);
70        mHandler.removeCallbacks(mMoveSaverRunnable);
71        layoutClockSaver();
72        mHandler.post(mMoveSaverRunnable);
73    }
74
75    @Override
76    public void onAttachedToWindow() {
77        if (DEBUG) Log.d(TAG, "Screensaver attached to window");
78        super.onAttachedToWindow();
79
80        // We want the screen saver to exit upon user interaction.
81        setInteractive(false);
82
83        setFullscreen(true);
84
85        layoutClockSaver();
86
87        mHandler.post(mMoveSaverRunnable);
88    }
89
90    @Override
91    public void onDetachedFromWindow() {
92        if (DEBUG) Log.d(TAG, "Screensaver detached from window");
93        super.onDetachedFromWindow();
94
95        mHandler.removeCallbacks(mMoveSaverRunnable);
96    }
97
98    private void setClockStyle() {
99        Utils.setClockStyle(this, mDigitalClock, mAnalogClock,
100                ScreensaverSettingsActivity.KEY_CLOCK_STYLE);
101        mSaverView = findViewById(R.id.main_clock);
102        boolean dimNightMode = PreferenceManager.getDefaultSharedPreferences(this)
103                .getBoolean(ScreensaverSettingsActivity.KEY_NIGHT_MODE, false);
104        Utils.dimClockView(dimNightMode, mSaverView);
105        setScreenBright(!dimNightMode);
106    }
107
108    private void layoutClockSaver() {
109        setContentView(R.layout.desk_clock_saver);
110        mDigitalClock = findViewById(R.id.digital_clock);
111        mAnalogClock =findViewById(R.id.analog_clock);
112        setClockStyle();
113        mContentView = (View) mSaverView.getParent();
114        mSaverView.setAlpha(0);
115
116        mMoveSaverRunnable.registerViews(mContentView, mSaverView);
117
118        Utils.updateDate(mDateFormat, mDateFormatForAccessibility,mContentView);
119        Utils.refreshAlarm(Screensaver.this, mContentView);
120    }
121}
122