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