FolderProperties.java revision 4689cb7100a8b1cfa188499a9910912d15b3dad3
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;
18
19import com.android.emailcommon.provider.EmailContent.MailboxColumns;
20import com.android.emailcommon.provider.Mailbox;
21
22import android.content.Context;
23import android.content.res.TypedArray;
24import android.database.Cursor;
25import android.graphics.drawable.Drawable;
26
27
28// TODO When the UI is settled, cache all strings/drawables
29/**
30 * Stores names and icons for system folders. System folders are those with special
31 * meaning, such as Inbox, Drafts, Trash, etc... Although these folders may or may
32 * not exist on the server, we want to ensure they are displayed in a very specific
33 * manner.
34 *
35 * Some methods probably should belong to {@link Mailbox}, but as this class uses resources,
36 * we can't move them to emailcommon...
37 */
38public class FolderProperties {
39
40    private static FolderProperties sInstance;
41
42    private final Context mContext;
43
44    // Caches for frequently accessed resources.
45    private final String[] mSpecialMailbox;
46    private final TypedArray mSpecialMailboxDrawable;
47    private final Drawable mSummaryStarredMailboxDrawable;
48    private final Drawable mSummaryCombinedInboxDrawable;
49
50    private FolderProperties(Context context) {
51        mContext = context.getApplicationContext();
52        mSpecialMailbox = context.getResources().getStringArray(R.array.mailbox_display_names);
53        for (int i = 0; i < mSpecialMailbox.length; ++i) {
54            if ("".equals(mSpecialMailbox[i])) {
55                // there is no localized name, so use the display name from the server
56                mSpecialMailbox[i] = null;
57            }
58        }
59        mSpecialMailboxDrawable =
60            context.getResources().obtainTypedArray(R.array.mailbox_display_icons);
61        mSummaryStarredMailboxDrawable =
62            context.getResources().getDrawable(R.drawable.ic_folder_star_holo_light);
63        mSummaryCombinedInboxDrawable =
64            context.getResources().getDrawable(R.drawable.ic_list_combined_inbox);
65    }
66
67    public static synchronized FolderProperties getInstance(Context context) {
68        if (sInstance == null) {
69            sInstance = new FolderProperties(context);
70        }
71        return sInstance;
72    }
73
74    public String getCombinedMailboxName(long mailboxId) {
75        // Special combined mailboxes
76        int resId = 0;
77
78        // Can't use long for switch!?
79        if (mailboxId == Mailbox.QUERY_ALL_INBOXES) {
80            resId = R.string.account_folder_list_summary_inbox;
81        } else if (mailboxId == Mailbox.QUERY_ALL_FAVORITES) {
82            resId = R.string.account_folder_list_summary_starred;
83        } else if (mailboxId == Mailbox.QUERY_ALL_DRAFTS) {
84            resId = R.string.account_folder_list_summary_drafts;
85        } else if (mailboxId == Mailbox.QUERY_ALL_OUTBOX) {
86            resId = R.string.account_folder_list_summary_outbox;
87        }
88        if (resId != 0) {
89            return mContext.getString(resId);
90        }
91        return null;
92    }
93
94    /**
95     * Lookup names of localized special mailboxes
96     */
97    private String getDisplayName(int type, long mailboxId) {
98        String name = getCombinedMailboxName(mailboxId);
99
100        if ((name == null) && (type < mSpecialMailbox.length)) {
101            name = mSpecialMailbox[type];
102        }
103        return name;
104    }
105
106    /**
107     * Return the display name for a mailbox for UI.  For normal mailboxes, it just returns
108     * {@code originalDisplayName}, but for special mailboxes (such as combined mailboxes) it
109     * returns a name obtained from the resource.
110     *
111     * @param mailboxType Such as {@link Mailbox#TYPE_INBOX}
112     * @param mailboxId ID of a mailbox.
113     * @param originalDisplayName Display name of the mailbox stored in the database.
114     */
115    public String getDisplayName(int mailboxType, long mailboxId, String originalDisplayName) {
116        String name = getDisplayName(mailboxType, mailboxId);
117        if (name != null) {
118            return name;
119        }
120        return originalDisplayName;
121    }
122
123    /**
124     * Same as {@link #getDisplayName(int, long, String)}, but gets information form a mailbox
125     * cursor.  The cursor must contain the following columns:
126     * {@link MailboxColumns#ID}, {@link MailboxColumns#TYPE} and
127     * {@link MailboxColumns#DISPLAY_NAME}.
128     */
129    public String getDisplayName(Cursor mailboxCursor) {
130        final Cursor c = mailboxCursor;
131        return getDisplayName(
132                c.getInt(c.getColumnIndex(MailboxColumns.TYPE)),
133                c.getLong(c.getColumnIndex(MailboxColumns.ID)),
134                c.getString(c.getColumnIndex(MailboxColumns.DISPLAY_NAME))
135                );
136    }
137
138    /**
139     * Return the message count which should be shown with a mailbox name.  Depending on
140     * the mailbox type, we change what to show.
141     *
142     * @param mailboxType Such as {@link Mailbox#TYPE_INBOX}
143     * @param unreadCount Count obtained from {@link MailboxColumns#UNREAD_COUNT}
144     * @param totalCount Count obtained from {@link MailboxColumns#MESSAGE_COUNT}
145     */
146    public int getMessageCount(int mailboxType, int unreadCount, int totalCount) {
147        switch (mailboxType) {
148            case Mailbox.TYPE_DRAFTS:
149            case Mailbox.TYPE_OUTBOX:
150                return totalCount;
151            case Mailbox.TYPE_SENT:
152            case Mailbox.TYPE_TRASH:
153                return 0; // We don't show a count for sent/trash.
154        }
155        return unreadCount;
156    }
157
158    /**
159     * Same as {@link #getMessageCount(int, int, int)}, but gets information form a mailbox
160     * cursor.  The cursor must contain the following columns:
161     * {@link MailboxColumns#TYPE}, {@link MailboxColumns#UNREAD_COUNT} and
162     * {@link MailboxColumns#MESSAGE_COUNT}.
163     */
164    public int getMessageCount(Cursor mailboxCursor) {
165        final Cursor c = mailboxCursor;
166        return getMessageCount(
167                c.getInt(c.getColumnIndex(MailboxColumns.TYPE)),
168                c.getInt(c.getColumnIndex(MailboxColumns.UNREAD_COUNT)),
169                c.getInt(c.getColumnIndex(MailboxColumns.MESSAGE_COUNT))
170                );
171    }
172
173    /**
174     * Lookup icons of special mailboxes
175     */
176    public Drawable getIcon(int type, long mailboxId, int mailboxFlags) {
177        if (mailboxId == Mailbox.QUERY_ALL_INBOXES) {
178            return mSummaryCombinedInboxDrawable;
179        } else if (mailboxId == Mailbox.QUERY_ALL_FAVORITES) {
180            return mSummaryStarredMailboxDrawable;
181        } else if (mailboxId == Mailbox.QUERY_ALL_DRAFTS) {
182            return mSpecialMailboxDrawable.getDrawable(Mailbox.TYPE_DRAFTS);
183        } else if (mailboxId == Mailbox.QUERY_ALL_OUTBOX) {
184            return mSpecialMailboxDrawable.getDrawable(Mailbox.TYPE_OUTBOX);
185        }
186        if (0 <= type && type < mSpecialMailboxDrawable.length()) {
187            final int resId = mSpecialMailboxDrawable.getResourceId(type, -1);
188            if (resId != -1) {
189                return mContext.getResources().getDrawable(resId);
190            }
191        }
192        return null; // No icon
193    }
194}
195
196