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 super.onCreate(icicle); 68 getPreferenceManager().setPreferenceComparisonCallback(null); 69 Bundle args = getArguments(); 70 final Activity activity = getActivity(); 71 mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), 72 (UserManager) getSystemService(Context.USER_SERVICE), args, 73 activity.getIntent().getExtras()); 74 if (args != null) { 75 if (args.containsKey(KEY_ACCOUNT)) { 76 mAccount = args.getParcelable(KEY_ACCOUNT); 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 } 85 mAccountSynController.init(mAccount, mUserHandle); 86 mRemoveAccountController.init(mAccount, mUserHandle); 87 } 88 89 @Override onActivityCreated(Bundle savedInstanceState)90 public void onActivityCreated(Bundle savedInstanceState) { 91 super.onActivityCreated(savedInstanceState); 92 if (mAccountLabel != null) { 93 getActivity().setTitle(mAccountLabel); 94 } 95 updateUi(); 96 } 97 98 @VisibleForTesting finishIfAccountMissing()99 void finishIfAccountMissing() { 100 final Context context = getContext(); 101 final UserManager um = context.getSystemService(UserManager.class); 102 final AccountManager accountManager = context.getSystemService(AccountManager.class); 103 for (UserHandle userHandle : um.getUserProfiles()) { 104 for (Account account : accountManager.getAccountsAsUser(userHandle.getIdentifier())) { 105 if (account.equals(mAccount)) { 106 return; 107 } 108 } 109 } 110 finish(); 111 } 112 113 @Override onResume()114 public void onResume() { 115 super.onResume(); 116 finishIfAccountMissing(); 117 } 118 119 @Override getMetricsCategory()120 public int getMetricsCategory() { 121 return SettingsEnums.ACCOUNT; 122 } 123 124 @Override getLogTag()125 protected String getLogTag() { 126 return TAG; 127 } 128 129 @Override getHelpResource()130 public int getHelpResource() { 131 return R.string.help_url_account_detail; 132 } 133 134 @Override getPreferenceScreenResId()135 protected int getPreferenceScreenResId() { 136 return R.xml.account_type_settings; 137 } 138 139 @Override createPreferenceControllers(Context context)140 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 141 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 142 mAccountSynController = new AccountSyncPreferenceController(context); 143 controllers.add(mAccountSynController); 144 mRemoveAccountController = new RemoveAccountPreferenceController(context, this); 145 controllers.add(mRemoveAccountController); 146 controllers.add(new AccountHeaderPreferenceController( 147 context, getSettingsLifecycle(), getActivity(), this /* host */, getArguments())); 148 return controllers; 149 } 150 151 @Override displayTile(Tile tile)152 protected boolean displayTile(Tile tile) { 153 if (!super.displayTile(tile)) { 154 return false; 155 } 156 if (mAccountType == null) { 157 return false; 158 } 159 final Bundle metadata = tile.getMetaData(); 160 if (metadata == null) { 161 return false; 162 } 163 final boolean display = mAccountType.equals(metadata.getString(METADATA_IA_ACCOUNT)); 164 if (display) { 165 final Intent intent = tile.getIntent(); 166 intent.putExtra(EXTRA_ACCOUNT_NAME, mAccount.name); 167 intent.putExtra(EXTRA_USER, mUserHandle); 168 } 169 return display; 170 } 171 172 @VisibleForTesting updateUi()173 void updateUi() { 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); 181 final AccountTypePreferenceLoader accountTypePreferenceLoader = 182 new AccountTypePreferenceLoader(this, helper, userHandle); 183 PreferenceScreen prefs = accountTypePreferenceLoader.addPreferencesForType( 184 mAccountType, getPreferenceScreen()); 185 if (prefs != null) { 186 accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount); 187 } 188 } 189 } 190