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