1/*
2 * Copyright (C) 2008-2009 Marc Blank
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.exchange;
19
20import com.android.emailcommon.provider.Mailbox;
21import com.android.emailcommon.service.EmailServiceProxy;
22import com.android.mail.utils.LogUtils;
23
24import java.text.SimpleDateFormat;
25import java.util.Locale;
26import java.util.TimeZone;
27
28/**
29 * Constants used throughout the EAS implementation are stored here.
30 *
31 */
32public class Eas {
33
34    // For logging.
35    public static final String LOG_TAG = "Exchange";
36
37    // For debugging
38    public static boolean WAIT_DEBUG = false;   // DO NOT CHECK IN WITH THIS SET TO TRUE
39    public static boolean DEBUG = false;         // DO NOT CHECK IN WITH THIS SET TO TRUE
40
41    // The following two are for user logging (the second providing more detail)
42    public static boolean USER_LOG = false;     // DO NOT CHECK IN WITH THIS SET TO TRUE
43    public static boolean PARSER_LOG = false;   // DO NOT CHECK IN WITH THIS SET TO TRUE
44    public static boolean FILE_LOG = false;     // DO NOT CHECK IN WITH THIS SET TO TRUE
45
46    public static final String CLIENT_VERSION = "EAS-2.0";
47    public static final String ACCOUNT_MAILBOX_PREFIX = "__eas";
48
49    // Define our default protocol version as 2.5 (Exchange 2003)
50    public static final String SUPPORTED_PROTOCOL_EX2003 = "2.5";
51    public static final double SUPPORTED_PROTOCOL_EX2003_DOUBLE = 2.5;
52    public static final String SUPPORTED_PROTOCOL_EX2007 = "12.0";
53    public static final double SUPPORTED_PROTOCOL_EX2007_DOUBLE = 12.0;
54    public static final String SUPPORTED_PROTOCOL_EX2007_SP1 = "12.1";
55    public static final double SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE = 12.1;
56    public static final String SUPPORTED_PROTOCOL_EX2010 = "14.0";
57    public static final double SUPPORTED_PROTOCOL_EX2010_DOUBLE = 14.0;
58    public static final String SUPPORTED_PROTOCOL_EX2010_SP1 = "14.1";
59    public static final double SUPPORTED_PROTOCOL_EX2010_SP1_DOUBLE = 14.1;
60    public static final String DEFAULT_PROTOCOL_VERSION = SUPPORTED_PROTOCOL_EX2003;
61    public static final boolean DEFAULT_PROTOCOL_IS_EAS14 = false;
62
63    public static final String EXCHANGE_ACCOUNT_MANAGER_TYPE =
64            com.android.exchange.Configuration.EXCHANGE_ACCOUNT_MANAGER_TYPE;
65    public static final String PROTOCOL = com.android.exchange.Configuration.EXCHANGE_PROTOCOL;
66    public static final String EXCHANGE_SERVICE_INTENT_ACTION =
67            com.android.exchange.Configuration.EXCHANGE_SERVICE_INTENT_ACTION;
68
69    // From EAS spec
70    //                Mail Cal
71    // 0 No filter    Yes  Yes
72    // 1 1 day ago    Yes  No
73    // 2 3 days ago   Yes  No
74    // 3 1 week ago   Yes  No
75    // 4 2 weeks ago  Yes  Yes
76    // 5 1 month ago  Yes  Yes
77    // 6 3 months ago No   Yes
78    // 7 6 months ago No   Yes
79
80    // TODO Rationalize this with SYNC_WINDOW_ALL
81    public static final String FILTER_ALL = "0";
82    public static final String FILTER_1_DAY = "1";
83    public static final String FILTER_3_DAYS =  "2";
84    public static final String FILTER_1_WEEK =  "3";
85    public static final String FILTER_2_WEEKS =  "4";
86    public static final String FILTER_1_MONTH =  "5";
87    public static final String FILTER_3_MONTHS = "6";
88    public static final String FILTER_6_MONTHS = "7";
89
90    public static final String BODY_PREFERENCE_TEXT = "1";
91    public static final String BODY_PREFERENCE_HTML = "2";
92
93    public static final String MIME_BODY_PREFERENCE_TEXT = "0";
94    public static final String MIME_BODY_PREFERENCE_MIME = "2";
95
96    // Mailbox Types
97    // Section 2.2.3.170.3 Type (FolderSync)
98    // http://msdn.microsoft.com/en-us/library/gg650877(v=exchg.80).aspx
99    public static final int MAILBOX_TYPE_USER_GENERIC = 1;
100    public static final int MAILBOX_TYPE_INBOX = 2;
101    public static final int MAILBOX_TYPE_DRAFTS = 3;
102    public static final int MAILBOX_TYPE_DELETED = 4;
103    public static final int MAILBOX_TYPE_SENT = 5;
104    public static final int MAILBOX_TYPE_OUTBOX = 6;
105//    public static final int MAILBOX_TYPE_TASKS = 7;
106    public static final int MAILBOX_TYPE_CALENDAR = 8;
107    public static final int MAILBOX_TYPE_CONTACTS = 9;
108//    public static final int MAILBOX_TYPE_NOTES = 10;
109//    public static final int MAILBOX_TYPE_JOURNAL = 11;
110    public static final int MAILBOX_TYPE_USER_MAIL = 12;
111    public static final int MAILBOX_TYPE_USER_CALENDAR = 13;
112    public static final int MAILBOX_TYPE_USER_CONTACTS = 14;
113//    public static final int MAILBOX_TYPE_USER_TASKS = 15;
114//    public static final int MAILBOX_TYPE_USER_JOURNAL = 16;
115//    public static final int MAILBOX_TYPE_USER_NOTES = 17;
116//    public static final int MAILBOX_TYPE_UNKNOWN = 18;
117//    public static final int MAILBOX_TYPE_RECIPIENT_INFORMATION_CACHE = 19;
118
119
120    // These limits must never exceed about 500k which is half the max size of a Binder IPC buffer.
121
122    // For EAS 12, we use HTML, so we want a larger size than in EAS 2.5
123    public static final String EAS12_TRUNCATION_SIZE = "200000";
124    // For EAS 2.5, truncation is a code; the largest is "7", which is 100k
125    public static final String EAS2_5_TRUNCATION_SIZE = "7";
126
127    public static final int FOLDER_STATUS_OK = 1;
128    public static final int FOLDER_STATUS_INVALID_KEY = 9;
129
130    public static final int EXCHANGE_ERROR_NOTIFICATION = 0x10;
131
132    public static void setUserDebug(int state) {
133        // DEBUG takes precedence and is never true in a user build
134        if (!DEBUG) {
135            USER_LOG = (state & EmailServiceProxy.DEBUG_BIT) != 0;
136            PARSER_LOG = (state & EmailServiceProxy.DEBUG_VERBOSE_BIT) != 0;
137            FILE_LOG = (state & EmailServiceProxy.DEBUG_FILE_BIT) != 0;
138            if (FILE_LOG || PARSER_LOG) {
139                USER_LOG = true;
140            }
141            LogUtils.d("Eas Debug", "Logging: " + (USER_LOG ? "User " : "") +
142                    (PARSER_LOG ? "Parser " : "") + (FILE_LOG ? "File" : ""));
143        }
144    }
145
146    static public Double getProtocolVersionDouble(String version) {
147        if (SUPPORTED_PROTOCOL_EX2003.equals(version)) {
148            return SUPPORTED_PROTOCOL_EX2003_DOUBLE;
149        } else if (SUPPORTED_PROTOCOL_EX2007.equals(version)) {
150            return SUPPORTED_PROTOCOL_EX2007_DOUBLE;
151        } if (SUPPORTED_PROTOCOL_EX2007_SP1.equals(version)) {
152            return SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE;
153        } if (SUPPORTED_PROTOCOL_EX2010.equals(version)) {
154            return SUPPORTED_PROTOCOL_EX2010_DOUBLE;
155        } if (SUPPORTED_PROTOCOL_EX2010_SP1.equals(version)) {
156            return SUPPORTED_PROTOCOL_EX2010_SP1_DOUBLE;
157        }
158        throw new IllegalArgumentException("illegal protocol version");
159    }
160
161    static public boolean isProtocolEas14(String version) {
162        if (version == null) {
163            return DEFAULT_PROTOCOL_IS_EAS14;
164        }
165        return getProtocolVersionDouble(version) >= SUPPORTED_PROTOCOL_EX2010_DOUBLE;
166    }
167
168    /**
169     * Gets the Exchange folder class for a mailbox type (PIM collections have different values
170     * from email), needed when forming the request.
171     * @param mailboxType The type of the mailbox we're interested in, from {@link Mailbox}.
172     * @return The folder class for the mailbox we're interested in.
173     */
174    public static String getFolderClass(final int mailboxType) {
175        switch (mailboxType) {
176            case Mailbox.TYPE_CALENDAR:
177                return "Calendar";
178            case Mailbox.TYPE_CONTACTS:
179                return "Contacts";
180            default:
181                return "Email";
182        }
183    }
184
185    // Time format documented at http://msdn.microsoft.com/en-us/library/ee201818(v=exchg.80).aspx
186    public static final SimpleDateFormat DATE_FORMAT;
187    static {
188        DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'", Locale.US);
189        DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
190    }
191}
192