• 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.format.DateFormat;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.TogglePreferenceController;
28 
29 import java.util.Calendar;
30 import java.util.Date;
31 
32 public class TimeFormatPreferenceController extends TogglePreferenceController {
33 
34     static final String HOURS_12 = "12";
35     static final String HOURS_24 = "24";
36 
37     // Used for showing the current date format, which looks like "12/31/2010", "2010/12/13", etc.
38     // The date value is stubs (independent of actual date).
39     private final Calendar mDummyDate;
40     private boolean mIsFromSUW;
41     private UpdateTimeAndDateCallback mUpdateTimeAndDateCallback;
42 
TimeFormatPreferenceController(Context context, String key)43     public TimeFormatPreferenceController(Context context, String key) {
44         super(context, key);
45         mDummyDate = Calendar.getInstance();
46         // This is a no-op implementation of UpdateTimeAndDateCallback to avoid a NPE when
47         // setTimeAndDateCallback() isn't called, e.g. for slices and other cases where the
48         // controller is instantiated outside of the context of the real Date & Time settings
49         // screen.
50         mUpdateTimeAndDateCallback  = (c) -> {};
51     }
52 
53     /**
54      * Set the Time and Date callback
55      */
setTimeAndDateCallback( UpdateTimeAndDateCallback callback)56     public TimeFormatPreferenceController setTimeAndDateCallback(
57             UpdateTimeAndDateCallback callback) {
58         mUpdateTimeAndDateCallback = callback;
59         return this;
60     }
61 
62     /**
63      * Set if current fragment is launched via SUW
64      */
setFromSUW(boolean isFromSUW)65     public TimeFormatPreferenceController setFromSUW(boolean isFromSUW) {
66         mIsFromSUW = isFromSUW;
67         return this;
68     }
69 
70     @Override
getAvailabilityStatus()71     public int getAvailabilityStatus() {
72         if (mIsFromSUW) {
73             return DISABLED_DEPENDENT_SETTING;
74         }
75         return AVAILABLE;
76     }
77 
78     @Override
updateState(Preference preference)79     public void updateState(Preference preference) {
80         super.updateState(preference);
81         preference.setEnabled(getAvailabilityStatus() == AVAILABLE);
82         refreshSummary(preference);
83     }
84 
85     @Override
isChecked()86     public boolean isChecked() {
87         return is24Hour();
88     }
89 
90     @Override
setChecked(boolean isChecked)91     public boolean setChecked(boolean isChecked) {
92         update24HourFormat(mContext, isChecked);
93         mUpdateTimeAndDateCallback.updateTimeAndDateDisplay(mContext);
94         return true;
95     }
96 
97     @Override
getSummary()98     public CharSequence getSummary() {
99         final Calendar now = Calendar.getInstance();
100         mDummyDate.setTimeZone(now.getTimeZone());
101         // We use December 31st because it's unambiguous when demonstrating the date format.
102         // We use 13:00 so we can demonstrate the 12/24 hour options.
103         mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
104         final Date dummyDate = mDummyDate.getTime();
105         return DateFormat.getTimeFormat(mContext).format(dummyDate);
106     }
107 
108     @Override
getSliceHighlightMenuRes()109     public int getSliceHighlightMenuRes() {
110         return R.string.menu_key_system;
111     }
112 
is24Hour()113     private boolean is24Hour() {
114         return DateFormat.is24HourFormat(mContext);
115     }
116 
update24HourFormat(Context context, Boolean is24Hour)117     private static void update24HourFormat(Context context, Boolean is24Hour) {
118         set24Hour(context, is24Hour);
119         timeUpdated(context, is24Hour);
120     }
121 
timeUpdated(Context context, Boolean is24Hour)122     private static void timeUpdated(Context context, Boolean is24Hour) {
123         Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
124         timeChanged.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
125         int timeFormatPreference;
126         if (is24Hour == null) {
127             timeFormatPreference = Intent.EXTRA_TIME_PREF_VALUE_USE_LOCALE_DEFAULT;
128         } else {
129             timeFormatPreference = is24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
130                     : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
131         }
132         timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
133         context.sendBroadcast(timeChanged);
134     }
135 
set24Hour(Context context, boolean is24Hour)136     private static void set24Hour(Context context, boolean is24Hour) {
137         String value = is24Hour ? HOURS_24 : HOURS_12;
138         Settings.System.putString(context.getContentResolver(),
139                 Settings.System.TIME_12_24, value);
140     }
141 }
142