• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.provider.Settings;
21 import android.provider.Settings.System;
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.Locale;
33 
34 public class AutoTimeFormatPreferenceController extends AbstractPreferenceController
35           implements PreferenceControllerMixin {
36 
37     private static final String KEY_AUTO_24_HOUR = "auto_24hour";
38 
AutoTimeFormatPreferenceController(Context context, UpdateTimeAndDateCallback callback)39     public AutoTimeFormatPreferenceController(Context context, UpdateTimeAndDateCallback callback) {
40         super(context);
41     }
42 
43     @Override
isAvailable()44     public boolean isAvailable() {
45         return true;
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return KEY_AUTO_24_HOUR;
51     }
52 
53     @Override
updateState(Preference preference)54     public void updateState(Preference preference) {
55         if (!(preference instanceof SwitchPreference)) {
56             return;
57         }
58         ((SwitchPreference) preference).setChecked(isAutoTimeFormatSelection(mContext));
59     }
60 
61     @Override
handlePreferenceTreeClick(Preference preference)62     public boolean handlePreferenceTreeClick(Preference preference) {
63         if (!(preference instanceof TwoStatePreference)
64             || !TextUtils.equals(KEY_AUTO_24_HOUR, preference.getKey())) {
65             return false;
66         }
67         boolean auto24HourEnabled = ((SwitchPreference) preference).isChecked();
68         Boolean is24Hour;
69         if (auto24HourEnabled) {
70             is24Hour = null;
71         } else {
72             is24Hour = is24HourLocale(mContext.getResources().getConfiguration().locale);
73         }
74         TimeFormatPreferenceController.update24HourFormat(mContext, is24Hour);
75         return true;
76     }
77 
is24HourLocale(Locale locale)78     boolean is24HourLocale(Locale locale) {
79         return DateFormat.is24HourLocale(locale);
80     }
81 
82     /**
83      * Returns if the system is currently configured to pick the time format automatically based on
84      * the locale.
85      */
isAutoTimeFormatSelection(Context context)86     static boolean isAutoTimeFormatSelection(Context context) {
87         return Settings.System.getString(context.getContentResolver(), System.TIME_12_24) == null;
88     }
89 }
90