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 17 package com.android.tv.settings.accounts; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 24 import androidx.annotation.IntDef; 25 26 import com.android.settingslib.RestrictedLockUtils; 27 import com.android.settingslib.RestrictedLockUtilsInternal; 28 import com.android.tv.settings.MainFragment; 29 import com.android.tv.settings.R; 30 import com.android.tv.settings.overlay.FlavorUtils; 31 import com.android.tv.settings.util.SliceUtils; 32 33 import java.lang.annotation.Retention; 34 import java.lang.annotation.RetentionPolicy; 35 36 /** Utility methods for accounts settings */ 37 public class AccountsUtil { 38 39 @Retention(RetentionPolicy.SOURCE) 40 @IntDef({ACCOUNTS_FRAGMENT_DEFAULT, 41 ACCOUNTS_SLICE_FRAGMENT, 42 ACCOUNTS_BASIC_MODE_FRAGMENT, 43 ACCOUNTS_SYSTEM_INTENT, 44 ACCOUNTS_FRAGMENT_RESTRICTED}) 45 public @interface AccountsFragmentType {} 46 public static final int ACCOUNTS_FRAGMENT_DEFAULT = 0; 47 public static final int ACCOUNTS_SLICE_FRAGMENT = 1; 48 public static final int ACCOUNTS_BASIC_MODE_FRAGMENT = 2; 49 public static final int ACCOUNTS_SYSTEM_INTENT = 3; 50 public static final int ACCOUNTS_FRAGMENT_RESTRICTED = 4; 51 52 private static final String ACTION_ACCOUNTS = "com.android.tv.settings.ACCOUNTS"; 53 54 /** 55 * Get the correct accounts settings fragment based on restrictions and other features. 56 * @param context 57 * @return the accounts fragment to launch 58 */ getAccountsFragmentToLaunch(Context context)59 public static @AccountsFragmentType int getAccountsFragmentToLaunch(Context context) { 60 if (AccountsUtil.isAdminRestricted(context)) { 61 return ACCOUNTS_FRAGMENT_RESTRICTED; 62 } 63 64 if (FlavorUtils.getFeatureFactory(context).getBasicModeFeatureProvider() 65 .isBasicMode(context)) { 66 return ACCOUNTS_BASIC_MODE_FRAGMENT; 67 } 68 69 // If the intent can be handled, use it. 70 Intent accountsIntent = new Intent(ACTION_ACCOUNTS); 71 if (MainFragment.systemIntentIsHandled(context, accountsIntent) != null) { 72 return ACCOUNTS_SYSTEM_INTENT; 73 } 74 75 // If a slice is available, use it to display the accounts settings, otherwise fall back to 76 // use AccountsFragment. 77 String uri = context.getString(R.string.account_slice_uri); 78 if (SliceUtils.isSliceProviderValid(context, uri)) { 79 return ACCOUNTS_SLICE_FRAGMENT; 80 } 81 82 return ACCOUNTS_FRAGMENT_DEFAULT; 83 } 84 85 /** 86 * Verifies if the no_modify_accounts restriction is active 87 * @param context 88 * @return if the no_modify_accounts restriction is active 89 */ isAdminRestricted(Context context)90 public static boolean isAdminRestricted(Context context) { 91 RestrictedLockUtils.EnforcedAdmin admin = 92 RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context, 93 UserManager.DISALLOW_MODIFY_ACCOUNTS, UserHandle.myUserId()); 94 return (admin != null); 95 } 96 } 97