AccountSelectorAdapter.java revision 187d0334849cfd922f0df05e57d77224f2f70378
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.CursorLoader;
24import android.content.Loader;
25import android.database.Cursor;
26import android.net.Uri;
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 *
40 * TODO We actually don't need the auto-requery.  Just refresh it when we modify accounts.
41 */
42public class AccountSelectorAdapter extends CursorAdapter {
43    private static final String[] PROJECTION = new String[] {
44        EmailContent.RECORD_ID,
45        EmailContent.Account.DISPLAY_NAME,
46        EmailContent.Account.EMAIL_ADDRESS
47    };
48
49    private static final int ID_COLUMN = 0;
50    private static final int DISPLAY_NAME_COLUMN = 1;
51    private static final int EMAIL_ADDRESS_COLUMN = 2;
52
53    /** Sort order.  Show the default account first. */
54    private static final String ORDER_BY =
55            EmailContent.Account.IS_DEFAULT + " desc, " + EmailContent.Account.RECORD_ID;
56
57    public static Loader<Cursor> createLoader(Context context) {
58        return new NoAutoRequeryCursorLoader(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    }
65
66    @Override
67    public void bindView(View view, Context context, Cursor cursor) {
68        TextView v = (TextView) view;
69        v.setText(cursor.getString(EMAIL_ADDRESS_COLUMN));
70    }
71
72    @Override
73    public View newView(Context context, Cursor cursor, ViewGroup parent) {
74        return new TextView(context);
75    }
76
77    /** @return Account id extracted from a Cursor. */
78    public static long getAccountId(Cursor c) {
79        return c.getLong(ID_COLUMN);
80    }
81}
82