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