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_NOT_ALLOWED; 20 import static android.app.time.Capabilities.CAPABILITY_NOT_APPLICABLE; 21 import static android.app.time.Capabilities.CAPABILITY_NOT_SUPPORTED; 22 import static android.app.time.Capabilities.CAPABILITY_POSSESSED; 23 24 import android.app.time.TimeManager; 25 import android.app.time.TimeZoneCapabilities; 26 import android.app.time.TimeZoneCapabilitiesAndConfig; 27 import android.app.time.TimeZoneConfiguration; 28 import android.content.Context; 29 30 import com.android.internal.annotations.VisibleForTesting; 31 import com.android.settings.R; 32 import com.android.settings.core.TogglePreferenceController; 33 34 public class AutoTimeZonePreferenceController extends TogglePreferenceController { 35 36 private boolean mIsFromSUW; 37 private UpdateTimeAndDateCallback mCallback; 38 private final TimeManager mTimeManager; 39 AutoTimeZonePreferenceController(Context context, String preferenceKey)40 public AutoTimeZonePreferenceController(Context context, String preferenceKey) { 41 super(context, preferenceKey); 42 mTimeManager = context.getSystemService(TimeManager.class); 43 } 44 45 /** 46 * Set the Time and Date callback 47 */ setTimeAndDateCallback( UpdateTimeAndDateCallback callback)48 public AutoTimeZonePreferenceController setTimeAndDateCallback( 49 UpdateTimeAndDateCallback callback) { 50 mCallback = callback; 51 return this; 52 } 53 54 /** 55 * Set if current fragment is launched via SUW 56 */ setFromSUW(boolean isFromSUW)57 public AutoTimeZonePreferenceController setFromSUW(boolean isFromSUW) { 58 mIsFromSUW = isFromSUW; 59 return this; 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 if (mIsFromSUW) { 65 return DISABLED_DEPENDENT_SETTING; 66 } 67 68 TimeZoneCapabilities timeZoneCapabilities = 69 getTimeZoneCapabilitiesAndConfig().getCapabilities(); 70 int capability = timeZoneCapabilities.getConfigureAutoDetectionEnabledCapability(); 71 72 // The preference has three states: visible, not visible, and visible but disabled. 73 // This method handles the "is visible?" check. 74 switch (capability) { 75 case CAPABILITY_NOT_SUPPORTED: 76 return DISABLED_DEPENDENT_SETTING; 77 case CAPABILITY_POSSESSED: 78 case CAPABILITY_NOT_ALLOWED: 79 // This case is expected for enterprise restrictions, where the toggle should be 80 // present but disabled. Disabling is handled declaratively via the 81 // settings:userRestriction attribute in .xml. The client-side logic is expected to 82 // concur with the capabilities logic in the system server. 83 case CAPABILITY_NOT_APPLICABLE: 84 // CAPABILITY_NOT_APPLICABLE is not currently expected, so this is return value is 85 // arbitrary. 86 return AVAILABLE; 87 default: 88 throw new IllegalStateException("Unknown capability=" + capability); 89 } 90 } 91 92 @Override isChecked()93 public boolean isChecked() { 94 return isEnabled(); 95 } 96 97 @Override setChecked(boolean isChecked)98 public boolean setChecked(boolean isChecked) { 99 TimeZoneConfiguration configuration = new TimeZoneConfiguration.Builder() 100 .setAutoDetectionEnabled(isChecked) 101 .build(); 102 boolean result = mTimeManager.updateTimeZoneConfiguration(configuration); 103 104 mCallback.updateTimeAndDateDisplay(mContext); 105 return result; 106 } 107 108 @Override getSliceHighlightMenuRes()109 public int getSliceHighlightMenuRes() { 110 return R.string.menu_key_system; 111 } 112 113 @Override getSummary()114 public CharSequence getSummary() { 115 // If auto time zone cannot enable telephony fallback and is capable of location, then auto 116 // time zone must use location. 117 if (LocationProviderStatusPreferenceController.hasLocationTimeZoneNoTelephonyFallback( 118 mTimeManager.getTimeZoneCapabilitiesAndConfig().getDetectorStatus())) { 119 return mContext.getResources().getString(R.string.auto_zone_requires_location_summary); 120 } 121 // If the user has a dedicated toggle to control location use, the summary can 122 // be empty because the use of location is explicit. 123 return ""; 124 } 125 126 @VisibleForTesting isEnabled()127 boolean isEnabled() { 128 TimeZoneConfiguration config = getTimeZoneCapabilitiesAndConfig().getConfiguration(); 129 return config.isAutoDetectionEnabled(); 130 } 131 getTimeZoneCapabilitiesAndConfig()132 private TimeZoneCapabilitiesAndConfig getTimeZoneCapabilitiesAndConfig() { 133 return mTimeManager.getTimeZoneCapabilitiesAndConfig(); 134 } 135 } 136