blob: 68cdd230fc894d9fa0762e517bfc686553e60aa0 [file] [log] [blame]
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -08001/*******************************************************************************
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.providers;
19
Mindy Pereirae0828392012-03-08 10:38:40 -080020import android.content.Context;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080021import android.database.Cursor;
Mindy Pereiraa5603f32012-03-08 16:15:15 -080022import android.graphics.Color;
23import android.graphics.drawable.PaintDrawable;
Mindy Pereiracfb7f332012-02-28 10:23:43 -080024import android.net.Uri;
Mindy Pereira68f2e222012-03-07 10:36:54 -080025import android.net.Uri.Builder;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080026import android.os.Parcel;
27import android.os.Parcelable;
Marc Blank48a4aed2012-03-10 14:07:00 -080028import android.os.Parcelable.Creator;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080029import android.text.TextUtils;
Mindy Pereiraa5603f32012-03-08 16:15:15 -080030import android.view.View;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080031
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080032import com.android.mail.utils.LogUtils;
Marc Blank9ace18a2012-02-21 16:34:07 -080033import com.google.common.collect.Maps;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080034
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080035import java.util.Collection;
36import java.util.Map;
37import java.util.regex.Pattern;
38
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080039/**
40 * A folder is a collection of conversations, and perhaps other folders.
41 */
42public class Folder implements Parcelable {
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080043 /**
44 *
45 */
46 private static final String FOLDER_UNINITIALIZED = "Uninitialized!";
47
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080048 // Try to match the order of members with the order of constants in UIProvider.
49
50 /**
Mindy Pereirad92e9f32012-02-03 09:10:58 -080051 * Unique id of this folder.
52 */
Mindy Pereira67d75722012-02-13 10:46:21 -080053 public String id;
Mindy Pereirad92e9f32012-02-03 09:10:58 -080054
55 /**
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080056 * The content provider URI that returns this folder for this account.
57 */
Mindy Pereiracfb7f332012-02-28 10:23:43 -080058 public Uri uri;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080059
60 /**
61 * The human visible name for this folder.
62 */
63 public String name;
64
65 /**
66 * The possible capabilities that this folder supports.
67 */
68 public int capabilities;
69
70 /**
71 * Whether or not this folder has children folders.
72 */
73 public boolean hasChildren;
74
75 /**
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080076 * How large the synchronization window is: how many days worth of data is retained on the
77 * device.
78 */
79 public int syncWindow;
80
81 /**
82 * The content provider URI to return the list of conversations in this
83 * folder.
84 */
Mindy Pereiracfb7f332012-02-28 10:23:43 -080085 public Uri conversationListUri;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080086
87 /**
88 * The content provider URI to return the list of child folders of this folder.
89 */
Mindy Pereiracfb7f332012-02-28 10:23:43 -080090 public Uri childFoldersListUri;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -080091
92 /**
93 * The number of messages that are unread in this folder.
94 */
95 public int unreadCount;
96
97 /**
98 * The total number of messages in this folder.
99 */
100 public int totalCount;
101
102 /**
Mindy Pereira9c002102012-02-17 14:45:58 -0800103 * The content provider URI to force a refresh of this folder.
104 */
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800105 public Uri refreshUri;
Mindy Pereira9c002102012-02-17 14:45:58 -0800106
107 /**
Marc Blank9ace18a2012-02-21 16:34:07 -0800108 * The current sync status of the folder
Mindy Pereira77528642012-02-17 15:51:10 -0800109 */
Marc Blank9ace18a2012-02-21 16:34:07 -0800110 public int syncStatus;
111
112 /**
113 * The result of the last sync for this folder
114 */
115 public int lastSyncResult;
Mindy Pereira77528642012-02-17 15:51:10 -0800116
117 /**
Mindy Pereira78664722012-03-05 13:53:07 -0800118 * Folder type. 0 is default.
119 */
120 public int type;
121
122 /**
123 * Icon for this folder; 0 implies no icon.
124 */
125 public long iconResId;
126
Mindy Pereira9275a062012-03-08 15:12:14 -0800127 public String bgColor;
128 public String fgColor;
129
Mindy Pereira78664722012-03-05 13:53:07 -0800130 /**
Marc Blank48a4aed2012-03-10 14:07:00 -0800131 * The content provider URI to request additional conversations
132 */
133 public Uri loadMoreUri;
134
135 /**
Mindy Pereira518ee422012-02-23 18:38:23 -0800136 * Total number of members that comprise an instance of a folder. This is
137 * the number of members that need to be serialized or parceled.
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800138 */
Mindy Pereira518ee422012-02-23 18:38:23 -0800139 private static final int NUMBER_MEMBERS = UIProvider.FOLDERS_PROJECTION.length;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800140
141 /**
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800142 * Used only for debugging.
143 */
144 private static final String LOG_TAG = new LogUtils().getLogTag();
145
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800146 /**
Mindy Pereira518ee422012-02-23 18:38:23 -0800147 * Examples of expected format for the joined folder strings
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800148 *
Mindy Pereira518ee422012-02-23 18:38:23 -0800149 * Example of a joined folder string:
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800150 * 630107622^*^^i^*^^i^*^0
151 * <id>^*^<canonical name>^*^<name>^*^<color index>
152 *
Mindy Pereira518ee422012-02-23 18:38:23 -0800153 * The sqlite queries will return a list of folder strings separated with "^**^"
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800154 * Example of a query result:
155 * 630107622^*^^i^*^^i^*^0^**^630107626^*^^u^*^^u^*^0^**^630107627^*^^f^*^^f^*^0
156 */
Mindy Pereira518ee422012-02-23 18:38:23 -0800157 private static final String FOLDER_COMPONENT_SEPARATOR = "^*^";
158 private static final Pattern FOLDER_COMPONENT_SEPARATOR_PATTERN =
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800159 Pattern.compile("\\^\\*\\^");
160
Mindy Pereirab5080d52012-03-09 11:26:44 -0800161 public static final String FOLDER_SEPARATOR = "^**^";
162 public static final Pattern FOLDER_SEPARATOR_PATTERN =
163 Pattern.compile("\\^\\*\\*\\^");
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800164
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800165 public Folder(Parcel in) {
Vikram Aggarwal27e85f22012-03-05 16:29:17 -0800166 assert (in.dataSize() == NUMBER_MEMBERS);
Mindy Pereira67d75722012-02-13 10:46:21 -0800167 id = in.readString();
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800168 uri = in.readParcelable(null);
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800169 name = in.readString();
170 capabilities = in.readInt();
171 // 1 for true, 0 for false.
172 hasChildren = in.readInt() == 1;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800173 syncWindow = in.readInt();
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800174 conversationListUri = in.readParcelable(null);
175 childFoldersListUri = in.readParcelable(null);
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800176 unreadCount = in.readInt();
177 totalCount = in.readInt();
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800178 refreshUri = in.readParcelable(null);
Marc Blank9ace18a2012-02-21 16:34:07 -0800179 syncStatus = in.readInt();
180 lastSyncResult = in.readInt();
Mindy Pereira78664722012-03-05 13:53:07 -0800181 type = in.readInt();
182 iconResId = in.readLong();
Mindy Pereira9275a062012-03-08 15:12:14 -0800183 bgColor = in.readString();
184 fgColor = in.readString();
Marc Blank48a4aed2012-03-10 14:07:00 -0800185 loadMoreUri = in.readParcelable(null);
Marc Blank9ace18a2012-02-21 16:34:07 -0800186 }
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800187
188 public Folder(Cursor cursor) {
Vikram Aggarwal27e85f22012-03-05 16:29:17 -0800189 assert (cursor.getColumnCount() == NUMBER_MEMBERS);
Mindy Pereira67d75722012-02-13 10:46:21 -0800190 id = cursor.getString(UIProvider.FOLDER_ID_COLUMN);
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800191 uri = Uri.parse(cursor.getString(UIProvider.FOLDER_URI_COLUMN));
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800192 name = cursor.getString(UIProvider.FOLDER_NAME_COLUMN);
193 capabilities = cursor.getInt(UIProvider.FOLDER_CAPABILITIES_COLUMN);
194 // 1 for true, 0 for false.
195 hasChildren = cursor.getInt(UIProvider.FOLDER_HAS_CHILDREN_COLUMN) == 1;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800196 syncWindow = cursor.getInt(UIProvider.FOLDER_SYNC_WINDOW_COLUMN);
Marc Blank48a4aed2012-03-10 14:07:00 -0800197 String convList = cursor.getString(UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN);
198 conversationListUri = !TextUtils.isEmpty(convList) ? Uri.parse(convList) : null;
199 String childList = cursor.getString(UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN);
200 childFoldersListUri = (hasChildren && !TextUtils.isEmpty(childList)) ? Uri.parse(childList)
201 : null;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800202 unreadCount = cursor.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN);
203 totalCount = cursor.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN);
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800204 String refresh = cursor.getString(UIProvider.FOLDER_REFRESH_URI_COLUMN);
205 refreshUri = !TextUtils.isEmpty(refresh) ? Uri.parse(refresh) : null;
Marc Blank9ace18a2012-02-21 16:34:07 -0800206 syncStatus = cursor.getInt(UIProvider.FOLDER_SYNC_STATUS_COLUMN);
207 lastSyncResult = cursor.getInt(UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN);
Mindy Pereira78664722012-03-05 13:53:07 -0800208 type = cursor.getInt(UIProvider.FOLDER_TYPE_COLUMN);
209 iconResId = cursor.getLong(UIProvider.FOLDER_ICON_RES_ID_COLUMN);
Mindy Pereira9275a062012-03-08 15:12:14 -0800210 bgColor = cursor.getString(UIProvider.FOLDER_BG_COLOR_COLUMN);
211 fgColor = cursor.getString(UIProvider.FOLDER_FG_COLOR_COLUMN);
Marc Blank48a4aed2012-03-10 14:07:00 -0800212 String loadMore = cursor.getString(UIProvider.FOLDER_LOAD_MORE_URI_COLUMN);
213 loadMoreUri = !TextUtils.isEmpty(loadMore) ? Uri.parse(loadMore) : null;
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800214 }
215
216 @Override
217 public void writeToParcel(Parcel dest, int flags) {
Mindy Pereira67d75722012-02-13 10:46:21 -0800218 dest.writeString(id);
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800219 dest.writeParcelable(uri, 0);
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800220 dest.writeString(name);
221 dest.writeInt(capabilities);
222 // 1 for true, 0 for false.
223 dest.writeInt(hasChildren ? 1 : 0);
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800224 dest.writeInt(syncWindow);
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800225 dest.writeParcelable(conversationListUri, 0);
226 dest.writeParcelable(childFoldersListUri, 0);
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800227 dest.writeInt(unreadCount);
228 dest.writeInt(totalCount);
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800229 dest.writeParcelable(refreshUri, 0);
Marc Blank9ace18a2012-02-21 16:34:07 -0800230 dest.writeInt(syncStatus);
231 dest.writeInt(lastSyncResult);
Mindy Pereira78664722012-03-05 13:53:07 -0800232 dest.writeInt(type);
233 dest.writeLong(iconResId);
Mindy Pereira9275a062012-03-08 15:12:14 -0800234 dest.writeString(bgColor);
235 dest.writeString(fgColor);
Marc Blank48a4aed2012-03-10 14:07:00 -0800236 dest.writeParcelable(loadMoreUri, 0);
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800237 }
238
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800239 /**
240 * Return a serialized String for this folder.
241 */
242 public synchronized String serialize(){
243 StringBuilder out = new StringBuilder();
Mindy Pereira518ee422012-02-23 18:38:23 -0800244 out.append(id).append(FOLDER_COMPONENT_SEPARATOR);
245 out.append(uri).append(FOLDER_COMPONENT_SEPARATOR);
246 out.append(name).append(FOLDER_COMPONENT_SEPARATOR);
247 out.append(capabilities).append(FOLDER_COMPONENT_SEPARATOR);
248 out.append(hasChildren ? "1": "0").append(FOLDER_COMPONENT_SEPARATOR);
249 out.append(syncWindow).append(FOLDER_COMPONENT_SEPARATOR);
250 out.append(conversationListUri).append(FOLDER_COMPONENT_SEPARATOR);
251 out.append(childFoldersListUri).append(FOLDER_COMPONENT_SEPARATOR);
252 out.append(unreadCount).append(FOLDER_COMPONENT_SEPARATOR);
253 out.append(totalCount).append(FOLDER_COMPONENT_SEPARATOR);
254 out.append(refreshUri).append(FOLDER_COMPONENT_SEPARATOR);
255 out.append(syncStatus).append(FOLDER_COMPONENT_SEPARATOR);
Mindy Pereira78664722012-03-05 13:53:07 -0800256 out.append(lastSyncResult).append(FOLDER_COMPONENT_SEPARATOR);
257 out.append(type).append(FOLDER_COMPONENT_SEPARATOR);
Mindy Pereira9275a062012-03-08 15:12:14 -0800258 out.append(iconResId).append(FOLDER_COMPONENT_SEPARATOR);
259 out.append(bgColor).append(FOLDER_COMPONENT_SEPARATOR);
Marc Blank48a4aed2012-03-10 14:07:00 -0800260 out.append(fgColor).append(FOLDER_COMPONENT_SEPARATOR);
261 out.append(loadMoreUri);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800262 return out.toString();
263 }
264
Mindy Pereirae0828392012-03-08 10:38:40 -0800265 /**
266 * Construct a folder that queries for search results. Do not call on the UI
267 * thread.
268 */
269 public static Folder forSearchResults(Account account, String query, Context context) {
270 Folder searchFolder = null;
Mindy Pereirab849dfb2012-03-07 18:13:15 -0800271 if (account.searchUri != null) {
272 Builder searchBuilder = account.searchUri.buildUpon();
273 searchBuilder.appendQueryParameter(UIProvider.SearchQueryParameters.QUERY, query);
274 Uri searchUri = searchBuilder.build();
Mindy Pereirae0828392012-03-08 10:38:40 -0800275 Cursor folderCursor = context.getContentResolver().query(searchUri,
276 UIProvider.FOLDERS_PROJECTION, null, null, null);
277 if (folderCursor != null) {
278 folderCursor.moveToFirst();
279 searchFolder = new Folder(folderCursor);
280 }
Mindy Pereirab849dfb2012-03-07 18:13:15 -0800281 }
Mindy Pereira68f2e222012-03-07 10:36:54 -0800282 return searchFolder;
283 }
284
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800285 /**
286 * Construct a new Folder instance from a previously serialized string.
287 * @param serializedFolder string obtained from {@link #serialize()} on a valid folder.
288 */
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800289 public Folder(String serializedFolder) {
Mindy Pereira518ee422012-02-23 18:38:23 -0800290 String[] folderMembers = TextUtils.split(serializedFolder,
291 FOLDER_COMPONENT_SEPARATOR_PATTERN);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800292 if (folderMembers.length != NUMBER_MEMBERS) {
Mindy Pereira518ee422012-02-23 18:38:23 -0800293 throw new IllegalArgumentException(
Mindy Pereirab5080d52012-03-09 11:26:44 -0800294 "Folder de-serializing failed. Wrong number of members detected."
295 + folderMembers.length);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800296 }
Mindy Pereira518ee422012-02-23 18:38:23 -0800297 id = folderMembers[0];
Mindy Pereiracfb7f332012-02-28 10:23:43 -0800298 uri = Uri.parse(folderMembers[1]);
Mindy Pereira518ee422012-02-23 18:38:23 -0800299 name = folderMembers[2];
300 capabilities = Integer.valueOf(folderMembers[3]);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800301 // 1 for true, 0 for false
Mindy Pereira518ee422012-02-23 18:38:23 -0800302 hasChildren = folderMembers[4] == "1";
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800303 syncWindow = Integer.valueOf(folderMembers[5]);
Marc Blank48a4aed2012-03-10 14:07:00 -0800304 String convList = folderMembers[6];
305 conversationListUri = !TextUtils.isEmpty(convList) ? Uri.parse(convList) : null;
306 String childList = folderMembers[7];
307 childFoldersListUri = (hasChildren && !TextUtils.isEmpty(childList)) ? Uri.parse(childList)
308 : null;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800309 unreadCount = Integer.valueOf(folderMembers[8]);
310 totalCount = Integer.valueOf(folderMembers[9]);
Marc Blank48a4aed2012-03-10 14:07:00 -0800311 String refresh = folderMembers[10];
312 refreshUri = !TextUtils.isEmpty(refresh) ? Uri.parse(refresh) : null;
Marc Blank9ace18a2012-02-21 16:34:07 -0800313 syncStatus = Integer.valueOf(folderMembers[11]);
314 lastSyncResult = Integer.valueOf(folderMembers[12]);
Mindy Pereira78664722012-03-05 13:53:07 -0800315 type = Integer.valueOf(folderMembers[13]);
316 iconResId = Long.valueOf(folderMembers[14]);
Mindy Pereira9275a062012-03-08 15:12:14 -0800317 bgColor = folderMembers[15];
318 fgColor = folderMembers[16];
Marc Blank48a4aed2012-03-10 14:07:00 -0800319 String loadMore = folderMembers[17];
320 loadMoreUri = !TextUtils.isEmpty(loadMore) ? Uri.parse(loadMore) : null;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800321 }
322
323 /**
324 * Constructor that leaves everything uninitialized. For use only by {@link #serialize()}
325 * which is responsible for filling in all the fields
326 */
Mindy Pereira9b875682012-02-15 18:10:54 -0800327 public Folder() {
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800328 name = FOLDER_UNINITIALIZED;
329 }
330
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800331 @SuppressWarnings("hiding")
332 public static final Creator<Folder> CREATOR = new Creator<Folder>() {
333 @Override
334 public Folder createFromParcel(Parcel source) {
335 return new Folder(source);
336 }
337
338 @Override
339 public Folder[] newArray(int size) {
340 return new Folder[size];
341 }
342 };
343
344 @Override
345 public int describeContents() {
346 // Return a sort of version number for this parcelable folder. Starting with zero.
347 return 0;
348 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800349
350 /**
351 * Create a Folder map from a string of serialized folders. This can only be done on the output
352 * of {@link #serialize(Map)}.
353 * @param serializedFolder A string obtained from {@link #serialize(Map)}
354 * @return a Map of folder name to folder.
355 */
356 public static Map<String, Folder> parseFoldersFromString(String serializedFolder) {
Mindy Pereira518ee422012-02-23 18:38:23 -0800357 LogUtils.d(LOG_TAG, "folder query result: %s", serializedFolder);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800358
359 Map<String, Folder> folderMap = Maps.newHashMap();
360 if (serializedFolder == null || serializedFolder == "") {
361 return folderMap;
362 }
363 String[] folderPieces = TextUtils.split(
Mindy Pereira518ee422012-02-23 18:38:23 -0800364 serializedFolder, FOLDER_COMPONENT_SEPARATOR_PATTERN);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800365 for (int i = 0, n = folderPieces.length; i < n; i++) {
366 Folder folder = new Folder(folderPieces[i]);
367 if (folder.name != FOLDER_UNINITIALIZED) {
368 folderMap.put(folder.name, folder);
369 }
370 }
371 return folderMap;
372 }
373
374 /**
Paul Westbrookc808fac2012-02-22 16:42:18 -0800375 * Returns a boolean indicating whether network activity (sync) is occuring for this folder.
376 */
377 public boolean isSyncInProgress() {
378 return 0 != (syncStatus & (UIProvider.SyncStatus.BACKGROUND_SYNC |
379 UIProvider.SyncStatus.USER_REFRESH |
380 UIProvider.SyncStatus.USER_QUERY |
381 UIProvider.SyncStatus.USER_MORE_RESULTS));
382 }
383
384 /**
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800385 * Serialize the given list of folders
386 * @param folderMap A valid map of folder names to Folders
387 * @return a string containing a serialized output of folder maps.
388 */
389 public static String serialize(Map<String, Folder> folderMap) {
390 Collection<Folder> folderCollection = folderMap.values();
391 Folder[] folderList = folderCollection.toArray(new Folder[]{} );
Mindy Pereira518ee422012-02-23 18:38:23 -0800392 int numFolders = folderList.length;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800393 StringBuilder result = new StringBuilder();
Mindy Pereira518ee422012-02-23 18:38:23 -0800394 for (int i = 0; i < numFolders; i++) {
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800395 if (i > 0) {
Mindy Pereira518ee422012-02-23 18:38:23 -0800396 result.append(FOLDER_SEPARATOR);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800397 }
398 Folder folder = folderList[i];
399 result.append(folder.serialize());
400 }
401 return result.toString();
402 }
Paul Westbrook334e64a2012-02-23 13:26:35 -0800403
404 public boolean supportsCapability(int capability) {
405 return (capabilities & capability) != 0;
406 }
Mindy Pereiraa5603f32012-03-08 16:15:15 -0800407
Mindy Pereira30fd47b2012-03-09 09:24:00 -0800408 // Show black text on a transparent swatch for system folders, effectively hiding the
Mindy Pereiraa5603f32012-03-08 16:15:15 -0800409 // swatch (see bug 2431925).
410 public static void setFolderBlockColor(Folder folder, View colorBlock) {
Mindy Pereira30fd47b2012-03-09 09:24:00 -0800411 final boolean showBg = !TextUtils.isEmpty(folder.bgColor);
Mindy Pereiraa5603f32012-03-08 16:15:15 -0800412 final int backgroundColor = showBg ? Color.parseColor(folder.bgColor) : 0;
413
414 if (!showBg) {
415 colorBlock.setBackgroundDrawable(null);
416 } else {
417 PaintDrawable paintDrawable = new PaintDrawable();
418 paintDrawable.getPaint().setColor(backgroundColor);
419 colorBlock.setBackgroundDrawable(paintDrawable);
420 }
421 }
Mindy Pereirab5080d52012-03-09 11:26:44 -0800422
423 /**
424 * Return if the type of the folder matches a provider defined folder.
425 */
426 public static boolean isProviderFolder(Folder folder) {
427 int type = folder.type;
428 return type == UIProvider.FolderType.DEFAULT ||
429 type == UIProvider.FolderType.INBOX ||
430 type == UIProvider.FolderType.DRAFT ||
431 type == UIProvider.FolderType.OUTBOX ||
432 type == UIProvider.FolderType.SENT ||
433 type == UIProvider.FolderType.TRASH ||
434 type == UIProvider.FolderType.SPAM;
435 }
Vikram Aggarwalff7d02a2012-01-11 16:37:45 -0800436}