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.content.Context; 26 import android.os.UserManager; 27 import android.widget.Toast; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.FragmentController; 31 import com.android.car.settings.enterprise.EnterpriseUtils; 32 33 /** 34 * Generic helper methods for this package. 35 */ 36 // TODO(b/186905050): add unit tests for this class and {@code PreferenceController} that uses 37 // this class's methods. 38 public final class DatetimeUtils { 39 40 /** 41 * Returns {@code PreferenceController.AVAILABLE_FOR_VIEWING} when there is 42 * {@code DISALLOW_CONFIG_DATE_TIME} on the user. Otherwise, returns 43 * {@code PreferenceController.AVAILABLE}. 44 */ getAvailabilityStatus(Context context)45 public static int getAvailabilityStatus(Context context) { 46 if (context.getSystemService(UserManager.class) 47 .hasUserRestriction(DISALLOW_CONFIG_DATE_TIME)) { 48 return AVAILABLE_FOR_VIEWING; 49 } 50 return AVAILABLE; 51 } 52 53 /** 54 * Shows {@code ActionDisabledByAdminDialog} when the action is disallowed by 55 * a device owner or a profile owner. Otherwise, a {@code Toast} will be shwon to inform the 56 * user that the action is disabled. 57 */ runClickableWhileDisabled(Context context, FragmentController fragmentController)58 public static void runClickableWhileDisabled(Context context, 59 FragmentController fragmentController) { 60 if (hasUserRestrictionByDpm(context, DISALLOW_CONFIG_DATE_TIME)) { 61 showActionDisabledByAdminDialog(context, fragmentController); 62 } else { 63 Toast.makeText(context, context.getString(R.string.action_unavailable), 64 Toast.LENGTH_LONG).show(); 65 } 66 } 67 showActionDisabledByAdminDialog(Context context, FragmentController fragmentController)68 private static void showActionDisabledByAdminDialog(Context context, 69 FragmentController fragmentController) { 70 fragmentController.showDialog( 71 EnterpriseUtils.getActionDisabledByAdminDialog(context, 72 DISALLOW_CONFIG_DATE_TIME), 73 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG); 74 } 75 DatetimeUtils()76 private DatetimeUtils() { 77 throw new UnsupportedOperationException("Provides only static methods"); 78 } 79 } 80