1 /* 2 * Copyright (C) 2021 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 package com.android.car.settings.datetime; 17 18 import static android.os.UserManager.DISALLOW_CONFIG_DATE_TIME; 19 20 import static com.android.car.settings.common.PreferenceController.AVAILABLE; 21 import static com.android.car.settings.common.PreferenceController.AVAILABLE_FOR_VIEWING; 22 import static com.android.car.settings.enterprise.ActionDisabledByAdminDialogFragment.DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG; 23 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm; 24 25 import android.app.time.Capabilities; 26 import android.app.time.TimeConfiguration; 27 import android.app.time.TimeManager; 28 import android.app.time.TimeZoneConfiguration; 29 import android.content.Context; 30 import android.os.UserManager; 31 import android.widget.Toast; 32 33 import com.android.car.settings.R; 34 import com.android.car.settings.common.FragmentController; 35 import com.android.car.settings.enterprise.EnterpriseUtils; 36 37 /** 38 * Generic helper methods for this package. 39 */ 40 // TODO(b/186905050): add unit tests for this class and {@code PreferenceController} that uses 41 // this class's methods. 42 public final class DatetimeUtils { 43 44 /** 45 * Returns {@code PreferenceController.AVAILABLE_FOR_VIEWING} when there is 46 * {@code DISALLOW_CONFIG_DATE_TIME} on the user. Otherwise, returns 47 * {@code PreferenceController.AVAILABLE}. 48 */ getAvailabilityStatus(Context context)49 public static int getAvailabilityStatus(Context context) { 50 if (context.getSystemService(UserManager.class) 51 .hasUserRestriction(DISALLOW_CONFIG_DATE_TIME)) { 52 return AVAILABLE_FOR_VIEWING; 53 } 54 return AVAILABLE; 55 } 56 57 /** 58 * Shows {@code ActionDisabledByAdminDialog} when the action is disallowed by 59 * a device owner or a profile owner. Otherwise, a {@code Toast} will be shwon to inform the 60 * user that the action is disabled. 61 */ runClickableWhileDisabled(Context context, FragmentController fragmentController)62 public static void runClickableWhileDisabled(Context context, 63 FragmentController fragmentController) { 64 if (hasUserRestrictionByDpm(context, DISALLOW_CONFIG_DATE_TIME)) { 65 showActionDisabledByAdminDialog(context, fragmentController); 66 } else { 67 Toast.makeText(context, context.getString(R.string.action_unavailable), 68 Toast.LENGTH_LONG).show(); 69 } 70 } 71 showActionDisabledByAdminDialog(Context context, FragmentController fragmentController)72 private static void showActionDisabledByAdminDialog(Context context, 73 FragmentController fragmentController) { 74 fragmentController.showDialog( 75 EnterpriseUtils.getActionDisabledByAdminDialog(context, 76 DISALLOW_CONFIG_DATE_TIME), 77 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG); 78 } 79 80 /** 81 * Uses {@link android.app.time.TimeManager} to determine if auto time detection capabilities 82 * exist in the system context. 83 */ isAutoTimeDetectionCapabilityPossessed(TimeManager timeManager)84 public static boolean isAutoTimeDetectionCapabilityPossessed(TimeManager timeManager) { 85 return timeManager.getTimeCapabilitiesAndConfig().getCapabilities() 86 .getConfigureAutoDetectionEnabledCapability() == Capabilities.CAPABILITY_POSSESSED; 87 } 88 89 /** 90 * Uses {@link android.app.time.TimeManager} to determine if auto time zone detection 91 * capabilities exist in the system context. 92 */ isAutoTimeZoneDetectionCapabilityPossessed(TimeManager timeManager)93 public static boolean isAutoTimeZoneDetectionCapabilityPossessed(TimeManager timeManager) { 94 return timeManager.getTimeZoneCapabilitiesAndConfig().getCapabilities() 95 .getConfigureAutoDetectionEnabledCapability() == Capabilities.CAPABILITY_POSSESSED; 96 } 97 98 /** 99 * Uses {@link android.app.time.TimeManager} to determine if auto time and time zone detection 100 * has been enabled in the system context. 101 */ isAutoLocalTimeDetectionEnabled(TimeManager timeManager)102 public static boolean isAutoLocalTimeDetectionEnabled(TimeManager timeManager) { 103 TimeConfiguration timeConfiguration = 104 timeManager.getTimeCapabilitiesAndConfig().getConfiguration(); 105 TimeZoneConfiguration timeZoneConfiguration = 106 timeManager.getTimeZoneCapabilitiesAndConfig().getConfiguration(); 107 return isAutoTimeDetectionCapabilityPossessed(timeManager) 108 && timeConfiguration.isAutoDetectionEnabled() 109 && isAutoTimeZoneDetectionCapabilityPossessed(timeManager) 110 && timeZoneConfiguration.isAutoDetectionEnabled(); 111 } 112 DatetimeUtils()113 private DatetimeUtils() { 114 throw new UnsupportedOperationException("Provides only static methods"); 115 } 116 } 117