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.format.DateFormat;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.TogglePreferenceController;
26 
27 import java.util.Locale;
28 
29 public class AutoTimeFormatPreferenceController extends TogglePreferenceController {
30 
AutoTimeFormatPreferenceController(Context context, String preferenceKey)31     public AutoTimeFormatPreferenceController(Context context, String preferenceKey) {
32         super(context, preferenceKey);
33     }
34 
35     @Override
getAvailabilityStatus()36     public int getAvailabilityStatus() {
37         return AVAILABLE;
38     }
39 
40     @Override
isChecked()41     public boolean isChecked() {
42         return isAutoTimeFormatSelection(mContext);
43     }
44 
45     @Override
setChecked(boolean isChecked)46     public boolean setChecked(boolean isChecked) {
47         Boolean is24Hour;
48         if (isChecked) {
49             is24Hour = null;
50         } else {
51             is24Hour = is24HourLocale(mContext.getResources().getConfiguration().locale);
52         }
53         TimeFormatPreferenceController.update24HourFormat(mContext, is24Hour);
54         return true;
55     }
56 
57     @Override
getSliceHighlightMenuRes()58     public int getSliceHighlightMenuRes() {
59         return R.string.menu_key_system;
60     }
61 
is24HourLocale(Locale locale)62     boolean is24HourLocale(Locale locale) {
63         return DateFormat.is24HourLocale(locale);
64     }
65 
66     /**
67      * Returns if the system is currently configured to pick the time format automatically based on
68      * the locale.
69      */
isAutoTimeFormatSelection(Context context)70     static boolean isAutoTimeFormatSelection(Context context) {
71         return Settings.System.getString(context.getContentResolver(), System.TIME_12_24) == null;
72     }
73 }
74