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