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