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.content.Intent.EXTRA_USER;
19 
20 import android.accounts.Account;
21 import android.accounts.AccountManager;
22 import android.app.Activity;
23 import android.app.settings.SettingsEnums;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.R;
34 import com.android.settings.Utils;
35 import com.android.settings.dashboard.DashboardFragment;
36 import com.android.settingslib.accounts.AuthenticatorHelper;
37 import com.android.settingslib.core.AbstractPreferenceController;
38 import com.android.settingslib.drawer.Tile;
39 
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 public class AccountDetailDashboardFragment extends DashboardFragment {
44 
45     private static final String TAG = "AccountDetailDashboard";
46     private static final String METADATA_IA_ACCOUNT = "com.android.settings.ia.account";
47     private static final String EXTRA_ACCOUNT_NAME = "extra.accountName";
48 
49     public static final String KEY_ACCOUNT = "account";
50     public static final String KEY_ACCOUNT_TYPE = "account_type";
51     public static final String KEY_ACCOUNT_LABEL = "account_label";
52     public static final String KEY_ACCOUNT_TITLE_RES = "account_title_res";
53     public static final String KEY_USER_HANDLE = "user_handle";
54 
55     @VisibleForTesting
56     Account mAccount;
57     private String mAccountLabel;
58     @VisibleForTesting
59     String mAccountType;
60     private AccountSyncPreferenceController mAccountSynController;
61     private RemoveAccountPreferenceController mRemoveAccountController;
62     @VisibleForTesting
63     UserHandle mUserHandle;
64 
65     @Override
onCreate(Bundle icicle)66     public void onCreate(Bundle icicle) {
67         // Initialize the parameters since displayTile() will be called in super.onCreate().
68         Bundle args = getArguments();
69         final Activity activity = getActivity();
70         mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(),
71                 (UserManager) getSystemService(Context.USER_SERVICE), args,
72                 activity.getIntent().getExtras());
73         if (args != null) {
74             if (args.containsKey(KEY_ACCOUNT)) {
75                 mAccount = args.getParcelable(KEY_ACCOUNT);
76             }
77             if (args.containsKey(KEY_ACCOUNT_LABEL)) {
78                 mAccountLabel = args.getString(KEY_ACCOUNT_LABEL);
79             }
80             if (args.containsKey(KEY_ACCOUNT_TYPE)) {
81                 mAccountType = args.getString(KEY_ACCOUNT_TYPE);
82             }
83         }
84 
85         super.onCreate(icicle);
86         getPreferenceManager().setPreferenceComparisonCallback(null);
87         mAccountSynController.init(mAccount, mUserHandle);
88         mRemoveAccountController.init(mAccount, mUserHandle);
89     }
90 
91     @Override
onActivityCreated(Bundle savedInstanceState)92     public void onActivityCreated(Bundle savedInstanceState) {
93         super.onActivityCreated(savedInstanceState);
94         if (mAccountLabel != null) {
95             getActivity().setTitle(mAccountLabel);
96         }
97         updateUi();
98     }
99 
100     @VisibleForTesting
finishIfAccountMissing()101     void finishIfAccountMissing() {
102         final Context context = getContext();
103         final UserManager um = context.getSystemService(UserManager.class);
104         final AccountManager accountManager = context.getSystemService(AccountManager.class);
105         for (UserHandle userHandle : um.getUserProfiles()) {
106             for (Account account : accountManager.getAccountsAsUser(userHandle.getIdentifier())) {
107                 if (account.equals(mAccount)) {
108                     return;
109                 }
110             }
111         }
112         finish();
113     }
114 
115     @Override
onResume()116     public void onResume() {
117         super.onResume();
118         finishIfAccountMissing();
119     }
120 
121     @Override
getMetricsCategory()122     public int getMetricsCategory() {
123         return SettingsEnums.ACCOUNT_DETAIL;
124     }
125 
126     @Override
getLogTag()127     protected String getLogTag() {
128         return TAG;
129     }
130 
131     @Override
getHelpResource()132     public int getHelpResource() {
133         return R.string.help_url_account_detail;
134     }
135 
136     @Override
getPreferenceScreenResId()137     protected int getPreferenceScreenResId() {
138         return R.xml.account_type_settings;
139     }
140 
141     @Override
createPreferenceControllers(Context context)142     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
143         final List<AbstractPreferenceController> controllers = new ArrayList<>();
144         mAccountSynController = new AccountSyncPreferenceController(context);
145         controllers.add(mAccountSynController);
146         mRemoveAccountController = new RemoveAccountPreferenceController(context, this);
147         controllers.add(mRemoveAccountController);
148         controllers.add(new AccountHeaderPreferenceController(
149                 context, getSettingsLifecycle(), getActivity(), this /* host */, getArguments()));
150         return controllers;
151     }
152 
153     @Override
displayTile(Tile tile)154     protected boolean displayTile(Tile tile) {
155         if (!super.displayTile(tile)) {
156             return false;
157         }
158         if (mAccountType == null) {
159             return false;
160         }
161         final Bundle metadata = tile.getMetaData();
162         if (metadata == null) {
163             return false;
164         }
165         final boolean display = mAccountType.equals(metadata.getString(METADATA_IA_ACCOUNT));
166         if (display) {
167             final Intent intent = tile.getIntent();
168             intent.putExtra(EXTRA_ACCOUNT_NAME, mAccount.name);
169             intent.putExtra(EXTRA_USER, mUserHandle);
170         }
171         return display;
172     }
173 
174     @VisibleForTesting
updateUi()175     void updateUi() {
176         final Context context = getContext();
177         UserHandle userHandle = null;
178         Bundle args = getArguments();
179         if (args != null && args.containsKey(KEY_USER_HANDLE)) {
180             userHandle = args.getParcelable(KEY_USER_HANDLE);
181         }
182         final AuthenticatorHelper helper = new AuthenticatorHelper(context, userHandle, null);
183         final AccountTypePreferenceLoader accountTypePreferenceLoader =
184                 new AccountTypePreferenceLoader(this, helper, userHandle);
185         PreferenceScreen prefs = accountTypePreferenceLoader.addPreferencesForType(
186                 mAccountType, getPreferenceScreen());
187         if (prefs != null) {
188             accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount);
189         }
190     }
191 }
192