Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [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.contacts.editor; |
| 17 | |
| 18 | import android.content.Context; |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 19 | import android.os.Bundle; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 20 | import android.support.annotation.NonNull; |
| 21 | import android.support.annotation.StringRes; |
| 22 | import android.view.View; |
| 23 | import android.widget.AdapterView; |
| 24 | import android.widget.ImageView; |
| 25 | import android.widget.ListPopupWindow; |
| 26 | import android.widget.TextView; |
| 27 | |
| 28 | import com.android.contacts.R; |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 29 | import com.android.contacts.model.account.AccountInfo; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 30 | import com.android.contacts.model.account.AccountWithDataSet; |
| 31 | import com.android.contacts.util.AccountsListAdapter; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 32 | import com.android.contacts.util.UiClosables; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
| 37 | * Controls the display of an account selector or header. |
| 38 | * |
| 39 | * TODO: This was mostly copied from {@link RawContactEditorView}. The code in that class |
| 40 | * should probably be modified to use this instead of leaving it duplicated. |
| 41 | */ |
| 42 | public class AccountHeaderPresenter { |
| 43 | |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 44 | private static final String KEY_SELECTED_ACCOUNT = "accountHeaderSelectedAccount"; |
| 45 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 46 | public interface Observer { |
| 47 | void onChange(AccountHeaderPresenter sender); |
| 48 | |
| 49 | public static final Observer NONE = new Observer() { |
| 50 | @Override |
| 51 | public void onChange(AccountHeaderPresenter sender) { |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | private final Context mContext; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 57 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 58 | private List<AccountInfo> mAccounts; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 59 | private AccountWithDataSet mCurrentAccount; |
| 60 | |
| 61 | // Account header |
| 62 | private final View mAccountHeaderContainer; |
| 63 | private TextView mAccountHeaderType; |
| 64 | private TextView mAccountHeaderName; |
| 65 | private ImageView mAccountHeaderIcon; |
| 66 | private ImageView mAccountHeaderExpanderIcon; |
| 67 | |
| 68 | // This would be different if the account was readonly |
| 69 | @StringRes |
| 70 | private int mSelectorTitle = R.string.editor_account_selector_title; |
| 71 | |
| 72 | private Observer mObserver = Observer.NONE; |
| 73 | |
| 74 | public AccountHeaderPresenter(View container) { |
| 75 | mContext = container.getContext(); |
| 76 | mAccountHeaderContainer = container; |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 77 | // mAccountHeaderType is optional and may not be in the container view in which case |
| 78 | // the variable will be null |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 79 | mAccountHeaderType = (TextView) container.findViewById(R.id.account_type); |
| 80 | mAccountHeaderName = (TextView) container.findViewById(R.id.account_name); |
| 81 | mAccountHeaderIcon = (ImageView) container.findViewById(R.id.account_type_icon); |
| 82 | mAccountHeaderExpanderIcon = (ImageView) container.findViewById(R.id.account_expander_icon); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public void setObserver(Observer observer) { |
| 86 | mObserver = observer; |
| 87 | } |
| 88 | |
| 89 | public void setCurrentAccount(@NonNull AccountWithDataSet account) { |
| 90 | if (mCurrentAccount != null && mCurrentAccount.equals(account)) { |
| 91 | return; |
| 92 | } |
| 93 | mCurrentAccount = account; |
| 94 | if (mObserver != null) { |
| 95 | mObserver.onChange(this); |
| 96 | } |
| 97 | updateDisplayedAccount(); |
| 98 | } |
| 99 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 100 | public void setAccounts(List<AccountInfo> accounts) { |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 101 | mAccounts = accounts; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 102 | // If the current account was removed just switch to the next one in the list. |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 103 | if (mCurrentAccount != null && !AccountInfo.contains(mAccounts, mCurrentAccount)) { |
| 104 | mCurrentAccount = mAccounts.isEmpty() ? null : accounts.get(0).getAccount(); |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 105 | mObserver.onChange(this); |
| 106 | } |
| 107 | updateDisplayedAccount(); |
| 108 | } |
| 109 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 110 | public AccountWithDataSet getCurrentAccount() { |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 111 | return mCurrentAccount != null ? mCurrentAccount : null; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 114 | public void onSaveInstanceState(Bundle outState) { |
| 115 | outState.putParcelable(KEY_SELECTED_ACCOUNT, mCurrentAccount); |
| 116 | } |
| 117 | |
| 118 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 119 | if (savedInstanceState == null) return; |
| 120 | if (mCurrentAccount == null) { |
| 121 | mCurrentAccount = savedInstanceState.getParcelable(KEY_SELECTED_ACCOUNT); |
| 122 | } |
| 123 | updateDisplayedAccount(); |
| 124 | } |
| 125 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 126 | private void updateDisplayedAccount() { |
| 127 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 128 | if (mCurrentAccount == null) return; |
Marcus Hagerott | e7a71cb | 2016-12-09 16:26:14 -0800 | [diff] [blame] | 129 | if (mAccounts == null) return; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 130 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 131 | final String accountLabel = getAccountLabel(mCurrentAccount); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 132 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 133 | if (mAccounts.size() > 1) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 134 | addAccountSelector(accountLabel); |
| 135 | } else { |
| 136 | addAccountHeader(accountLabel); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private void addAccountHeader(String accountLabel) { |
| 141 | mAccountHeaderContainer.setVisibility(View.VISIBLE); |
| 142 | |
| 143 | // Set the account name |
| 144 | mAccountHeaderName.setVisibility(View.VISIBLE); |
| 145 | mAccountHeaderName.setText(accountLabel); |
| 146 | |
| 147 | // Set the account type |
| 148 | final String selectorTitle = mContext.getResources().getString(mSelectorTitle); |
Marcus Hagerott | 7217e69 | 2016-11-10 10:18:28 -0800 | [diff] [blame] | 149 | if (mAccountHeaderType != null) { |
| 150 | mAccountHeaderType.setText(selectorTitle); |
| 151 | } |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 152 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 153 | final AccountInfo accountInfo = AccountInfo.getAccount(mAccounts, mCurrentAccount); |
| 154 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 155 | // Set the icon |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 156 | mAccountHeaderIcon.setImageDrawable(accountInfo.getIcon()); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 157 | |
| 158 | // Set the content description |
| 159 | mAccountHeaderContainer.setContentDescription( |
| 160 | EditorUiUtils.getAccountInfoContentDescription(accountLabel, |
| 161 | selectorTitle)); |
| 162 | } |
| 163 | |
| 164 | private void addAccountSelector(CharSequence nameLabel) { |
| 165 | final View.OnClickListener onClickListener = new View.OnClickListener() { |
| 166 | @Override |
| 167 | public void onClick(View v) { |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 168 | showPopup(); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | setUpAccountSelector(nameLabel.toString(), onClickListener); |
| 172 | } |
| 173 | |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 174 | private void showPopup() { |
| 175 | final ListPopupWindow popup = new ListPopupWindow(mContext); |
| 176 | final AccountsListAdapter adapter = |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 177 | new AccountsListAdapter(mContext, mAccounts, mCurrentAccount); |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 178 | popup.setWidth(mAccountHeaderContainer.getWidth()); |
| 179 | popup.setAnchorView(mAccountHeaderContainer); |
| 180 | popup.setAdapter(adapter); |
| 181 | popup.setModal(true); |
| 182 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 183 | popup.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 184 | @Override |
| 185 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 186 | long id) { |
| 187 | UiClosables.closeQuietly(popup); |
| 188 | final AccountWithDataSet newAccount = adapter.getItem(position); |
| 189 | setCurrentAccount(newAccount); |
Marcus Hagerott | d3869c7 | 2016-11-21 11:13:44 -0800 | [diff] [blame] | 190 | // Make sure the new selection will be announced once it's changed |
| 191 | mAccountHeaderContainer.setAccessibilityLiveRegion( |
| 192 | View.ACCESSIBILITY_LIVE_REGION_POLITE); |
Marcus Hagerott | 73b283f | 2016-10-21 15:42:00 -0700 | [diff] [blame] | 193 | } |
| 194 | }); |
| 195 | mAccountHeaderContainer.post(new Runnable() { |
| 196 | @Override |
| 197 | public void run() { |
| 198 | popup.show(); |
| 199 | } |
| 200 | }); |
| 201 | } |
| 202 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 203 | private void setUpAccountSelector(String nameLabel, View.OnClickListener listener) { |
| 204 | addAccountHeader(nameLabel); |
| 205 | // Add handlers for choosing another account to save to. |
| 206 | mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE); |
Marcus Hagerott | 4b11232 | 2016-11-18 10:09:32 -0800 | [diff] [blame] | 207 | // Add the listener to the icon so that it will be announced by talkback as a clickable |
| 208 | // element |
| 209 | mAccountHeaderExpanderIcon.setOnClickListener(listener); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 210 | mAccountHeaderContainer.setOnClickListener(listener); |
| 211 | } |
| 212 | |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 213 | private String getAccountLabel(AccountWithDataSet account) { |
| 214 | final AccountInfo accountInfo = AccountInfo.getAccount(mAccounts, account); |
| 215 | return accountInfo != null ? accountInfo.getNameLabel().toString() : null; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 216 | } |
| 217 | } |