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     }
47 
48     /**
49      * Set the Time and Date callback
50      */
setTimeAndDateCallback( UpdateTimeAndDateCallback callback)51     public TimeFormatPreferenceController setTimeAndDateCallback(
52             UpdateTimeAndDateCallback callback) {
53         mUpdateTimeAndDateCallback = callback;
54         return this;
55     }
56 
57     /**
58      * Set if current fragment is launched via SUW
59      */
setFromSUW(boolean isFromSUW)60     public TimeFormatPreferenceController setFromSUW(boolean isFromSUW) {
61         mIsFromSUW = isFromSUW;
62         return this;
63     }
64 
65     @Override
getAvailabilityStatus()66     public int getAvailabilityStatus() {
67         if (mIsFromSUW) {
68             return DISABLED_DEPENDENT_SETTING;
69         }
70         if (AutoTimeFormatPreferenceController.isAutoTimeFormatSelection(mContext)) {
71             return DISABLED_DEPENDENT_SETTING;
72         }
73         return AVAILABLE;
74     }
75 
76     @Override
updateState(Preference preference)77     public void updateState(Preference preference) {
78         super.updateState(preference);
79         preference.setEnabled(getAvailabilityStatus() == AVAILABLE);
80         refreshSummary(preference);
81     }
82 
83     @Override
isChecked()84     public boolean isChecked() {
85         return is24Hour();
86     }
87 
88     @Override
setChecked(boolean isChecked)89     public boolean setChecked(boolean isChecked) {
90         update24HourFormat(mContext, isChecked);
91         mUpdateTimeAndDateCallback.updateTimeAndDateDisplay(mContext);
92         return true;
93     }
94 
95     @Override
getSummary()96     public CharSequence getSummary() {
97         final Calendar now = Calendar.getInstance();
98         mDummyDate.setTimeZone(now.getTimeZone());
99         // We use December 31st because it's unambiguous when demonstrating the date format.
100         // We use 13:00 so we can demonstrate the 12/24 hour options.
101         mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
102         final Date dummyDate = mDummyDate.getTime();
103         return DateFormat.getTimeFormat(mContext).format(dummyDate);
104     }
105 
106     @Override
getSliceHighlightMenuRes()107     public int getSliceHighlightMenuRes() {
108         return R.string.menu_key_system;
109     }
110 
is24Hour()111     private boolean is24Hour() {
112         return DateFormat.is24HourFormat(mContext);
113     }
114 
update24HourFormat(Context context, Boolean is24Hour)115     static void update24HourFormat(Context context, Boolean is24Hour) {
116         set24Hour(context, is24Hour);
117         timeUpdated(context, is24Hour);
118     }
119 
timeUpdated(Context context, Boolean is24Hour)120     static void timeUpdated(Context context, Boolean is24Hour) {
121         Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
122         timeChanged.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
123         int timeFormatPreference;
124         if (is24Hour == null) {
125             timeFormatPreference = Intent.EXTRA_TIME_PREF_VALUE_USE_LOCALE_DEFAULT;
126         } else {
127             timeFormatPreference = is24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
128                 : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
129         }
130         timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
131         context.sendBroadcast(timeChanged);
132     }
133 
set24Hour(Context context, Boolean is24Hour)134     static void set24Hour(Context context, Boolean is24Hour) {
135         String value = is24Hour == null ? null :
136             is24Hour ? HOURS_24 : HOURS_12;
137         Settings.System.putString(context.getContentResolver(),
138                 Settings.System.TIME_12_24, value);
139     }
140 }
141