AccountSelectorAdapter.java revision 7ab6f25e5417bdcfd514ddb57677f0e778aa6786
1/* 2 * Copyright (C) 2010 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 17package com.android.email.activity; 18 19import com.android.email.data.NoAutoRequeryCursorLoader; 20import com.android.email.provider.EmailContent; 21 22import android.content.Context; 23import android.content.Loader; 24import android.database.Cursor; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.ViewGroup; 28import android.widget.CursorAdapter; 29import android.widget.TextView; 30 31/** 32 * Adapter for the account selector on {@link MessageListXL}. 33 * 34 * TODO Test it! 35 * TODO Use layout? Or use the standard resources that ActionBarDemo uses? 36 * TODO Revisit the sort order when we get more detailed UI spec. (current sort order makes things 37 * simpler for now.) Maybe we can just use SimpleCursorAdapter. 38 * 39 * TODO We actually don't need the auto-requery. Just refresh it when we modify accounts. 40 */ 41public class AccountSelectorAdapter extends CursorAdapter { 42 private static final String[] PROJECTION = new String[] { 43 EmailContent.RECORD_ID, 44 EmailContent.Account.DISPLAY_NAME, 45 EmailContent.Account.EMAIL_ADDRESS 46 }; 47 48 private static final int ID_COLUMN = 0; 49 private static final int DISPLAY_NAME_COLUMN = 1; 50 private static final int EMAIL_ADDRESS_COLUMN = 2; 51 52 /** Sort order. Show the default account first. */ 53 private static final String ORDER_BY = 54 EmailContent.Account.IS_DEFAULT + " desc, " + EmailContent.Account.RECORD_ID; 55 56 private final LayoutInflater mInflater; 57 58 public static Loader<Cursor> createLoader(Context context) { 59 return new NoAutoRequeryCursorLoader(context, EmailContent.Account.CONTENT_URI, PROJECTION, 60 null, null, ORDER_BY); 61 } 62 63 public AccountSelectorAdapter(Context context, Cursor c) { 64 super(context, c, 0 /* no auto-requery */); 65 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 66 } 67 68 @Override 69 public View getDropDownView(int position, View convertView, ViewGroup parent) { 70 View view = mInflater.inflate(android.R.layout.simple_spinner_dropdown_item, null); 71 TextView textView = (TextView) view.findViewById(android.R.id.text1); 72 textView.setText(getAccountName(position)); 73 return view; 74 } 75 76 @Override 77 public void bindView(View view, Context context, Cursor cursor) { 78 TextView textView = (TextView) view.findViewById(android.R.id.text1); 79 textView.setText(getAccountName(cursor)); 80 } 81 82 @Override 83 public View newView(Context context, Cursor cursor, ViewGroup parent) { 84 return mInflater.inflate(android.R.layout.simple_spinner_item, null); 85 } 86 87 /** @return Account id extracted from a Cursor. */ 88 public static long getAccountId(Cursor c) { 89 return c.getLong(ID_COLUMN); 90 } 91 92 private String getAccountName(int position) { 93 final Cursor c = getCursor(); 94 return c.moveToPosition(position) ? getAccountName(c) : null; 95 } 96 97 /** @return Account name extracted from a Cursor. */ 98 public static String getAccountName(Cursor cursor) { 99 return cursor.getString(EMAIL_ADDRESS_COLUMN); 100 } 101} 102