Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 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 | |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 19 | import static com.android.settings.accounts.AccountDetailDashboardFragment.KEY_ACCOUNT; |
| 20 | import static com.android.settings.accounts.AccountDetailDashboardFragment.KEY_USER_HANDLE; |
| 21 | |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 22 | import android.accounts.Account; |
| 23 | import android.app.Activity; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.os.Bundle; |
| 26 | import android.os.UserHandle; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 27 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 28 | import androidx.preference.PreferenceFragmentCompat; |
| 29 | import androidx.preference.PreferenceScreen; |
| 30 | |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 31 | import com.android.settings.R; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 32 | import com.android.settings.core.PreferenceControllerMixin; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 33 | import com.android.settings.widget.EntityHeaderController; |
| 34 | import com.android.settingslib.accounts.AuthenticatorHelper; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 35 | import com.android.settingslib.core.AbstractPreferenceController; |
Fan Zhang | 841d1d5 | 2017-05-22 09:50:38 -0700 | [diff] [blame] | 36 | import com.android.settingslib.core.lifecycle.Lifecycle; |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 37 | import com.android.settingslib.core.lifecycle.LifecycleObserver; |
| 38 | import com.android.settingslib.core.lifecycle.events.OnResume; |
tmfang | dce94bb | 2018-11-26 18:41:01 +0800 | [diff] [blame] | 39 | import com.android.settingslib.widget.LayoutPreference; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 40 | |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 41 | public class AccountHeaderPreferenceController extends AbstractPreferenceController |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 42 | implements PreferenceControllerMixin, LifecycleObserver, OnResume { |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String KEY_ACCOUNT_HEADER = "account_header"; |
| 45 | |
| 46 | private final Activity mActivity; |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 47 | private final PreferenceFragmentCompat mHost; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 48 | private final Account mAccount; |
| 49 | private final UserHandle mUserHandle; |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 50 | |
| 51 | private LayoutPreference mHeaderPreference; |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 52 | |
Fan Zhang | 841d1d5 | 2017-05-22 09:50:38 -0700 | [diff] [blame] | 53 | public AccountHeaderPreferenceController(Context context, Lifecycle lifecycle, |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 54 | Activity activity, PreferenceFragmentCompat host, Bundle args) { |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 55 | super(context); |
| 56 | mActivity = activity; |
| 57 | mHost = host; |
| 58 | if (args != null && args.containsKey(KEY_ACCOUNT)) { |
| 59 | mAccount = args.getParcelable(KEY_ACCOUNT); |
| 60 | } else { |
| 61 | mAccount = null; |
| 62 | } |
| 63 | |
| 64 | if (args != null && args.containsKey(KEY_USER_HANDLE)) { |
| 65 | mUserHandle = args.getParcelable(KEY_USER_HANDLE); |
| 66 | } else { |
| 67 | mUserHandle = null; |
| 68 | } |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 69 | if (lifecycle != null) { |
| 70 | lifecycle.addObserver(this); |
| 71 | } |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean isAvailable() { |
| 76 | return mAccount != null && mUserHandle != null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String getPreferenceKey() { |
| 81 | return KEY_ACCOUNT_HEADER; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void displayPreference(PreferenceScreen screen) { |
| 86 | super.displayPreference(screen); |
Fan Zhang | 7db118e | 2019-02-14 12:25:29 -0800 | [diff] [blame] | 87 | mHeaderPreference = screen.findPreference(KEY_ACCOUNT_HEADER); |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 88 | } |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 89 | |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void onResume() { |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 92 | final AuthenticatorHelper helper = new AuthenticatorHelper(mContext, mUserHandle, null); |
| 93 | |
| 94 | EntityHeaderController |
Fan Zhang | 50eb81f | 2017-08-02 10:47:37 -0700 | [diff] [blame] | 95 | .newInstance(mActivity, mHost, mHeaderPreference.findViewById(R.id.entity_header)) |
Fan Zhang | 98289a8 | 2017-05-18 15:11:01 -0700 | [diff] [blame] | 96 | .setLabel(mAccount.name) |
| 97 | .setIcon(helper.getDrawableForType(mContext, mAccount.type)) |
| 98 | .done(mActivity, true /* rebindButtons */); |
| 99 | } |
| 100 | } |