Folder.java revision 5ff63747a1b5c6e2197528972cbc3ba808b09d8d
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.providers; 19 20import com.google.common.collect.Lists; 21import com.google.common.collect.Maps; 22 23import android.content.Context; 24import android.database.Cursor; 25import android.graphics.Color; 26import android.graphics.drawable.PaintDrawable; 27import android.net.Uri; 28import android.net.Uri.Builder; 29import android.os.Parcel; 30import android.os.Parcelable; 31import android.text.TextUtils; 32import android.view.View; 33 34import com.android.mail.utils.LogUtils; 35 36import java.util.Collection; 37import java.util.List; 38import java.util.Map; 39import java.util.regex.Pattern; 40 41/** 42 * A folder is a collection of conversations, and perhaps other folders. 43 */ 44public class Folder implements Parcelable, Comparable<Folder> { 45 /** 46 * 47 */ 48 private static final String FOLDER_UNINITIALIZED = "Uninitialized!"; 49 50 // Try to match the order of members with the order of constants in UIProvider. 51 52 /** 53 * Unique id of this folder. 54 */ 55 public int id; 56 57 /** 58 * The content provider URI that returns this folder for this account. 59 */ 60 public Uri uri; 61 62 /** 63 * The human visible name for this folder. 64 */ 65 public String name; 66 67 /** 68 * The possible capabilities that this folder supports. 69 */ 70 public int capabilities; 71 72 /** 73 * Whether or not this folder has children folders. 74 */ 75 public boolean hasChildren; 76 77 /** 78 * How large the synchronization window is: how many days worth of data is retained on the 79 * device. 80 */ 81 public int syncWindow; 82 83 /** 84 * The content provider URI to return the list of conversations in this 85 * folder. 86 */ 87 public Uri conversationListUri; 88 89 /** 90 * The content provider URI to return the list of child folders of this folder. 91 */ 92 public Uri childFoldersListUri; 93 94 /** 95 * The number of messages that are unread in this folder. 96 */ 97 public int unreadCount; 98 99 /** 100 * The total number of messages in this folder. 101 */ 102 public int totalCount; 103 104 /** 105 * The content provider URI to force a refresh of this folder. 106 */ 107 public Uri refreshUri; 108 109 /** 110 * The current sync status of the folder 111 */ 112 public int syncStatus; 113 114 /** 115 * The result of the last sync for this folder 116 */ 117 public int lastSyncResult; 118 119 /** 120 * Folder type. 0 is default. 121 */ 122 public int type; 123 124 /** 125 * Icon for this folder; 0 implies no icon. 126 */ 127 public long iconResId; 128 129 public String bgColor; 130 public String fgColor; 131 132 /** 133 * The content provider URI to request additional conversations 134 */ 135 public Uri loadMoreUri; 136 137 /** 138 * Total number of members that comprise an instance of a folder. This is 139 * the number of members that need to be serialized or parceled. 140 */ 141 private static final int NUMBER_MEMBERS = UIProvider.FOLDERS_PROJECTION.length; 142 143 /** 144 * Used only for debugging. 145 */ 146 private static final String LOG_TAG = new LogUtils().getLogTag(); 147 148 /** 149 * Examples of expected format for the joined folder strings 150 * 151 * Example of a joined folder string: 152 * 630107622^*^^i^*^^i^*^0 153 * <id>^*^<canonical name>^*^<name>^*^<color index> 154 * 155 * The sqlite queries will return a list of folder strings separated with "^**^" 156 * Example of a query result: 157 * 630107622^*^^i^*^^i^*^0^**^630107626^*^^u^*^^u^*^0^**^630107627^*^^f^*^^f^*^0 158 */ 159 private static final String FOLDER_COMPONENT_SEPARATOR = "^*^"; 160 private static final Pattern FOLDER_COMPONENT_SEPARATOR_PATTERN = 161 Pattern.compile("\\^\\*\\^"); 162 163 public static final String FOLDER_SEPARATOR = "^**^"; 164 public static final Pattern FOLDER_SEPARATOR_PATTERN = 165 Pattern.compile("\\^\\*\\*\\^"); 166 167 public Folder(Parcel in) { 168 assert (in.dataSize() == NUMBER_MEMBERS); 169 id = in.readInt(); 170 uri = in.readParcelable(null); 171 name = in.readString(); 172 capabilities = in.readInt(); 173 // 1 for true, 0 for false. 174 hasChildren = in.readInt() == 1; 175 syncWindow = in.readInt(); 176 conversationListUri = in.readParcelable(null); 177 childFoldersListUri = in.readParcelable(null); 178 unreadCount = in.readInt(); 179 totalCount = in.readInt(); 180 refreshUri = in.readParcelable(null); 181 syncStatus = in.readInt(); 182 lastSyncResult = in.readInt(); 183 type = in.readInt(); 184 iconResId = in.readLong(); 185 bgColor = in.readString(); 186 fgColor = in.readString(); 187 loadMoreUri = in.readParcelable(null); 188 } 189 190 public Folder(Cursor cursor) { 191 assert (cursor.getColumnCount() == NUMBER_MEMBERS); 192 id = cursor.getInt(UIProvider.FOLDER_ID_COLUMN); 193 uri = Uri.parse(cursor.getString(UIProvider.FOLDER_URI_COLUMN)); 194 name = cursor.getString(UIProvider.FOLDER_NAME_COLUMN); 195 capabilities = cursor.getInt(UIProvider.FOLDER_CAPABILITIES_COLUMN); 196 // 1 for true, 0 for false. 197 hasChildren = cursor.getInt(UIProvider.FOLDER_HAS_CHILDREN_COLUMN) == 1; 198 syncWindow = cursor.getInt(UIProvider.FOLDER_SYNC_WINDOW_COLUMN); 199 String convList = cursor.getString(UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN); 200 conversationListUri = !TextUtils.isEmpty(convList) ? Uri.parse(convList) : null; 201 String childList = cursor.getString(UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN); 202 childFoldersListUri = (hasChildren && !TextUtils.isEmpty(childList)) ? Uri.parse(childList) 203 : null; 204 unreadCount = cursor.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN); 205 totalCount = cursor.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN); 206 String refresh = cursor.getString(UIProvider.FOLDER_REFRESH_URI_COLUMN); 207 refreshUri = !TextUtils.isEmpty(refresh) ? Uri.parse(refresh) : null; 208 syncStatus = cursor.getInt(UIProvider.FOLDER_SYNC_STATUS_COLUMN); 209 lastSyncResult = cursor.getInt(UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN); 210 type = cursor.getInt(UIProvider.FOLDER_TYPE_COLUMN); 211 iconResId = cursor.getLong(UIProvider.FOLDER_ICON_RES_ID_COLUMN); 212 bgColor = cursor.getString(UIProvider.FOLDER_BG_COLOR_COLUMN); 213 fgColor = cursor.getString(UIProvider.FOLDER_FG_COLOR_COLUMN); 214 String loadMore = cursor.getString(UIProvider.FOLDER_LOAD_MORE_URI_COLUMN); 215 loadMoreUri = !TextUtils.isEmpty(loadMore) ? Uri.parse(loadMore) : null; 216 } 217 218 @Override 219 public void writeToParcel(Parcel dest, int flags) { 220 dest.writeInt(id); 221 dest.writeParcelable(uri, 0); 222 dest.writeString(name); 223 dest.writeInt(capabilities); 224 // 1 for true, 0 for false. 225 dest.writeInt(hasChildren ? 1 : 0); 226 dest.writeInt(syncWindow); 227 dest.writeParcelable(conversationListUri, 0); 228 dest.writeParcelable(childFoldersListUri, 0); 229 dest.writeInt(unreadCount); 230 dest.writeInt(totalCount); 231 dest.writeParcelable(refreshUri, 0); 232 dest.writeInt(syncStatus); 233 dest.writeInt(lastSyncResult); 234 dest.writeInt(type); 235 dest.writeLong(iconResId); 236 dest.writeString(bgColor); 237 dest.writeString(fgColor); 238 dest.writeParcelable(loadMoreUri, 0); 239 } 240 241 /** 242 * Return a serialized String for this folder. 243 */ 244 public synchronized String serialize() { 245 StringBuilder out = new StringBuilder(); 246 out.append(id).append(FOLDER_COMPONENT_SEPARATOR); 247 out.append(uri).append(FOLDER_COMPONENT_SEPARATOR); 248 out.append(name).append(FOLDER_COMPONENT_SEPARATOR); 249 out.append(capabilities).append(FOLDER_COMPONENT_SEPARATOR); 250 out.append(hasChildren ? "1": "0").append(FOLDER_COMPONENT_SEPARATOR); 251 out.append(syncWindow).append(FOLDER_COMPONENT_SEPARATOR); 252 out.append(conversationListUri).append(FOLDER_COMPONENT_SEPARATOR); 253 out.append(childFoldersListUri).append(FOLDER_COMPONENT_SEPARATOR); 254 out.append(unreadCount).append(FOLDER_COMPONENT_SEPARATOR); 255 out.append(totalCount).append(FOLDER_COMPONENT_SEPARATOR); 256 out.append(refreshUri).append(FOLDER_COMPONENT_SEPARATOR); 257 out.append(syncStatus).append(FOLDER_COMPONENT_SEPARATOR); 258 out.append(lastSyncResult).append(FOLDER_COMPONENT_SEPARATOR); 259 out.append(type).append(FOLDER_COMPONENT_SEPARATOR); 260 out.append(iconResId).append(FOLDER_COMPONENT_SEPARATOR); 261 out.append(bgColor).append(FOLDER_COMPONENT_SEPARATOR); 262 out.append(fgColor).append(FOLDER_COMPONENT_SEPARATOR); 263 out.append(loadMoreUri); 264 return out.toString(); 265 } 266 267 /** 268 * Construct a folder that queries for search results. Do not call on the UI 269 * thread. 270 */ 271 public static Folder forSearchResults(Account account, String query, Context context) { 272 Folder searchFolder = null; 273 if (account.searchUri != null) { 274 Builder searchBuilder = account.searchUri.buildUpon(); 275 searchBuilder.appendQueryParameter(UIProvider.SearchQueryParameters.QUERY, query); 276 Uri searchUri = searchBuilder.build(); 277 Cursor folderCursor = context.getContentResolver().query(searchUri, 278 UIProvider.FOLDERS_PROJECTION, null, null, null); 279 if (folderCursor != null) { 280 folderCursor.moveToFirst(); 281 searchFolder = new Folder(folderCursor); 282 } 283 } 284 return searchFolder; 285 } 286 287 public static List<Folder> forFoldersString(String foldersString) { 288 final List<Folder> folders = Lists.newArrayList(); 289 if (foldersString == null) { 290 return folders; 291 } 292 for (String folderStr : TextUtils.split(foldersString, FOLDER_SEPARATOR_PATTERN)) { 293 folders.add(new Folder(folderStr)); 294 } 295 return folders; 296 } 297 298 /** 299 * Construct a new Folder instance from a previously serialized string. 300 * @param serializedFolder string obtained from {@link #serialize()} on a valid folder. 301 */ 302 public Folder(String serializedFolder) { 303 String[] folderMembers = TextUtils.split(serializedFolder, 304 FOLDER_COMPONENT_SEPARATOR_PATTERN); 305 if (folderMembers.length != NUMBER_MEMBERS) { 306 throw new IllegalArgumentException( 307 "Folder de-serializing failed. Wrong number of members detected." 308 + folderMembers.length); 309 } 310 id = Integer.valueOf(folderMembers[0]); 311 uri = Uri.parse(folderMembers[1]); 312 name = folderMembers[2]; 313 capabilities = Integer.valueOf(folderMembers[3]); 314 // 1 for true, 0 for false 315 hasChildren = folderMembers[4] == "1"; 316 syncWindow = Integer.valueOf(folderMembers[5]); 317 String convList = folderMembers[6]; 318 conversationListUri = !TextUtils.isEmpty(convList) ? Uri.parse(convList) : null; 319 String childList = folderMembers[7]; 320 childFoldersListUri = (hasChildren && !TextUtils.isEmpty(childList)) ? Uri.parse(childList) 321 : null; 322 unreadCount = Integer.valueOf(folderMembers[8]); 323 totalCount = Integer.valueOf(folderMembers[9]); 324 String refresh = folderMembers[10]; 325 refreshUri = !TextUtils.isEmpty(refresh) ? Uri.parse(refresh) : null; 326 syncStatus = Integer.valueOf(folderMembers[11]); 327 lastSyncResult = Integer.valueOf(folderMembers[12]); 328 type = Integer.valueOf(folderMembers[13]); 329 iconResId = Long.valueOf(folderMembers[14]); 330 bgColor = folderMembers[15]; 331 fgColor = folderMembers[16]; 332 String loadMore = folderMembers[17]; 333 loadMoreUri = !TextUtils.isEmpty(loadMore) ? Uri.parse(loadMore) : null; 334 } 335 336 /** 337 * Constructor that leaves everything uninitialized. For use only by {@link #serialize()} 338 * which is responsible for filling in all the fields 339 */ 340 public Folder() { 341 name = FOLDER_UNINITIALIZED; 342 } 343 344 @SuppressWarnings("hiding") 345 public static final Creator<Folder> CREATOR = new Creator<Folder>() { 346 @Override 347 public Folder createFromParcel(Parcel source) { 348 return new Folder(source); 349 } 350 351 @Override 352 public Folder[] newArray(int size) { 353 return new Folder[size]; 354 } 355 }; 356 357 @Override 358 public int describeContents() { 359 // Return a sort of version number for this parcelable folder. Starting with zero. 360 return 0; 361 } 362 363 @Override 364 public boolean equals(Object o) { 365 if (o == null || !(o instanceof Folder)) { 366 return false; 367 } 368 final Uri otherUri = ((Folder) o).uri; 369 if (uri == null) { 370 return (otherUri == null); 371 } 372 return uri.equals(otherUri); 373 } 374 375 @Override 376 public int hashCode() { 377 return uri == null ? 0 : uri.hashCode(); 378 } 379 380 @Override 381 public int compareTo(Folder other) { 382 return name.compareToIgnoreCase(other.name); 383 } 384 385 /** 386 * Create a Folder map from a string of serialized folders. This can only be done on the output 387 * of {@link #serialize(Map)}. 388 * @param serializedFolder A string obtained from {@link #serialize(Map)} 389 * @return a Map of folder name to folder. 390 */ 391 public static Map<String, Folder> parseFoldersFromString(String serializedFolder) { 392 LogUtils.d(LOG_TAG, "folder query result: %s", serializedFolder); 393 394 Map<String, Folder> folderMap = Maps.newHashMap(); 395 if (serializedFolder == null || serializedFolder == "") { 396 return folderMap; 397 } 398 String[] folderPieces = TextUtils.split( 399 serializedFolder, FOLDER_COMPONENT_SEPARATOR_PATTERN); 400 for (int i = 0, n = folderPieces.length; i < n; i++) { 401 Folder folder = new Folder(folderPieces[i]); 402 if (folder.name != FOLDER_UNINITIALIZED) { 403 folderMap.put(folder.name, folder); 404 } 405 } 406 return folderMap; 407 } 408 409 /** 410 * Returns a boolean indicating whether network activity (sync) is occuring for this folder. 411 */ 412 public boolean isSyncInProgress() { 413 return 0 != (syncStatus & (UIProvider.SyncStatus.BACKGROUND_SYNC | 414 UIProvider.SyncStatus.USER_REFRESH | 415 UIProvider.SyncStatus.USER_QUERY | 416 UIProvider.SyncStatus.USER_MORE_RESULTS)); 417 } 418 419 /** 420 * Serialize the given list of folders 421 * @param folderMap A valid map of folder names to Folders 422 * @return a string containing a serialized output of folder maps. 423 */ 424 public static String serialize(Map<String, Folder> folderMap) { 425 Collection<Folder> folderCollection = folderMap.values(); 426 Folder[] folderList = folderCollection.toArray(new Folder[]{} ); 427 int numFolders = folderList.length; 428 StringBuilder result = new StringBuilder(); 429 for (int i = 0; i < numFolders; i++) { 430 if (i > 0) { 431 result.append(FOLDER_SEPARATOR); 432 } 433 Folder folder = folderList[i]; 434 result.append(folder.serialize()); 435 } 436 return result.toString(); 437 } 438 439 public boolean supportsCapability(int capability) { 440 return (capabilities & capability) != 0; 441 } 442 443 // Show black text on a transparent swatch for system folders, effectively hiding the 444 // swatch (see bug 2431925). 445 public static void setFolderBlockColor(Folder folder, View colorBlock) { 446 final boolean showBg = !TextUtils.isEmpty(folder.bgColor); 447 final int backgroundColor = showBg ? Color.parseColor(folder.bgColor) : 0; 448 449 if (!showBg) { 450 colorBlock.setBackgroundDrawable(null); 451 } else { 452 PaintDrawable paintDrawable = new PaintDrawable(); 453 paintDrawable.getPaint().setColor(backgroundColor); 454 colorBlock.setBackgroundDrawable(paintDrawable); 455 } 456 } 457 458 /** 459 * Return if the type of the folder matches a provider defined folder. 460 */ 461 public static boolean isProviderFolder(Folder folder) { 462 int type = folder.type; 463 return type == UIProvider.FolderType.DEFAULT || 464 type == UIProvider.FolderType.INBOX || 465 type == UIProvider.FolderType.DRAFT || 466 type == UIProvider.FolderType.OUTBOX || 467 type == UIProvider.FolderType.SENT || 468 type == UIProvider.FolderType.TRASH || 469 type == UIProvider.FolderType.SPAM; 470 } 471 472 public int getBackgroundColor(int defaultColor) { 473 return TextUtils.isEmpty(bgColor) ? defaultColor : Integer.parseInt(bgColor); 474 } 475 476 public int getForegroundColor(int defaultColor) { 477 return TextUtils.isEmpty(fgColor) ? defaultColor : Integer.parseInt(fgColor); 478 } 479 480} 481