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.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 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settingslib.core.AbstractPreferenceController; 30 31 import java.util.Calendar; 32 import java.util.Date; 33 34 public class TimeFormatPreferenceController extends AbstractPreferenceController 35 implements PreferenceControllerMixin { 36 37 static final String HOURS_12 = "12"; 38 static final String HOURS_24 = "24"; 39 40 private static final String KEY_TIME_FORMAT = "24 hour"; 41 42 // Used for showing the current date format, which looks like "12/31/2010", "2010/12/13", etc. 43 // The date value is dummy (independent of actual date). 44 private final Calendar mDummyDate; 45 private final boolean mIsFromSUW; 46 private final UpdateTimeAndDateCallback mUpdateTimeAndDateCallback; 47 48 public TimeFormatPreferenceController(Context context, UpdateTimeAndDateCallback callback, 49 boolean isFromSUW) { 50 super(context); 51 mIsFromSUW = isFromSUW; 52 mDummyDate = Calendar.getInstance(); 53 mUpdateTimeAndDateCallback = callback; 54 } 55 56 @Override 57 public boolean isAvailable() { 58 return !mIsFromSUW; 59 } 60 61 @Override 62 public void updateState(Preference preference) { 63 if (!(preference instanceof TwoStatePreference)) { 64 return; 65 } 66 preference.setEnabled( 67 !AutoTimeFormatPreferenceController.isAutoTimeFormatSelection(mContext)); 68 ((TwoStatePreference) preference).setChecked(is24Hour()); 69 final Calendar now = Calendar.getInstance(); 70 mDummyDate.setTimeZone(now.getTimeZone()); 71 // We use December 31st because it's unambiguous when demonstrating the date format. 72 // We use 13:00 so we can demonstrate the 12/24 hour options. 73 mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0); 74 final Date dummyDate = mDummyDate.getTime(); 75 preference.setSummary(DateFormat.getTimeFormat(mContext).format(dummyDate)); 76 } 77 78 @Override 79 public boolean handlePreferenceTreeClick(Preference preference) { 80 if (!(preference instanceof TwoStatePreference) 81 || !TextUtils.equals(KEY_TIME_FORMAT, preference.getKey())) { 82 return false; 83 } 84 final boolean is24Hour = ((SwitchPreference) preference).isChecked(); 85 update24HourFormat(mContext, is24Hour); 86 mUpdateTimeAndDateCallback.updateTimeAndDateDisplay(mContext); 87 return true; 88 } 89 90 @Override 91 public String getPreferenceKey() { 92 return KEY_TIME_FORMAT; 93 } 94 95 private boolean is24Hour() { 96 return DateFormat.is24HourFormat(mContext); 97 } 98 99 static void update24HourFormat(Context context, Boolean is24Hour) { 100 set24Hour(context, is24Hour); 101 timeUpdated(context, is24Hour); 102 } 103 104 static void timeUpdated(Context context, Boolean is24Hour) { 105 Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED); 106 timeChanged.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); 107 int timeFormatPreference; 108 if (is24Hour == null) { 109 timeFormatPreference = Intent.EXTRA_TIME_PREF_VALUE_USE_LOCALE_DEFAULT; 110 } else { 111 timeFormatPreference = is24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR 112 : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR; 113 } 114 timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference); 115 context.sendBroadcast(timeChanged); 116 } 117 118 static void set24Hour(Context context, Boolean is24Hour) { 119 String value = is24Hour == null ? null : 120 is24Hour ? HOURS_24 : HOURS_12; 121 Settings.System.putString(context.getContentResolver(), 122 Settings.System.TIME_12_24, value); 123 } 124 } 125