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 time selection. 36 */ 37 public class TimePickerPreferenceController 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 TimePickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)47 public TimePickerPreferenceController(Context context, String preferenceKey, 48 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 49 super(context, preferenceKey, fragmentController, uxRestrictions); 50 // ACTION_TIME_CHANGED listens to changes to the autoDatetime toggle to update the time. 51 // ACTION_TIME_TICK listens to the minute changes to update the shown time. 52 // ACTION_TIMEZONE_CHANGED listens to time zone changes to update the shown time. 53 mIntentFilter = new IntentFilter(); 54 mIntentFilter.addAction(Intent.ACTION_TIME_CHANGED); 55 mIntentFilter.addAction(Intent.ACTION_TIME_TICK); 56 mIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 57 } 58 59 @Override getPreferenceType()60 protected Class<Preference> getPreferenceType() { 61 return Preference.class; 62 } 63 64 /** Starts the broadcast receiver which listens for time changes */ 65 @Override onStartInternal()66 protected void onStartInternal() { 67 getContext().registerReceiver(mTimeChangeReceiver, mIntentFilter, 68 Context.RECEIVER_NOT_EXPORTED); 69 } 70 71 /** Stops the broadcast receiver which listens for time changes */ 72 @Override onStopInternal()73 protected void onStopInternal() { 74 getContext().unregisterReceiver(mTimeChangeReceiver); 75 } 76 77 @Override updateState(Preference preference)78 public void updateState(Preference preference) { 79 preference.setSummary( 80 DateFormat.getTimeFormat(getContext()).format(Calendar.getInstance().getTime())); 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