1 /*
2  * Copyright (C) 2015 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.ObjectAnimator;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.graphics.drawable.ColorDrawable;
25 import android.os.Bundle;
26 import android.support.v7.app.AppCompatActivity;
27 
28 /**
29  * Base activity class that changes with window's background color dynamically based on the
30  * current hour.
31  */
32 public class BaseActivity extends AppCompatActivity {
33 
34     /**
35      * Key used to save/restore the current background color from the saved instance state.
36      */
37     private static final String KEY_BACKGROUND_COLOR = "background_color";
38 
39     /**
40      * Duration in millis to animate changes to the background color.
41      */
42     private static final long BACKGROUND_COLOR_ANIMATION_DURATION = 3000L;
43 
44     /**
45      * {@link BroadcastReceiver} to update the background color whenever the system time changes.
46      */
47     private BroadcastReceiver mOnTimeChangedReceiver;
48 
49     /**
50      * {@link ColorDrawable} used to draw the window's background.
51      */
52     private ColorDrawable mBackground;
53 
54     @Override
onCreate(Bundle savedInstanceState)55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57 
58         final int currentColor = Utils.getCurrentHourColor();
59         final int backgroundColor = savedInstanceState == null ? currentColor
60                 : savedInstanceState.getInt(KEY_BACKGROUND_COLOR, currentColor);
61         setBackgroundColor(backgroundColor, false /* animate */);
62     }
63 
64     @Override
onResume()65     protected void onResume() {
66         super.onResume();
67 
68         // Register mOnTimeChangedReceiver to update current background color periodically.
69         if (mOnTimeChangedReceiver == null) {
70             final IntentFilter filter = new IntentFilter();
71             filter.addAction(Intent.ACTION_TIME_TICK);
72             filter.addAction(Intent.ACTION_TIME_CHANGED);
73             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
74             registerReceiver(mOnTimeChangedReceiver = new BroadcastReceiver() {
75                 @Override
76                 public void onReceive(Context context, Intent intent) {
77                     setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
78                 }
79             }, filter);
80         }
81 
82         // Ensure the background color is up-to-date.
83         setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
84     }
85 
86     @Override
onPause()87     protected void onPause() {
88         super.onPause();
89 
90         // Stop updating the background color when not active.
91         if (mOnTimeChangedReceiver != null) {
92             unregisterReceiver(mOnTimeChangedReceiver);
93             mOnTimeChangedReceiver = null;
94         }
95     }
96 
97     @Override
onSaveInstanceState(Bundle outState)98     protected void onSaveInstanceState(Bundle outState) {
99         super.onSaveInstanceState(outState);
100 
101         // Save the background color so we can animate the change when the activity is restored.
102         if (mBackground != null) {
103             outState.putInt(KEY_BACKGROUND_COLOR, mBackground.getColor());
104         }
105     }
106 
107     /**
108      * Sets the current background color to the provided value and animates the change if desired.
109      *
110      * @param color the ARGB value to set as the current background color
111      * @param animate {@code true} if the change should be animated
112      */
setBackgroundColor(int color, boolean animate)113     protected void setBackgroundColor(int color, boolean animate) {
114         if (mBackground == null) {
115             mBackground = new ColorDrawable(color);
116             getWindow().setBackgroundDrawable(mBackground);
117         }
118 
119         if (mBackground.getColor() != color) {
120             if (animate) {
121                 ObjectAnimator.ofObject(mBackground, "color", AnimatorUtils.ARGB_EVALUATOR, color)
122                         .setDuration(BACKGROUND_COLOR_ANIMATION_DURATION)
123                         .start();
124             } else {
125                 mBackground.setColor(color);
126             }
127         }
128     }
129 }
130