blob: 1485500931b1e44ea3aa5a3fe771a27005d33718 [file] [log] [blame]
Doris Ling20d4b042016-11-22 16:37:06 -08001/*
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 */
16package com.android.settings.accounts;
17
Doris Ling3e586a52019-01-28 13:37:31 -080018import static android.content.Intent.EXTRA_USER;
19
Doris Ling20d4b042016-11-22 16:37:06 -080020import android.accounts.Account;
Antony Sargente0202b22018-09-20 16:35:06 -070021import android.accounts.AccountManager;
Doris Ling20d4b042016-11-22 16:37:06 -080022import android.app.Activity;
Fan Zhang31b21002019-01-16 13:49:47 -080023import android.app.settings.SettingsEnums;
Doris Ling20d4b042016-11-22 16:37:06 -080024import android.content.Context;
Doris Ling3e586a52019-01-28 13:37:31 -080025import android.content.Intent;
Doris Ling20d4b042016-11-22 16:37:06 -080026import android.os.Bundle;
27import android.os.UserHandle;
28import android.os.UserManager;
Fan Zhang98289a82017-05-18 15:11:01 -070029
Fan Zhang3506b742018-08-07 13:52:52 -070030import androidx.annotation.VisibleForTesting;
31import androidx.preference.PreferenceScreen;
32
Doris Ling20d4b042016-11-22 16:37:06 -080033import com.android.settings.R;
34import com.android.settings.Utils;
Doris Ling20d4b042016-11-22 16:37:06 -080035import com.android.settings.dashboard.DashboardFragment;
Doris Lingfd06d2f2016-12-29 14:41:15 -080036import com.android.settingslib.accounts.AuthenticatorHelper;
Tony Mantler1d583e12017-06-13 13:09:25 -070037import com.android.settingslib.core.AbstractPreferenceController;
Doris Ling20d4b042016-11-22 16:37:06 -080038import com.android.settingslib.drawer.Tile;
39
40import java.util.ArrayList;
41import java.util.List;
42
43public 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";
Doris Lingcae66392017-06-13 16:50:46 -070047 private static final String EXTRA_ACCOUNT_NAME = "extra.accountName";
Doris Ling20d4b042016-11-22 16:37:06 -080048
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";
Doris Lingfd06d2f2016-12-29 14:41:15 -080053 public static final String KEY_USER_HANDLE = "user_handle";
Doris Ling20d4b042016-11-22 16:37:06 -080054
Fan Zhang2ed0f992017-05-17 14:15:34 -070055 @VisibleForTesting
Doris Lingfd06d2f2016-12-29 14:41:15 -080056 Account mAccount;
Doris Ling20d4b042016-11-22 16:37:06 -080057 private String mAccountLabel;
Fan Zhang2ed0f992017-05-17 14:15:34 -070058 @VisibleForTesting
Doris Ling20d4b042016-11-22 16:37:06 -080059 String mAccountType;
60 private AccountSyncPreferenceController mAccountSynController;
Doris Lingfd06d2f2016-12-29 14:41:15 -080061 private RemoveAccountPreferenceController mRemoveAccountController;
Doris Ling3e586a52019-01-28 13:37:31 -080062 @VisibleForTesting
63 UserHandle mUserHandle;
Doris Ling20d4b042016-11-22 16:37:06 -080064
65 @Override
66 public void onCreate(Bundle icicle) {
67 super.onCreate(icicle);
Doris Ling328e1462017-06-29 14:46:09 -070068 getPreferenceManager().setPreferenceComparisonCallback(null);
Doris Ling20d4b042016-11-22 16:37:06 -080069 Bundle args = getArguments();
70 final Activity activity = getActivity();
Doris Ling3e586a52019-01-28 13:37:31 -080071 mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(),
Fan Zhang7e6df832017-01-24 14:02:17 -080072 (UserManager) getSystemService(Context.USER_SERVICE), args,
73 activity.getIntent().getExtras());
Doris Ling20d4b042016-11-22 16:37:06 -080074 if (args != null) {
75 if (args.containsKey(KEY_ACCOUNT)) {
Doris Lingfd06d2f2016-12-29 14:41:15 -080076 mAccount = args.getParcelable(KEY_ACCOUNT);
Doris Ling20d4b042016-11-22 16:37:06 -080077 }
78 if (args.containsKey(KEY_ACCOUNT_LABEL)) {
79 mAccountLabel = args.getString(KEY_ACCOUNT_LABEL);
80 }
81 if (args.containsKey(KEY_ACCOUNT_TYPE)) {
82 mAccountType = args.getString(KEY_ACCOUNT_TYPE);
83 }
84 }
Doris Ling3e586a52019-01-28 13:37:31 -080085 mAccountSynController.init(mAccount, mUserHandle);
86 mRemoveAccountController.init(mAccount, mUserHandle);
Doris Ling20d4b042016-11-22 16:37:06 -080087 }
88
89 @Override
90 public void onActivityCreated(Bundle savedInstanceState) {
91 super.onActivityCreated(savedInstanceState);
92 if (mAccountLabel != null) {
93 getActivity().setTitle(mAccountLabel);
94 }
Doris Ling13ac8842017-02-10 14:22:58 -080095 updateUi();
Doris Ling20d4b042016-11-22 16:37:06 -080096 }
97
Antony Sargente0202b22018-09-20 16:35:06 -070098 @VisibleForTesting
99 void finishIfAccountMissing() {
Antony Sargent95f34b42018-11-05 14:56:28 -0800100 final Context context = getContext();
101 final UserManager um = context.getSystemService(UserManager.class);
James Lemieux3a55de42018-11-20 14:26:31 -0800102 final AccountManager accountManager = context.getSystemService(AccountManager.class);
Antony Sargent95f34b42018-11-05 14:56:28 -0800103 for (UserHandle userHandle : um.getUserProfiles()) {
104 for (Account account : accountManager.getAccountsAsUser(userHandle.getIdentifier())) {
105 if (account.equals(mAccount)) {
106 return;
107 }
Antony Sargente0202b22018-09-20 16:35:06 -0700108 }
109 }
Antony Sargent95f34b42018-11-05 14:56:28 -0800110 finish();
Antony Sargente0202b22018-09-20 16:35:06 -0700111 }
112
113 @Override
114 public void onResume() {
115 super.onResume();
116 finishIfAccountMissing();
117 }
118
Doris Ling20d4b042016-11-22 16:37:06 -0800119 @Override
120 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800121 return SettingsEnums.ACCOUNT;
Doris Ling20d4b042016-11-22 16:37:06 -0800122 }
123
124 @Override
Doris Ling20d4b042016-11-22 16:37:06 -0800125 protected String getLogTag() {
126 return TAG;
127 }
128
129 @Override
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800130 public int getHelpResource() {
Fan Zhang179645e2017-06-05 13:13:42 -0700131 return R.string.help_url_account_detail;
132 }
133
134 @Override
Doris Ling20d4b042016-11-22 16:37:06 -0800135 protected int getPreferenceScreenResId() {
136 return R.xml.account_type_settings;
137 }
138
139 @Override
Fan Zhangf7843ad2018-02-22 13:51:41 -0800140 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
Tony Mantler1d583e12017-06-13 13:09:25 -0700141 final List<AbstractPreferenceController> controllers = new ArrayList<>();
Doris Ling20d4b042016-11-22 16:37:06 -0800142 mAccountSynController = new AccountSyncPreferenceController(context);
143 controllers.add(mAccountSynController);
Doris Lingfd06d2f2016-12-29 14:41:15 -0800144 mRemoveAccountController = new RemoveAccountPreferenceController(context, this);
145 controllers.add(mRemoveAccountController);
Fan Zhang98289a82017-05-18 15:11:01 -0700146 controllers.add(new AccountHeaderPreferenceController(
tmfang27c84de2018-06-28 11:39:05 +0800147 context, getSettingsLifecycle(), getActivity(), this /* host */, getArguments()));
Doris Ling20d4b042016-11-22 16:37:06 -0800148 return controllers;
149 }
150
151 @Override
152 protected boolean displayTile(Tile tile) {
Yanting Yang4e56cb22019-04-18 21:37:31 +0800153 if (!super.displayTile(tile)) {
154 return false;
155 }
Doris Ling20d4b042016-11-22 16:37:06 -0800156 if (mAccountType == null) {
157 return false;
158 }
Fan Zhang60243e62018-08-03 09:13:53 -0700159 final Bundle metadata = tile.getMetaData();
Doris Ling20d4b042016-11-22 16:37:06 -0800160 if (metadata == null) {
161 return false;
162 }
Doris Lingcae66392017-06-13 16:50:46 -0700163 final boolean display = mAccountType.equals(metadata.getString(METADATA_IA_ACCOUNT));
Fan Zhang3506b742018-08-07 13:52:52 -0700164 if (display) {
Doris Ling3e586a52019-01-28 13:37:31 -0800165 final Intent intent = tile.getIntent();
166 intent.putExtra(EXTRA_ACCOUNT_NAME, mAccount.name);
167 intent.putExtra(EXTRA_USER, mUserHandle);
Doris Lingcae66392017-06-13 16:50:46 -0700168 }
169 return display;
Doris Ling20d4b042016-11-22 16:37:06 -0800170 }
171
Doris Lingfd06d2f2016-12-29 14:41:15 -0800172 @VisibleForTesting
Doris Ling13ac8842017-02-10 14:22:58 -0800173 void updateUi() {
Doris Lingfd06d2f2016-12-29 14:41:15 -0800174 final Context context = getContext();
175 UserHandle userHandle = null;
176 Bundle args = getArguments();
177 if (args != null && args.containsKey(KEY_USER_HANDLE)) {
178 userHandle = args.getParcelable(KEY_USER_HANDLE);
179 }
180 final AuthenticatorHelper helper = new AuthenticatorHelper(context, userHandle, null);
Doris Ling13ac8842017-02-10 14:22:58 -0800181 final AccountTypePreferenceLoader accountTypePreferenceLoader =
Fan Zhang98289a82017-05-18 15:11:01 -0700182 new AccountTypePreferenceLoader(this, helper, userHandle);
183 PreferenceScreen prefs = accountTypePreferenceLoader.addPreferencesForType(
184 mAccountType, getPreferenceScreen());
Doris Ling13ac8842017-02-10 14:22:58 -0800185 if (prefs != null) {
186 accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount);
187 }
Fan Zhang7e6df832017-01-24 14:02:17 -0800188 }
Antony Sargent95f34b42018-11-05 14:56:28 -0800189}