• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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.systemui.statusbar.policy;
18 
19 import libcore.icu.LocaleData;
20 
21 import android.app.ActivityManager;
22 import android.app.StatusBarManager;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.res.Configuration;
28 import android.content.res.TypedArray;
29 import android.graphics.Rect;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.SystemClock;
33 import android.os.UserHandle;
34 import android.text.Spannable;
35 import android.text.SpannableStringBuilder;
36 import android.text.format.DateFormat;
37 import android.text.style.CharacterStyle;
38 import android.text.style.RelativeSizeSpan;
39 import android.util.AttributeSet;
40 import android.view.Display;
41 import android.view.View;
42 import android.widget.TextView;
43 
44 import com.android.systemui.DemoMode;
45 import com.android.systemui.Dependency;
46 import com.android.systemui.FontSizeUtils;
47 import com.android.systemui.R;
48 import com.android.systemui.SysUiServiceProvider;
49 import com.android.systemui.statusbar.CommandQueue;
50 import com.android.systemui.statusbar.phone.StatusBarIconController;
51 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
52 import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
53 import com.android.systemui.tuner.TunerService;
54 import com.android.systemui.tuner.TunerService.Tunable;
55 
56 import java.text.SimpleDateFormat;
57 import java.util.Calendar;
58 import java.util.Locale;
59 import java.util.TimeZone;
60 
61 /**
62  * Digital clock for the status bar.
63  */
64 public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.Callbacks,
65         DarkReceiver, ConfigurationListener {
66 
67     public static final String CLOCK_SECONDS = "clock_seconds";
68 
69     private boolean mClockVisibleByPolicy = true;
70     private boolean mClockVisibleByUser = true;
71 
72     private boolean mAttached;
73     private Calendar mCalendar;
74     private String mClockFormatString;
75     private SimpleDateFormat mClockFormat;
76     private SimpleDateFormat mContentDescriptionFormat;
77     private Locale mLocale;
78 
79     private static final int AM_PM_STYLE_NORMAL  = 0;
80     private static final int AM_PM_STYLE_SMALL   = 1;
81     private static final int AM_PM_STYLE_GONE    = 2;
82 
83     private final int mAmPmStyle;
84     private final boolean mShowDark;
85     private boolean mShowSeconds;
86     private Handler mSecondsHandler;
87 
Clock(Context context)88     public Clock(Context context) {
89         this(context, null);
90     }
91 
Clock(Context context, AttributeSet attrs)92     public Clock(Context context, AttributeSet attrs) {
93         this(context, attrs, 0);
94     }
95 
Clock(Context context, AttributeSet attrs, int defStyle)96     public Clock(Context context, AttributeSet attrs, int defStyle) {
97         super(context, attrs, defStyle);
98         TypedArray a = context.getTheme().obtainStyledAttributes(
99                 attrs,
100                 R.styleable.Clock,
101                 0, 0);
102         try {
103             mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE);
104             mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);
105         } finally {
106             a.recycle();
107         }
108     }
109 
110     @Override
onAttachedToWindow()111     protected void onAttachedToWindow() {
112         super.onAttachedToWindow();
113 
114         if (!mAttached) {
115             mAttached = true;
116             IntentFilter filter = new IntentFilter();
117 
118             filter.addAction(Intent.ACTION_TIME_TICK);
119             filter.addAction(Intent.ACTION_TIME_CHANGED);
120             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
121             filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
122             filter.addAction(Intent.ACTION_USER_SWITCHED);
123 
124             getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
125                     null, Dependency.get(Dependency.TIME_TICK_HANDLER));
126             Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS,
127                     StatusBarIconController.ICON_BLACKLIST);
128             SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
129             if (mShowDark) {
130                 Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this);
131             }
132         }
133 
134         // NOTE: It's safe to do these after registering the receiver since the receiver always runs
135         // in the main thread, therefore the receiver can't run before this method returns.
136 
137         // The time zone may have changed while the receiver wasn't registered, so update the Time
138         mCalendar = Calendar.getInstance(TimeZone.getDefault());
139 
140         // Make sure we update to the current time
141         updateClock();
142         updateShowSeconds();
143     }
144 
145     @Override
onDetachedFromWindow()146     protected void onDetachedFromWindow() {
147         super.onDetachedFromWindow();
148         if (mAttached) {
149             getContext().unregisterReceiver(mIntentReceiver);
150             mAttached = false;
151             Dependency.get(TunerService.class).removeTunable(this);
152             SysUiServiceProvider.getComponent(getContext(), CommandQueue.class)
153                     .removeCallbacks(this);
154             if (mShowDark) {
155                 Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this);
156             }
157         }
158     }
159 
160     private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
161         @Override
162         public void onReceive(Context context, Intent intent) {
163             String action = intent.getAction();
164             if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
165                 String tz = intent.getStringExtra("time-zone");
166                 getHandler().post(() -> {
167                     mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
168                     if (mClockFormat != null) {
169                         mClockFormat.setTimeZone(mCalendar.getTimeZone());
170                     }
171                 });
172             } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
173                 final Locale newLocale = getResources().getConfiguration().locale;
174                 getHandler().post(() -> {
175                     if (!newLocale.equals(mLocale)) {
176                         mLocale = newLocale;
177                         mClockFormatString = ""; // force refresh
178                     }
179                 });
180             }
181             getHandler().post(() -> updateClock());
182         }
183     };
184 
setClockVisibleByUser(boolean visible)185     public void setClockVisibleByUser(boolean visible) {
186         mClockVisibleByUser = visible;
187         updateClockVisibility();
188     }
189 
setClockVisibilityByPolicy(boolean visible)190     public void setClockVisibilityByPolicy(boolean visible) {
191         mClockVisibleByPolicy = visible;
192         updateClockVisibility();
193     }
194 
updateClockVisibility()195     private void updateClockVisibility() {
196         int visibility = (mClockVisibleByPolicy && mClockVisibleByUser)
197                 ? View.VISIBLE : View.GONE;
198         setVisibility(visibility);
199     }
200 
updateClock()201     final void updateClock() {
202         if (mDemoMode) return;
203         mCalendar.setTimeInMillis(System.currentTimeMillis());
204         setText(getSmallTime());
205         setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
206     }
207 
208     @Override
onTuningChanged(String key, String newValue)209     public void onTuningChanged(String key, String newValue) {
210         if (CLOCK_SECONDS.equals(key)) {
211             mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0;
212             updateShowSeconds();
213         } else {
214             setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(newValue)
215                     .contains("clock"));
216             updateClockVisibility();
217         }
218     }
219 
220     @Override
disable(int state1, int state2, boolean animate)221     public void disable(int state1, int state2, boolean animate) {
222         boolean clockVisibleByPolicy = (state1 & StatusBarManager.DISABLE_CLOCK) == 0;
223         if (clockVisibleByPolicy != mClockVisibleByPolicy) {
224             setClockVisibilityByPolicy(clockVisibleByPolicy);
225         }
226     }
227 
228     @Override
onDarkChanged(Rect area, float darkIntensity, int tint)229     public void onDarkChanged(Rect area, float darkIntensity, int tint) {
230         setTextColor(DarkIconDispatcher.getTint(area, this, tint));
231     }
232 
233     @Override
onDensityOrFontScaleChanged()234     public void onDensityOrFontScaleChanged() {
235         FontSizeUtils.updateFontSize(this, R.dimen.status_bar_clock_size);
236         setPaddingRelative(
237                 mContext.getResources().getDimensionPixelSize(
238                         R.dimen.status_bar_clock_starting_padding),
239                 0,
240                 mContext.getResources().getDimensionPixelSize(
241                         R.dimen.status_bar_clock_end_padding),
242                 0);
243     }
244 
updateShowSeconds()245     private void updateShowSeconds() {
246         if (mShowSeconds) {
247             // Wait until we have a display to start trying to show seconds.
248             if (mSecondsHandler == null && getDisplay() != null) {
249                 mSecondsHandler = new Handler();
250                 if (getDisplay().getState() == Display.STATE_ON) {
251                     mSecondsHandler.postAtTime(mSecondTick,
252                             SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
253                 }
254                 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
255                 filter.addAction(Intent.ACTION_SCREEN_ON);
256                 mContext.registerReceiver(mScreenReceiver, filter);
257             }
258         } else {
259             if (mSecondsHandler != null) {
260                 mContext.unregisterReceiver(mScreenReceiver);
261                 mSecondsHandler.removeCallbacks(mSecondTick);
262                 mSecondsHandler = null;
263                 updateClock();
264             }
265         }
266     }
267 
getSmallTime()268     private final CharSequence getSmallTime() {
269         Context context = getContext();
270         boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
271         LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
272 
273         final char MAGIC1 = '\uEF00';
274         final char MAGIC2 = '\uEF01';
275 
276         SimpleDateFormat sdf;
277         String format = mShowSeconds
278                 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms
279                 : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
280         if (!format.equals(mClockFormatString)) {
281             mContentDescriptionFormat = new SimpleDateFormat(format);
282             /*
283              * Search for an unquoted "a" in the format string, so we can
284              * add dummy characters around it to let us find it again after
285              * formatting and change its size.
286              */
287             if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
288                 int a = -1;
289                 boolean quoted = false;
290                 for (int i = 0; i < format.length(); i++) {
291                     char c = format.charAt(i);
292 
293                     if (c == '\'') {
294                         quoted = !quoted;
295                     }
296                     if (!quoted && c == 'a') {
297                         a = i;
298                         break;
299                     }
300                 }
301 
302                 if (a >= 0) {
303                     // Move a back so any whitespace before AM/PM is also in the alternate size.
304                     final int b = a;
305                     while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
306                         a--;
307                     }
308                     format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
309                         + "a" + MAGIC2 + format.substring(b + 1);
310                 }
311             }
312             mClockFormat = sdf = new SimpleDateFormat(format);
313             mClockFormatString = format;
314         } else {
315             sdf = mClockFormat;
316         }
317         String result = sdf.format(mCalendar.getTime());
318 
319         if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
320             int magic1 = result.indexOf(MAGIC1);
321             int magic2 = result.indexOf(MAGIC2);
322             if (magic1 >= 0 && magic2 > magic1) {
323                 SpannableStringBuilder formatted = new SpannableStringBuilder(result);
324                 if (mAmPmStyle == AM_PM_STYLE_GONE) {
325                     formatted.delete(magic1, magic2+1);
326                 } else {
327                     if (mAmPmStyle == AM_PM_STYLE_SMALL) {
328                         CharacterStyle style = new RelativeSizeSpan(0.7f);
329                         formatted.setSpan(style, magic1, magic2,
330                                           Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
331                     }
332                     formatted.delete(magic2, magic2 + 1);
333                     formatted.delete(magic1, magic1 + 1);
334                 }
335                 return formatted;
336             }
337         }
338 
339         return result;
340 
341     }
342 
343     private boolean mDemoMode;
344 
345     @Override
dispatchDemoCommand(String command, Bundle args)346     public void dispatchDemoCommand(String command, Bundle args) {
347         if (!mDemoMode && command.equals(COMMAND_ENTER)) {
348             mDemoMode = true;
349         } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
350             mDemoMode = false;
351             updateClock();
352         } else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
353             String millis = args.getString("millis");
354             String hhmm = args.getString("hhmm");
355             if (millis != null) {
356                 mCalendar.setTimeInMillis(Long.parseLong(millis));
357             } else if (hhmm != null && hhmm.length() == 4) {
358                 int hh = Integer.parseInt(hhmm.substring(0, 2));
359                 int mm = Integer.parseInt(hhmm.substring(2));
360                 boolean is24 = DateFormat.is24HourFormat(
361                         getContext(), ActivityManager.getCurrentUser());
362                 if (is24) {
363                     mCalendar.set(Calendar.HOUR_OF_DAY, hh);
364                 } else {
365                     mCalendar.set(Calendar.HOUR, hh);
366                 }
367                 mCalendar.set(Calendar.MINUTE, mm);
368             }
369             setText(getSmallTime());
370             setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
371         }
372     }
373 
374     private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
375         @Override
376         public void onReceive(Context context, Intent intent) {
377             String action = intent.getAction();
378             if (Intent.ACTION_SCREEN_OFF.equals(action)) {
379                 if (mSecondsHandler != null) {
380                     mSecondsHandler.removeCallbacks(mSecondTick);
381                 }
382             } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
383                 if (mSecondsHandler != null) {
384                     mSecondsHandler.postAtTime(mSecondTick,
385                             SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
386                 }
387             }
388         }
389     };
390 
391     private final Runnable mSecondTick = new Runnable() {
392         @Override
393         public void run() {
394             if (mCalendar != null) {
395                 updateClock();
396             }
397             mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
398         }
399     };
400 }
401 
402