• 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.app.Activity;
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.provider.SearchIndexableResource;
24 import android.text.BidiFormatter;
25 import android.text.TextUtils;
26 
27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
28 import com.android.settings.R;
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.settings.users.AutoSyncDataPreferenceController;
33 import com.android.settings.users.AutoSyncPersonalDataPreferenceController;
34 import com.android.settings.users.AutoSyncWorkDataPreferenceController;
35 import com.android.settingslib.accounts.AuthenticatorHelper;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 
42 public class AccountDashboardFragment extends DashboardFragment {
43 
44     private static final String TAG = "AccountDashboardFrag";
45 
46 
47     @Override
getMetricsCategory()48     public int getMetricsCategory() {
49         return MetricsEvent.ACCOUNT;
50     }
51 
52     @Override
getLogTag()53     protected String getLogTag() {
54         return TAG;
55     }
56 
57     @Override
getPreferenceScreenResId()58     protected int getPreferenceScreenResId() {
59         return R.xml.accounts_dashboard_settings;
60     }
61 
62     @Override
getHelpResource()63     public int getHelpResource() {
64         return R.string.help_url_user_and_account_dashboard;
65     }
66 
67     @Override
createPreferenceControllers(Context context)68     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
69         final List<AbstractPreferenceController> controllers = new ArrayList<>();
70         final String[] authorities = getIntent().getStringArrayExtra(EXTRA_AUTHORITIES);
71         final AccountPreferenceController accountPrefController =
72                 new AccountPreferenceController(context, this, authorities);
73         getLifecycle().addObserver(accountPrefController);
74         controllers.add(accountPrefController);
75         controllers.add(new AutoSyncDataPreferenceController(context, this /*parent */));
76         controllers.add(new AutoSyncPersonalDataPreferenceController(context, this /*parent */));
77         controllers.add(new AutoSyncWorkDataPreferenceController(context, this /* parent */));
78         return controllers;
79     }
80 
81     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
82 
83         private final Context mContext;
84         private final SummaryLoader mSummaryLoader;
85 
SummaryProvider(Context context, SummaryLoader summaryLoader)86         public SummaryProvider(Context context, SummaryLoader summaryLoader) {
87             mContext = context;
88             mSummaryLoader = summaryLoader;
89         }
90 
91         @Override
setListening(boolean listening)92         public void setListening(boolean listening) {
93             if (listening) {
94                 final AuthenticatorHelper authHelper = new AuthenticatorHelper(mContext,
95                         UserHandle.of(UserHandle.myUserId()), null /* OnAccountsUpdateListener */);
96                 final String[] types = authHelper.getEnabledAccountTypes();
97 
98                 final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
99 
100                 CharSequence summary = null;
101                 if (types == null || types.length == 0) {
102                     summary = mContext.getString(R.string.account_dashboard_default_summary);
103                 } else {
104                     // Show up to 3 account types, ignore any null value
105                     int accountToAdd = Math.min(3, types.length);
106 
107                     for (int i = 0; i < types.length && accountToAdd > 0; i++) {
108                         final CharSequence label = authHelper.getLabelForType(mContext, types[i]);
109                         if (TextUtils.isEmpty(label)) {
110                             continue;
111                         }
112                         if (summary == null) {
113                             summary = bidiFormatter.unicodeWrap(label);
114                         } else {
115                             summary = mContext.getString(R.string.join_many_items_middle, summary,
116                                     bidiFormatter.unicodeWrap(label));
117                         }
118                         accountToAdd--;
119                     }
120                 }
121                 mSummaryLoader.setSummary(this, summary);
122             }
123         }
124     }
125 
126     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
127             = new SummaryLoader.SummaryProviderFactory() {
128         @Override
129         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
130                 SummaryLoader summaryLoader) {
131             return new SummaryProvider(activity, summaryLoader);
132         }
133     };
134 
135     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
136             new BaseSearchIndexProvider() {
137                 @Override
138                 public List<SearchIndexableResource> getXmlResourcesToIndex(
139                         Context context, boolean enabled) {
140                     final SearchIndexableResource sir = new SearchIndexableResource(context);
141                     sir.xmlResId = R.xml.accounts_dashboard_settings;
142                     return Arrays.asList(sir);
143                 }
144             };
145 }