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