FolderProperties.java revision 31d9acbf0623872f9d4a2b3210b5970854b654c7
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.Mailbox;
20
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.drawable.Drawable;
24
25
26// TODO When the UI is settled, cache all strings/drawables
27// TODO When the UI is settled, write up tests
28// TODO When the UI is settled, remove backward-compatibility methods
29public class FolderProperties {
30
31    private static FolderProperties sInstance;
32
33    private final Context mContext;
34
35    // Caches for frequently accessed resources.
36    private final String[] mSpecialMailbox;
37    private final TypedArray mSpecialMailboxDrawable;
38    private final Drawable mSummaryStarredMailboxDrawable;
39    private final Drawable mSummaryCombinedInboxDrawable;
40
41    private FolderProperties(Context context) {
42        mContext = context.getApplicationContext();
43        mSpecialMailbox = context.getResources().getStringArray(R.array.mailbox_display_names);
44        for (int i = 0; i < mSpecialMailbox.length; ++i) {
45            if ("".equals(mSpecialMailbox[i])) {
46                // there is no localized name, so use the display name from the server
47                mSpecialMailbox[i] = null;
48            }
49        }
50        mSpecialMailboxDrawable =
51            context.getResources().obtainTypedArray(R.array.mailbox_display_icons);
52        mSummaryStarredMailboxDrawable =
53            context.getResources().getDrawable(R.drawable.ic_folder_star_holo_light);
54        mSummaryCombinedInboxDrawable =
55            context.getResources().getDrawable(R.drawable.ic_list_combined_inbox);
56    }
57
58    public static synchronized FolderProperties getInstance(Context context) {
59        if (sInstance == null) {
60            sInstance = new FolderProperties(context);
61        }
62        return sInstance;
63    }
64
65    // For backward compatibility.
66    public String getDisplayName(int type) {
67        return getDisplayName(type, -1);
68    }
69
70    // For backward compatibility.
71    public Drawable getSummaryMailboxIconIds(long id) {
72        return getIcon(-1, id);
73    }
74
75    /**
76     * Lookup names of localized special mailboxes
77     */
78    public String getDisplayName(int type, long mailboxId) {
79        // Special combined mailboxes
80        int resId = 0;
81
82        // Can't use long for switch!?
83        if (mailboxId == Mailbox.QUERY_ALL_INBOXES) {
84            resId = R.string.account_folder_list_summary_inbox;
85        } else if (mailboxId == Mailbox.QUERY_ALL_FAVORITES) {
86            resId = R.string.account_folder_list_summary_starred;
87        } else if (mailboxId == Mailbox.QUERY_ALL_DRAFTS) {
88            resId = R.string.account_folder_list_summary_drafts;
89        } else if (mailboxId == Mailbox.QUERY_ALL_OUTBOX) {
90            resId = R.string.account_folder_list_summary_outbox;
91        }
92        if (resId != 0) {
93            return mContext.getString(resId);
94        }
95
96        if (type < mSpecialMailbox.length) {
97            return mSpecialMailbox[type];
98        }
99        return null;
100    }
101
102    /**
103     * Lookup icons of special mailboxes
104     */
105    public Drawable getIcon(int type, long mailboxId) {
106        if (mailboxId == Mailbox.QUERY_ALL_INBOXES) {
107            return mSummaryCombinedInboxDrawable;
108        } else if (mailboxId == Mailbox.QUERY_ALL_FAVORITES) {
109            return mSummaryStarredMailboxDrawable;
110        } else if (mailboxId == Mailbox.QUERY_ALL_DRAFTS) {
111            return mSpecialMailboxDrawable.getDrawable(Mailbox.TYPE_DRAFTS);
112        } else if (mailboxId == Mailbox.QUERY_ALL_OUTBOX) {
113            return mSpecialMailboxDrawable.getDrawable(Mailbox.TYPE_OUTBOX);
114        }
115        if (0 <= type && type < mSpecialMailboxDrawable.length()) {
116            final int resId = mSpecialMailboxDrawable.getResourceId(type, -1);
117            if (resId != -1) {
118                return mContext.getResources().getDrawable(resId);
119            }
120        }
121        return null; // No icon
122    }
123}
124
125