Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 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 | |
Doris Ling | 3e586a5 | 2019-01-28 13:37:31 -0800 | [diff] [blame] | 18 | import static android.content.Intent.EXTRA_USER; |
| 19 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 20 | import android.accounts.Account; |
Antony Sargent | e0202b2 | 2018-09-20 16:35:06 -0700 | [diff] [blame] | 21 | import android.accounts.AccountManager; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 23 | import android.app.settings.SettingsEnums; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Doris Ling | 3e586a5 | 2019-01-28 13:37:31 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.os.UserHandle; |
| 28 | import android.os.UserManager; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 29 | |
Fan Zhang | 3506b74 | 2018-08-07 13:52:52 -0700 | [diff] [blame] | 30 | import androidx.annotation.VisibleForTesting; |
| 31 | import androidx.preference.PreferenceScreen; |
| 32 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 33 | import com.android.settings.R; |
| 34 | import com.android.settings.Utils; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 35 | import com.android.settings.dashboard.DashboardFragment; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 36 | import com.android.settingslib.accounts.AuthenticatorHelper; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 37 | import com.android.settingslib.core.AbstractPreferenceController; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 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"; |
Doris Ling | cae6639 | 2017-06-13 16:50:46 -0700 | [diff] [blame] | 47 | private static final String EXTRA_ACCOUNT_NAME = "extra.accountName"; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 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"; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 53 | public static final String KEY_USER_HANDLE = "user_handle"; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 54 | |
Fan Zhang | 2ed0f99 | 2017-05-17 14:15:34 -0700 | [diff] [blame] | 55 | @VisibleForTesting |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 56 | Account mAccount; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 57 | private String mAccountLabel; |
Fan Zhang | 2ed0f99 | 2017-05-17 14:15:34 -0700 | [diff] [blame] | 58 | @VisibleForTesting |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 59 | String mAccountType; |
| 60 | private AccountSyncPreferenceController mAccountSynController; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 61 | private RemoveAccountPreferenceController mRemoveAccountController; |
Doris Ling | 3e586a5 | 2019-01-28 13:37:31 -0800 | [diff] [blame] | 62 | @VisibleForTesting |
| 63 | UserHandle mUserHandle; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 64 | |
| 65 | @Override |
| 66 | public void onCreate(Bundle icicle) { |
| 67 | super.onCreate(icicle); |
Doris Ling | 328e146 | 2017-06-29 14:46:09 -0700 | [diff] [blame] | 68 | getPreferenceManager().setPreferenceComparisonCallback(null); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 69 | Bundle args = getArguments(); |
| 70 | final Activity activity = getActivity(); |
Doris Ling | 3e586a5 | 2019-01-28 13:37:31 -0800 | [diff] [blame] | 71 | mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), |
Fan Zhang | 7e6df83 | 2017-01-24 14:02:17 -0800 | [diff] [blame] | 72 | (UserManager) getSystemService(Context.USER_SERVICE), args, |
| 73 | activity.getIntent().getExtras()); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 74 | if (args != null) { |
| 75 | if (args.containsKey(KEY_ACCOUNT)) { |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 76 | mAccount = args.getParcelable(KEY_ACCOUNT); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 77 | } |
| 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 Ling | 3e586a5 | 2019-01-28 13:37:31 -0800 | [diff] [blame] | 85 | mAccountSynController.init(mAccount, mUserHandle); |
| 86 | mRemoveAccountController.init(mAccount, mUserHandle); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onActivityCreated(Bundle savedInstanceState) { |
| 91 | super.onActivityCreated(savedInstanceState); |
| 92 | if (mAccountLabel != null) { |
| 93 | getActivity().setTitle(mAccountLabel); |
| 94 | } |
Doris Ling | 13ac884 | 2017-02-10 14:22:58 -0800 | [diff] [blame] | 95 | updateUi(); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Antony Sargent | e0202b2 | 2018-09-20 16:35:06 -0700 | [diff] [blame] | 98 | @VisibleForTesting |
| 99 | void finishIfAccountMissing() { |
Antony Sargent | 95f34b4 | 2018-11-05 14:56:28 -0800 | [diff] [blame] | 100 | final Context context = getContext(); |
| 101 | final UserManager um = context.getSystemService(UserManager.class); |
James Lemieux | 3a55de4 | 2018-11-20 14:26:31 -0800 | [diff] [blame] | 102 | final AccountManager accountManager = context.getSystemService(AccountManager.class); |
Antony Sargent | 95f34b4 | 2018-11-05 14:56:28 -0800 | [diff] [blame] | 103 | for (UserHandle userHandle : um.getUserProfiles()) { |
| 104 | for (Account account : accountManager.getAccountsAsUser(userHandle.getIdentifier())) { |
| 105 | if (account.equals(mAccount)) { |
| 106 | return; |
| 107 | } |
Antony Sargent | e0202b2 | 2018-09-20 16:35:06 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
Antony Sargent | 95f34b4 | 2018-11-05 14:56:28 -0800 | [diff] [blame] | 110 | finish(); |
Antony Sargent | e0202b2 | 2018-09-20 16:35:06 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onResume() { |
| 115 | super.onResume(); |
| 116 | finishIfAccountMissing(); |
| 117 | } |
| 118 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 119 | @Override |
| 120 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 121 | return SettingsEnums.ACCOUNT; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 125 | protected String getLogTag() { |
| 126 | return TAG; |
| 127 | } |
| 128 | |
| 129 | @Override |
Fan Zhang | e0b0e9f | 2017-11-29 14:55:59 -0800 | [diff] [blame] | 130 | public int getHelpResource() { |
Fan Zhang | 179645e | 2017-06-05 13:13:42 -0700 | [diff] [blame] | 131 | return R.string.help_url_account_detail; |
| 132 | } |
| 133 | |
| 134 | @Override |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 135 | protected int getPreferenceScreenResId() { |
| 136 | return R.xml.account_type_settings; |
| 137 | } |
| 138 | |
| 139 | @Override |
Fan Zhang | f7843ad | 2018-02-22 13:51:41 -0800 | [diff] [blame] | 140 | protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 141 | final List<AbstractPreferenceController> controllers = new ArrayList<>(); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 142 | mAccountSynController = new AccountSyncPreferenceController(context); |
| 143 | controllers.add(mAccountSynController); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 144 | mRemoveAccountController = new RemoveAccountPreferenceController(context, this); |
| 145 | controllers.add(mRemoveAccountController); |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 146 | controllers.add(new AccountHeaderPreferenceController( |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 147 | context, getSettingsLifecycle(), getActivity(), this /* host */, getArguments())); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 148 | return controllers; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected boolean displayTile(Tile tile) { |
Yanting Yang | 4e56cb2 | 2019-04-18 21:37:31 +0800 | [diff] [blame] | 153 | if (!super.displayTile(tile)) { |
| 154 | return false; |
| 155 | } |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 156 | if (mAccountType == null) { |
| 157 | return false; |
| 158 | } |
Fan Zhang | 60243e6 | 2018-08-03 09:13:53 -0700 | [diff] [blame] | 159 | final Bundle metadata = tile.getMetaData(); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 160 | if (metadata == null) { |
| 161 | return false; |
| 162 | } |
Doris Ling | cae6639 | 2017-06-13 16:50:46 -0700 | [diff] [blame] | 163 | final boolean display = mAccountType.equals(metadata.getString(METADATA_IA_ACCOUNT)); |
Fan Zhang | 3506b74 | 2018-08-07 13:52:52 -0700 | [diff] [blame] | 164 | if (display) { |
Doris Ling | 3e586a5 | 2019-01-28 13:37:31 -0800 | [diff] [blame] | 165 | final Intent intent = tile.getIntent(); |
| 166 | intent.putExtra(EXTRA_ACCOUNT_NAME, mAccount.name); |
| 167 | intent.putExtra(EXTRA_USER, mUserHandle); |
Doris Ling | cae6639 | 2017-06-13 16:50:46 -0700 | [diff] [blame] | 168 | } |
| 169 | return display; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 172 | @VisibleForTesting |
Doris Ling | 13ac884 | 2017-02-10 14:22:58 -0800 | [diff] [blame] | 173 | void updateUi() { |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 174 | 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 Ling | 13ac884 | 2017-02-10 14:22:58 -0800 | [diff] [blame] | 181 | final AccountTypePreferenceLoader accountTypePreferenceLoader = |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 182 | new AccountTypePreferenceLoader(this, helper, userHandle); |
| 183 | PreferenceScreen prefs = accountTypePreferenceLoader.addPreferencesForType( |
| 184 | mAccountType, getPreferenceScreen()); |
Doris Ling | 13ac884 | 2017-02-10 14:22:58 -0800 | [diff] [blame] | 185 | if (prefs != null) { |
| 186 | accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount); |
| 187 | } |
Fan Zhang | 7e6df83 | 2017-01-24 14:02:17 -0800 | [diff] [blame] | 188 | } |
Antony Sargent | 95f34b4 | 2018-11-05 14:56:28 -0800 | [diff] [blame] | 189 | } |