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