1 /* 2 * Copyright (C) 2016 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 package com.android.settings.accounts; 17 18 import static android.provider.Settings.EXTRA_AUTHORITIES; 19 20 import android.accounts.Account; 21 import android.accounts.AccountManager; 22 import android.app.settings.SettingsEnums; 23 import android.content.Context; 24 import android.content.pm.UserInfo; 25 import android.credentials.CredentialManager; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.provider.SearchIndexableResource; 29 30 import com.android.settings.R; 31 import com.android.settings.applications.autofill.PasswordsPreferenceController; 32 import com.android.settings.applications.credentials.CredentialManagerPreferenceController; 33 import com.android.settings.applications.credentials.DefaultCombinedPreferenceController; 34 import com.android.settings.applications.credentials.DefaultWorkCombinedPreferenceController; 35 import com.android.settings.applications.defaultapps.DefaultAutofillPreferenceController; 36 import com.android.settings.applications.defaultapps.DefaultWorkAutofillPreferenceController; 37 import com.android.settings.dashboard.DashboardFragment; 38 import com.android.settings.dashboard.profileselector.ProfileSelectFragment; 39 import com.android.settings.search.BaseSearchIndexProvider; 40 import com.android.settings.users.AutoSyncDataPreferenceController; 41 import com.android.settings.users.AutoSyncPersonalDataPreferenceController; 42 import com.android.settings.users.AutoSyncWorkDataPreferenceController; 43 import com.android.settingslib.core.AbstractPreferenceController; 44 import com.android.settingslib.search.SearchIndexable; 45 import com.android.settingslib.search.SearchIndexableRaw; 46 47 import java.util.ArrayList; 48 import java.util.List; 49 50 @SearchIndexable 51 public class AccountDashboardFragment extends DashboardFragment { 52 private static final String TAG = "AccountDashboardFrag"; 53 54 @Override getMetricsCategory()55 public int getMetricsCategory() { 56 return SettingsEnums.ACCOUNT; 57 } 58 59 @Override getLogTag()60 protected String getLogTag() { 61 return TAG; 62 } 63 64 @Override getPreferenceScreenResId()65 protected int getPreferenceScreenResId() { 66 return getPreferenceLayoutResId(this.getContext()); 67 } 68 69 @Override getHelpResource()70 public int getHelpResource() { 71 return R.string.help_url_user_and_account_dashboard; 72 } 73 74 @Override onAttach(Context context)75 public void onAttach(Context context) { 76 super.onAttach(context); 77 if (CredentialManager.isServiceEnabled(context)) { 78 CredentialManagerPreferenceController cmpp = 79 use(CredentialManagerPreferenceController.class); 80 CredentialManagerPreferenceController.Delegate delegate = 81 new CredentialManagerPreferenceController.Delegate() { 82 public void setActivityResult(int resultCode) { 83 getActivity().setResult(resultCode); 84 } 85 public void forceDelegateRefresh() { 86 forceUpdatePreferences(); 87 } 88 }; 89 cmpp.init(this, getFragmentManager(), getIntent(), delegate, /*isWorkProfile=*/false); 90 } else { 91 getSettingsLifecycle().addObserver(use(PasswordsPreferenceController.class)); 92 } 93 } 94 95 @Override createPreferenceControllers(Context context)96 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 97 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 98 buildAutofillPreferenceControllers(context, controllers); 99 final String[] authorities = getIntent().getStringArrayExtra(EXTRA_AUTHORITIES); 100 buildAccountPreferenceControllers(context, this /* parent */, authorities, controllers); 101 return controllers; 102 } 103 104 @Override shouldSkipForInitialSUW()105 protected boolean shouldSkipForInitialSUW() { 106 return true; 107 } 108 buildAutofillPreferenceControllers( Context context, List<AbstractPreferenceController> controllers)109 static void buildAutofillPreferenceControllers( 110 Context context, List<AbstractPreferenceController> controllers) { 111 if (CredentialManager.isServiceEnabled(context)) { 112 controllers.add(new DefaultCombinedPreferenceController(context)); 113 controllers.add(new DefaultWorkCombinedPreferenceController(context)); 114 } else { 115 controllers.add(new DefaultAutofillPreferenceController(context)); 116 controllers.add(new DefaultWorkAutofillPreferenceController(context)); 117 } 118 } 119 buildAccountPreferenceControllers( Context context, DashboardFragment parent, String[] authorities, List<AbstractPreferenceController> controllers)120 private static void buildAccountPreferenceControllers( 121 Context context, 122 DashboardFragment parent, 123 String[] authorities, 124 List<AbstractPreferenceController> controllers) { 125 final AccountPreferenceController accountPrefController = 126 new AccountPreferenceController( 127 context, parent, authorities, ProfileSelectFragment.ProfileType.ALL); 128 if (parent != null) { 129 parent.getSettingsLifecycle().addObserver(accountPrefController); 130 } 131 controllers.add(accountPrefController); 132 controllers.add(new AutoSyncDataPreferenceController(context, parent)); 133 controllers.add(new AutoSyncPersonalDataPreferenceController(context, parent)); 134 controllers.add(new AutoSyncWorkDataPreferenceController(context, parent)); 135 } 136 getPreferenceLayoutResId(Context context)137 private static int getPreferenceLayoutResId(Context context) { 138 return (context != null && CredentialManager.isServiceEnabled(context)) 139 ? R.xml.accounts_dashboard_settings_credman 140 : R.xml.accounts_dashboard_settings; 141 } 142 143 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 144 new BaseSearchIndexProvider() { 145 @Override 146 public List<SearchIndexableResource> getXmlResourcesToIndex( 147 Context context, boolean enabled) { 148 final SearchIndexableResource sir = new SearchIndexableResource(context); 149 sir.xmlResId = getPreferenceLayoutResId(context); 150 return List.of(sir); 151 } 152 153 @Override 154 public List<AbstractPreferenceController> createPreferenceControllers( 155 Context context) { 156 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 157 buildAccountPreferenceControllers( 158 context, null /* parent */, null /* authorities*/, controllers); 159 buildAutofillPreferenceControllers(context, controllers); 160 return controllers; 161 } 162 163 @SuppressWarnings("MissingSuperCall") // TODO: Fix me 164 @Override 165 public List<SearchIndexableRaw> getDynamicRawDataToIndex( 166 Context context, boolean enabled) { 167 final List<SearchIndexableRaw> indexRaws = new ArrayList<>(); 168 final UserManager userManager = 169 (UserManager) context.getSystemService(Context.USER_SERVICE); 170 final List<UserInfo> profiles = userManager.getProfiles(UserHandle.myUserId()); 171 for (final UserInfo userInfo : profiles) { 172 if (userInfo.isManagedProfile()) { 173 return indexRaws; 174 } 175 } 176 177 final AccountManager accountManager = AccountManager.get(context); 178 final Account[] accounts = accountManager.getAccounts(); 179 for (Account account : accounts) { 180 final SearchIndexableRaw raw = new SearchIndexableRaw(context); 181 raw.key = AccountTypePreference.buildKey(account); 182 raw.title = account.name; 183 indexRaws.add(raw); 184 } 185 186 return indexRaws; 187 } 188 }; 189 } 190