1 /* 2 * Copyright (C) 2016 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.settings.datetime; 18 19 import static android.app.time.Capabilities.CAPABILITY_POSSESSED; 20 21 import android.app.time.TimeManager; 22 import android.app.time.TimeZoneCapabilities; 23 import android.content.Context; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settingslib.RestrictedPreference; 30 import com.android.settingslib.datetime.ZoneGetter; 31 32 import java.util.Calendar; 33 34 public class TimeZonePreferenceController extends BasePreferenceController { 35 36 private final TimeManager mTimeManager; 37 TimeZonePreferenceController(Context context, String preferenceKey)38 public TimeZonePreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 mTimeManager = context.getSystemService(TimeManager.class); 41 } 42 43 @Override getSummary()44 public CharSequence getSummary() { 45 return getTimeZoneOffsetAndName(); 46 } 47 48 @Override getAvailabilityStatus()49 public int getAvailabilityStatus() { 50 return shouldEnableManualTimeZoneSelection() ? AVAILABLE : DISABLED_DEPENDENT_SETTING; 51 } 52 53 @Override updateState(Preference preference)54 public void updateState(Preference preference) { 55 super.updateState(preference); 56 57 if (preference instanceof RestrictedPreference 58 && ((RestrictedPreference) preference).isDisabledByAdmin()) { 59 return; 60 } 61 62 preference.setEnabled(shouldEnableManualTimeZoneSelection()); 63 } 64 65 @VisibleForTesting getTimeZoneOffsetAndName()66 CharSequence getTimeZoneOffsetAndName() { 67 final Calendar now = Calendar.getInstance(); 68 return ZoneGetter.getTimeZoneOffsetAndName(mContext, 69 now.getTimeZone(), now.getTime()); 70 } 71 shouldEnableManualTimeZoneSelection()72 private boolean shouldEnableManualTimeZoneSelection() { 73 TimeZoneCapabilities timeZoneCapabilities = 74 mTimeManager.getTimeZoneCapabilitiesAndConfig().getCapabilities(); 75 int suggestManualTimeZoneCapability = 76 timeZoneCapabilities.getSetManualTimeZoneCapability(); 77 return suggestManualTimeZoneCapability == CAPABILITY_POSSESSED; 78 } 79 } 80