Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 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 | |
| 18 | package com.android.mail.providers; |
| 19 | |
| 20 | import android.database.Cursor; |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 21 | import android.net.Uri; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 22 | import android.net.Uri.Builder; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
| 26 | |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 27 | import com.android.mail.utils.LogUtils; |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 28 | import com.google.common.collect.Maps; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 29 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 30 | import java.util.Collection; |
| 31 | import java.util.Map; |
| 32 | import java.util.regex.Pattern; |
| 33 | |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 34 | /** |
| 35 | * A folder is a collection of conversations, and perhaps other folders. |
| 36 | */ |
| 37 | public class Folder implements Parcelable { |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 38 | /** |
| 39 | * |
| 40 | */ |
| 41 | private static final String FOLDER_UNINITIALIZED = "Uninitialized!"; |
| 42 | |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 43 | // Try to match the order of members with the order of constants in UIProvider. |
| 44 | |
| 45 | /** |
Mindy Pereira | d92e9f3 | 2012-02-03 09:10:58 -0800 | [diff] [blame] | 46 | * Unique id of this folder. |
| 47 | */ |
Mindy Pereira | 67d7572 | 2012-02-13 10:46:21 -0800 | [diff] [blame] | 48 | public String id; |
Mindy Pereira | d92e9f3 | 2012-02-03 09:10:58 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 51 | * The content provider URI that returns this folder for this account. |
| 52 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 53 | public Uri uri; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * The human visible name for this folder. |
| 57 | */ |
| 58 | public String name; |
| 59 | |
| 60 | /** |
| 61 | * The possible capabilities that this folder supports. |
| 62 | */ |
| 63 | public int capabilities; |
| 64 | |
| 65 | /** |
| 66 | * Whether or not this folder has children folders. |
| 67 | */ |
| 68 | public boolean hasChildren; |
| 69 | |
| 70 | /** |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 71 | * How large the synchronization window is: how many days worth of data is retained on the |
| 72 | * device. |
| 73 | */ |
| 74 | public int syncWindow; |
| 75 | |
| 76 | /** |
| 77 | * The content provider URI to return the list of conversations in this |
| 78 | * folder. |
| 79 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 80 | public Uri conversationListUri; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * The content provider URI to return the list of child folders of this folder. |
| 84 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 85 | public Uri childFoldersListUri; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * The number of messages that are unread in this folder. |
| 89 | */ |
| 90 | public int unreadCount; |
| 91 | |
| 92 | /** |
| 93 | * The total number of messages in this folder. |
| 94 | */ |
| 95 | public int totalCount; |
| 96 | |
| 97 | /** |
Mindy Pereira | 9c00210 | 2012-02-17 14:45:58 -0800 | [diff] [blame] | 98 | * The content provider URI to force a refresh of this folder. |
| 99 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 100 | public Uri refreshUri; |
Mindy Pereira | 9c00210 | 2012-02-17 14:45:58 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 103 | * The current sync status of the folder |
Mindy Pereira | 7752864 | 2012-02-17 15:51:10 -0800 | [diff] [blame] | 104 | */ |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 105 | public int syncStatus; |
| 106 | |
| 107 | /** |
| 108 | * The result of the last sync for this folder |
| 109 | */ |
| 110 | public int lastSyncResult; |
Mindy Pereira | 7752864 | 2012-02-17 15:51:10 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
Mindy Pereira | 7866472 | 2012-03-05 13:53:07 -0800 | [diff] [blame] | 113 | * Folder type. 0 is default. |
| 114 | */ |
| 115 | public int type; |
| 116 | |
| 117 | /** |
| 118 | * Icon for this folder; 0 implies no icon. |
| 119 | */ |
| 120 | public long iconResId; |
| 121 | |
| 122 | /** |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 123 | * Total number of members that comprise an instance of a folder. This is |
| 124 | * the number of members that need to be serialized or parceled. |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 125 | */ |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 126 | private static final int NUMBER_MEMBERS = UIProvider.FOLDERS_PROJECTION.length; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 127 | |
| 128 | /** |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 129 | * Used only for debugging. |
| 130 | */ |
| 131 | private static final String LOG_TAG = new LogUtils().getLogTag(); |
| 132 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 133 | /** |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 134 | * Examples of expected format for the joined folder strings |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 135 | * |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 136 | * Example of a joined folder string: |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 137 | * 630107622^*^^i^*^^i^*^0 |
| 138 | * <id>^*^<canonical name>^*^<name>^*^<color index> |
| 139 | * |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 140 | * The sqlite queries will return a list of folder strings separated with "^**^" |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 141 | * Example of a query result: |
| 142 | * 630107622^*^^i^*^^i^*^0^**^630107626^*^^u^*^^u^*^0^**^630107627^*^^f^*^^f^*^0 |
| 143 | */ |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 144 | private static final String FOLDER_COMPONENT_SEPARATOR = "^*^"; |
| 145 | private static final Pattern FOLDER_COMPONENT_SEPARATOR_PATTERN = |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 146 | Pattern.compile("\\^\\*\\^"); |
| 147 | |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 148 | private static final String FOLDER_SEPARATOR = "^**^"; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 149 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 150 | public static final Uri SEARCH_RESULTS_URI = Uri.parse("content://fakeSearchResults/"); |
| 151 | |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 152 | public Folder(Parcel in) { |
Mindy Pereira | 67d7572 | 2012-02-13 10:46:21 -0800 | [diff] [blame] | 153 | id = in.readString(); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 154 | uri = in.readParcelable(null); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 155 | name = in.readString(); |
| 156 | capabilities = in.readInt(); |
| 157 | // 1 for true, 0 for false. |
| 158 | hasChildren = in.readInt() == 1; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 159 | syncWindow = in.readInt(); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 160 | conversationListUri = in.readParcelable(null); |
| 161 | childFoldersListUri = in.readParcelable(null); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 162 | unreadCount = in.readInt(); |
| 163 | totalCount = in.readInt(); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 164 | refreshUri = in.readParcelable(null); |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 165 | syncStatus = in.readInt(); |
| 166 | lastSyncResult = in.readInt(); |
Mindy Pereira | 7866472 | 2012-03-05 13:53:07 -0800 | [diff] [blame] | 167 | type = in.readInt(); |
| 168 | iconResId = in.readLong(); |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 169 | } |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 170 | |
| 171 | public Folder(Cursor cursor) { |
Mindy Pereira | 67d7572 | 2012-02-13 10:46:21 -0800 | [diff] [blame] | 172 | id = cursor.getString(UIProvider.FOLDER_ID_COLUMN); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 173 | uri = Uri.parse(cursor.getString(UIProvider.FOLDER_URI_COLUMN)); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 174 | name = cursor.getString(UIProvider.FOLDER_NAME_COLUMN); |
| 175 | capabilities = cursor.getInt(UIProvider.FOLDER_CAPABILITIES_COLUMN); |
| 176 | // 1 for true, 0 for false. |
| 177 | hasChildren = cursor.getInt(UIProvider.FOLDER_HAS_CHILDREN_COLUMN) == 1; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 178 | syncWindow = cursor.getInt(UIProvider.FOLDER_SYNC_WINDOW_COLUMN); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 179 | conversationListUri = Uri.parse(cursor |
| 180 | .getString(UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN)); |
| 181 | childFoldersListUri = hasChildren ? Uri.parse(cursor |
| 182 | .getString(UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN)) : null; |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 183 | unreadCount = cursor.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN); |
| 184 | totalCount = cursor.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 185 | String refresh = cursor.getString(UIProvider.FOLDER_REFRESH_URI_COLUMN); |
| 186 | refreshUri = !TextUtils.isEmpty(refresh) ? Uri.parse(refresh) : null; |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 187 | syncStatus = cursor.getInt(UIProvider.FOLDER_SYNC_STATUS_COLUMN); |
| 188 | lastSyncResult = cursor.getInt(UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN); |
Mindy Pereira | 7866472 | 2012-03-05 13:53:07 -0800 | [diff] [blame] | 189 | type = cursor.getInt(UIProvider.FOLDER_TYPE_COLUMN); |
| 190 | iconResId = cursor.getLong(UIProvider.FOLDER_ICON_RES_ID_COLUMN); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void writeToParcel(Parcel dest, int flags) { |
Mindy Pereira | 67d7572 | 2012-02-13 10:46:21 -0800 | [diff] [blame] | 195 | dest.writeString(id); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 196 | dest.writeParcelable(uri, 0); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 197 | dest.writeString(name); |
| 198 | dest.writeInt(capabilities); |
| 199 | // 1 for true, 0 for false. |
| 200 | dest.writeInt(hasChildren ? 1 : 0); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 201 | dest.writeInt(syncWindow); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 202 | dest.writeParcelable(conversationListUri, 0); |
| 203 | dest.writeParcelable(childFoldersListUri, 0); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 204 | dest.writeInt(unreadCount); |
| 205 | dest.writeInt(totalCount); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 206 | dest.writeParcelable(refreshUri, 0); |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 207 | dest.writeInt(syncStatus); |
| 208 | dest.writeInt(lastSyncResult); |
Mindy Pereira | 7866472 | 2012-03-05 13:53:07 -0800 | [diff] [blame] | 209 | dest.writeInt(type); |
| 210 | dest.writeLong(iconResId); |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Return a serialized String for this folder. |
| 215 | */ |
| 216 | public synchronized String serialize(){ |
| 217 | StringBuilder out = new StringBuilder(); |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 218 | out.append(id).append(FOLDER_COMPONENT_SEPARATOR); |
| 219 | out.append(uri).append(FOLDER_COMPONENT_SEPARATOR); |
| 220 | out.append(name).append(FOLDER_COMPONENT_SEPARATOR); |
| 221 | out.append(capabilities).append(FOLDER_COMPONENT_SEPARATOR); |
| 222 | out.append(hasChildren ? "1": "0").append(FOLDER_COMPONENT_SEPARATOR); |
| 223 | out.append(syncWindow).append(FOLDER_COMPONENT_SEPARATOR); |
| 224 | out.append(conversationListUri).append(FOLDER_COMPONENT_SEPARATOR); |
| 225 | out.append(childFoldersListUri).append(FOLDER_COMPONENT_SEPARATOR); |
| 226 | out.append(unreadCount).append(FOLDER_COMPONENT_SEPARATOR); |
| 227 | out.append(totalCount).append(FOLDER_COMPONENT_SEPARATOR); |
| 228 | out.append(refreshUri).append(FOLDER_COMPONENT_SEPARATOR); |
| 229 | out.append(syncStatus).append(FOLDER_COMPONENT_SEPARATOR); |
Mindy Pereira | 7866472 | 2012-03-05 13:53:07 -0800 | [diff] [blame] | 230 | out.append(lastSyncResult).append(FOLDER_COMPONENT_SEPARATOR); |
| 231 | out.append(type).append(FOLDER_COMPONENT_SEPARATOR); |
| 232 | out.append(iconResId); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 233 | return out.toString(); |
| 234 | } |
| 235 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 236 | public static Folder forSearchResults(Account account, String query) { |
| 237 | Folder searchFolder = new Folder(); |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame^] | 238 | if (account.searchUri != null) { |
| 239 | Builder searchBuilder = account.searchUri.buildUpon(); |
| 240 | searchBuilder.appendQueryParameter(UIProvider.SearchQueryParameters.QUERY, query); |
| 241 | Uri searchUri = searchBuilder.build(); |
| 242 | searchFolder.uri = SEARCH_RESULTS_URI; |
| 243 | searchFolder.conversationListUri = searchUri; |
| 244 | searchFolder.refreshUri = searchUri; |
| 245 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 246 | return searchFolder; |
| 247 | } |
| 248 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Construct a new Folder instance from a previously serialized string. |
| 251 | * @param serializedFolder string obtained from {@link #serialize()} on a valid folder. |
| 252 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 253 | public Folder(String serializedFolder) { |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 254 | String[] folderMembers = TextUtils.split(serializedFolder, |
| 255 | FOLDER_COMPONENT_SEPARATOR_PATTERN); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 256 | if (folderMembers.length != NUMBER_MEMBERS) { |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 257 | throw new IllegalArgumentException( |
| 258 | "Folder de-serializing failed. Wrong number of members detected."); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 259 | } |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 260 | id = folderMembers[0]; |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 261 | uri = Uri.parse(folderMembers[1]); |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 262 | name = folderMembers[2]; |
| 263 | capabilities = Integer.valueOf(folderMembers[3]); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 264 | // 1 for true, 0 for false |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 265 | hasChildren = folderMembers[4] == "1"; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 266 | syncWindow = Integer.valueOf(folderMembers[5]); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 267 | conversationListUri = Uri.parse(folderMembers[6]); |
| 268 | childFoldersListUri = hasChildren ? Uri.parse(folderMembers[7]) : null; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 269 | unreadCount = Integer.valueOf(folderMembers[8]); |
| 270 | totalCount = Integer.valueOf(folderMembers[9]); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 271 | refreshUri = Uri.parse(folderMembers[10]); |
Marc Blank | 9ace18a | 2012-02-21 16:34:07 -0800 | [diff] [blame] | 272 | syncStatus = Integer.valueOf(folderMembers[11]); |
| 273 | lastSyncResult = Integer.valueOf(folderMembers[12]); |
Mindy Pereira | 7866472 | 2012-03-05 13:53:07 -0800 | [diff] [blame] | 274 | type = Integer.valueOf(folderMembers[13]); |
| 275 | iconResId = Long.valueOf(folderMembers[14]); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Constructor that leaves everything uninitialized. For use only by {@link #serialize()} |
| 280 | * which is responsible for filling in all the fields |
| 281 | */ |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 282 | public Folder() { |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 283 | name = FOLDER_UNINITIALIZED; |
| 284 | } |
| 285 | |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 286 | @SuppressWarnings("hiding") |
| 287 | public static final Creator<Folder> CREATOR = new Creator<Folder>() { |
| 288 | @Override |
| 289 | public Folder createFromParcel(Parcel source) { |
| 290 | return new Folder(source); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public Folder[] newArray(int size) { |
| 295 | return new Folder[size]; |
| 296 | } |
| 297 | }; |
| 298 | |
| 299 | @Override |
| 300 | public int describeContents() { |
| 301 | // Return a sort of version number for this parcelable folder. Starting with zero. |
| 302 | return 0; |
| 303 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * Create a Folder map from a string of serialized folders. This can only be done on the output |
| 307 | * of {@link #serialize(Map)}. |
| 308 | * @param serializedFolder A string obtained from {@link #serialize(Map)} |
| 309 | * @return a Map of folder name to folder. |
| 310 | */ |
| 311 | public static Map<String, Folder> parseFoldersFromString(String serializedFolder) { |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 312 | LogUtils.d(LOG_TAG, "folder query result: %s", serializedFolder); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 313 | |
| 314 | Map<String, Folder> folderMap = Maps.newHashMap(); |
| 315 | if (serializedFolder == null || serializedFolder == "") { |
| 316 | return folderMap; |
| 317 | } |
| 318 | String[] folderPieces = TextUtils.split( |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 319 | serializedFolder, FOLDER_COMPONENT_SEPARATOR_PATTERN); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 320 | for (int i = 0, n = folderPieces.length; i < n; i++) { |
| 321 | Folder folder = new Folder(folderPieces[i]); |
| 322 | if (folder.name != FOLDER_UNINITIALIZED) { |
| 323 | folderMap.put(folder.name, folder); |
| 324 | } |
| 325 | } |
| 326 | return folderMap; |
| 327 | } |
| 328 | |
| 329 | /** |
Paul Westbrook | c808fac | 2012-02-22 16:42:18 -0800 | [diff] [blame] | 330 | * Returns a boolean indicating whether network activity (sync) is occuring for this folder. |
| 331 | */ |
| 332 | public boolean isSyncInProgress() { |
| 333 | return 0 != (syncStatus & (UIProvider.SyncStatus.BACKGROUND_SYNC | |
| 334 | UIProvider.SyncStatus.USER_REFRESH | |
| 335 | UIProvider.SyncStatus.USER_QUERY | |
| 336 | UIProvider.SyncStatus.USER_MORE_RESULTS)); |
| 337 | } |
| 338 | |
| 339 | /** |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 340 | * Serialize the given list of folders |
| 341 | * @param folderMap A valid map of folder names to Folders |
| 342 | * @return a string containing a serialized output of folder maps. |
| 343 | */ |
| 344 | public static String serialize(Map<String, Folder> folderMap) { |
| 345 | Collection<Folder> folderCollection = folderMap.values(); |
| 346 | Folder[] folderList = folderCollection.toArray(new Folder[]{} ); |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 347 | int numFolders = folderList.length; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 348 | StringBuilder result = new StringBuilder(); |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 349 | for (int i = 0; i < numFolders; i++) { |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 350 | if (i > 0) { |
Mindy Pereira | 518ee42 | 2012-02-23 18:38:23 -0800 | [diff] [blame] | 351 | result.append(FOLDER_SEPARATOR); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 352 | } |
| 353 | Folder folder = folderList[i]; |
| 354 | result.append(folder.serialize()); |
| 355 | } |
| 356 | return result.toString(); |
| 357 | } |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 358 | |
| 359 | public boolean supportsCapability(int capability) { |
| 360 | return (capabilities & capability) != 0; |
| 361 | } |
Vikram Aggarwal | ff7d02a | 2012-01-11 16:37:45 -0800 | [diff] [blame] | 362 | } |