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