Folder.java revision d92e9f3d45cfedd3ebfedcf0805283007053e091
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.Maps; 21 22import android.database.Cursor; 23import android.os.Parcel; 24import android.os.Parcelable; 25import android.text.TextUtils; 26 27import com.android.mail.utils.LogUtils; 28 29import java.util.Collection; 30import java.util.Map; 31import java.util.regex.Pattern; 32 33/** 34 * A folder is a collection of conversations, and perhaps other folders. 35 */ 36public class Folder implements Parcelable { 37 /** 38 * 39 */ 40 private static final String FOLDER_UNINITIALIZED = "Uninitialized!"; 41 42 // Try to match the order of members with the order of constants in UIProvider. 43 44 /** 45 * Unique id of this folder. 46 */ 47 private long id; 48 49 /** 50 * The content provider URI that returns this folder for this account. 51 */ 52 public String uri; 53 54 /** 55 * The human visible name for this folder. 56 */ 57 public String name; 58 59 /** 60 * The possible capabilities that this folder supports. 61 */ 62 public int capabilities; 63 64 /** 65 * Whether or not this folder has children folders. 66 */ 67 public boolean hasChildren; 68 69 /** 70 * How often this folder should be synchronized with the server. 71 */ 72 public int syncFrequency; 73 74 /** 75 * How large the synchronization window is: how many days worth of data is retained on the 76 * device. 77 */ 78 public int syncWindow; 79 80 /** 81 * The content provider URI to return the list of conversations in this 82 * folder. 83 */ 84 public String conversationListUri; 85 86 /** 87 * The content provider URI to return the list of child folders of this folder. 88 */ 89 public String childFoldersListUri; 90 91 /** 92 * The number of messages that are unread in this folder. 93 */ 94 public int unreadCount; 95 96 /** 97 * The total number of messages in this folder. 98 */ 99 public int totalCount; 100 101 /** 102 * Total number of members that comprise an instance of a folder. Count up the members above. 103 * This is the number of members that need to be serialized or parceled. 104 */ 105 private static final int NUMBER_MEMBERS = 10; 106 107 /** 108 * Used only for debugging. 109 */ 110 private static final String LOG_TAG = new LogUtils().getLogTag(); 111 112 /** 113 * Examples of expected format for the joined label strings 114 * 115 * Example of a joined label string: 116 * 630107622^*^^i^*^^i^*^0 117 * <id>^*^<canonical name>^*^<name>^*^<color index> 118 * 119 * The sqlite queries will return a list of labels strings separated with "^**^" 120 * Example of a query result: 121 * 630107622^*^^i^*^^i^*^0^**^630107626^*^^u^*^^u^*^0^**^630107627^*^^f^*^^f^*^0 122 */ 123 private static final String LABEL_COMPONENT_SEPARATOR = "^*^"; 124 private static final Pattern LABEL_COMPONENT_SEPARATOR_PATTERN = 125 Pattern.compile("\\^\\*\\^"); 126 127 private static final String LABEL_SEPARATOR = "^**^"; 128 private static final Pattern LABEL_SEPARATOR_PATTERN = Pattern.compile("\\^\\*\\*\\^"); 129 130 public Folder(Parcel in) { 131 id = in.readLong(); 132 uri = in.readString(); 133 name = in.readString(); 134 capabilities = in.readInt(); 135 // 1 for true, 0 for false. 136 hasChildren = in.readInt() == 1; 137 syncFrequency = in.readInt(); 138 syncWindow = in.readInt(); 139 conversationListUri = in.readString(); 140 childFoldersListUri = in.readString(); 141 unreadCount = in.readInt(); 142 totalCount = in.readInt(); 143 } 144 145 public Folder(Cursor cursor) { 146 id = cursor.getLong(UIProvider.FOLDER_ID_COLUMN); 147 uri = cursor.getString(UIProvider.FOLDER_URI_COLUMN); 148 name = cursor.getString(UIProvider.FOLDER_NAME_COLUMN); 149 capabilities = cursor.getInt(UIProvider.FOLDER_CAPABILITIES_COLUMN); 150 // 1 for true, 0 for false. 151 hasChildren = cursor.getInt(UIProvider.FOLDER_HAS_CHILDREN_COLUMN) == 1; 152 syncFrequency = cursor.getInt(UIProvider.FOLDER_SYNC_FREQUENCY_COLUMN); 153 syncWindow = cursor.getInt(UIProvider.FOLDER_SYNC_WINDOW_COLUMN); 154 conversationListUri = cursor.getString(UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN); 155 childFoldersListUri = cursor.getString(UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN); 156 unreadCount = cursor.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN); 157 totalCount = cursor.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN); 158 } 159 160 @Override 161 public void writeToParcel(Parcel dest, int flags) { 162 dest.writeLong(id); 163 dest.writeString(uri); 164 dest.writeString(name); 165 dest.writeInt(capabilities); 166 // 1 for true, 0 for false. 167 dest.writeInt(hasChildren ? 1 : 0); 168 dest.writeInt(syncFrequency); 169 dest.writeInt(syncWindow); 170 dest.writeString(conversationListUri); 171 dest.writeString(childFoldersListUri); 172 dest.writeInt(unreadCount); 173 dest.writeInt(totalCount); 174 } 175 176 /** 177 * Return a serialized String for this folder. 178 */ 179 public synchronized String serialize(){ 180 StringBuilder out = new StringBuilder(); 181 out.append(id).append(LABEL_COMPONENT_SEPARATOR); 182 out.append(uri).append(LABEL_COMPONENT_SEPARATOR); 183 out.append(name).append(LABEL_COMPONENT_SEPARATOR); 184 out.append(capabilities).append(LABEL_COMPONENT_SEPARATOR); 185 out.append(hasChildren ? "1": "0").append(LABEL_COMPONENT_SEPARATOR); 186 out.append(syncFrequency).append(LABEL_COMPONENT_SEPARATOR); 187 out.append(syncWindow).append(LABEL_COMPONENT_SEPARATOR); 188 out.append(conversationListUri).append(LABEL_COMPONENT_SEPARATOR); 189 out.append(childFoldersListUri).append(LABEL_COMPONENT_SEPARATOR); 190 out.append(unreadCount).append(LABEL_COMPONENT_SEPARATOR); 191 out.append(totalCount).append(LABEL_COMPONENT_SEPARATOR); 192 return out.toString(); 193 } 194 195 /** 196 * Construct a new Folder instance from a previously serialized string. 197 * @param serializedFolder string obtained from {@link #serialize()} on a valid folder. 198 */ 199 private Folder(String serializedFolder){ 200 Folder out = new Folder(); 201 String[] folderMembers = TextUtils.split(serializedFolder, LABEL_SEPARATOR_PATTERN); 202 if (folderMembers.length != NUMBER_MEMBERS) { 203 // This is a problem. 204 // TODO(viki): Find out the appropriate exception for this. 205 return; 206 } 207 uri = folderMembers[0]; 208 name = folderMembers[1]; 209 capabilities = Integer.valueOf(folderMembers[2]); 210 // 1 for true, 0 for false 211 hasChildren = folderMembers[3] == "1"; 212 syncFrequency = Integer.valueOf(folderMembers[4]); 213 syncWindow = Integer.valueOf(folderMembers[5]); 214 conversationListUri = folderMembers[6]; 215 childFoldersListUri = folderMembers[7]; 216 unreadCount = Integer.valueOf(folderMembers[8]); 217 totalCount = Integer.valueOf(folderMembers[9]); 218 } 219 220 /** 221 * Constructor that leaves everything uninitialized. For use only by {@link #serialize()} 222 * which is responsible for filling in all the fields 223 */ 224 private Folder() { 225 name = FOLDER_UNINITIALIZED; 226 } 227 228 @SuppressWarnings("hiding") 229 public static final Creator<Folder> CREATOR = new Creator<Folder>() { 230 @Override 231 public Folder createFromParcel(Parcel source) { 232 return new Folder(source); 233 } 234 235 @Override 236 public Folder[] newArray(int size) { 237 return new Folder[size]; 238 } 239 }; 240 241 @Override 242 public int describeContents() { 243 // Return a sort of version number for this parcelable folder. Starting with zero. 244 return 0; 245 } 246 247 /** 248 * Create a Folder map from a string of serialized folders. This can only be done on the output 249 * of {@link #serialize(Map)}. 250 * @param serializedFolder A string obtained from {@link #serialize(Map)} 251 * @return a Map of folder name to folder. 252 */ 253 public static Map<String, Folder> parseFoldersFromString(String serializedFolder) { 254 LogUtils.d(LOG_TAG, "label query result: %s", serializedFolder); 255 256 Map<String, Folder> folderMap = Maps.newHashMap(); 257 if (serializedFolder == null || serializedFolder == "") { 258 return folderMap; 259 } 260 String[] folderPieces = TextUtils.split( 261 serializedFolder, LABEL_COMPONENT_SEPARATOR_PATTERN); 262 for (int i = 0, n = folderPieces.length; i < n; i++) { 263 Folder folder = new Folder(folderPieces[i]); 264 if (folder.name != FOLDER_UNINITIALIZED) { 265 folderMap.put(folder.name, folder); 266 } 267 } 268 return folderMap; 269 } 270 271 /** 272 * Serialize the given list of folders 273 * @param folderMap A valid map of folder names to Folders 274 * @return a string containing a serialized output of folder maps. 275 */ 276 public static String serialize(Map<String, Folder> folderMap) { 277 Collection<Folder> folderCollection = folderMap.values(); 278 Folder[] folderList = folderCollection.toArray(new Folder[]{} ); 279 int numLabels = folderList.length; 280 StringBuilder result = new StringBuilder(); 281 for (int i = 0; i < numLabels; i++) { 282 if (i > 0) { 283 result.append(LABEL_SEPARATOR); 284 } 285 Folder folder = folderList[i]; 286 result.append(folder.serialize()); 287 } 288 return result.toString(); 289 } 290 291} 292