• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.settings.datetime;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.provider.Settings;
22 import android.text.TextUtils;
23 import android.text.format.DateFormat;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.SwitchPreference;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 
32 import java.util.Calendar;
33 import java.util.Date;
34 
35 public class TimeFormatPreferenceController extends AbstractPreferenceController
36         implements PreferenceControllerMixin {
37 
38     static final String HOURS_12 = "12";
39     static final String HOURS_24 = "24";
40 
41     private static final String KEY_TIME_FORMAT = "24 hour";
42 
43     // Used for showing the current date format, which looks like "12/31/2010", "2010/12/13", etc.
44     // The date value is dummy (independent of actual date).
45     private final Calendar mDummyDate;
46     private final boolean mIsFromSUW;
47     private final UpdateTimeAndDateCallback mUpdateTimeAndDateCallback;
48 
TimeFormatPreferenceController(Context context, UpdateTimeAndDateCallback callback, boolean isFromSUW)49     public TimeFormatPreferenceController(Context context, UpdateTimeAndDateCallback callback,
50             boolean isFromSUW) {
51         super(context);
52         mIsFromSUW = isFromSUW;
53         mDummyDate = Calendar.getInstance();
54         mUpdateTimeAndDateCallback = callback;
55     }
56 
57     @Override
isAvailable()58     public boolean isAvailable() {
59         return !mIsFromSUW;
60     }
61 
62     @Override
updateState(Preference preference)63     public void updateState(Preference preference) {
64         if (!(preference instanceof TwoStatePreference)) {
65             return;
66         }
67         preference.setEnabled(
68             !AutoTimeFormatPreferenceController.isAutoTimeFormatSelection(mContext));
69         ((TwoStatePreference) preference).setChecked(is24Hour());
70         final Calendar now = Calendar.getInstance();
71         mDummyDate.setTimeZone(now.getTimeZone());
72         // We use December 31st because it's unambiguous when demonstrating the date format.
73         // We use 13:00 so we can demonstrate the 12/24 hour options.
74         mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
75         final Date dummyDate = mDummyDate.getTime();
76         preference.setSummary(DateFormat.getTimeFormat(mContext).format(dummyDate));
77     }
78 
79     @Override
handlePreferenceTreeClick(Preference preference)80     public boolean handlePreferenceTreeClick(Preference preference) {
81         if (!(preference instanceof TwoStatePreference)
82                 || !TextUtils.equals(KEY_TIME_FORMAT, preference.getKey())) {
83             return false;
84         }
85         final boolean is24Hour = ((SwitchPreference) preference).isChecked();
86         update24HourFormat(mContext, is24Hour);
87         mUpdateTimeAndDateCallback.updateTimeAndDateDisplay(mContext);
88         return true;
89     }
90 
91     @Override
getPreferenceKey()92     public String getPreferenceKey() {
93         return KEY_TIME_FORMAT;
94     }
95 
is24Hour()96     private boolean is24Hour() {
97         return DateFormat.is24HourFormat(mContext);
98     }
99 
update24HourFormat(Context context, Boolean is24Hour)100     static void update24HourFormat(Context context, Boolean is24Hour) {
101         set24Hour(context, is24Hour);
102         timeUpdated(context, is24Hour);
103     }
104 
timeUpdated(Context context, Boolean is24Hour)105     static void timeUpdated(Context context, Boolean is24Hour) {
106         Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
107         timeChanged.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
108         int timeFormatPreference;
109         if (is24Hour == null) {
110             timeFormatPreference = Intent.EXTRA_TIME_PREF_VALUE_USE_LOCALE_DEFAULT;
111         } else {
112             timeFormatPreference = is24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
113                 : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
114         }
115         timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
116         context.sendBroadcast(timeChanged);
117     }
118 
set24Hour(Context context, Boolean is24Hour)119     static void set24Hour(Context context, Boolean is24Hour) {
120         String value = is24Hour == null ? null :
121             is24Hour ? HOURS_24 : HOURS_12;
122         Settings.System.putString(context.getContentResolver(),
123                 Settings.System.TIME_12_24, value);
124     }
125 }
126