1 /* 2 * Copyright (C) 2018 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.car.settings.datetime; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.provider.Settings; 25 import android.text.format.DateFormat; 26 27 import androidx.preference.TwoStatePreference; 28 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.PreferenceController; 31 32 import java.util.Calendar; 33 34 /** 35 * Business logic for toggle which chooses between 12 hour or 24 hour formats. 36 */ 37 public class TimeFormatTogglePreferenceController extends PreferenceController<TwoStatePreference> { 38 public static final String HOURS_12 = "12"; 39 public static final String HOURS_24 = "24"; 40 41 private static final int DEMO_MONTH = 11; 42 private static final int DEMO_DAY_OF_MONTH = 31; 43 private static final int DEMO_HOUR_OF_DAY = 13; 44 private static final int DEMO_MINUTE = 0; 45 private static final int DEMO_SECOND = 0; 46 private final Calendar mTimeFormatDemoDate = Calendar.getInstance(); 47 private final BroadcastReceiver mTimeChangeReceiver = new BroadcastReceiver() { 48 @Override 49 public void onReceive(Context context, Intent intent) { 50 refreshUi(); 51 } 52 }; 53 TimeFormatTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)54 public TimeFormatTogglePreferenceController(Context context, String preferenceKey, 55 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 56 super(context, preferenceKey, fragmentController, uxRestrictions); 57 } 58 59 @Override getPreferenceType()60 protected Class<TwoStatePreference> getPreferenceType() { 61 return TwoStatePreference.class; 62 } 63 64 /** Starts the broadcast receiver which listens for time changes */ 65 @Override onStartInternal()66 protected void onStartInternal() { 67 // Listens to ACTION_TIME_CHANGED because the description needs to be changed based on 68 // the ACTION_TIME_CHANGED intent that this toggle sends. 69 getContext().registerReceiver(mTimeChangeReceiver, 70 new IntentFilter(Intent.ACTION_TIME_CHANGED)); 71 } 72 73 /** Stops the broadcast receiver which listens for time changes */ 74 @Override onStopInternal()75 protected void onStopInternal() { 76 getContext().unregisterReceiver(mTimeChangeReceiver); 77 } 78 79 @Override updateState(TwoStatePreference preference)80 protected void updateState(TwoStatePreference preference) { 81 Calendar now = Calendar.getInstance(); 82 mTimeFormatDemoDate.setTimeZone(now.getTimeZone()); 83 // We use December 31st because it's unambiguous when demonstrating the date format. 84 // We use 13:00 so we can demonstrate the 12/24 hour options. 85 mTimeFormatDemoDate.set(now.get(Calendar.YEAR), DEMO_MONTH, DEMO_DAY_OF_MONTH, 86 DEMO_HOUR_OF_DAY, DEMO_MINUTE, DEMO_SECOND); 87 preference.setSummary( 88 DateFormat.getTimeFormat(getContext()).format(mTimeFormatDemoDate.getTime())); 89 preference.setChecked(is24Hour()); 90 } 91 92 @Override handlePreferenceChanged(TwoStatePreference preference, Object newValue)93 protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) { 94 boolean isUse24HourFormatEnabled = (boolean) newValue; 95 Settings.System.putString(getContext().getContentResolver(), 96 Settings.System.TIME_12_24, 97 isUse24HourFormatEnabled ? HOURS_24 : HOURS_12); 98 Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED); 99 int timeFormatPreference = 100 isUse24HourFormatEnabled ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR 101 : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR; 102 timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, 103 timeFormatPreference); 104 getContext().sendBroadcast(timeChanged); 105 return true; 106 } 107 is24Hour()108 private boolean is24Hour() { 109 return DateFormat.is24HourFormat(getContext()); 110 } 111 } 112