MailboxMoveToAdapter.java revision 12b82d9374947c9268217f45befe8a74bd9b60d7
1/*
2 * Copyright (C) 2011 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.Email;
20import com.android.email.FolderProperties;
21import com.android.email.data.ThrottlingCursorLoader;
22import com.android.email.mail.Store;
23import com.android.emailcommon.Logging;
24import com.android.emailcommon.provider.EmailContent.Account;
25import com.android.emailcommon.provider.EmailContent.MailboxColumns;
26import com.android.emailcommon.provider.Mailbox;
27import com.android.emailcommon.utility.Utility;
28
29import android.content.Context;
30import android.content.Loader;
31import android.database.Cursor;
32import android.util.Log;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.view.ViewGroup;
36import android.widget.CursorAdapter;
37import android.widget.TextView;
38
39/**
40 * Cursor adapter for the "move to mailbox" dialog.
41 * TODO We've detached this class from {@link MailboxesAdapter} and {@link MailboxFragmentAdapter}.
42 * Depending upon the UX for the dialog and nested folders, we may want to bring these three
43 * adapter classes back into alignment.
44 */
45class MailboxMoveToAdapter extends CursorAdapter {
46    private static final String ALL_MAILBOX_SELECTION = MailboxColumns.ACCOUNT_KEY + "=?" +
47        " AND " + Mailbox.USER_VISIBLE_MAILBOX_SELECTION;
48    private static final String MOVE_TO_TARGET_MAILBOX_SELECTION =
49        MailboxColumns.TYPE + " NOT IN (" + Mailbox.TYPE_DRAFTS + "," +
50        Mailbox.TYPE_OUTBOX + "," + Mailbox.TYPE_SENT + "," + Mailbox.TYPE_TRASH + ") AND (" +
51        MailboxColumns.FLAGS + " & " + Mailbox.FLAG_ACCEPTS_MOVED_MAIL + " != 0)";
52    /** The main selection to populate the "move to" dialog */
53    private static final String MOVE_TO_SELECTION =
54        ALL_MAILBOX_SELECTION + " AND " + MOVE_TO_TARGET_MAILBOX_SELECTION;
55    /** Projection that uses the server id column as the mailbox name */
56    private static final String[] MOVE_TO_PROJECTION_SERVER_ID = new String[] {
57        MailboxColumns.ID,
58        MailboxColumns.ID + " AS org_mailbox_id",
59        MailboxColumns.SERVER_ID,
60        MailboxColumns.TYPE,
61    };
62    /** Projection that uses the display name column as the mailbox name */
63    private static final String[] MOVE_TO_PROJECTION_DISPLAY_NAME = new String[] {
64        MailboxColumns.ID,
65        MailboxColumns.ID + " AS org_mailbox_id",
66        MailboxColumns.DISPLAY_NAME,
67        MailboxColumns.TYPE,
68    };
69    /** Sort order for special mailboxes */
70    private static final String MOVE_TO_ORDER_BY_STATIC =
71        "CASE " + MailboxColumns.TYPE
72        + " WHEN " + Mailbox.TYPE_INBOX   + " THEN 0"
73        + " WHEN " + Mailbox.TYPE_JUNK    + " THEN 1"
74        + " ELSE 10 END";
75    /** Server id sort order */
76    private static final String MOVE_TO_ORDER_BY_SERVER_ID =
77        MOVE_TO_ORDER_BY_STATIC
78        // All other mailboxes are shown in alphabetical order.
79        + ", " + MailboxColumns.SERVER_ID;
80    /** Display name sort order */
81    private static final String MOVE_TO_ORDER_BY_DISPLAY_NAME =
82        MOVE_TO_ORDER_BY_STATIC
83        // All other mailboxes are shown in alphabetical order.
84        + ", " + MailboxColumns.DISPLAY_NAME;
85
86    // Column 0 is only for ListView; we don't use it in our code.
87    private static final int COLUMN_ID = 1;
88    private static final int COLUMN_MAILBOX_NAME = 2;
89    private static final int COLUMN_TYPE = 3;
90
91    /** Cached layout inflater */
92    private final LayoutInflater mInflater;
93
94    public MailboxMoveToAdapter(Context context) {
95        super(context, null, 0 /* flags; no content observer */);
96        mInflater = LayoutInflater.from(context);
97    }
98
99    @Override
100    public void bindView(View view, Context context, Cursor cursor) {
101        TextView t = (TextView) view;
102        t.setText(getDisplayText(context, cursor));
103    }
104
105    @Override
106    public View newView(Context context, Cursor cursor, ViewGroup parent) {
107        return mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
108    }
109
110    static Loader<Cursor> createLoader(Context context, long accountId) {
111        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
112            Log.d(Logging.LOG_TAG, "MailboxDialogAdapter#createLoader accountId=" + accountId);
113        }
114        return new MailboxMoveToLoader(context, accountId);
115    }
116
117    /**
118     * Returns the mailbox name to display in the dialog list. If the mailbox is of
119     * certain, well known, types, use a predefined name. Otherwise, use the server
120     * provided name.
121     */
122    private static String getDisplayText(Context context, Cursor cursor) {
123        final int type = cursor.getInt(COLUMN_TYPE);
124        final long mailboxId = cursor.getLong(COLUMN_ID);
125        String name = FolderProperties.getInstance(context).getDisplayName(type, mailboxId);
126        if (name == null) {
127            name = cursor.getString(COLUMN_MAILBOX_NAME);
128        }
129        return name;
130    }
131
132    /** Loader for the "move to mailbox" dialog. */
133    private static class MailboxMoveToLoader extends ThrottlingCursorLoader {
134        private final long mAccountId;
135        public MailboxMoveToLoader(Context context, long accountId) {
136            super(context, Mailbox.CONTENT_URI,
137                    null, MOVE_TO_SELECTION,
138                    new String[] { String.valueOf(accountId) }, null);
139            mAccountId = accountId;
140        }
141
142        @Override
143        public Cursor loadInBackground() {
144            // TODO Create a common way to store the fully qualified path name for all account types
145            final String protocol = Account.getProtocol(getContext(), mAccountId);
146            if (Store.STORE_SCHEME_EAS.equals(protocol)) {
147                // For EAS accounts; use the display name
148                setProjection(MOVE_TO_PROJECTION_DISPLAY_NAME);
149                setSortOrder(MOVE_TO_ORDER_BY_DISPLAY_NAME);
150            } else {
151                // For all other accounts; use the server id
152                setProjection(MOVE_TO_PROJECTION_SERVER_ID);
153                setSortOrder(MOVE_TO_ORDER_BY_SERVER_ID);
154            }
155            final Cursor mailboxesCursor = super.loadInBackground();
156            return Utility.CloseTraceCursorWrapper.get(mailboxesCursor);
157        }
158    }
159}
160