1 /* 2 * Copyright (C) 2018 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.content.Context; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.provider.Settings; 23 24 import androidx.annotation.XmlRes; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.CarSettingActivities; 28 import com.android.car.settings.common.SettingsFragment; 29 import com.android.car.settings.search.CarBaseSearchIndexProvider; 30 import com.android.car.settings.users.UserHelper; 31 import com.android.car.ui.toolbar.MenuItem; 32 import com.android.settingslib.search.SearchIndexable; 33 34 import java.util.Arrays; 35 import java.util.Collections; 36 import java.util.HashSet; 37 import java.util.List; 38 import java.util.Set; 39 40 /** 41 * Lists the user's accounts and any related options. 42 */ 43 @SearchIndexable 44 public class AccountSettingsFragment extends SettingsFragment { 45 private MenuItem mAddAccountButton; 46 47 @Override 48 @XmlRes getPreferenceScreenResId()49 protected int getPreferenceScreenResId() { 50 return R.xml.account_settings_fragment; 51 } 52 53 @Override getToolbarMenuItems()54 protected List<MenuItem> getToolbarMenuItems() { 55 return Collections.singletonList(mAddAccountButton); 56 } 57 58 @Override onCreate(Bundle savedInstanceState)59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 62 boolean canModifyAccounts = UserHelper.getInstance(getContext()) 63 .canCurrentProcessModifyAccounts(); 64 65 mAddAccountButton = new MenuItem.Builder(getContext()) 66 .setTitle(R.string.user_add_account_menu) 67 .setOnClickListener(i -> onAddAccountClicked()) 68 .setVisible(canModifyAccounts) 69 .build(); 70 } 71 72 @Override onAttach(Context context)73 public void onAttach(Context context) { 74 super.onAttach(context); 75 76 String[] authorities = getActivity().getIntent().getStringArrayExtra( 77 Settings.EXTRA_AUTHORITIES); 78 if (authorities != null) { 79 use(AccountListPreferenceController.class, R.string.pk_account_list) 80 .setAuthorities(authorities); 81 } 82 } 83 onAddAccountClicked()84 private void onAddAccountClicked() { 85 AccountTypesHelper helper = new AccountTypesHelper(getContext()); 86 Intent activityIntent = requireActivity().getIntent(); 87 88 String[] authorities = activityIntent.getStringArrayExtra(Settings.EXTRA_AUTHORITIES); 89 if (authorities != null) { 90 helper.setAuthorities(Arrays.asList(authorities)); 91 } 92 93 String[] accountTypesForFilter = 94 activityIntent.getStringArrayExtra(Settings.EXTRA_ACCOUNT_TYPES); 95 if (accountTypesForFilter != null) { 96 helper.setAccountTypesFilter( 97 new HashSet<>(Arrays.asList(accountTypesForFilter))); 98 } 99 100 Set<String> authorizedAccountTypes = helper.getAuthorizedAccountTypes(); 101 102 if (authorizedAccountTypes.size() == 1) { 103 String accountType = authorizedAccountTypes.iterator().next(); 104 startActivity( 105 AddAccountActivity.createAddAccountActivityIntent(getContext(), accountType)); 106 } else { 107 startActivity(new Intent(getContext(), 108 CarSettingActivities.ChooseAccountActivity.class)); 109 } 110 } 111 112 /** 113 * Data provider for Settings Search. 114 */ 115 public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 116 new CarBaseSearchIndexProvider(R.xml.account_settings_fragment, 117 Settings.ACTION_SYNC_SETTINGS); 118 } 119