Home | History | Annotate | Download | only in deskclock
      1 /*
      2  * Copyright (C) 2012 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.app.Activity;
     20 import android.app.AlarmManager;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.IntentFilter;
     25 import android.content.res.Configuration;
     26 import android.os.BatteryManager;
     27 import android.os.Handler;
     28 import android.util.Log;
     29 import android.view.View;
     30 import android.view.Window;
     31 import android.view.WindowManager;
     32 import android.widget.TextClock;
     33 
     34 import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable;
     35 
     36 public class ScreensaverActivity extends Activity {
     37     static final boolean DEBUG = false;
     38     static final String TAG = "DeskClock/ScreensaverActivity";
     39 
     40     // This value must match android:defaultValue of
     41     // android:key="screensaver_clock_style" in dream_settings.xml
     42     static final String DEFAULT_CLOCK_STYLE = "digital";
     43 
     44     private View mContentView, mSaverView;
     45     private View mAnalogClock, mDigitalClock;
     46 
     47     private final Handler mHandler = new Handler();
     48     private final ScreensaverMoveSaverRunnable mMoveSaverRunnable;
     49     private String mDateFormat;
     50     private String mDateFormatForAccessibility;
     51     private String mClockStyle;
     52     private boolean mPluggedIn = true;
     53     private final int mFlags = (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
     54             | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
     55             | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
     56             | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     57 
     58     private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
     59         @Override
     60         public void onReceive(Context context, Intent intent) {
     61             if (DEBUG) {
     62                 Log.v(TAG, "ScreensaverActivity onReceive, action: " + intent.getAction());
     63             }
     64 
     65             boolean changed = intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
     66                     || intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED);
     67             if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
     68                 mPluggedIn = true;
     69                 setWakeLock();
     70             } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
     71                 mPluggedIn = false;
     72                 setWakeLock();
     73             } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
     74                 finish();
     75             }
     76 
     77             if (changed) {
     78                 Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView);
     79                 Utils.refreshAlarm(ScreensaverActivity.this, mContentView);
     80                 Utils.setMidnightUpdater(mHandler, mMidnightUpdater);
     81             }
     82 
     83             if (intent.getAction().equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) {
     84                 Utils.refreshAlarm(ScreensaverActivity.this, mContentView);
     85             }
     86         }
     87     };
     88 
     89     // Thread that runs every midnight and refreshes the date.
     90     private final Runnable mMidnightUpdater = new Runnable() {
     91         @Override
     92         public void run() {
     93             Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView);
     94             Utils.setMidnightUpdater(mHandler, mMidnightUpdater);
     95         }
     96     };
     97 
     98     public ScreensaverActivity() {
     99         if (DEBUG) Log.d(TAG, "Screensaver allocated");
    100         mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler);
    101     }
    102 
    103     @Override
    104     public void onStart() {
    105         super.onStart();
    106         IntentFilter filter = new IntentFilter();
    107         filter.addAction(Intent.ACTION_POWER_CONNECTED);
    108         filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
    109         filter.addAction(Intent.ACTION_USER_PRESENT);
    110         filter.addAction(Intent.ACTION_TIME_CHANGED);
    111         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
    112         registerReceiver(mIntentReceiver, filter);
    113     }
    114 
    115     @Override
    116     public void onResume() {
    117         super.onResume();
    118         Intent chargingIntent =
    119                 registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    120         int plugged = chargingIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    121         mPluggedIn = plugged == BatteryManager.BATTERY_PLUGGED_AC
    122                 || plugged == BatteryManager.BATTERY_PLUGGED_USB
    123                 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
    124 
    125         mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
    126         mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year);
    127 
    128         setWakeLock();
    129         layoutClockSaver();
    130         mHandler.post(mMoveSaverRunnable);
    131 
    132         Utils.setMidnightUpdater(mHandler, mMidnightUpdater);
    133     }
    134 
    135     @Override
    136     public void onPause() {
    137         mHandler.removeCallbacks(mMoveSaverRunnable);
    138         Utils.cancelMidnightUpdater(mHandler, mMidnightUpdater);
    139         finish();
    140         super.onPause();
    141     }
    142 
    143     @Override
    144     public void onStop() {
    145         unregisterReceiver(mIntentReceiver);
    146         super.onStop();
    147    }
    148 
    149     @Override
    150     public void onConfigurationChanged(Configuration newConfig) {
    151         if (DEBUG) Log.d(TAG, "Screensaver configuration changed");
    152         super.onConfigurationChanged(newConfig);
    153         mHandler.removeCallbacks(mMoveSaverRunnable);
    154         layoutClockSaver();
    155         mHandler.postDelayed(mMoveSaverRunnable, 250);
    156     }
    157 
    158     @Override
    159     public void onUserInteraction() {
    160         // We want the screen saver to exit upon user interaction.
    161         finish();
    162     }
    163 
    164     private void setWakeLock() {
    165         Window win = getWindow();
    166         WindowManager.LayoutParams winParams = win.getAttributes();
    167         winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    168         if (mPluggedIn)
    169             winParams.flags |= mFlags;
    170         else
    171             winParams.flags &= (~mFlags);
    172         win.setAttributes(winParams);
    173     }
    174 
    175     private void setClockStyle() {
    176         Utils.setClockStyle(this, mDigitalClock, mAnalogClock,
    177                 SettingsActivity.KEY_CLOCK_STYLE);
    178         mSaverView = findViewById(R.id.main_clock);
    179         mClockStyle = (mSaverView == mDigitalClock ?
    180                 Utils.CLOCK_TYPE_DIGITAL : Utils.CLOCK_TYPE_ANALOG);
    181         Utils.dimClockView(true, mSaverView);
    182     }
    183 
    184     private void layoutClockSaver() {
    185         setContentView(R.layout.desk_clock_saver);
    186         mDigitalClock = findViewById(R.id.digital_clock);
    187         mAnalogClock = findViewById(R.id.analog_clock);
    188         setClockStyle();
    189         Utils.setTimeFormat((TextClock)mDigitalClock,
    190             (int)getResources().getDimension(R.dimen.main_ampm_font_size));
    191 
    192         mContentView = (View) mSaverView.getParent();
    193         mContentView.forceLayout();
    194         mSaverView.forceLayout();
    195         mSaverView.setAlpha(0);
    196 
    197         mMoveSaverRunnable.registerViews(mContentView, mSaverView);
    198 
    199         mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
    200                 | View.SYSTEM_UI_FLAG_FULLSCREEN
    201                 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    202 
    203         Utils.updateDate(mDateFormat, mDateFormatForAccessibility,mContentView);
    204         Utils.refreshAlarm(ScreensaverActivity.this, mContentView);
    205     }
    206 
    207 }
    208