1 /*
2  * Copyright (C) 2023 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.AutoSyncPrivateDataPreferenceController;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 public class AccountPrivateDashboardFragment extends DashboardFragment {
40     private static final String TAG = "AccountPrivateFrag";
41 
42     @Override
getMetricsCategory()43     public int getMetricsCategory() {
44         return SettingsEnums.ACCOUNT_PRIVATE;
45     }
46 
47     @Override
getLogTag()48     protected String getLogTag() {
49         return TAG;
50     }
51 
52     @Override
getPreferenceScreenResId()53     protected int getPreferenceScreenResId() {
54         if (this.getContext() != null && CredentialManager.isServiceEnabled(this.getContext())) {
55             return R.xml.accounts_private_dashboard_settings_credman;
56         }
57         return R.xml.accounts_private_dashboard_settings;
58     }
59 
60     @Override
getHelpResource()61     public int getHelpResource() {
62         return R.string.help_url_user_and_account_dashboard;
63     }
64 
65     @Override
onAttach(Context context)66     public void onAttach(Context context) {
67         super.onAttach(context);
68         if (CredentialManager.isServiceEnabled(context)) {
69             CredentialManagerPreferenceController cmpp =
70                     use(CredentialManagerPreferenceController.class);
71             CredentialManagerPreferenceController.Delegate delegate =
72                     new CredentialManagerPreferenceController.Delegate() {
73                         public void setActivityResult(int resultCode) {
74                             getActivity().setResult(resultCode);
75                         }
76                         public void forceDelegateRefresh() {
77                             forceUpdatePreferences();
78                         }
79                     };
80             cmpp.init(this, getFragmentManager(), getIntent(), delegate, /*isWorkProfile=*/false);
81         } else {
82             getSettingsLifecycle().addObserver(use(PasswordsPreferenceController.class));
83         }
84     }
85 
86     @Override
createPreferenceControllers(Context context)87     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
88         final List<AbstractPreferenceController> controllers = new ArrayList<>();
89         buildAutofillPreferenceControllers(context, controllers);
90         final String[] authorities = getIntent().getStringArrayExtra(EXTRA_AUTHORITIES);
91         buildAccountPreferenceControllers(context, authorities, controllers);
92         return controllers;
93     }
94 
buildAccountPreferenceControllers( Context context, String[] authorities, List<AbstractPreferenceController> controllers)95     private void buildAccountPreferenceControllers(
96             Context context,
97             String[] authorities,
98             List<AbstractPreferenceController> controllers) {
99         final AccountPreferenceController accountPrefController =
100                 new AccountPreferenceController(
101                         context,
102                         this,
103                         authorities,
104                         ProfileSelectFragment.ProfileType.PRIVATE);
105         controllers.add(accountPrefController);
106         controllers.add(new AutoSyncDataPreferenceController(context, this));
107         controllers.add(new AutoSyncPrivateDataPreferenceController(context, this));
108     }
109 }
110