1 /*
2  * Copyright (C) 2019 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.settings.accounts;
18 
19 import static android.provider.Settings.EXTRA_AUTHORITIES;
20 
21 import static com.android.settings.accounts.AccountDashboardFragment.buildAutofillPreferenceControllers;
22 
23 import android.app.settings.SettingsEnums;
24 import android.content.Context;
25 import android.credentials.CredentialManager;
26 
27 import com.android.settings.R;
28 import com.android.settings.applications.autofill.PasswordsPreferenceController;
29 import com.android.settings.applications.credentials.CredentialManagerPreferenceController;
30 import com.android.settings.dashboard.DashboardFragment;
31 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
32 import com.android.settings.users.AutoSyncDataPreferenceController;
33 import com.android.settings.users.AutoSyncPersonalDataPreferenceController;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 /** Account Setting page for personal profile. */
40 public class AccountPersonalDashboardFragment extends DashboardFragment {
41     private static final String TAG = "AccountPersonalFrag";
42 
43     @Override
getMetricsCategory()44     public int getMetricsCategory() {
45         return SettingsEnums.ACCOUNT_PERSONAL;
46     }
47 
48     @Override
getLogTag()49     protected String getLogTag() {
50         return TAG;
51     }
52 
53     @Override
getPreferenceScreenResId()54     protected int getPreferenceScreenResId() {
55         if (this.getContext() != null && CredentialManager.isServiceEnabled(this.getContext())) {
56             return R.xml.accounts_personal_dashboard_settings_credman;
57         }
58         return R.xml.accounts_personal_dashboard_settings;
59     }
60 
61     @Override
getHelpResource()62     public int getHelpResource() {
63         return R.string.help_url_user_and_account_dashboard;
64     }
65 
66     @Override
onAttach(Context context)67     public void onAttach(Context context) {
68         super.onAttach(context);
69         if (CredentialManager.isServiceEnabled(context)) {
70             CredentialManagerPreferenceController cmpp =
71                     use(CredentialManagerPreferenceController.class);
72             CredentialManagerPreferenceController.Delegate delegate =
73                     new CredentialManagerPreferenceController.Delegate() {
74                 public void setActivityResult(int resultCode) {
75                     getActivity().setResult(resultCode);
76                 }
77                 public void forceDelegateRefresh() {
78                     forceUpdatePreferences();
79                 }
80             };
81             cmpp.init(this, getFragmentManager(), getIntent(), delegate, /*isWorkProfile=*/false);
82         } else {
83             getSettingsLifecycle().addObserver(use(PasswordsPreferenceController.class));
84         }
85     }
86 
87     @Override
createPreferenceControllers(Context context)88     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
89         final List<AbstractPreferenceController> controllers = new ArrayList<>();
90         buildAutofillPreferenceControllers(context, controllers);
91         final String[] authorities = getIntent().getStringArrayExtra(EXTRA_AUTHORITIES);
92         buildAccountPreferenceControllers(context, this /* parent */, authorities, controllers);
93         return controllers;
94     }
95 
buildAccountPreferenceControllers( Context context, DashboardFragment parent, String[] authorities, List<AbstractPreferenceController> controllers)96     private static void buildAccountPreferenceControllers(
97             Context context,
98             DashboardFragment parent,
99             String[] authorities,
100             List<AbstractPreferenceController> controllers) {
101         final AccountPreferenceController accountPrefController =
102                 new AccountPreferenceController(
103                         context, parent, authorities, ProfileSelectFragment.ProfileType.PERSONAL);
104         if (parent != null) {
105             parent.getSettingsLifecycle().addObserver(accountPrefController);
106         }
107         controllers.add(accountPrefController);
108         controllers.add(new AutoSyncDataPreferenceController(context, parent));
109         controllers.add(new AutoSyncPersonalDataPreferenceController(context, parent));
110     }
111 
112     // TODO: b/141601408. After featureFlag settings_work_profile is launched, unmark this
113     //    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
114     //            new BaseSearchIndexProvider(R.xml.accounts_personal_dashboard_settings) {
115     //
116     //                @Override
117     //                public List<AbstractPreferenceController> createPreferenceControllers(
118     //                        Context context) {
119     //                    ..Add autofill here too..
120     //                    return buildPreferenceControllers(
121     //                            context, null /* parent */, null /* authorities*/);
122     //                }
123     //            };
124 }
125