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 } 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( 79 DateFormat.getTimeFormat(getContext()).format(Calendar.getInstance().getTime())); 80 preference.setEnabled(!autoDatetimeIsEnabled()); 81 } 82 autoDatetimeIsEnabled()83 private boolean autoDatetimeIsEnabled() { 84 return Settings.Global.getInt(getContext().getContentResolver(), 85 Settings.Global.AUTO_TIME, 0) > 0; 86 } 87 } 88