• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SwitchPreference;
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<SwitchPreference> {
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<SwitchPreference> getPreferenceType() {
61         return SwitchPreference.class;
62     }
63 
64     @Override
onCreateInternal()65     protected void onCreateInternal() {
66         super.onCreateInternal();
67         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p ->
68                 DatetimeUtils.runClickableWhileDisabled(getContext(), getFragmentController()));
69     }
70 
71     /** Starts the broadcast receiver which listens for time changes */
72     @Override
onStartInternal()73     protected void onStartInternal() {
74         // Listens to ACTION_TIME_CHANGED because the description needs to be changed based on
75         // the ACTION_TIME_CHANGED intent that this toggle sends.
76         getContext().registerReceiver(mTimeChangeReceiver,
77                 new IntentFilter(Intent.ACTION_TIME_CHANGED));
78     }
79 
80     /** Stops the broadcast receiver which listens for time changes */
81     @Override
onStopInternal()82     protected void onStopInternal() {
83         getContext().unregisterReceiver(mTimeChangeReceiver);
84     }
85 
86     @Override
updateState(SwitchPreference preference)87     protected void updateState(SwitchPreference preference) {
88         Calendar now = Calendar.getInstance();
89         mTimeFormatDemoDate.setTimeZone(now.getTimeZone());
90         // We use December 31st because it's unambiguous when demonstrating the date format.
91         // We use 13:00 so we can demonstrate the 12/24 hour options.
92         mTimeFormatDemoDate.set(now.get(Calendar.YEAR), DEMO_MONTH, DEMO_DAY_OF_MONTH,
93                 DEMO_HOUR_OF_DAY, DEMO_MINUTE, DEMO_SECOND);
94         preference.setSummary(
95                 DateFormat.getTimeFormat(getContext()).format(mTimeFormatDemoDate.getTime()));
96         preference.setChecked(is24Hour());
97     }
98 
99     @Override
handlePreferenceChanged(SwitchPreference preference, Object newValue)100     protected boolean handlePreferenceChanged(SwitchPreference preference, Object newValue) {
101         boolean isUse24HourFormatEnabled = (boolean) newValue;
102         Settings.System.putString(getContext().getContentResolver(),
103                 Settings.System.TIME_12_24,
104                 isUse24HourFormatEnabled ? HOURS_24 : HOURS_12);
105         Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
106         int timeFormatPreference =
107                 isUse24HourFormatEnabled ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
108                         : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
109         timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT,
110                 timeFormatPreference);
111         getContext().sendBroadcast(timeChanged);
112         return true;
113     }
114 
115     @Override
getDefaultAvailabilityStatus()116     public int getDefaultAvailabilityStatus() {
117         return DatetimeUtils.getAvailabilityStatus(getContext());
118     }
119 
is24Hour()120     private boolean is24Hour() {
121         return DateFormat.is24HourFormat(getContext());
122     }
123 }
124