1 /* 2 * Copyright (C) 2020 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.car.settings.accounts; 18 19 import static android.os.UserManager.DISALLOW_MODIFY_ACCOUNTS; 20 21 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByUm; 22 23 import android.car.drivingstate.CarUxRestrictions; 24 import android.content.Context; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.PreferenceController; 31 import com.android.car.settings.profiles.ProfileHelper; 32 33 import java.util.Arrays; 34 import java.util.HashSet; 35 import java.util.Set; 36 37 /** 38 * Business Logic for preference starts the add account flow. 39 */ 40 public class AddAccountPreferenceController extends PreferenceController<Preference> { 41 42 private String[] mAuthorities; 43 private String[] mAccountTypes; 44 AddAccountPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)45 public AddAccountPreferenceController(Context context, String preferenceKey, 46 FragmentController fragmentController, 47 CarUxRestrictions uxRestrictions) { 48 super(context, preferenceKey, fragmentController, uxRestrictions); 49 } 50 51 /** Sets the account authorities that are available. */ setAuthorities(String[] authorities)52 public AddAccountPreferenceController setAuthorities(String[] authorities) { 53 mAuthorities = authorities; 54 return this; 55 } 56 57 /** Sets the account authorities that are available. */ setAccountTypes(String[] accountTypes)58 public AddAccountPreferenceController setAccountTypes(String[] accountTypes) { 59 mAccountTypes = accountTypes; 60 return this; 61 } 62 63 @Override getPreferenceType()64 protected Class<Preference> getPreferenceType() { 65 return Preference.class; 66 } 67 68 @Override onCreateInternal()69 protected void onCreateInternal() { 70 super.onCreateInternal(); 71 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> getProfileHelper() 72 .runClickableWhileDisabled(getContext(), getFragmentController())); 73 } 74 75 @Override handlePreferenceClicked(Preference preference)76 protected boolean handlePreferenceClicked(Preference preference) { 77 AccountTypesHelper helper = getAccountTypesHelper(); 78 79 if (mAuthorities != null) { 80 helper.setAuthorities(Arrays.asList(mAuthorities)); 81 } 82 if (mAccountTypes != null) { 83 helper.setAccountTypesFilter( 84 new HashSet<>(Arrays.asList(mAccountTypes))); 85 } 86 87 Set<String> authorizedAccountTypes = helper.getAuthorizedAccountTypes(); 88 89 if (authorizedAccountTypes.size() == 1) { 90 String accountType = authorizedAccountTypes.iterator().next(); 91 getContext().startActivity( 92 AddAccountActivity.createAddAccountActivityIntent(getContext(), accountType)); 93 } else { 94 getFragmentController().launchFragment(new ChooseAccountFragment()); 95 } 96 return true; 97 } 98 99 @Override getAvailabilityStatus()100 protected int getAvailabilityStatus() { 101 if (getProfileHelper().canCurrentProcessModifyAccounts()) { 102 return AVAILABLE; 103 } 104 if (getProfileHelper().isDemoOrGuest() 105 || hasUserRestrictionByUm(getContext(), DISALLOW_MODIFY_ACCOUNTS)) { 106 return DISABLED_FOR_PROFILE; 107 } 108 return AVAILABLE_FOR_VIEWING; 109 } 110 111 @VisibleForTesting getProfileHelper()112 ProfileHelper getProfileHelper() { 113 return ProfileHelper.getInstance(getContext()); 114 } 115 116 @VisibleForTesting getAccountTypesHelper()117 AccountTypesHelper getAccountTypesHelper() { 118 return new AccountTypesHelper(getContext()); 119 } 120 } 121