Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2012, Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.mail.providers; |
| 18 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
| 20 | |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 21 | import com.android.mail.providers.UIProvider.AccountColumns; |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 22 | import com.android.mail.providers.UIProvider.AutoAdvance; |
| 23 | import com.android.mail.providers.UIProvider.DefaultReplyBehavior; |
| 24 | import com.android.mail.providers.UIProvider.MessageTextSize; |
| 25 | import com.android.mail.providers.UIProvider.SnapHeaderValue; |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 26 | import com.android.mail.providers.UIProvider.Swipe; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 27 | import com.android.mail.utils.LogTag; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 28 | import com.android.mail.utils.LogUtils; |
| 29 | import com.android.mail.utils.Utils; |
| 30 | |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 31 | import android.database.Cursor; |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 36 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 37 | import org.json.JSONException; |
| 38 | import org.json.JSONObject; |
| 39 | |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Model to hold Settings for an account. |
| 42 | */ |
| 43 | public class Settings implements Parcelable { |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 44 | private static final String LOG_TAG = LogTag.getLogTag(); |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 45 | |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 46 | static final Settings EMPTY_SETTINGS = new Settings(); |
| 47 | |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 48 | // Max size for attachments (5 megs). Will be overridden by an account |
| 49 | // setting, if found. |
| 50 | private static final int DEFAULT_MAX_ATTACHMENT_SIZE = 5 * 1024 * 1024; |
| 51 | |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 52 | public static final int SWIPE_SETTING_ARCHIVE = 0; |
| 53 | public static final int SWIPE_SETTING_DELETE = 1; |
| 54 | public static final int SWIPE_SETTING_DISABLED = 2; |
| 55 | |
| 56 | private static final int DEFAULT = SWIPE_SETTING_ARCHIVE; |
| 57 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 58 | public final String signature; |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Auto advance setting for this account. |
| 61 | * Integer, one of {@link AutoAdvance#LIST}, {@link AutoAdvance#NEWER}, |
| 62 | * {@link AutoAdvance#OLDER} or {@link AutoAdvance#UNSET} |
| 63 | */ |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 64 | private final int mAutoAdvance; |
| 65 | private Integer mTransientAutoAdvance = null; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 66 | public final int messageTextSize; |
| 67 | public final int snapHeaders; |
| 68 | public final int replyBehavior; |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 69 | public final boolean hideCheckboxes; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 70 | public final boolean confirmDelete; |
| 71 | public final boolean confirmArchive; |
| 72 | public final boolean confirmSend; |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 73 | public final int conversationViewMode; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 74 | public final Uri defaultInbox; |
Vikram Aggarwal | 5e63e8a | 2012-07-19 10:05:24 -0700 | [diff] [blame] | 75 | /** |
| 76 | * The name of the default inbox: "Inbox" or "Priority Inbox", internationalized... |
| 77 | */ |
| 78 | public final String defaultInboxName; |
| 79 | // If you find the need for more default Inbox information: ID or capabilities, then |
| 80 | // ask viki to replace the above two members with a single JSON object representing the default |
| 81 | // folder. That should make all the information about the folder available without an |
| 82 | // explosion in the number of members. |
| 83 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 84 | public final boolean forceReplyFromDefault; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 85 | public final int maxAttachmentSize; |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 86 | public final int swipe; |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 87 | /** True if arrows on the priority inbox are enabled. */ |
| 88 | public final boolean priorityArrowsEnabled; |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 89 | public final Uri setupIntentUri; |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 90 | public final String veiledAddressPattern; |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 91 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 92 | /** Cached value of hashCode */ |
| 93 | private int mHashCode; |
| 94 | |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 95 | /** Safe defaults to be used if some values are unspecified. */ |
| 96 | private static final Settings sDefault = EMPTY_SETTINGS; |
| 97 | |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 98 | private Settings() { |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 99 | signature = ""; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 100 | mAutoAdvance = AutoAdvance.LIST; |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 101 | messageTextSize = MessageTextSize.NORMAL; |
| 102 | snapHeaders = SnapHeaderValue.ALWAYS; |
| 103 | replyBehavior = DefaultReplyBehavior.REPLY; |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 104 | hideCheckboxes = false; |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 105 | confirmDelete = false; |
| 106 | confirmArchive = false; |
| 107 | confirmSend = false; |
Vikram Aggarwal | 5e63e8a | 2012-07-19 10:05:24 -0700 | [diff] [blame] | 108 | defaultInbox = Uri.EMPTY; |
| 109 | defaultInboxName = ""; |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 110 | forceReplyFromDefault = false; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 111 | maxAttachmentSize = 0; |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 112 | swipe = DEFAULT; |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 113 | priorityArrowsEnabled = false; |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 114 | setupIntentUri = Uri.EMPTY; |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 115 | conversationViewMode = UIProvider.ConversationViewMode.UNDEFINED; |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 116 | veiledAddressPattern = null; |
Paul Westbrook | 19e5908 | 2012-04-08 12:44:57 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 119 | public Settings(Parcel inParcel) { |
| 120 | signature = inParcel.readString(); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 121 | mAutoAdvance = inParcel.readInt(); |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 122 | messageTextSize = inParcel.readInt(); |
| 123 | snapHeaders = inParcel.readInt(); |
| 124 | replyBehavior = inParcel.readInt(); |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 125 | hideCheckboxes = inParcel.readInt() != 0; |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 126 | confirmDelete = inParcel.readInt() != 0; |
| 127 | confirmArchive = inParcel.readInt() != 0; |
| 128 | confirmSend = inParcel.readInt() != 0; |
Vikram Aggarwal | 5e63e8a | 2012-07-19 10:05:24 -0700 | [diff] [blame] | 129 | defaultInbox = Utils.getValidUri(inParcel.readString()); |
| 130 | defaultInboxName = inParcel.readString(); |
Mindy Pereira | eee6307 | 2012-04-06 08:32:02 -0700 | [diff] [blame] | 131 | forceReplyFromDefault = inParcel.readInt() != 0; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 132 | maxAttachmentSize = inParcel.readInt(); |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 133 | swipe = inParcel.readInt(); |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 134 | priorityArrowsEnabled = inParcel.readInt() != 0; |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 135 | setupIntentUri = Utils.getValidUri(inParcel.readString()); |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 136 | conversationViewMode = inParcel.readInt(); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 137 | veiledAddressPattern = inParcel.readString(); |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public Settings(Cursor cursor) { |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 141 | signature = cursor.getString(UIProvider.ACCOUNT_SETTINGS_SIGNATURE_COLUMN); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 142 | mAutoAdvance = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_AUTO_ADVANCE_COLUMN); |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 143 | messageTextSize = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_MESSAGE_TEXT_SIZE_COLUMN); |
| 144 | snapHeaders = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_SNAP_HEADERS_COLUMN); |
| 145 | replyBehavior = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_REPLY_BEHAVIOR_COLUMN); |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 146 | hideCheckboxes = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_HIDE_CHECKBOXES_COLUMN) != 0; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 147 | confirmDelete = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_CONFIRM_DELETE_COLUMN) != 0; |
| 148 | confirmArchive = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_CONFIRM_ARCHIVE_COLUMN) != 0; |
| 149 | confirmSend = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_CONFIRM_SEND_COLUMN) != 0; |
Vikram Aggarwal | 5e63e8a | 2012-07-19 10:05:24 -0700 | [diff] [blame] | 150 | defaultInbox = Utils.getValidUri( |
| 151 | cursor.getString(UIProvider.ACCOUNT_SETTINGS_DEFAULT_INBOX_COLUMN)); |
| 152 | defaultInboxName = cursor.getString(UIProvider.ACCOUNT_SETTINGS_DEFAULT_INBOX_NAME_COLUMN); |
Mindy Pereira | eee6307 | 2012-04-06 08:32:02 -0700 | [diff] [blame] | 153 | forceReplyFromDefault = cursor.getInt( |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 154 | UIProvider.ACCOUNT_SETTINGS_FORCE_REPLY_FROM_DEFAULT_COLUMN) != 0; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 155 | maxAttachmentSize = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_MAX_ATTACHMENT_SIZE_COLUMN); |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 156 | swipe = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_SWIPE_COLUMN); |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 157 | priorityArrowsEnabled = |
| 158 | cursor.getInt(UIProvider.ACCOUNT_SETTINGS_PRIORITY_ARROWS_ENABLED_COLUMN) != 0; |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 159 | setupIntentUri = Utils.getValidUri( |
| 160 | cursor.getString(UIProvider.ACCOUNT_SETTINGS_SETUP_INTENT_URI)); |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 161 | conversationViewMode = cursor.getInt(UIProvider.ACCOUNT_SETTINGS_CONVERSATION_MODE_COLUMN); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 162 | veiledAddressPattern = |
| 163 | cursor.getString(UIProvider.ACCOUNT_SETTINGS_VEILED_ADDRESS_PATTERN_COLUMN); |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 166 | private Settings(JSONObject json) { |
| 167 | signature = json.optString(AccountColumns.SettingsColumns.SIGNATURE, sDefault.signature); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 168 | mAutoAdvance = json.optInt(AccountColumns.SettingsColumns.AUTO_ADVANCE, |
| 169 | sDefault.getAutoAdvanceSetting()); |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 170 | messageTextSize = json.optInt(AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE, |
| 171 | sDefault.messageTextSize); |
| 172 | snapHeaders = json.optInt(AccountColumns.SettingsColumns.SNAP_HEADERS, |
| 173 | sDefault.snapHeaders); |
| 174 | replyBehavior = json.optInt(AccountColumns.SettingsColumns.REPLY_BEHAVIOR, |
| 175 | sDefault.replyBehavior); |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 176 | hideCheckboxes = json.optBoolean(AccountColumns.SettingsColumns.HIDE_CHECKBOXES, |
| 177 | sDefault.hideCheckboxes); |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 178 | confirmDelete = json.optBoolean(AccountColumns.SettingsColumns.CONFIRM_DELETE, |
| 179 | sDefault.confirmDelete); |
| 180 | confirmArchive = json.optBoolean(AccountColumns.SettingsColumns.CONFIRM_ARCHIVE, |
| 181 | sDefault.confirmArchive); |
| 182 | confirmSend = json.optBoolean(AccountColumns.SettingsColumns.CONFIRM_SEND, |
| 183 | sDefault.confirmSend); |
Vikram Aggarwal | 5e63e8a | 2012-07-19 10:05:24 -0700 | [diff] [blame] | 184 | defaultInbox = Utils.getValidUri( |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 185 | json.optString(AccountColumns.SettingsColumns.DEFAULT_INBOX)); |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 186 | defaultInboxName = json.optString(AccountColumns.SettingsColumns.DEFAULT_INBOX_NAME, |
| 187 | sDefault.defaultInboxName); |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 188 | forceReplyFromDefault = |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 189 | json.optBoolean(AccountColumns.SettingsColumns.FORCE_REPLY_FROM_DEFAULT, |
| 190 | sDefault.forceReplyFromDefault); |
| 191 | maxAttachmentSize = json.optInt(AccountColumns.SettingsColumns.MAX_ATTACHMENT_SIZE, |
| 192 | sDefault.maxAttachmentSize); |
| 193 | swipe = json.optInt(AccountColumns.SettingsColumns.SWIPE, sDefault.swipe); |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 194 | priorityArrowsEnabled = |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 195 | json.optBoolean(AccountColumns.SettingsColumns.PRIORITY_ARROWS_ENABLED, |
| 196 | sDefault.priorityArrowsEnabled); |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 197 | setupIntentUri = Utils.getValidUri( |
| 198 | json.optString(AccountColumns.SettingsColumns.SETUP_INTENT_URI)); |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 199 | conversationViewMode = json.optInt(AccountColumns.SettingsColumns.CONVERSATION_VIEW_MODE, |
| 200 | UIProvider.ConversationViewMode.UNDEFINED); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 201 | veiledAddressPattern = |
| 202 | json.optString(AccountColumns.SettingsColumns.VEILED_ADDRESS_PATTERN, null); |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 206 | * Return a serialized String for these settings. |
| 207 | */ |
| 208 | public synchronized String serialize() { |
| 209 | final JSONObject json = toJSON(); |
| 210 | return json.toString(); |
| 211 | } |
| 212 | |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 213 | private static final Object getNonNull(Object candidate, Object fallback){ |
| 214 | if (candidate == null) |
| 215 | return fallback; |
| 216 | return candidate; |
| 217 | } |
| 218 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Return a JSONObject for these settings. |
| 221 | */ |
| 222 | public synchronized JSONObject toJSON() { |
| 223 | final JSONObject json = new JSONObject(); |
| 224 | try { |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 225 | json.put(AccountColumns.SettingsColumns.SIGNATURE, |
| 226 | getNonNull(signature, sDefault.signature)); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 227 | json.put(AccountColumns.SettingsColumns.AUTO_ADVANCE, getAutoAdvanceSetting()); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 228 | json.put(AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE, messageTextSize); |
| 229 | json.put(AccountColumns.SettingsColumns.SNAP_HEADERS, snapHeaders); |
| 230 | json.put(AccountColumns.SettingsColumns.REPLY_BEHAVIOR, replyBehavior); |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 231 | json.put(AccountColumns.SettingsColumns.HIDE_CHECKBOXES, hideCheckboxes); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 232 | json.put(AccountColumns.SettingsColumns.CONFIRM_DELETE, confirmDelete); |
| 233 | json.put(AccountColumns.SettingsColumns.CONFIRM_ARCHIVE, confirmArchive); |
| 234 | json.put(AccountColumns.SettingsColumns.CONFIRM_SEND, confirmSend); |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 235 | json.put(AccountColumns.SettingsColumns.DEFAULT_INBOX, |
| 236 | getNonNull(defaultInbox, sDefault.defaultInbox)); |
| 237 | json.put(AccountColumns.SettingsColumns.DEFAULT_INBOX_NAME, |
| 238 | getNonNull(defaultInboxName, sDefault.defaultInboxName)); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 239 | json.put(AccountColumns.SettingsColumns.FORCE_REPLY_FROM_DEFAULT, |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 240 | forceReplyFromDefault); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 241 | json.put(AccountColumns.SettingsColumns.MAX_ATTACHMENT_SIZE, |
| 242 | maxAttachmentSize); |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 243 | json.put(AccountColumns.SettingsColumns.SWIPE, swipe); |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 244 | json.put(AccountColumns.SettingsColumns.PRIORITY_ARROWS_ENABLED, priorityArrowsEnabled); |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 245 | json.put(AccountColumns.SettingsColumns.SETUP_INTENT_URI, setupIntentUri); |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 246 | json.put(AccountColumns.SettingsColumns.CONVERSATION_VIEW_MODE, conversationViewMode); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 247 | json.put(AccountColumns.SettingsColumns.VEILED_ADDRESS_PATTERN, veiledAddressPattern); |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 248 | } catch (JSONException e) { |
| 249 | LogUtils.wtf(LOG_TAG, e, "Could not serialize settings"); |
| 250 | } |
| 251 | return json; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Create a new instance of an Settings object using a serialized instance created previously |
| 256 | * using {@link #serialize()}. This returns null if the serialized instance was invalid or does |
| 257 | * not represent a valid account object. |
| 258 | * |
| 259 | * @param serializedAccount |
| 260 | * @return |
| 261 | */ |
| 262 | public static Settings newInstance(String serializedSettings) { |
| 263 | JSONObject json = null; |
| 264 | try { |
| 265 | json = new JSONObject(serializedSettings); |
| 266 | return new Settings(json); |
| 267 | } catch (JSONException e) { |
Andy Huang | 0a88d81 | 2012-07-17 15:43:10 -0700 | [diff] [blame] | 268 | LogUtils.e(LOG_TAG, e, "Could not create an settings from this input: \"%s\"", |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 269 | serializedSettings); |
| 270 | return null; |
| 271 | } |
| 272 | } |
| 273 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 274 | /** |
| 275 | * Create a new instance of an Settings object using a JSONObject instance created previously |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 276 | * using {@link #toJSON()}. This returns null if the serialized instance was invalid or does |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 277 | * not represent a valid account object. |
| 278 | * |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 279 | * @param json |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 280 | * @return |
| 281 | */ |
| 282 | public static Settings newInstance(JSONObject json) { |
| 283 | if (json == null) { |
| 284 | return null; |
| 285 | } |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 286 | return new Settings(json); |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 289 | @Override |
| 290 | public int describeContents() { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void writeToParcel(Parcel dest, int flags) { |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 296 | dest.writeString((String) getNonNull(signature, sDefault.signature)); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 297 | dest.writeInt(getAutoAdvanceSetting()); |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 298 | dest.writeInt(messageTextSize); |
| 299 | dest.writeInt(snapHeaders); |
| 300 | dest.writeInt(replyBehavior); |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 301 | dest.writeInt(hideCheckboxes ? 1 : 0); |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 302 | dest.writeInt(confirmDelete ? 1 : 0); |
| 303 | dest.writeInt(confirmArchive? 1 : 0); |
| 304 | dest.writeInt(confirmSend? 1 : 0); |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 305 | dest.writeString(((Uri) getNonNull(defaultInbox, sDefault.defaultInbox)).toString()); |
| 306 | dest.writeString((String) getNonNull(defaultInboxName, sDefault.defaultInboxName)); |
Mindy Pereira | eee6307 | 2012-04-06 08:32:02 -0700 | [diff] [blame] | 307 | dest.writeInt(forceReplyFromDefault ? 1 : 0); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 308 | dest.writeInt(maxAttachmentSize); |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 309 | dest.writeInt(swipe); |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 310 | dest.writeInt(priorityArrowsEnabled ? 1 : 0); |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 311 | dest.writeString(((Uri) getNonNull(setupIntentUri, sDefault.setupIntentUri)).toString()); |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 312 | dest.writeInt(conversationViewMode); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 313 | dest.writeString(veiledAddressPattern); |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 316 | /** |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 317 | * Returns the URI of the current account's default inbox if available, otherwise |
| 318 | * returns the empty URI {@link Uri#EMPTY} |
| 319 | * @param settings a settings object, possibly null. |
| 320 | * @return a valid default Inbox URI, or {@link Uri#EMPTY} if settings are null or no default |
| 321 | * is specified. |
| 322 | */ |
| 323 | public static Uri getDefaultInboxUri(Settings settings) { |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 324 | if (settings == null) { |
| 325 | return sDefault.defaultInbox; |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 326 | } |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 327 | return (Uri) getNonNull(settings.defaultInbox, sDefault.defaultInbox); |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 331 | * Gets the autoadvance setting for this object, which may have changed since the settings were |
| 332 | * initially loaded. |
| 333 | */ |
| 334 | public int getAutoAdvanceSetting() { |
| 335 | if (mTransientAutoAdvance != null) { |
| 336 | return mTransientAutoAdvance.intValue(); |
| 337 | } |
| 338 | |
| 339 | return mAutoAdvance; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Sets the transient autoadvance setting, which will override the initial autoadvance setting. |
| 344 | */ |
| 345 | public void setAutoAdvanceSetting(final int autoAdvance) { |
| 346 | mTransientAutoAdvance = Integer.valueOf(autoAdvance); |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Return the swipe setting for the settings provided. It is safe to pass this method |
| 351 | * a null object. It always returns a valid {@link Swipe} setting. |
| 352 | * @return the auto advance setting, a constant from {@link Swipe} |
| 353 | */ |
| 354 | public static int getSwipeSetting(Settings settings) { |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 355 | return settings != null ? settings.swipe : sDefault.swipe; |
Mindy Pereira | 71a8f5e | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 356 | } |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 357 | |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 358 | @SuppressWarnings("hiding") |
| 359 | public static final Creator<Settings> CREATOR = new Creator<Settings>() { |
| 360 | @Override |
| 361 | public Settings createFromParcel(Parcel source) { |
| 362 | return new Settings(source); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public Settings[] newArray(int size) { |
| 367 | return new Settings[size]; |
| 368 | } |
| 369 | }; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 372 | * Get the maximum size in bytes for attachments. |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 373 | */ |
| 374 | public int getMaxAttachmentSize() { |
| 375 | return maxAttachmentSize <= 0 ? DEFAULT_MAX_ATTACHMENT_SIZE : maxAttachmentSize; |
| 376 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 377 | |
| 378 | @Override |
| 379 | public boolean equals(final Object aThat) { |
| 380 | LogUtils.d(LOG_TAG, "Settings.equals(%s)", aThat); |
| 381 | if (this == aThat) { |
| 382 | return true; |
| 383 | } |
| 384 | if ((aThat == null) || (aThat.getClass() != this.getClass())) { |
| 385 | return false; |
| 386 | } |
| 387 | final Settings that = (Settings) aThat; |
Vikram Aggarwal | b122b1e | 2012-08-15 09:49:09 -0700 | [diff] [blame] | 388 | return (TextUtils.equals(signature, that.signature) |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 389 | && mAutoAdvance == that.mAutoAdvance |
| 390 | && mTransientAutoAdvance == that.mTransientAutoAdvance |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 391 | && messageTextSize == that.messageTextSize |
Andy Huang | 8f18778 | 2012-11-06 17:49:25 -0800 | [diff] [blame] | 392 | && snapHeaders == that.snapHeaders |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 393 | && replyBehavior == that.replyBehavior |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 394 | && hideCheckboxes == that.hideCheckboxes |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 395 | && confirmDelete == that.confirmDelete |
| 396 | && confirmArchive == that.confirmArchive |
| 397 | && confirmSend == that.confirmSend |
| 398 | && Objects.equal(defaultInbox, that.defaultInbox) |
| 399 | // Not checking default Inbox name, since is is identical to the URI check above. |
| 400 | && forceReplyFromDefault == that.forceReplyFromDefault |
Vikram Aggarwal | 707f24c | 2012-07-31 15:59:36 -0700 | [diff] [blame] | 401 | && maxAttachmentSize == that.maxAttachmentSize |
| 402 | && swipe == that.swipe |
Marc Blank | 5789908 | 2012-09-05 11:35:57 -0700 | [diff] [blame] | 403 | && priorityArrowsEnabled == that.priorityArrowsEnabled |
Paul Westbrook | fa255c0 | 2012-10-13 14:32:52 -0700 | [diff] [blame] | 404 | && setupIntentUri == that.setupIntentUri |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 405 | && conversationViewMode == that.conversationViewMode |
| 406 | && TextUtils.equals(veiledAddressPattern, that.veiledAddressPattern)); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public int hashCode() { |
| 411 | if (mHashCode == 0) { |
| 412 | mHashCode = calculateHashCode(); |
| 413 | } |
| 414 | return mHashCode; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Returns the hash code for this object. |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 419 | */ |
| 420 | private final int calculateHashCode() { |
| 421 | return super.hashCode() |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 422 | ^ Objects.hashCode(signature, mAutoAdvance, mTransientAutoAdvance, messageTextSize, |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame^] | 423 | snapHeaders, replyBehavior, hideCheckboxes, confirmDelete, confirmArchive, |
Andy Huang | 8f18778 | 2012-11-06 17:49:25 -0800 | [diff] [blame] | 424 | confirmSend, defaultInbox, forceReplyFromDefault, maxAttachmentSize, swipe, |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 425 | priorityArrowsEnabled, setupIntentUri, conversationViewMode, |
| 426 | veiledAddressPattern); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 427 | } |
Mindy Pereira | 8bd8215 | 2012-03-01 10:06:35 -0800 | [diff] [blame] | 428 | } |