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 android.app.Activity;
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.provider.SearchIndexableResource;
25 
26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
27 import com.android.settings.R;
28 import com.android.settings.core.PreferenceController;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.dashboard.SummaryLoader;
31 import com.android.settings.search.BaseSearchIndexProvider;
32 import com.android.settingslib.drawer.Tile;
33 
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 
38 import static android.provider.Settings.EXTRA_AUTHORITIES;
39 
40 public class UserAndAccountDashboardFragment extends DashboardFragment {
41 
42     private static final String TAG = "UserAndAccountDashboard";
43     private static final String METADATA_IA_ACCOUNT = "com.android.settings.ia.account";
44 
45     @Override
getMetricsCategory()46     public int getMetricsCategory() {
47         return MetricsEvent.ACCOUNT;
48     }
49 
50     @Override
getLogTag()51     protected String getLogTag() {
52         return TAG;
53     }
54 
55     @Override
getPreferenceScreenResId()56     protected int getPreferenceScreenResId() {
57         return R.xml.user_and_accounts_settings;
58     }
59 
60     @Override
getPreferenceControllers(Context context)61     protected List<PreferenceController> getPreferenceControllers(Context context) {
62         final List<PreferenceController> controllers = new ArrayList<>();
63         controllers.add(new EmergencyInfoPreferenceController(context));
64         AddUserWhenLockedPreferenceController addUserWhenLockedPrefController =
65                 new AddUserWhenLockedPreferenceController(context);
66         controllers.add(addUserWhenLockedPrefController);
67         getLifecycle().addObserver(addUserWhenLockedPrefController);
68         controllers.add(new AutoSyncDataPreferenceController(context, this));
69         controllers.add(new AutoSyncPersonalDataPreferenceController(context, this));
70         controllers.add(new AutoSyncWorkDataPreferenceController(context, this));
71         String[] authorities = getIntent().getStringArrayExtra(EXTRA_AUTHORITIES);
72         final AccountPreferenceController accountPrefController =
73                 new AccountPreferenceController(context, this, authorities);
74         getLifecycle().addObserver(accountPrefController);
75         controllers.add(accountPrefController);
76         return controllers;
77     }
78 
79     @Override
displayTile(Tile tile)80     protected boolean displayTile(Tile tile) {
81         final Bundle metadata = tile.metaData;
82         if (metadata != null) {
83             return metadata.getString(METADATA_IA_ACCOUNT) == null;
84         }
85         return true;
86     }
87 
88     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
89 
90         private final Context mContext;
91         private final SummaryLoader mSummaryLoader;
92 
SummaryProvider(Context context, SummaryLoader summaryLoader)93         public SummaryProvider(Context context, SummaryLoader summaryLoader) {
94             mContext = context;
95             mSummaryLoader = summaryLoader;
96         }
97 
98         @Override
setListening(boolean listening)99         public void setListening(boolean listening) {
100             if (listening) {
101                 UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
102                         UserHandle.myUserId());
103                 mSummaryLoader.setSummary(this,
104                     mContext.getString(R.string.users_and_accounts_summary, info.name));
105             }
106         }
107     }
108 
109     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
110             = new SummaryLoader.SummaryProviderFactory() {
111         @Override
112         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
113                 SummaryLoader summaryLoader) {
114             return new SummaryProvider(activity, summaryLoader);
115         }
116     };
117 
118     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
119             new BaseSearchIndexProvider() {
120                 @Override
121                 public List<SearchIndexableResource> getXmlResourcesToIndex(
122                         Context context, boolean enabled) {
123                     final SearchIndexableResource sir = new SearchIndexableResource(context);
124                     sir.xmlResId = R.xml.user_and_accounts_settings;
125                     return Arrays.asList(sir);
126                 }
127             };
128 }