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.Preference;
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 the preference which allows for picking the date.
36  */
37 public class DatePickerPreferenceController extends PreferenceController<Preference> {
38 
39     private final IntentFilter mIntentFilter;
40     private final BroadcastReceiver mTimeChangeReceiver = new BroadcastReceiver() {
41         @Override
42         public void onReceive(Context context, Intent intent) {
43             refreshUi();
44         }
45     };
46 
DatePickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)47     public DatePickerPreferenceController(Context context, String preferenceKey,
48             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
49         super(context, preferenceKey, fragmentController, uxRestrictions);
50         // Listens to all three actions because they can all affect the date shown on the
51         // screen.
52         mIntentFilter = new IntentFilter();
53         mIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
54         mIntentFilter.addAction(Intent.ACTION_TIME_TICK);
55         mIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
56     }
57 
58     @Override
getPreferenceType()59     protected Class<Preference> getPreferenceType() {
60         return Preference.class;
61     }
62 
63     /** Starts the broadcast receiver which listens for time changes */
64     @Override
onStartInternal()65     protected void onStartInternal() {
66         getContext().registerReceiver(mTimeChangeReceiver, mIntentFilter,
67                 Context.RECEIVER_NOT_EXPORTED);
68     }
69 
70     /** Stops the broadcast receiver which listens for time changes */
71     @Override
onStopInternal()72     protected void onStopInternal() {
73         getContext().unregisterReceiver(mTimeChangeReceiver);
74     }
75 
76     @Override
updateState(Preference preference)77     public void updateState(Preference preference) {
78         preference.setSummary(DateFormat.getLongDateFormat(getContext()).format(
79                 Calendar.getInstance().getTime()));
80         // When the status is AVAILABLE_FOR_VIEWING, this preference should always be disabled
81         boolean isAvailable = getAvailabilityStatus() == AVAILABLE;
82         preference.setEnabled(!autoDatetimeIsEnabled() && isAvailable);
83         setClickableWhileDisabled(preference, !isAvailable, p ->
84                 DatetimeUtils.runClickableWhileDisabled(getContext(), getFragmentController()));
85     }
86 
87     @Override
getDefaultAvailabilityStatus()88     public int getDefaultAvailabilityStatus() {
89         return DatetimeUtils.getAvailabilityStatus(getContext());
90     }
91 
autoDatetimeIsEnabled()92     private boolean autoDatetimeIsEnabled() {
93         return Settings.Global.getInt(getContext().getContentResolver(),
94                 Settings.Global.AUTO_TIME, 0) > 0;
95     }
96 }
97 
98