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.TimeCapabilities;
25 import android.app.time.TimeCapabilitiesAndConfig;
26 import android.app.time.TimeConfiguration;
27 import android.app.time.TimeManager;
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 AutoTimePreferenceController extends TogglePreferenceController {
35 
36     private UpdateTimeAndDateCallback mCallback;
37     private final TimeManager mTimeManager;
38 
AutoTimePreferenceController(Context context, String preferenceKey)39     public AutoTimePreferenceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41         mTimeManager = context.getSystemService(TimeManager.class);
42     }
43 
setDateAndTimeCallback(UpdateTimeAndDateCallback callback)44     public void setDateAndTimeCallback(UpdateTimeAndDateCallback callback) {
45         mCallback = callback;
46     }
47 
48     @Override
getAvailabilityStatus()49     public int getAvailabilityStatus() {
50         TimeCapabilities timeCapabilities =
51                 getTimeCapabilitiesAndConfig().getCapabilities();
52         int capability = timeCapabilities.getConfigureAutoDetectionEnabledCapability();
53 
54         // The preference has three states: visible, not visible, and visible but disabled.
55         // This method handles the "is visible?" check.
56         switch (capability) {
57             case CAPABILITY_NOT_SUPPORTED:
58                 return DISABLED_DEPENDENT_SETTING;
59             case CAPABILITY_POSSESSED:
60             case CAPABILITY_NOT_ALLOWED:
61                 // This case is expected for enterprise restrictions, where the toggle should be
62                 // present but disabled. Disabling is handled declaratively via the
63                 // settings:userRestriction attribute in .xml. The client-side logic is expected to
64                 // concur with the capabilities logic in the system server.
65             case CAPABILITY_NOT_APPLICABLE:
66                 // CAPABILITY_NOT_APPLICABLE is not currently expected, so this is return value is
67                 // arbitrary.
68                 return AVAILABLE;
69             default:
70                 throw new IllegalStateException("Unknown capability=" + capability);
71         }
72     }
73 
74     @Override
isChecked()75     public boolean isChecked() {
76         return isEnabled();
77     }
78 
79     @Override
setChecked(boolean isChecked)80     public boolean setChecked(boolean isChecked) {
81         TimeConfiguration configuration = new TimeConfiguration.Builder()
82                 .setAutoDetectionEnabled(isChecked)
83                 .build();
84         boolean result = mTimeManager.updateTimeConfiguration(configuration);
85 
86         mCallback.updateTimeAndDateDisplay(mContext);
87         return result;
88     }
89 
90     @Override
getSliceHighlightMenuRes()91     public int getSliceHighlightMenuRes() {
92         return R.string.menu_key_system;
93     }
94 
95     /** Returns whether the preference should be "checked", i.e. set to the "on" position. */
96     @VisibleForTesting
isEnabled()97     public boolean isEnabled() {
98         TimeConfiguration config = getTimeCapabilitiesAndConfig().getConfiguration();
99         return config.isAutoDetectionEnabled();
100     }
101 
getTimeCapabilitiesAndConfig()102     private TimeCapabilitiesAndConfig getTimeCapabilitiesAndConfig() {
103         return mTimeManager.getTimeCapabilitiesAndConfig();
104     }
105 }
106