Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 18 | package com.android.mail; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 19 | |
| 20 | import android.content.Context; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 24 | import android.widget.BaseAdapter; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 25 | import android.widget.TextView; |
| 26 | |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 27 | import com.android.mail.providers.Account; |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 28 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 29 | import com.android.mail.providers.UIProvider; |
Vikram Aggarwal | 1a4bcc0 | 2012-03-01 10:09:44 -0800 | [diff] [blame] | 30 | import com.android.mail.ui.RecentFolderList; |
Mindy Pereira | c1469c7 | 2012-03-20 13:56:12 -0700 | [diff] [blame] | 31 | import com.android.mail.utils.Utils; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 32 | |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 35 | /** |
Mindy Pereira | ae18020 | 2012-02-29 13:09:58 -0800 | [diff] [blame] | 36 | * An adapter to return the list of accounts and folders for the Account Spinner. |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 37 | * This class keeps the account and folder information and returns appropriate views. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 38 | */ |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 39 | public class AccountSpinnerAdapter extends BaseAdapter { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 40 | private final LayoutInflater mInflater; |
| 41 | /** |
| 42 | * The current account being viewed |
| 43 | */ |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 44 | private Account mCurrentAccount = null; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Total number of accounts. |
| 47 | */ |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 48 | private int mNumAccounts = 0; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 49 | /** |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 50 | * Array of all the accounts on the device. |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 51 | */ |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 52 | private Account[] mAccounts = new Account[0]; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 55 | * The name of the account is the 2nd column in {@link UIProvider#ACCOUNTS_PROJECTION} |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 56 | */ |
| 57 | static final int NAME_COLUMN = 2; |
| 58 | /** |
Vikram Aggarwal | 1a4bcc0 | 2012-03-01 10:09:44 -0800 | [diff] [blame] | 59 | * An object that provides a collection of recent folders, per account. |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 60 | */ |
Vikram Aggarwal | 9d6b6b0 | 2012-03-08 10:36:30 -0800 | [diff] [blame] | 61 | private final RecentFolderList mRecentFolders; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 62 | /** |
Vikram Aggarwal | 1a4bcc0 | 2012-03-01 10:09:44 -0800 | [diff] [blame] | 63 | * The actual collection of sorted recent folders obtained from {@link #mRecentFolders} |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 64 | */ |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 65 | private ArrayList<Folder> mRecentFolderList = new ArrayList<Folder>(); |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 66 | /** |
| 67 | * Boolean indicating whether the "Show All Folders" items should be shown |
| 68 | */ |
| 69 | private final boolean mShowAllFoldersItem; |
Vikram Aggarwal | 1a4bcc0 | 2012-03-01 10:09:44 -0800 | [diff] [blame] | 70 | |
| 71 | /** The folder currently being viewed */ |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 72 | private Folder mCurrentFolder; |
Mindy Pereira | c1469c7 | 2012-03-20 13:56:12 -0700 | [diff] [blame] | 73 | private Context mContext; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 74 | |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 75 | public static final int TYPE_ACCOUNT = 0; |
| 76 | public static final int TYPE_HEADER = 1; |
| 77 | public static final int TYPE_FOLDER = 2; |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 78 | public static final int TYPE_ALL_FOLDERS = 3; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 79 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 80 | /** |
| 81 | * There can be three types of views: Accounts (test@android.com, fifi@example.com), folders |
| 82 | * (Inbox, Outbox) or header and footer. This method returns the type of account at given |
| 83 | * position in the drop down list. |
| 84 | * @param position |
| 85 | * @return the type of account: one of {@link #TYPE_ACCOUNT}, {@link #TYPE_HEADER}, or |
| 86 | * {@link #TYPE_FOLDER}. |
| 87 | */ |
Marc Blank | 41e764d | 2012-03-20 20:46:28 -0700 | [diff] [blame] | 88 | public int getType(int position) { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 89 | // First the accounts |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 90 | if (position < mNumAccounts) { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 91 | return TYPE_ACCOUNT; |
| 92 | } |
| 93 | // Then the header |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 94 | if (position == mNumAccounts) { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 95 | return TYPE_HEADER; |
| 96 | } |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 97 | if (mShowAllFoldersItem) { |
| 98 | // The first few positions have accounts, and then the header. |
| 99 | final int offset = position - mNumAccounts - 1; |
| 100 | if (offset >= mRecentFolderList.size()) { |
| 101 | return TYPE_ALL_FOLDERS; |
| 102 | } |
| 103 | } |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 104 | // Finally, the recent folders. |
| 105 | return TYPE_FOLDER; |
| 106 | } |
| 107 | |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 108 | /** |
Vikram Aggarwal | e6340bc | 2012-03-26 15:57:09 -0700 | [diff] [blame] | 109 | * Returns the position of the dead, unselectable element in the spinner. |
| 110 | * @return |
| 111 | */ |
| 112 | public int getSpacerPosition() { |
| 113 | return mNumAccounts; |
| 114 | } |
| 115 | |
| 116 | /** |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 117 | * Create a spinner adapter with the context and the list of recent folders. |
| 118 | * @param context |
| 119 | * @param recentFolders |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 120 | * @param showAllFolders |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 121 | */ |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 122 | public AccountSpinnerAdapter(Context context, RecentFolderList recentFolders, |
| 123 | boolean showAllFolders) { |
Mindy Pereira | c1469c7 | 2012-03-20 13:56:12 -0700 | [diff] [blame] | 124 | mContext = context; |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 125 | mInflater = LayoutInflater.from(context); |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 126 | mRecentFolders = recentFolders; |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 127 | mShowAllFoldersItem = showAllFolders; |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Set the accounts for this spinner. |
| 132 | * @param accounts |
| 133 | */ |
| 134 | public void setAccounts(Account[] accounts) { |
| 135 | mAccounts = accounts; |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 136 | mNumAccounts = accounts.length; |
| 137 | notifyDataSetChanged(); |
| 138 | } |
| 139 | |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 140 | /** |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 141 | * Set the current folder. |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 142 | * @param folder |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 143 | * @return if changed. |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 144 | */ |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 145 | public boolean setCurrentFolder(Folder folder) { |
| 146 | if (folder != null && folder != mCurrentFolder) { |
| 147 | mCurrentFolder = folder; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 148 | mRecentFolderList = mRecentFolders.getRecentFolderList(folder); |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 149 | notifyDataSetChanged(); |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Set the current account. |
| 157 | * @param account |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 158 | * @return if changed. |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 159 | */ |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 160 | public boolean setCurrentAccount(Account account) { |
| 161 | if (account != null && !account.equals(mCurrentAccount)) { |
| 162 | mCurrentAccount = account; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 163 | mRecentFolderList = mRecentFolders.getRecentFolderList(mCurrentFolder); |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 164 | notifyDataSetChanged(); |
| 165 | return true; |
| 166 | } |
| 167 | return false; |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 170 | @Override |
| 171 | public int getCount() { |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 172 | // All the accounts, plus one header, plus recent folders, plus one if the |
| 173 | // "show all folders" item should be shown |
| 174 | return mNumAccounts + 1 + mRecentFolderList.size() + (mShowAllFoldersItem ? 1 : 0); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 177 | @Override |
| 178 | public Object getItem(int position) { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 179 | switch (getType(position)){ |
| 180 | case TYPE_ACCOUNT: |
| 181 | return getAccount(position); |
| 182 | case TYPE_HEADER: |
| 183 | return "account spinner header"; |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 184 | case TYPE_ALL_FOLDERS: |
| 185 | return "show all folders"; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 186 | default: |
Vikram Aggarwal | 1a4bcc0 | 2012-03-01 10:09:44 -0800 | [diff] [blame] | 187 | // The first few positions have accounts, and then the header. |
| 188 | final int offset = position - mNumAccounts - 1; |
Marc Blank | 41e764d | 2012-03-20 20:46:28 -0700 | [diff] [blame] | 189 | // Return the folder at this location. |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 190 | return mRecentFolderList.get(offset); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 191 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 194 | @Override |
| 195 | public long getItemId(int position) { |
| 196 | // We use the position as the ID |
| 197 | return position; |
| 198 | } |
| 199 | |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 200 | private String getFolderLabel() { |
| 201 | return mCurrentFolder != null ? mCurrentFolder.name : ""; |
| 202 | } |
| 203 | |
Mindy Pereira | e973f22 | 2012-03-06 14:29:03 -0800 | [diff] [blame] | 204 | private int getFolderUnreadCount() { |
| 205 | return mCurrentFolder != null ? mCurrentFolder.unreadCount : 0; |
| 206 | } |
| 207 | |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 208 | private String getCurrentAccountName() { |
| 209 | return mCurrentAccount != null ? mCurrentAccount.name : ""; |
| 210 | } |
| 211 | |
| 212 | // This call renders the view that will be shown in the header. |
| 213 | // Since we are tracking the current folder/ account, just |
| 214 | // always return what we believe that view is. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 215 | @Override |
| 216 | public View getView(int position, View convertView, ViewGroup parent) { |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 217 | convertView = mInflater.inflate(R.layout.account_switch_spinner_item, null); |
| 218 | ((TextView) convertView.findViewById(R.id.account_spinner_account_name)) |
| 219 | .setText(getCurrentAccountName()); |
| 220 | ((TextView) convertView.findViewById(R.id.account_spinner_folder)) |
| 221 | .setText(getFolderLabel()); |
| 222 | populateUnreadCountView((TextView) convertView.findViewById(R.id.unread), |
| 223 | getFolderUnreadCount()); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 224 | return convertView; |
| 225 | } |
| 226 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 227 | @Override |
| 228 | public int getViewTypeCount() { |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 229 | // Two views, and one header, and potentially one "show all folders" item |
| 230 | return 3 + (mShowAllFoldersItem ? 1 : 0); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 233 | @Override |
| 234 | public boolean hasStableIds() { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 235 | // The account manager could add new accounts, so don't claim that the IDs are stable. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 239 | @Override |
| 240 | public boolean isEmpty() { |
| 241 | // No item will be empty. |
| 242 | return false; |
| 243 | } |
| 244 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 245 | @Override |
| 246 | public View getDropDownView(int position, View convertView, ViewGroup parent) { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 247 | String textLabel = ""; |
| 248 | int unreadCount = 0; |
| 249 | switch (getType(position)) { |
| 250 | case TYPE_HEADER: |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 251 | convertView = mInflater.inflate(R.layout.account_switch_spinner_dropdown_header, |
| 252 | null); |
| 253 | final String label = getCurrentAccountName(); |
| 254 | TextView accountLabel = ((TextView) convertView.findViewById( |
| 255 | R.id.account_spinner_header_account)); |
| 256 | if (accountLabel != null) { |
| 257 | accountLabel.setText(label); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 258 | } |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 259 | return convertView; |
| 260 | case TYPE_ACCOUNT: |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 261 | textLabel = getAccountFolder(position); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 262 | break; |
| 263 | case TYPE_FOLDER: |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 264 | final int offset = position - mNumAccounts - 1; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 265 | final Folder folder = mRecentFolderList.get(offset); |
Vikram Aggarwal | 1a4bcc0 | 2012-03-01 10:09:44 -0800 | [diff] [blame] | 266 | textLabel = folder.name; |
| 267 | unreadCount = folder.unreadCount; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 268 | break; |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame^] | 269 | case TYPE_ALL_FOLDERS: |
| 270 | textLabel = mContext.getResources().getString(R.string.show_all_folders); |
| 271 | break; |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 272 | } |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 273 | convertView = mInflater.inflate(R.layout.account_switch_spinner_dropdown_item, null); |
| 274 | ((TextView) convertView.findViewById(R.id.account_spinner_accountname)).setText(textLabel); |
| 275 | populateUnreadCountView( |
| 276 | (TextView) convertView.findViewById(R.id.account_spinner_unread_count), |
| 277 | unreadCount); |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 278 | return convertView; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 279 | |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Mindy Pereira | a6be848 | 2012-03-21 10:31:57 -0700 | [diff] [blame] | 282 | |
| 283 | private void populateUnreadCountView(TextView unreadCountView, int unreadCount) { |
| 284 | unreadCountView.setText(Utils.getUnreadCountString(mContext, unreadCount)); |
| 285 | unreadCountView.setVisibility(unreadCount == 0 ? View.GONE : View.VISIBLE); |
| 286 | } |
| 287 | |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 288 | /** |
Mindy Pereira | ae18020 | 2012-02-29 13:09:58 -0800 | [diff] [blame] | 289 | * Returns the name of the folder at the given position in the spinner. |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 290 | * @param position |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 291 | * @return the folder of the account at the given position. |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 292 | */ |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 293 | private String getAccountFolder(int position) { |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 294 | if (position >= mNumAccounts) { |
Paul Westbrook | 9732bdc | 2012-02-21 11:02:37 -0800 | [diff] [blame] | 295 | return ""; |
| 296 | } |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 297 | return mAccounts[position].name; |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 300 | /** |
| 301 | * Returns the account given position in the spinner. |
| 302 | * @param position |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 303 | * @return the account at the given position. |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 304 | */ |
| 305 | private Account getAccount(int position) { |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 306 | return mAccounts[position]; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 309 | |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 310 | @Override |
| 311 | public boolean isEnabled(int position) { |
| 312 | // Don't want the user selecting the header. |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 313 | return (getType(position) != TYPE_HEADER); |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public boolean areAllItemsEnabled() { |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 318 | // The header is not enabled, so return false here. |
Vikram Aggarwal | 6f6afe5 | 2012-01-04 14:43:44 -0800 | [diff] [blame] | 319 | return false; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 320 | } |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * Notify that the folder has changed. |
| 324 | */ |
| 325 | public void onFolderUpdated(Folder folder) { |
| 326 | mCurrentFolder = folder; |
| 327 | notifyDataSetChanged(); |
| 328 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 329 | } |