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 } 68 69 /** Stops the broadcast receiver which listens for time changes */ 70 @Override onStopInternal()71 protected void onStopInternal() { 72 getContext().unregisterReceiver(mTimeChangeReceiver); 73 } 74 75 @Override updateState(Preference preference)76 public void updateState(Preference preference) { 77 preference.setSummary(DateFormat.getLongDateFormat(getContext()).format( 78 Calendar.getInstance().getTime())); 79 preference.setEnabled(!autoDatetimeIsEnabled()); 80 } 81 autoDatetimeIsEnabled()82 private boolean autoDatetimeIsEnabled() { 83 return Settings.Global.getInt(getContext().getContentResolver(), 84 Settings.Global.AUTO_TIME, 0) > 0; 85 } 86 } 87 88