AccountSelectorAdapter.java revision c3b3b7a0a3eedccfeba78aea42f1143d0298489f
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    };
45
46    private static final int ID_COLUMN = 0;
47    private static final int DISPLAY_NAME_COLUMN = 1;
48
49    /** Sort order.  Show the default account first. */
50    private static final String ORDER_BY =
51            EmailContent.Account.IS_DEFAULT + " desc, " + EmailContent.Account.RECORD_ID;
52
53    private final LayoutInflater mInflater;
54
55    public static Loader<Cursor> createLoader(Context context) {
56        return new ThrottlingCursorLoader(context, EmailContent.Account.CONTENT_URI, PROJECTION,
57                null, null, ORDER_BY);
58    }
59
60    public AccountSelectorAdapter(Context context, Cursor c) {
61        super(context, c, 0 /* no auto-requery */);
62        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
63    }
64
65    @Override
66    public View getDropDownView(int position, View convertView, ViewGroup parent) {
67        View view = mInflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
68        TextView textView = (TextView) view.findViewById(android.R.id.text1);
69        textView.setText(getAccountDisplayName(position));
70        return view;
71    }
72
73    @Override
74    public void bindView(View view, Context context, Cursor cursor) {
75        TextView textView = (TextView) view.findViewById(android.R.id.text1);
76        textView.setText(getAccountDisplayName(cursor));
77    }
78
79    @Override
80    public View newView(Context context, Cursor cursor, ViewGroup parent) {
81        return mInflater.inflate(android.R.layout.simple_spinner_item, null);
82    }
83
84    /** @return Account id extracted from a Cursor. */
85    public static long getAccountId(Cursor c) {
86        return c.getLong(ID_COLUMN);
87    }
88
89    private String getAccountDisplayName(int position) {
90        final Cursor c = getCursor();
91        return c.moveToPosition(position) ? getAccountDisplayName(c) : null;
92    }
93
94    /** @return Account name extracted from a Cursor. */
95    public static String getAccountDisplayName(Cursor cursor) {
96        return cursor.getString(DISPLAY_NAME_COLUMN);
97    }
98}
99