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