1 /* 2 * Copyright (C) 2017 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 17 package com.android.settings.accounts; 18 19 import static com.android.settings.accounts.AccountDetailDashboardFragment.KEY_ACCOUNT; 20 import static com.android.settings.accounts.AccountDetailDashboardFragment.KEY_USER_HANDLE; 21 22 import android.accounts.Account; 23 import android.app.Activity; 24 import android.content.Context; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.support.v14.preference.PreferenceFragment; 28 import android.support.v7.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.applications.LayoutPreference; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settings.widget.EntityHeaderController; 34 import com.android.settingslib.accounts.AuthenticatorHelper; 35 import com.android.settingslib.core.AbstractPreferenceController; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 import com.android.settingslib.core.lifecycle.LifecycleObserver; 38 import com.android.settingslib.core.lifecycle.events.OnResume; 39 40 public class AccountHeaderPreferenceController extends AbstractPreferenceController 41 implements PreferenceControllerMixin, LifecycleObserver, OnResume { 42 43 private static final String KEY_ACCOUNT_HEADER = "account_header"; 44 45 private final Activity mActivity; 46 private final PreferenceFragment mHost; 47 private final Account mAccount; 48 private final UserHandle mUserHandle; 49 50 private LayoutPreference mHeaderPreference; 51 52 public AccountHeaderPreferenceController(Context context, Lifecycle lifecycle, 53 Activity activity, PreferenceFragment host, Bundle args) { 54 super(context); 55 mActivity = activity; 56 mHost = host; 57 if (args != null && args.containsKey(KEY_ACCOUNT)) { 58 mAccount = args.getParcelable(KEY_ACCOUNT); 59 } else { 60 mAccount = null; 61 } 62 63 if (args != null && args.containsKey(KEY_USER_HANDLE)) { 64 mUserHandle = args.getParcelable(KEY_USER_HANDLE); 65 } else { 66 mUserHandle = null; 67 } 68 if (lifecycle != null) { 69 lifecycle.addObserver(this); 70 } 71 } 72 73 @Override 74 public boolean isAvailable() { 75 return mAccount != null && mUserHandle != null; 76 } 77 78 @Override 79 public String getPreferenceKey() { 80 return KEY_ACCOUNT_HEADER; 81 } 82 83 @Override 84 public void displayPreference(PreferenceScreen screen) { 85 super.displayPreference(screen); 86 mHeaderPreference = (LayoutPreference) screen.findPreference(KEY_ACCOUNT_HEADER); 87 } 88 89 @Override 90 public void onResume() { 91 final AuthenticatorHelper helper = new AuthenticatorHelper(mContext, mUserHandle, null); 92 93 EntityHeaderController 94 .newInstance(mActivity, mHost, mHeaderPreference.findViewById(R.id.entity_header)) 95 .setLabel(mAccount.name) 96 .setIcon(helper.getDrawableForType(mContext, mAccount.type)) 97 .done(mActivity, true /* rebindButtons */); 98 } 99 } 100