-
Notifications
You must be signed in to change notification settings - Fork 133
/
Clock.java
462 lines (408 loc) · 17.9 KB
/
Clock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.statusbar.policy;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.TypedArray;
import android.database.ContentObserver;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.format.DateFormat;
import android.text.style.CharacterStyle;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import com.android.systemui.DemoMode;
import com.android.systemui.R;
import com.android.systemui.cm.UserContentObserver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import libcore.icu.LocaleData;
/**
* Digital clock for the status bar.
*/
public class Clock extends TextView implements DemoMode {
private boolean mAttached;
private Calendar mCalendar;
private String mClockFormatString;
private SimpleDateFormat mClockFormat;
private Locale mLocale;
public static final int AM_PM_STYLE_NORMAL = 0;
public static final int AM_PM_STYLE_SMALL = 1;
public static final int AM_PM_STYLE_GONE = 2;
private int mAmPmStyle = AM_PM_STYLE_GONE;
public static final int CLOCK_DATE_DISPLAY_GONE = 0;
public static final int CLOCK_DATE_DISPLAY_SMALL = 1;
public static final int CLOCK_DATE_DISPLAY_NORMAL = 2;
public static final int CLOCK_DATE_STYLE_REGULAR = 0;
public static final int CLOCK_DATE_STYLE_LOWERCASE = 1;
public static final int CLOCK_DATE_STYLE_UPPERCASE = 2;
public static final int STYLE_DATE_LEFT = 0;
public static final int STYLE_DATE_RIGHT = 1;
public static final int FONT_NORMAL = 0;
public static final int FONT_ITALIC = 1;
public static final int FONT_BOLD = 2;
public static final int FONT_BOLD_ITALIC = 3;
public static final int FONT_LIGHT = 4;
public static final int FONT_LIGHT_ITALIC = 5;
public static final int FONT_THIN = 6;
public static final int FONT_THIN_ITALIC = 7;
public static final int FONT_CONDENSED = 8;
public static final int FONT_CONDENSED_ITALIC = 9;
public static final int FONT_CONDENSED_BOLD = 10;
public static final int FONT_CONDENSED_BOLD_ITALIC = 11;
public static final int FONT_MEDIUM = 12;
public static final int FONT_MEDIUM_ITALIC = 13;
protected int mClockDateDisplay = CLOCK_DATE_DISPLAY_GONE;
protected int mClockDateStyle = CLOCK_DATE_STYLE_REGULAR;
private int mClockFontStyle = FONT_NORMAL;
private int mClockFontSize = 14;
private SettingsObserver mSettingsObserver;
protected class SettingsObserver extends ContentObserver {
SettingsObserver(Handler handler) {
super(handler);
}
void observe() {
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUS_BAR_DATE), false,
this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUS_BAR_DATE_STYLE), false,
this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUS_BAR_DATE_FORMAT), false,
this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUSBAR_CLOCK_COLOR), false,
this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUSBAR_CLOCK_FONT_STYLE), false,
this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUSBAR_CLOCK_FONT_SIZE), false,
this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.STATUSBAR_CLOCK_DATE_POSITION), false,
this, UserHandle.USER_ALL);
updateSettings();
}
@Override
public void onChange(boolean selfChange) {
updateSettings();
}
}
public Clock(Context context) {
this(context, null);
}
public Clock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Clock(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
filter.addAction(Intent.ACTION_USER_SWITCHED);
getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter,
null, getHandler());
}
// NOTE: It's safe to do these after registering the receiver since the receiver always runs
// in the main thread, therefore the receiver can't run before this method returns.
// The time zone may have changed while the receiver wasn't registered, so update the Time
mCalendar = Calendar.getInstance(TimeZone.getDefault());
if (mSettingsObserver == null) {
mSettingsObserver = new SettingsObserver(new Handler());
}
mSettingsObserver.observe();
updateSettings();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAttached) {
getContext().unregisterReceiver(mIntentReceiver);
getContext().getContentResolver().unregisterContentObserver(mSettingsObserver);
mAttached = false;
}
}
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
String tz = intent.getStringExtra("time-zone");
mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
if (mClockFormat != null) {
mClockFormat.setTimeZone(mCalendar.getTimeZone());
}
} else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
final Locale newLocale = getResources().getConfiguration().locale;
if (! newLocale.equals(mLocale)) {
mLocale = newLocale;
}
updateSettings();
return;
}
updateClock();
}
};
final void updateClock() {
if (mDemoMode || mCalendar == null) return;
ContentResolver resolver = mContext.getContentResolver();
int defaultColor = mContext.getResources().getColor(R.color.status_bar_clock_color);
int clockColor = Settings.System.getIntForUser(resolver,
Settings.System.STATUSBAR_CLOCK_COLOR, defaultColor,
UserHandle.USER_CURRENT);
if (clockColor == Integer.MIN_VALUE) {
// flag to reset the color
clockColor = defaultColor;
}
setTextColor(clockColor);
mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(getSmallTime());
}
private final CharSequence getSmallTime() {
Context context = getContext();
boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
final char MAGIC1 = '\uEF00';
final char MAGIC2 = '\uEF01';
SimpleDateFormat sdf;
String format = is24 ? d.timeFormat_Hm : d.timeFormat_hm;
if (!format.equals(mClockFormatString)) {
/*
* Search for an unquoted "a" in the format string, so we can
* add dummy characters around it to let us find it again after
* formatting and change its size.
*/
if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
int a = -1;
boolean quoted = false;
for (int i = 0; i < format.length(); i++) {
char c = format.charAt(i);
if (c == '\'') {
quoted = !quoted;
}
if (!quoted && c == 'a') {
a = i;
break;
}
}
if (a >= 0) {
// Move a back so any whitespace before AM/PM is also in the alternate size.
final int b = a;
while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
a--;
}
format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
+ "a" + MAGIC2 + format.substring(b + 1);
}
}
mClockFormat = sdf = new SimpleDateFormat(format);
mClockFormatString = format;
} else {
sdf = mClockFormat;
}
CharSequence dateString = null;
String result = "";
String timeResult = sdf.format(mCalendar.getTime());
String dateResult = "";
int clockDatePosition = Settings.System.getInt(getContext().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_POSITION, 0);
if (mClockDateDisplay != CLOCK_DATE_DISPLAY_GONE) {
Date now = new Date();
String clockDateFormat = Settings.System.getString(getContext().getContentResolver(),
Settings.System.STATUS_BAR_DATE_FORMAT);
if (clockDateFormat == null || clockDateFormat.isEmpty()) {
// Set dateString to short uppercase Weekday (Default for AOKP) if empty
dateString = DateFormat.format("EEE", now);
} else {
dateString = DateFormat.format(clockDateFormat, now) ;
}
if (mClockDateStyle == CLOCK_DATE_STYLE_LOWERCASE) {
// When Date style is small, convert date to lowercase
dateResult = dateString.toString().toLowerCase();
} else if (mClockDateStyle == CLOCK_DATE_STYLE_UPPERCASE) {
dateResult = dateString.toString().toUpperCase();
} else {
dateResult = dateString.toString();
}
result = (clockDatePosition == STYLE_DATE_LEFT) ?
dateResult + " " + timeResult : timeResult + " " + dateResult;
} else {
// No date, just show time
result = timeResult;
}
SpannableStringBuilder formatted = new SpannableStringBuilder(result);
if (mClockDateDisplay != CLOCK_DATE_DISPLAY_NORMAL) {
if (dateString != null) {
int dateStringLen = dateString.length();
int timeStringOffset =
(clockDatePosition == STYLE_DATE_RIGHT) ?
timeResult.length() + 1 : 0;
if (mClockDateDisplay == CLOCK_DATE_DISPLAY_GONE) {
formatted.delete(0, dateStringLen);
} else {
if (mClockDateDisplay == CLOCK_DATE_DISPLAY_SMALL) {
CharacterStyle style = new RelativeSizeSpan(0.7f);
formatted.setSpan(style, timeStringOffset,
timeStringOffset + dateStringLen,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
}
}
}
if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
int magic1 = result.indexOf(MAGIC1);
int magic2 = result.indexOf(MAGIC2);
if (magic1 >= 0 && magic2 > magic1) {
if (mAmPmStyle == AM_PM_STYLE_GONE) {
formatted.delete(magic1, magic2+1);
} else {
if (mAmPmStyle == AM_PM_STYLE_SMALL) {
CharacterStyle style = new RelativeSizeSpan(0.7f);
formatted.setSpan(style, magic1, magic2,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
formatted.delete(magic2, magic2 + 1);
formatted.delete(magic1, magic1 + 1);
}
}
}
return formatted;
}
protected void updateSettings() {
ContentResolver resolver = mContext.getContentResolver();
mClockFormatString = "";
mClockDateDisplay = Settings.System.getIntForUser(resolver,
Settings.System.STATUS_BAR_DATE, CLOCK_DATE_DISPLAY_GONE,
UserHandle.USER_CURRENT);
mClockDateStyle = Settings.System.getIntForUser(resolver,
Settings.System.STATUS_BAR_DATE_STYLE, CLOCK_DATE_STYLE_REGULAR,
UserHandle.USER_CURRENT);
mClockFontStyle = Settings.System.getIntForUser(resolver,
Settings.System.STATUSBAR_CLOCK_FONT_STYLE, FONT_NORMAL,
UserHandle.USER_CURRENT);
mClockFontSize = Settings.System.getIntForUser(resolver,
Settings.System.STATUSBAR_CLOCK_FONT_SIZE, 14,
UserHandle.USER_CURRENT);
getFontStyle(mClockFontStyle);
setTextSize(mClockFontSize);
updateClock();
}
public void getFontStyle(int font) {
switch (font) {
case FONT_NORMAL:
default:
setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
break;
case FONT_ITALIC:
setTypeface(Typeface.create("sans-serif", Typeface.ITALIC));
break;
case FONT_BOLD:
setTypeface(Typeface.create("sans-serif", Typeface.BOLD));
break;
case FONT_BOLD_ITALIC:
setTypeface(Typeface.create("sans-serif", Typeface.BOLD_ITALIC));
break;
case FONT_LIGHT:
setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
break;
case FONT_LIGHT_ITALIC:
setTypeface(Typeface.create("sans-serif-light", Typeface.ITALIC));
break;
case FONT_THIN:
setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));
break;
case FONT_THIN_ITALIC:
setTypeface(Typeface.create("sans-serif-thin", Typeface.ITALIC));
break;
case FONT_CONDENSED:
setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
break;
case FONT_CONDENSED_ITALIC:
setTypeface(Typeface.create("sans-serif-condensed", Typeface.ITALIC));
break;
case FONT_CONDENSED_BOLD:
setTypeface(Typeface.create("sans-serif-condensed", Typeface.BOLD));
break;
case FONT_CONDENSED_BOLD_ITALIC:
setTypeface(Typeface.create("sans-serif-condensed", Typeface.BOLD_ITALIC));
break;
case FONT_MEDIUM:
setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
break;
case FONT_MEDIUM_ITALIC:
setTypeface(Typeface.create("sans-serif-medium", Typeface.ITALIC));
break;
}
}
private boolean mDemoMode;
@Override
public void dispatchDemoCommand(String command, Bundle args) {
if (!mDemoMode && command.equals(COMMAND_ENTER)) {
mDemoMode = true;
} else if (mDemoMode && command.equals(COMMAND_EXIT)) {
mDemoMode = false;
updateClock();
} else if (mDemoMode && command.equals(COMMAND_CLOCK)) {
String millis = args.getString("millis");
String hhmm = args.getString("hhmm");
if (millis != null) {
mCalendar.setTimeInMillis(Long.parseLong(millis));
} else if (hhmm != null && hhmm.length() == 4) {
int hh = Integer.parseInt(hhmm.substring(0, 2));
int mm = Integer.parseInt(hhmm.substring(2));
boolean is24 = DateFormat.is24HourFormat(
getContext(), ActivityManager.getCurrentUser());
if (is24) {
mCalendar.set(Calendar.HOUR_OF_DAY, hh);
} else {
mCalendar.set(Calendar.HOUR, hh);
}
mCalendar.set(Calendar.MINUTE, mm);
}
setText(getSmallTime());
}
}
public void setAmPmStyle(int style) {
mAmPmStyle = style;
mClockFormatString = "";
updateClock();
}
}