• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.UserHandle;
26 import android.os.UserManager;
27 
28 import com.android.settings.R;
29 import com.android.settings.SettingsPreferenceFragment;
30 import com.android.settings.applications.autofill.PasswordsPreferenceController;
31 import com.android.settings.applications.defaultapps.DefaultAutofillPreferenceController;
32 import com.android.settings.applications.defaultapps.DefaultWorkAutofillPreferenceController;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settings.users.AutoSyncDataPreferenceController;
37 import com.android.settings.users.AutoSyncPersonalDataPreferenceController;
38 import com.android.settings.users.AutoSyncWorkDataPreferenceController;
39 import com.android.settingslib.core.AbstractPreferenceController;
40 import com.android.settingslib.search.SearchIndexable;
41 import com.android.settingslib.search.SearchIndexableRaw;
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 @SearchIndexable
47 public class AccountDashboardFragment extends DashboardFragment {
48 
49     private static final String TAG = "AccountDashboardFrag";
50 
51 
52     @Override
getMetricsCategory()53     public int getMetricsCategory() {
54         return SettingsEnums.ACCOUNT;
55     }
56 
57     @Override
getLogTag()58     protected String getLogTag() {
59         return TAG;
60     }
61 
62     @Override
getPreferenceScreenResId()63     protected int getPreferenceScreenResId() {
64         return R.xml.accounts_dashboard_settings;
65     }
66 
67     @Override
getHelpResource()68     public int getHelpResource() {
69         return R.string.help_url_user_and_account_dashboard;
70     }
71 
72     @Override
onAttach(Context context)73     public void onAttach(Context context) {
74         super.onAttach(context);
75         getSettingsLifecycle().addObserver(use(PasswordsPreferenceController.class));
76     }
77 
78     @Override
createPreferenceControllers(Context context)79     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
80         final List<AbstractPreferenceController> controllers = new ArrayList<>();
81         buildAutofillPreferenceControllers(context, controllers);
82         final String[] authorities = getIntent().getStringArrayExtra(EXTRA_AUTHORITIES);
83         buildAccountPreferenceControllers(context, this /* parent */, authorities, controllers);
84         return controllers;
85     }
86 
buildAutofillPreferenceControllers( Context context, List<AbstractPreferenceController> controllers)87     static void buildAutofillPreferenceControllers(
88             Context context, List<AbstractPreferenceController> controllers) {
89         controllers.add(new DefaultAutofillPreferenceController(context));
90         controllers.add(new DefaultWorkAutofillPreferenceController(context));
91     }
92 
buildAccountPreferenceControllers( Context context, SettingsPreferenceFragment parent, String[] authorities, List<AbstractPreferenceController> controllers)93     private static void buildAccountPreferenceControllers(
94             Context context, SettingsPreferenceFragment parent, String[] authorities,
95             List<AbstractPreferenceController> controllers) {
96         final AccountPreferenceController accountPrefController =
97                 new AccountPreferenceController(context, parent, authorities,
98                         ProfileSelectFragment.ProfileType.ALL);
99         if (parent != null) {
100             parent.getSettingsLifecycle().addObserver(accountPrefController);
101         }
102         controllers.add(accountPrefController);
103         controllers.add(new AutoSyncDataPreferenceController(context, parent));
104         controllers.add(new AutoSyncPersonalDataPreferenceController(context, parent));
105         controllers.add(new AutoSyncWorkDataPreferenceController(context, parent));
106     }
107 
108     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
109             new BaseSearchIndexProvider(R.xml.accounts_dashboard_settings) {
110 
111                 @Override
112                 public List<AbstractPreferenceController> createPreferenceControllers(
113                         Context context) {
114                     final List<AbstractPreferenceController> controllers = new ArrayList<>();
115                     buildAccountPreferenceControllers(
116                             context, null /* parent */, null /* authorities*/, controllers);
117                     buildAutofillPreferenceControllers(context, controllers);
118                     return controllers;
119                 }
120 
121                 @Override
122                 public List<SearchIndexableRaw> getDynamicRawDataToIndex(Context context,
123                         boolean enabled) {
124                     final List<SearchIndexableRaw> indexRaws = new ArrayList<>();
125                     final UserManager userManager = (UserManager) context.getSystemService(
126                             Context.USER_SERVICE);
127                     final List<UserInfo> profiles = userManager.getProfiles(UserHandle.myUserId());
128                     for (final UserInfo userInfo : profiles) {
129                         if (userInfo.isManagedProfile()) {
130                             return indexRaws;
131                         }
132                     }
133 
134                     final AccountManager accountManager = AccountManager.get(context);
135                     final Account[] accounts = accountManager.getAccounts();
136                     for (Account account : accounts) {
137                         final SearchIndexableRaw raw = new SearchIndexableRaw(context);
138                         raw.key = AccountTypePreference.buildKey(account);
139                         raw.title = account.name;
140                         indexRaws.add(raw);
141                     }
142 
143                     return indexRaws;
144                 }
145             };
146 }
147