FolderDisplayer.java revision 7bbd9438ba2e0489e6ff9e5e4f6e76e2eb2a9ee9
1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.ui;
19
20import com.android.mail.providers.Folder;
21import com.android.mail.utils.LogUtils;
22import com.android.mail.utils.Utils;
23import com.google.common.collect.Sets;
24
25import android.content.Context;
26import android.graphics.Color;
27import android.net.Uri;
28import android.os.Debug;
29import android.text.TextUtils;
30
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.SortedSet;
34
35/**
36 * Used to generate folder display information given a raw folders string.
37 * (The raw folders string can be obtained from {@link ConversationCursor#getRawFolders()}.)
38 *
39 */
40public class FolderDisplayer {
41    public static final String LOG_TAG = new LogUtils().getLogTag();
42    protected Context mContext;
43    protected String mAccount;
44    protected SortedSet<FolderValues> mFolderValuesSortedSet;
45
46    // Reference to the map of folder canonical name to folder id for the folders needed for
47    // the folder displayer.  This is set in loadConversationFolders
48    protected static class FolderValues implements Comparable<FolderValues> {
49        public final String colorId;
50
51        public final String name;
52
53        public final int folderId;
54
55        public final Uri folderUri;
56
57        public int backgroundColor;
58
59        public int textColor;
60
61        public boolean showBgColor;
62
63        public FolderValues(int id, Uri uri, String color, String n, String bgColor, String fgColor,
64                Context context) {
65            folderId = id;
66            colorId = color;
67            name = n;
68            folderUri = uri;
69            final boolean showBgColor = !TextUtils.isEmpty(bgColor);
70            if (showBgColor) {
71                backgroundColor = Integer.parseInt(bgColor);
72            } else {
73                backgroundColor = Utils.getDefaultFolderBackgroundColor(context);
74            }
75            // TODO(mindyp): add default fg text color and text color from preference.
76            final boolean showTextColor = !TextUtils.isEmpty(fgColor);
77            if (showTextColor) {
78                textColor = Integer.parseInt(fgColor);
79            } else {
80                textColor = Utils.getDefaultFolderTextColor(context);
81            }
82        }
83
84        @Override
85        public int compareTo(FolderValues another) {
86            return name.compareToIgnoreCase(another.name);
87        }
88    }
89
90    /**
91     * Initialize the FolderDisplayer for the specified account.
92     *
93     * @param context Context to use for loading string resources
94     * @param account
95     */
96    public void initialize(Context context, String account) {
97        mContext = context;
98        mAccount = account;
99    }
100
101    /**
102     * Configure the FolderDisplayer object by parsing the rawFolders string.
103     *
104     * @param folder string containing serialized folders to display.
105     */
106    public void loadConversationFolders(Folder ignoreFolder, String folderString) {
107        SortedSet<FolderValues> folderValuesSet = Sets.newTreeSet();
108        ArrayList<Folder> folders = new ArrayList<Folder>();
109        if (!TextUtils.isEmpty(folderString)) {
110            ArrayList<String> folderArray = new ArrayList<String>(Arrays.asList(TextUtils.split(
111                folderString, Folder.FOLDER_SEPARATOR_PATTERN)));
112            Folder f;
113            for (String folder : folderArray) {
114                f = new Folder(folder);
115                if (ignoreFolder == null || !ignoreFolder.uri.equals(f.uri)) {
116                    folders.add(f);
117                }
118            }
119        } else if (ignoreFolder != null) {
120            folders.add(ignoreFolder);
121        }
122        for (Folder folder : folders) {
123            int folderId = folder.id;
124            Uri uri = folder.uri;
125            String canonicalName = folder.name;
126            String colorId = folder.bgColor;
127
128            // We will sometimes see folders that the folder map does not yet know about or that
129            // do not have names yet.
130            if (TextUtils.isEmpty(canonicalName)) continue;
131            String stringToDisplay = null;
132
133            if (!Folder.isProviderFolder(folder)) {
134                stringToDisplay = folder.name;
135            }
136            stringToDisplay = folder.name;
137
138            if (stringToDisplay != null) {
139                folderValuesSet.add(
140                        new FolderValues(folderId, uri, colorId, stringToDisplay,
141                                folder.bgColor, folder.fgColor, mContext));
142            }
143        }
144
145        mFolderValuesSortedSet = folderValuesSet;
146    }
147}