EasSyncService.java revision 26d9677a1eb48553241897b63a77bbd33daa9f92
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.mail.Address;
21import com.android.emailcommon.mail.MeetingInfo;
22import com.android.emailcommon.mail.MessagingException;
23import com.android.emailcommon.mail.PackedString;
24import com.android.emailcommon.provider.EmailContent.Account;
25import com.android.emailcommon.provider.EmailContent.AccountColumns;
26import com.android.emailcommon.provider.EmailContent.Attachment;
27import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
28import com.android.emailcommon.provider.EmailContent.HostAuth;
29import com.android.emailcommon.provider.EmailContent.Mailbox;
30import com.android.emailcommon.provider.EmailContent.MailboxColumns;
31import com.android.emailcommon.provider.EmailContent.Message;
32import com.android.emailcommon.provider.EmailContent.MessageColumns;
33import com.android.emailcommon.provider.EmailContent.SyncColumns;
34import com.android.emailcommon.service.EmailServiceConstants;
35import com.android.emailcommon.service.EmailServiceProxy;
36import com.android.emailcommon.service.EmailServiceStatus;
37import com.android.emailcommon.service.PolicySet;
38import com.android.emailcommon.utility.AttachmentUtilities;
39import com.android.emailcommon.utility.Utility;
40import com.android.exchange.adapter.AbstractSyncAdapter;
41import com.android.exchange.adapter.AccountSyncAdapter;
42import com.android.exchange.adapter.CalendarSyncAdapter;
43import com.android.exchange.adapter.ContactsSyncAdapter;
44import com.android.exchange.adapter.EmailSyncAdapter;
45import com.android.exchange.adapter.FolderSyncParser;
46import com.android.exchange.adapter.GalParser;
47import com.android.exchange.adapter.MeetingResponseParser;
48import com.android.exchange.adapter.MoveItemsParser;
49import com.android.exchange.adapter.Parser.EasParserException;
50import com.android.exchange.adapter.PingParser;
51import com.android.exchange.adapter.ProvisionParser;
52import com.android.exchange.adapter.SearchParser;
53import com.android.exchange.adapter.Serializer;
54import com.android.exchange.adapter.Tags;
55import com.android.exchange.provider.GalResult;
56import com.android.exchange.provider.MailboxUtilities;
57import com.android.exchange.utility.CalendarUtilities;
58
59import org.apache.http.Header;
60import org.apache.http.HttpEntity;
61import org.apache.http.HttpResponse;
62import org.apache.http.HttpStatus;
63import org.apache.http.client.HttpClient;
64import org.apache.http.client.methods.HttpOptions;
65import org.apache.http.client.methods.HttpPost;
66import org.apache.http.client.methods.HttpRequestBase;
67import org.apache.http.conn.ClientConnectionManager;
68import org.apache.http.entity.ByteArrayEntity;
69import org.apache.http.entity.StringEntity;
70import org.apache.http.impl.client.DefaultHttpClient;
71import org.apache.http.params.BasicHttpParams;
72import org.apache.http.params.HttpConnectionParams;
73import org.apache.http.params.HttpParams;
74import org.xmlpull.v1.XmlPullParser;
75import org.xmlpull.v1.XmlPullParserException;
76import org.xmlpull.v1.XmlPullParserFactory;
77import org.xmlpull.v1.XmlSerializer;
78
79import android.content.ContentResolver;
80import android.content.ContentUris;
81import android.content.ContentValues;
82import android.content.Context;
83import android.content.Entity;
84import android.database.Cursor;
85import android.net.Uri;
86import android.os.Build;
87import android.os.Bundle;
88import android.os.RemoteException;
89import android.os.SystemClock;
90import android.provider.Calendar.Attendees;
91import android.provider.Calendar.Events;
92import android.text.TextUtils;
93import android.util.Base64;
94import android.util.Log;
95import android.util.Xml;
96
97import java.io.ByteArrayOutputStream;
98import java.io.File;
99import java.io.FileNotFoundException;
100import java.io.IOException;
101import java.io.InputStream;
102import java.io.OutputStream;
103import java.lang.Thread.State;
104import java.net.URI;
105import java.security.cert.CertificateException;
106import java.util.ArrayList;
107import java.util.HashMap;
108
109public class EasSyncService extends AbstractSyncService {
110    // DO NOT CHECK IN SET TO TRUE
111    public static final boolean DEBUG_GAL_SERVICE = false;
112
113    private static final String WHERE_ACCOUNT_KEY_AND_SERVER_ID =
114        MailboxColumns.ACCOUNT_KEY + "=? and " + MailboxColumns.SERVER_ID + "=?";
115    private static final String WHERE_ACCOUNT_AND_SYNC_INTERVAL_PING =
116        MailboxColumns.ACCOUNT_KEY + "=? and " + MailboxColumns.SYNC_INTERVAL +
117        '=' + Mailbox.CHECK_INTERVAL_PING;
118    private static final String AND_FREQUENCY_PING_PUSH_AND_NOT_ACCOUNT_MAILBOX = " AND " +
119        MailboxColumns.SYNC_INTERVAL + " IN (" + Mailbox.CHECK_INTERVAL_PING +
120        ',' + Mailbox.CHECK_INTERVAL_PUSH + ") AND " + MailboxColumns.TYPE + "!=\"" +
121        Mailbox.TYPE_EAS_ACCOUNT_MAILBOX + '\"';
122    private static final String WHERE_PUSH_HOLD_NOT_ACCOUNT_MAILBOX =
123        MailboxColumns.ACCOUNT_KEY + "=? and " + MailboxColumns.SYNC_INTERVAL +
124        '=' + Mailbox.CHECK_INTERVAL_PUSH_HOLD;
125    static private final int CHUNK_SIZE = 16*1024;
126
127    static private final String PING_COMMAND = "Ping";
128    // Command timeout is the the time allowed for reading data from an open connection before an
129    // IOException is thrown.  After a small added allowance, our watchdog alarm goes off (allowing
130    // us to detect a silently dropped connection).  The allowance is defined below.
131    static private final int COMMAND_TIMEOUT = 30*SECONDS;
132    // Connection timeout is the time given to connect to the server before reporting an IOException
133    static private final int CONNECTION_TIMEOUT = 20*SECONDS;
134    // The extra time allowed beyond the COMMAND_TIMEOUT before which our watchdog alarm triggers
135    static private final int WATCHDOG_TIMEOUT_ALLOWANCE = 30*SECONDS;
136
137    // The amount of time the account mailbox will sleep if there are no pingable mailboxes
138    // This could happen if the sync time is set to "never"; we always want to check in from time
139    // to time, however, for folder list/policy changes
140    static private final int ACCOUNT_MAILBOX_SLEEP_TIME = 20*MINUTES;
141    static private final String ACCOUNT_MAILBOX_SLEEP_TEXT =
142        "Account mailbox sleeping for " + (ACCOUNT_MAILBOX_SLEEP_TIME / MINUTES) + "m";
143
144    static private final String AUTO_DISCOVER_SCHEMA_PREFIX =
145        "http://schemas.microsoft.com/exchange/autodiscover/mobilesync/";
146    static private final String AUTO_DISCOVER_PAGE = "/autodiscover/autodiscover.xml";
147    static private final int AUTO_DISCOVER_REDIRECT_CODE = 451;
148
149    static private final int INTERNAL_SERVER_ERROR_CODE = 500;
150
151    static public final String EAS_12_POLICY_TYPE = "MS-EAS-Provisioning-WBXML";
152    static public final String EAS_2_POLICY_TYPE = "MS-WAP-Provisioning-XML";
153
154    static public final int MESSAGE_FLAG_MOVED_MESSAGE = 1 << Message.FLAG_SYNC_ADAPTER_SHIFT;
155
156    /**
157     * We start with an 8 minute timeout, and increase/decrease by 3 minutes at a time.  There's
158     * no point having a timeout shorter than 5 minutes, I think; at that point, we can just let
159     * the ping exception out.  The maximum I use is 17 minutes, which is really an empirical
160     * choice; too long and we risk silent connection loss and loss of push for that period.  Too
161     * short and we lose efficiency/battery life.
162     *
163     * If we ever have to drop the ping timeout, we'll never increase it again.  There's no point
164     * going into hysteresis; the NAT timeout isn't going to change without a change in connection,
165     * which will cause the sync service to be restarted at the starting heartbeat and going through
166     * the process again.
167     */
168    static private final int PING_MINUTES = 60; // in seconds
169    static private final int PING_FUDGE_LOW = 10;
170    static private final int PING_STARTING_HEARTBEAT = (8*PING_MINUTES)-PING_FUDGE_LOW;
171    static private final int PING_HEARTBEAT_INCREMENT = 3*PING_MINUTES;
172
173    // Maximum number of times we'll allow a sync to "loop" with MoreAvailable true before
174    // forcing it to stop.  This number has been determined empirically.
175    static private final int MAX_LOOPING_COUNT = 100;
176
177    static private final int PROTOCOL_PING_STATUS_COMPLETED = 1;
178
179    // The amount of time we allow for a thread to release its post lock after receiving an alert
180    static private final int POST_LOCK_TIMEOUT = 10*SECONDS;
181
182    // Fallbacks (in minutes) for ping loop failures
183    static private final int MAX_PING_FAILURES = 1;
184    static private final int PING_FALLBACK_INBOX = 5;
185    static private final int PING_FALLBACK_PIM = 25;
186
187    // MSFT's custom HTTP result code indicating the need to provision
188    static private final int HTTP_NEED_PROVISIONING = 449;
189
190    // The EAS protocol Provision status for "we implement all of the policies"
191    static private final String PROVISION_STATUS_OK = "1";
192    // The EAS protocol Provision status meaning "we partially implement the policies"
193    static private final String PROVISION_STATUS_PARTIAL = "2";
194
195    static /*package*/ final String DEVICE_TYPE = "Android";
196    static private final String USER_AGENT = DEVICE_TYPE + '/' + Build.VERSION.RELEASE + '-' +
197        Eas.CLIENT_VERSION;
198
199    // The shortest search query we'll accept
200    // TODO Check with UX whether this is correct
201    static private final int MIN_QUERY_LENGTH = 3;
202    // The largest number of results we'll ask for per server request
203    static private final int MAX_SEARCH_RESULTS = 100;
204
205    // Reasonable default
206    public String mProtocolVersion = Eas.DEFAULT_PROTOCOL_VERSION;
207    public Double mProtocolVersionDouble;
208    protected String mDeviceId = null;
209    /*package*/ String mAuthString = null;
210    /*package*/ String mCmdString = null;
211    public String mHostAddress;
212    public String mUserName;
213    public String mPassword;
214    private boolean mSsl = true;
215    private boolean mTrustSsl = false;
216    public ContentResolver mContentResolver;
217    private String[] mBindArguments = new String[2];
218    private ArrayList<String> mPingChangeList;
219    // The HttpPost in progress
220    private volatile HttpPost mPendingPost = null;
221    // Our heartbeat when we are waiting for ping boxes to be ready
222    /*package*/ int mPingForceHeartbeat = 2*PING_MINUTES;
223    // The minimum heartbeat we will send
224    /*package*/ int mPingMinHeartbeat = (5*PING_MINUTES)-PING_FUDGE_LOW;
225    // The maximum heartbeat we will send
226    /*package*/ int mPingMaxHeartbeat = (17*PING_MINUTES)-PING_FUDGE_LOW;
227    // The ping time (in seconds)
228    /*package*/ int mPingHeartbeat = PING_STARTING_HEARTBEAT;
229    // The longest successful ping heartbeat
230    private int mPingHighWaterMark = 0;
231    // Whether we've ever lowered the heartbeat
232    /*package*/ boolean mPingHeartbeatDropped = false;
233    // Whether a POST was aborted due to alarm (watchdog alarm)
234    private boolean mPostAborted = false;
235    // Whether a POST was aborted due to reset
236    private boolean mPostReset = false;
237    // Whether or not the sync service is valid (usable)
238    public boolean mIsValid = true;
239
240    public EasSyncService(Context _context, Mailbox _mailbox) {
241        super(_context, _mailbox);
242        mContentResolver = _context.getContentResolver();
243        if (mAccount == null) {
244            mIsValid = false;
245            return;
246        }
247        HostAuth ha = HostAuth.restoreHostAuthWithId(_context, mAccount.mHostAuthKeyRecv);
248        if (ha == null) {
249            mIsValid = false;
250            return;
251        }
252        mSsl = (ha.mFlags & HostAuth.FLAG_SSL) != 0;
253        mTrustSsl = (ha.mFlags & HostAuth.FLAG_TRUST_ALL) != 0;
254    }
255
256    private EasSyncService(String prefix) {
257        super(prefix);
258    }
259
260    public EasSyncService() {
261        this("EAS Validation");
262    }
263
264    @Override
265    /**
266     * Try to wake up a sync thread that is waiting on an HttpClient POST and has waited past its
267     * socket timeout without having thrown an Exception
268     *
269     * @return true if the POST was successfully stopped; false if we've failed and interrupted
270     * the thread
271     */
272    public boolean alarm() {
273        HttpPost post;
274        if (mThread == null) return true;
275        String threadName = mThread.getName();
276
277        // Synchronize here so that we are guaranteed to have valid mPendingPost and mPostLock
278        // executePostWithTimeout (which executes the HttpPost) also uses this lock
279        synchronized(getSynchronizer()) {
280            // Get a reference to the current post lock
281            post = mPendingPost;
282            if (post != null) {
283                if (Eas.USER_LOG) {
284                    URI uri = post.getURI();
285                    if (uri != null) {
286                        String query = uri.getQuery();
287                        if (query == null) {
288                            query = "POST";
289                        }
290                        userLog(threadName, ": Alert, aborting ", query);
291                    } else {
292                        userLog(threadName, ": Alert, no URI?");
293                    }
294                }
295                // Abort the POST
296                mPostAborted = true;
297                post.abort();
298            } else {
299                // If there's no POST, we're done
300                userLog("Alert, no pending POST");
301                return true;
302            }
303        }
304
305        // Wait for the POST to finish
306        try {
307            Thread.sleep(POST_LOCK_TIMEOUT);
308        } catch (InterruptedException e) {
309        }
310
311        State s = mThread.getState();
312        if (Eas.USER_LOG) {
313            userLog(threadName + ": State = " + s.name());
314        }
315
316        synchronized (getSynchronizer()) {
317            // If the thread is still hanging around and the same post is pending, let's try to
318            // stop the thread with an interrupt.
319            if ((s != State.TERMINATED) && (mPendingPost != null) && (mPendingPost == post)) {
320                mStop = true;
321                mThread.interrupt();
322                userLog("Interrupting...");
323                // Let the caller know we had to interrupt the thread
324                return false;
325            }
326        }
327        // Let the caller know that the alarm was handled normally
328        return true;
329    }
330
331    @Override
332    public void reset() {
333        synchronized(getSynchronizer()) {
334            if (mPendingPost != null) {
335                URI uri = mPendingPost.getURI();
336                if (uri != null) {
337                    String query = uri.getQuery();
338                    if (query.startsWith("Cmd=Ping")) {
339                        userLog("Reset, aborting Ping");
340                        mPostReset = true;
341                        mPendingPost.abort();
342                    }
343                }
344            }
345        }
346    }
347
348    @Override
349    public void stop() {
350        mStop = true;
351        synchronized(getSynchronizer()) {
352            if (mPendingPost != null) {
353                mPendingPost.abort();
354            }
355        }
356    }
357
358    @Override
359    public void addRequest(Request request) {
360        // Don't allow duplicates of requests; just refuse them
361        if (mRequestQueue.contains(request)) return;
362        // Add the request
363        super.addRequest(request);
364    }
365
366    /**
367     * Determine whether an HTTP code represents an authentication error
368     * @param code the HTTP code returned by the server
369     * @return whether or not the code represents an authentication error
370     */
371    protected boolean isAuthError(int code) {
372        return (code == HttpStatus.SC_UNAUTHORIZED) || (code == HttpStatus.SC_FORBIDDEN);
373    }
374
375    /**
376     * Determine whether an HTTP code represents a provisioning error
377     * @param code the HTTP code returned by the server
378     * @return whether or not the code represents an provisioning error
379     */
380    protected boolean isProvisionError(int code) {
381        return (code == HTTP_NEED_PROVISIONING) || (code == HttpStatus.SC_FORBIDDEN);
382    }
383
384    private void setupProtocolVersion(EasSyncService service, Header versionHeader)
385            throws MessagingException {
386        // The string is a comma separated list of EAS versions in ascending order
387        // e.g. 1.0,2.0,2.5,12.0,12.1
388        String supportedVersions = versionHeader.getValue();
389        userLog("Server supports versions: ", supportedVersions);
390        String[] supportedVersionsArray = supportedVersions.split(",");
391        String ourVersion = null;
392        // Find the most recent version we support
393        for (String version: supportedVersionsArray) {
394            if (version.equals(Eas.SUPPORTED_PROTOCOL_EX2003) ||
395                    version.equals(Eas.SUPPORTED_PROTOCOL_EX2007) ||
396                    version.equals(Eas.SUPPORTED_PROTOCOL_EX2007_SP1)) {
397                ourVersion = version;
398            }
399        }
400        // If we don't support any of the servers supported versions, throw an exception here
401        // This will cause validation to fail
402        if (ourVersion == null) {
403            Log.w(TAG, "No supported EAS versions: " + supportedVersions);
404            throw new MessagingException(MessagingException.PROTOCOL_VERSION_UNSUPPORTED);
405        } else {
406            service.mProtocolVersion = ourVersion;
407            service.mProtocolVersionDouble = Eas.getProtocolVersionDouble(ourVersion);
408            if (service.mAccount != null) {
409                service.mAccount.mProtocolVersion = ourVersion;
410            }
411        }
412    }
413
414    /**
415     * Create an EasSyncService for the specified account
416     *
417     * @param context the caller's context
418     * @param account the account
419     * @return the service, or null if the account is on hold or hasn't been initialized
420     */
421    private static EasSyncService setupServiceForAccount(Context context, Account account) {
422        // Just return null if we're on security hold
423        if ((account.mFlags & Account.FLAGS_SECURITY_HOLD) != 0) {
424            return null;
425        }
426        // If there's no protocol version, we're not initialized
427        String protocolVersion = account.mProtocolVersion;
428        if (protocolVersion == null) {
429            return null;
430        }
431        EasSyncService svc = new EasSyncService("OutOfBand");
432        HostAuth ha = HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv);
433        svc.mProtocolVersion = protocolVersion;
434        svc.mProtocolVersionDouble = Eas.getProtocolVersionDouble(protocolVersion);
435        svc.mContext = context;
436        svc.mHostAddress = ha.mAddress;
437        svc.mUserName = ha.mLogin;
438        svc.mPassword = ha.mPassword;
439        svc.mSsl = (ha.mFlags & HostAuth.FLAG_SSL) != 0;
440        svc.mTrustSsl = (ha.mFlags & HostAuth.FLAG_TRUST_ALL) != 0;
441        try {
442            svc.mDeviceId = ExchangeService.getDeviceId(context);
443        } catch (IOException e) {
444            return null;
445        }
446        svc.mAccount = account;
447        return svc;
448    }
449
450    public static int searchMessages(Context context, long accountId, long mailboxId,
451            boolean includeSubfolders, String query, int numResults, int firstResult,
452            long destMailboxId) {
453        // Sanity check for arguments
454        if (numResults < 0 || numResults > MAX_SEARCH_RESULTS || firstResult < 0) return 0;
455        // TODO Should this be checked in UI?  Are there guidelines for minimums?
456        if (query == null || query.length() < MIN_QUERY_LENGTH) return 0;
457
458        int res = 0;
459        Account account = Account.restoreAccountWithId(context, accountId);
460        if (account == null) return res;
461        EasSyncService svc = setupServiceForAccount(context, account);
462        if (svc == null) return res;
463        try {
464            Mailbox searchMailbox = Mailbox.restoreMailboxWithId(context, destMailboxId);
465            // Sanity check; account might have been deleted?
466            if (searchMailbox == null) return res;
467            svc.mMailbox = searchMailbox;
468            svc.mAccount = account;
469            Serializer s = new Serializer();
470            s.start(Tags.SEARCH_SEARCH).start(Tags.SEARCH_STORE);
471            s.data(Tags.SEARCH_NAME, "Mailbox");
472            s.start(Tags.SEARCH_QUERY).start(Tags.SEARCH_AND);
473            s.data(Tags.SYNC_CLASS, "Email");
474            s.data(Tags.SEARCH_FREE_TEXT, query);
475            s.end().end();              // SEARCH_AND, SEARCH_QUERY
476            s.start(Tags.SEARCH_OPTIONS);
477            if (firstResult == 0) {
478                s.tag(Tags.SEARCH_REBUILD_RESULTS);
479            }
480            if (includeSubfolders) {
481                s.tag(Tags.SEARCH_DEEP_TRAVERSAL);
482            }
483            // Range is sent in the form first-last (e.g. 0-9)
484            s.data(Tags.SEARCH_RANGE, firstResult + "-" + (firstResult + numResults - 1));
485            s.start(Tags.BASE_BODY_PREFERENCE);
486            s.data(Tags.BASE_TYPE, Eas.BODY_PREFERENCE_HTML);
487            s.data(Tags.BASE_TRUNCATION_SIZE, "20000");
488            s.end();                    // BASE_BODY_PREFERENCE
489            s.end().end().end().done(); // SEARCH_OPTIONS, SEARCH_STORE, SEARCH_SEARCH
490            HttpResponse resp = svc.sendHttpClientPost("Search", s.toByteArray());
491            HttpEntity entity = resp.getEntity();
492            try {
493                int code = resp.getStatusLine().getStatusCode();
494                if (code == HttpStatus.SC_OK) {
495                    InputStream is = entity.getContent();
496                    try {
497                        new SearchParser(is, svc, query).parse();
498                    } finally {
499                        is.close();
500                    }
501                } else {
502                    svc.userLog("Search returned " + code);
503                }
504            } finally {
505                if (entity != null) {
506                    entity.consumeContent();
507                }
508            }
509        } catch (IOException e) {
510            svc.userLog("Search exception " + e);
511        }
512        // TODO Capture and return the correct value
513        return res;
514    }
515
516    @Override
517    public Bundle validateAccount(String hostAddress, String userName, String password, int port,
518            boolean ssl, boolean trustCertificates, Context context) {
519        Bundle bundle = new Bundle();
520        int resultCode = MessagingException.NO_ERROR;
521        try {
522            userLog("Testing EAS: ", hostAddress, ", ", userName, ", ssl = ", ssl ? "1" : "0");
523            EasSyncService svc = new EasSyncService("%TestAccount%");
524            svc.mContext = context;
525            svc.mHostAddress = hostAddress;
526            svc.mUserName = userName;
527            svc.mPassword = password;
528            svc.mSsl = ssl;
529            svc.mTrustSsl = trustCertificates;
530            // We mustn't use the "real" device id or we'll screw up current accounts
531            // Any string will do, but we'll go for "validate"
532            svc.mDeviceId = "validate";
533            HttpResponse resp = svc.sendHttpClientOptions();
534            HttpEntity entity = resp.getEntity();
535            try {
536                int code = resp.getStatusLine().getStatusCode();
537                userLog("Validation (OPTIONS) response: " + code);
538                if (code == HttpStatus.SC_OK) {
539                    // No exception means successful validation
540                    Header commands = resp.getFirstHeader("MS-ASProtocolCommands");
541                    Header versions = resp.getFirstHeader("ms-asprotocolversions");
542                    // Make sure we've got the right protocol version set up
543                    try {
544                        if (commands == null || versions == null) {
545                            userLog("OPTIONS response without commands or versions");
546                            // We'll treat this as a protocol exception
547                            throw new MessagingException(0);
548                        }
549                        setupProtocolVersion(svc, versions);
550                    } catch (MessagingException e) {
551                        bundle.putInt(EmailServiceProxy.VALIDATE_BUNDLE_RESULT_CODE,
552                                MessagingException.PROTOCOL_VERSION_UNSUPPORTED);
553                        return bundle;
554                    }
555
556                    // Run second test here for provisioning failures using FolderSync
557                    userLog("Try folder sync");
558                    // Send "0" as the sync key for new accounts; otherwise, use the current key
559                    String syncKey = "0";
560                    Account existingAccount =
561                        Utility.findExistingAccount(context, -1L, hostAddress, userName);
562                    if (existingAccount != null && existingAccount.mSyncKey != null) {
563                        syncKey = existingAccount.mSyncKey;
564                    }
565                    Serializer s = new Serializer();
566                    s.start(Tags.FOLDER_FOLDER_SYNC).start(Tags.FOLDER_SYNC_KEY).text(syncKey)
567                        .end().end().done();
568                    resp = svc.sendHttpClientPost("FolderSync", s.toByteArray());
569                    code = resp.getStatusLine().getStatusCode();
570                    // We'll get one of the following responses if policies are required
571                    if (code == HttpStatus.SC_FORBIDDEN || code == HTTP_NEED_PROVISIONING) {
572                        // Get the policies and see if we are able to support them
573                        ProvisionParser pp = svc.canProvision();
574                        if (pp != null) {
575                            // Set the proper result code and save the PolicySet in our Bundle
576                            resultCode = MessagingException.SECURITY_POLICIES_REQUIRED;
577                            bundle.putParcelable(EmailServiceProxy.VALIDATE_BUNDLE_POLICY_SET,
578                                    pp.getPolicySet());
579                        } else
580                            // If not, set the proper code (the account will not be created)
581                            resultCode = MessagingException.SECURITY_POLICIES_UNSUPPORTED;
582                    } else if (code == HttpStatus.SC_NOT_FOUND) {
583                        // We get a 404 from OWA addresses (which are NOT EAS addresses)
584                        resultCode = MessagingException.PROTOCOL_VERSION_UNSUPPORTED;
585                    } else if (code != HttpStatus.SC_OK) {
586                        // Fail generically with anything other than success
587                        userLog("Unexpected response for FolderSync: ", code);
588                        resultCode = MessagingException.UNSPECIFIED_EXCEPTION;
589                    } else {
590                        userLog("Validation successful");
591                    }
592                } else if (isAuthError(code)) {
593                    userLog("Authentication failed");
594                    resultCode = MessagingException.AUTHENTICATION_FAILED;
595                } else if (code == INTERNAL_SERVER_ERROR_CODE) {
596                    // For Exchange 2003, this could mean an authentication failure OR server error
597                    userLog("Internal server error");
598                    resultCode = MessagingException.AUTHENTICATION_FAILED_OR_SERVER_ERROR;
599                } else {
600                    // TODO Need to catch other kinds of errors (e.g. policy) For now, report code.
601                    userLog("Validation failed, reporting I/O error: ", code);
602                    resultCode = MessagingException.IOERROR;
603                }
604            } finally {
605                if (entity != null) {
606                    entity.consumeContent();
607                }
608            }
609        } catch (IOException e) {
610            Throwable cause = e.getCause();
611            if (cause != null && cause instanceof CertificateException) {
612                userLog("CertificateException caught: ", e.getMessage());
613                resultCode = MessagingException.GENERAL_SECURITY;
614            }
615            userLog("IOException caught: ", e.getMessage());
616            resultCode = MessagingException.IOERROR;
617        }
618        bundle.putInt(EmailServiceProxy.VALIDATE_BUNDLE_RESULT_CODE, resultCode);
619        return bundle;
620    }
621
622    /**
623     * Gets the redirect location from the HTTP headers and uses that to modify the HttpPost so that
624     * it can be reused
625     *
626     * @param resp the HttpResponse that indicates a redirect (451)
627     * @param post the HttpPost that was originally sent to the server
628     * @return the HttpPost, updated with the redirect location
629     */
630    private HttpPost getRedirect(HttpResponse resp, HttpPost post) {
631        Header locHeader = resp.getFirstHeader("X-MS-Location");
632        if (locHeader != null) {
633            String loc = locHeader.getValue();
634            // If we've gotten one and it shows signs of looking like an address, we try
635            // sending our request there
636            if (loc != null && loc.startsWith("http")) {
637                post.setURI(URI.create(loc));
638                return post;
639            }
640        }
641        return null;
642    }
643
644    /**
645     * Send the POST command to the autodiscover server, handling a redirect, if necessary, and
646     * return the HttpResponse.  If we get a 401 (unauthorized) error and we're using the
647     * full email address, try the bare user name instead (e.g. foo instead of foo@bar.com)
648     *
649     * @param client the HttpClient to be used for the request
650     * @param post the HttpPost we're going to send
651     * @param canRetry whether we can retry using the bare name on an authentication failure (401)
652     * @return an HttpResponse from the original or redirect server
653     * @throws IOException on any IOException within the HttpClient code
654     * @throws MessagingException
655     */
656    private HttpResponse postAutodiscover(HttpClient client, HttpPost post, boolean canRetry)
657            throws IOException, MessagingException {
658        userLog("Posting autodiscover to: " + post.getURI());
659        HttpResponse resp = executePostWithTimeout(client, post, COMMAND_TIMEOUT);
660        int code = resp.getStatusLine().getStatusCode();
661        // On a redirect, try the new location
662        if (code == AUTO_DISCOVER_REDIRECT_CODE) {
663            post = getRedirect(resp, post);
664            if (post != null) {
665                userLog("Posting autodiscover to redirect: " + post.getURI());
666                return executePostWithTimeout(client, post, COMMAND_TIMEOUT);
667            }
668        // 401 (Unauthorized) is for true auth errors when used in Autodiscover
669        } else if (code == HttpStatus.SC_UNAUTHORIZED) {
670            if (canRetry && mUserName.contains("@")) {
671                // Try again using the bare user name
672                int atSignIndex = mUserName.indexOf('@');
673                mUserName = mUserName.substring(0, atSignIndex);
674                cacheAuthAndCmdString();
675                userLog("401 received; trying username: ", mUserName);
676                // Recreate the basic authentication string and reset the header
677                post.removeHeaders("Authorization");
678                post.setHeader("Authorization", mAuthString);
679                return postAutodiscover(client, post, false);
680            }
681            throw new MessagingException(MessagingException.AUTHENTICATION_FAILED);
682        // 403 (and others) we'll just punt on
683        } else if (code != HttpStatus.SC_OK) {
684            // We'll try the next address if this doesn't work
685            userLog("Code: " + code + ", throwing IOException");
686            throw new IOException();
687        }
688        return resp;
689    }
690
691    /**
692     * Use the Exchange 2007 AutoDiscover feature to try to retrieve server information using
693     * only an email address and the password
694     *
695     * @param userName the user's email address
696     * @param password the user's password
697     * @return a HostAuth ready to be saved in an Account or null (failure)
698     */
699    public Bundle tryAutodiscover(String userName, String password) throws RemoteException {
700        XmlSerializer s = Xml.newSerializer();
701        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
702        HostAuth hostAuth = new HostAuth();
703        Bundle bundle = new Bundle();
704        bundle.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE,
705                MessagingException.NO_ERROR);
706        try {
707            // Build the XML document that's sent to the autodiscover server(s)
708            s.setOutput(os, "UTF-8");
709            s.startDocument("UTF-8", false);
710            s.startTag(null, "Autodiscover");
711            s.attribute(null, "xmlns", AUTO_DISCOVER_SCHEMA_PREFIX + "requestschema/2006");
712            s.startTag(null, "Request");
713            s.startTag(null, "EMailAddress").text(userName).endTag(null, "EMailAddress");
714            s.startTag(null, "AcceptableResponseSchema");
715            s.text(AUTO_DISCOVER_SCHEMA_PREFIX + "responseschema/2006");
716            s.endTag(null, "AcceptableResponseSchema");
717            s.endTag(null, "Request");
718            s.endTag(null, "Autodiscover");
719            s.endDocument();
720            String req = os.toString();
721
722            // Initialize the user name and password
723            mUserName = userName;
724            mPassword = password;
725            // Make sure the authentication string is recreated and cached
726            cacheAuthAndCmdString();
727
728            // Split out the domain name
729            int amp = userName.indexOf('@');
730            // The UI ensures that userName is a valid email address
731            if (amp < 0) {
732                throw new RemoteException();
733            }
734            String domain = userName.substring(amp + 1);
735
736            // There are up to four attempts here; the two URLs that we're supposed to try per the
737            // specification, and up to one redirect for each (handled in postAutodiscover)
738            // Note: The expectation is that, of these four attempts, only a single server will
739            // actually be identified as the autodiscover server.  For the identified server,
740            // we may also try a 2nd connection with a different format (bare name).
741
742            // Try the domain first and see if we can get a response
743            HttpPost post = new HttpPost("https://" + domain + AUTO_DISCOVER_PAGE);
744            setHeaders(post, false);
745            post.setHeader("Content-Type", "text/xml");
746            post.setEntity(new StringEntity(req));
747            HttpClient client = getHttpClient(COMMAND_TIMEOUT);
748            HttpResponse resp;
749            try {
750                resp = postAutodiscover(client, post, true /*canRetry*/);
751            } catch (IOException e1) {
752                userLog("IOException in autodiscover; trying alternate address");
753                // We catch the IOException here because we have an alternate address to try
754                post.setURI(URI.create("https://autodiscover." + domain + AUTO_DISCOVER_PAGE));
755                // If we fail here, we're out of options, so we let the outer try catch the
756                // IOException and return null
757                resp = postAutodiscover(client, post, true /*canRetry*/);
758            }
759
760            HttpEntity entity = resp.getEntity();
761            try {
762                // Get the "final" code; if it's not 200, just return null
763                int code = resp.getStatusLine().getStatusCode();
764                userLog("Code: " + code);
765                if (code != HttpStatus.SC_OK) return null;
766
767                InputStream is = entity.getContent();
768                // The response to Autodiscover is regular XML (not WBXML)
769                // If we ever get an error in this process, we'll just punt and return null
770                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
771                XmlPullParser parser = factory.newPullParser();
772                parser.setInput(is, "UTF-8");
773                int type = parser.getEventType();
774                if (type == XmlPullParser.START_DOCUMENT) {
775                    type = parser.next();
776                    if (type == XmlPullParser.START_TAG) {
777                        String name = parser.getName();
778                        if (name.equals("Autodiscover")) {
779                            hostAuth = new HostAuth();
780                            parseAutodiscover(parser, hostAuth);
781                            // On success, we'll have a server address and login
782                            if (hostAuth.mAddress != null) {
783                                // Fill in the rest of the HostAuth
784                                // We use the user name and password that were successful during
785                                // the autodiscover process
786                                hostAuth.mLogin = mUserName;
787                                hostAuth.mPassword = mPassword;
788                                hostAuth.mPort = 443;
789                                hostAuth.mProtocol = "eas";
790                                hostAuth.mFlags =
791                                    HostAuth.FLAG_SSL | HostAuth.FLAG_AUTHENTICATE;
792                                bundle.putParcelable(
793                                        EmailServiceProxy.AUTO_DISCOVER_BUNDLE_HOST_AUTH, hostAuth);
794                            } else {
795                                bundle.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE,
796                                        MessagingException.UNSPECIFIED_EXCEPTION);
797                            }
798                        }
799                    }
800                }
801            } catch (XmlPullParserException e1) {
802                // This would indicate an I/O error of some sort
803                // We will simply return null and user can configure manually
804            } finally {
805               if (entity != null) {
806                   entity.consumeContent();
807               }
808            }
809        // There's no reason at all for exceptions to be thrown, and it's ok if so.
810        // We just won't do auto-discover; user can configure manually
811       } catch (IllegalArgumentException e) {
812             bundle.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE,
813                     MessagingException.UNSPECIFIED_EXCEPTION);
814       } catch (IllegalStateException e) {
815            bundle.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE,
816                    MessagingException.UNSPECIFIED_EXCEPTION);
817       } catch (IOException e) {
818            userLog("IOException in Autodiscover", e);
819            bundle.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE,
820                    MessagingException.IOERROR);
821        } catch (MessagingException e) {
822            bundle.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE,
823                    MessagingException.AUTHENTICATION_FAILED);
824        }
825        return bundle;
826    }
827
828    void parseServer(XmlPullParser parser, HostAuth hostAuth)
829            throws XmlPullParserException, IOException {
830        boolean mobileSync = false;
831        while (true) {
832            int type = parser.next();
833            if (type == XmlPullParser.END_TAG && parser.getName().equals("Server")) {
834                break;
835            } else if (type == XmlPullParser.START_TAG) {
836                String name = parser.getName();
837                if (name.equals("Type")) {
838                    if (parser.nextText().equals("MobileSync")) {
839                        mobileSync = true;
840                    }
841                } else if (mobileSync && name.equals("Url")) {
842                    String url = parser.nextText().toLowerCase();
843                    // This will look like https://<server address>/Microsoft-Server-ActiveSync
844                    // We need to extract the <server address>
845                    if (url.startsWith("https://") &&
846                            url.endsWith("/microsoft-server-activesync")) {
847                        int lastSlash = url.lastIndexOf('/');
848                        hostAuth.mAddress = url.substring(8, lastSlash);
849                        userLog("Autodiscover, server: " + hostAuth.mAddress);
850                    }
851                }
852            }
853        }
854    }
855
856    void parseSettings(XmlPullParser parser, HostAuth hostAuth)
857            throws XmlPullParserException, IOException {
858        while (true) {
859            int type = parser.next();
860            if (type == XmlPullParser.END_TAG && parser.getName().equals("Settings")) {
861                break;
862            } else if (type == XmlPullParser.START_TAG) {
863                String name = parser.getName();
864                if (name.equals("Server")) {
865                    parseServer(parser, hostAuth);
866                }
867            }
868        }
869    }
870
871    void parseAction(XmlPullParser parser, HostAuth hostAuth)
872            throws XmlPullParserException, IOException {
873        while (true) {
874            int type = parser.next();
875            if (type == XmlPullParser.END_TAG && parser.getName().equals("Action")) {
876                break;
877            } else if (type == XmlPullParser.START_TAG) {
878                String name = parser.getName();
879                if (name.equals("Error")) {
880                    // Should parse the error
881                } else if (name.equals("Redirect")) {
882                    Log.d(TAG, "Redirect: " + parser.nextText());
883                } else if (name.equals("Settings")) {
884                    parseSettings(parser, hostAuth);
885                }
886            }
887        }
888    }
889
890    void parseUser(XmlPullParser parser, HostAuth hostAuth)
891            throws XmlPullParserException, IOException {
892        while (true) {
893            int type = parser.next();
894            if (type == XmlPullParser.END_TAG && parser.getName().equals("User")) {
895                break;
896            } else if (type == XmlPullParser.START_TAG) {
897                String name = parser.getName();
898                if (name.equals("EMailAddress")) {
899                    String addr = parser.nextText();
900                    userLog("Autodiscover, email: " + addr);
901                } else if (name.equals("DisplayName")) {
902                    String dn = parser.nextText();
903                    userLog("Autodiscover, user: " + dn);
904                }
905            }
906        }
907    }
908
909    void parseResponse(XmlPullParser parser, HostAuth hostAuth)
910            throws XmlPullParserException, IOException {
911        while (true) {
912            int type = parser.next();
913            if (type == XmlPullParser.END_TAG && parser.getName().equals("Response")) {
914                break;
915            } else if (type == XmlPullParser.START_TAG) {
916                String name = parser.getName();
917                if (name.equals("User")) {
918                    parseUser(parser, hostAuth);
919                } else if (name.equals("Action")) {
920                    parseAction(parser, hostAuth);
921                }
922            }
923        }
924    }
925
926    void parseAutodiscover(XmlPullParser parser, HostAuth hostAuth)
927            throws XmlPullParserException, IOException {
928        while (true) {
929            int type = parser.nextTag();
930            if (type == XmlPullParser.END_TAG && parser.getName().equals("Autodiscover")) {
931                break;
932            } else if (type == XmlPullParser.START_TAG && parser.getName().equals("Response")) {
933                parseResponse(parser, hostAuth);
934            }
935        }
936    }
937
938    /**
939     * Contact the GAL and obtain a list of matching accounts
940     * @param context caller's context
941     * @param accountId the account Id to search
942     * @param filter the characters entered so far
943     * @return a result record or null for no data
944     *
945     * TODO: shorter timeout for interactive lookup
946     * TODO: make watchdog actually work (it doesn't understand our service w/Mailbox == 0)
947     * TODO: figure out why sendHttpClientPost() hangs - possibly pool exhaustion
948     */
949    static public GalResult searchGal(Context context, long accountId, String filter, int limit) {
950        Account acct = Account.restoreAccountWithId(context, accountId);
951        if (acct != null) {
952            EasSyncService svc = setupServiceForAccount(context, acct);
953            if (svc == null) return null;
954            try {
955                Serializer s = new Serializer();
956                s.start(Tags.SEARCH_SEARCH).start(Tags.SEARCH_STORE);
957                s.data(Tags.SEARCH_NAME, "GAL").data(Tags.SEARCH_QUERY, filter);
958                s.start(Tags.SEARCH_OPTIONS);
959                s.data(Tags.SEARCH_RANGE, "0-" + Integer.toString(limit - 1));
960                s.end().end().end().done();
961                HttpResponse resp = svc.sendHttpClientPost("Search", s.toByteArray());
962                HttpEntity entity = resp.getEntity();
963                try {
964                    int code = resp.getStatusLine().getStatusCode();
965                    if (code == HttpStatus.SC_OK) {
966                        InputStream is = entity.getContent();
967                        try {
968                            GalParser gp = new GalParser(is, svc);
969                            if (gp.parse()) {
970                                return gp.getGalResult();
971                            }
972                        } finally {
973                            is.close();
974                        }
975                    } else {
976                        svc.userLog("GAL lookup returned " + code);
977                    }
978                } finally {
979                    if (entity != null) {
980                        entity.consumeContent();
981                    }
982                }
983            } catch (IOException e) {
984                // GAL is non-critical; we'll just go on
985                svc.userLog("GAL lookup exception " + e);
986            }
987        }
988        return null;
989    }
990
991    private void doStatusCallback(long messageId, long attachmentId, int status) {
992        try {
993            ExchangeService.callback().loadAttachmentStatus(messageId, attachmentId, status, 0);
994        } catch (RemoteException e) {
995            // No danger if the client is no longer around
996        }
997    }
998
999    private void doProgressCallback(long messageId, long attachmentId, int progress) {
1000        try {
1001            ExchangeService.callback().loadAttachmentStatus(messageId, attachmentId,
1002                    EmailServiceStatus.IN_PROGRESS, progress);
1003        } catch (RemoteException e) {
1004            // No danger if the client is no longer around
1005        }
1006    }
1007
1008    public File createUniqueFileInternal(String dir, String filename) {
1009        File directory;
1010        if (dir == null) {
1011            directory = mContext.getFilesDir();
1012        } else {
1013            directory = new File(dir);
1014        }
1015        if (!directory.exists()) {
1016            directory.mkdirs();
1017        }
1018        File file = new File(directory, filename);
1019        if (!file.exists()) {
1020            return file;
1021        }
1022        // Get the extension of the file, if any.
1023        int index = filename.lastIndexOf('.');
1024        String name = filename;
1025        String extension = "";
1026        if (index != -1) {
1027            name = filename.substring(0, index);
1028            extension = filename.substring(index);
1029        }
1030        for (int i = 2; i < Integer.MAX_VALUE; i++) {
1031            file = new File(directory, name + '-' + i + extension);
1032            if (!file.exists()) {
1033                return file;
1034            }
1035        }
1036        return null;
1037    }
1038
1039    /**
1040     * Loads an attachment, based on the PartRequest passed in.  The PartRequest is basically our
1041     * wrapper for Attachment
1042     * @param req the part (attachment) to be retrieved
1043     * @throws IOException
1044     */
1045    protected void loadAttachment(PartRequest req) throws IOException {
1046        Attachment att = req.mAttachment;
1047        Message msg = Message.restoreMessageWithId(mContext, att.mMessageKey);
1048        if (msg == null) {
1049            doStatusCallback(att.mMessageKey, att.mId, EmailServiceStatus.MESSAGE_NOT_FOUND);
1050            return;
1051        } else {
1052            doProgressCallback(msg.mId, att.mId, 0);
1053        }
1054
1055        String cmd = "GetAttachment&AttachmentName=" + att.mLocation;
1056        HttpResponse res = sendHttpClientPost(cmd, null, COMMAND_TIMEOUT);
1057        HttpEntity entity = res.getEntity();
1058
1059        try {
1060            int status = res.getStatusLine().getStatusCode();
1061            if (status == HttpStatus.SC_OK) {
1062                int len = (int)entity.getContentLength();
1063                InputStream is = entity.getContent();
1064                OutputStream os = null;
1065                Uri attachmentUri = AttachmentUtilities.getAttachmentUri(att.mAccountKey, att.mId);
1066                String fileName = null;
1067                try {
1068                    os = mContentResolver.openOutputStream(attachmentUri);
1069                    userLog("Attachment filename retrieved as: " + fileName);
1070                } catch (FileNotFoundException e) {
1071                    userLog("Can't get attachment; write file not found?");
1072                }
1073                if (os != null) {
1074                    // len > 0 means that Content-Length was set in the headers
1075                    // len < 0 means "chunked" transfer-encoding
1076                    if (len != 0) {
1077                        try {
1078                            mPendingRequest = req;
1079                            byte[] bytes = new byte[CHUNK_SIZE];
1080                            int length = len;
1081                            // Loop terminates 1) when EOF is reached or 2) IOException occurs
1082                            // One of these is guaranteed to occur
1083                            int totalRead = 0;
1084                            userLog("Attachment content-length: ", len);
1085                            while (true) {
1086                                int read = is.read(bytes, 0, CHUNK_SIZE);
1087
1088                                // read < 0 means that EOF was reached
1089                                if (read < 0) {
1090                                    userLog("Attachment load reached EOF, totalRead: ", totalRead);
1091                                    break;
1092                                }
1093
1094                                // Keep track of how much we've read for progress callback
1095                                totalRead += read;
1096
1097                                // Write these bytes out
1098                                os.write(bytes, 0, read);
1099
1100                                // We can't report percentages if this is chunked; the
1101                                // length of incoming data is unknown
1102                                if (length > 0) {
1103                                    // Belt and suspenders check to prevent runaway reading
1104                                    if (totalRead > length) {
1105                                        errorLog("totalRead is greater than attachment length?");
1106                                        break;
1107                                    }
1108                                    int pct = (totalRead * 100) / length;
1109                                    doProgressCallback(msg.mId, att.mId, pct);
1110                                }
1111                            }
1112                        } finally {
1113                            mPendingRequest = null;
1114                        }
1115                    }
1116                    os.flush();
1117                    os.close();
1118
1119                    // EmailProvider will throw an exception if update an unsaved attachment
1120                    if (att.isSaved()) {
1121                        ContentValues cv = new ContentValues();
1122                        cv.put(AttachmentColumns.CONTENT_URI, attachmentUri.toString());
1123                        att.update(mContext, cv);
1124                        doStatusCallback(msg.mId, att.mId, EmailServiceStatus.SUCCESS);
1125                    }
1126                }
1127            } else {
1128                doStatusCallback(msg.mId, att.mId, EmailServiceStatus.MESSAGE_NOT_FOUND);
1129            }
1130        } finally {
1131            if (entity != null) {
1132                entity.consumeContent();
1133            }
1134        }
1135    }
1136
1137    /**
1138     * Send an email responding to a Message that has been marked as a meeting request.  The message
1139     * will consist a little bit of event information and an iCalendar attachment
1140     * @param msg the meeting request email
1141     */
1142    private void sendMeetingResponseMail(Message msg, int response) {
1143        // Get the meeting information; we'd better have some...
1144        PackedString meetingInfo = new PackedString(msg.mMeetingInfo);
1145        if (meetingInfo == null) return;
1146
1147        // This will come as "First Last" <box@server.blah>, so we use Address to
1148        // parse it into parts; we only need the email address part for the ics file
1149        Address[] addrs = Address.parse(meetingInfo.get(MeetingInfo.MEETING_ORGANIZER_EMAIL));
1150        // It shouldn't be possible, but handle it anyway
1151        if (addrs.length != 1) return;
1152        String organizerEmail = addrs[0].getAddress();
1153
1154        String dtStamp = meetingInfo.get(MeetingInfo.MEETING_DTSTAMP);
1155        String dtStart = meetingInfo.get(MeetingInfo.MEETING_DTSTART);
1156        String dtEnd = meetingInfo.get(MeetingInfo.MEETING_DTEND);
1157
1158        // What we're doing here is to create an Entity that looks like an Event as it would be
1159        // stored by CalendarProvider
1160        ContentValues entityValues = new ContentValues();
1161        Entity entity = new Entity(entityValues);
1162
1163        // Fill in times, location, title, and organizer
1164        entityValues.put("DTSTAMP",
1165                CalendarUtilities.convertEmailDateTimeToCalendarDateTime(dtStamp));
1166        entityValues.put(Events.DTSTART, Utility.parseEmailDateTimeToMillis(dtStart));
1167        entityValues.put(Events.DTEND, Utility.parseEmailDateTimeToMillis(dtEnd));
1168        entityValues.put(Events.EVENT_LOCATION, meetingInfo.get(MeetingInfo.MEETING_LOCATION));
1169        entityValues.put(Events.TITLE, meetingInfo.get(MeetingInfo.MEETING_TITLE));
1170        entityValues.put(Events.ORGANIZER, organizerEmail);
1171
1172        // Add ourselves as an attendee, using our account email address
1173        ContentValues attendeeValues = new ContentValues();
1174        attendeeValues.put(Attendees.ATTENDEE_RELATIONSHIP,
1175                Attendees.RELATIONSHIP_ATTENDEE);
1176        attendeeValues.put(Attendees.ATTENDEE_EMAIL, mAccount.mEmailAddress);
1177        entity.addSubValue(Attendees.CONTENT_URI, attendeeValues);
1178
1179        // Add the organizer
1180        ContentValues organizerValues = new ContentValues();
1181        organizerValues.put(Attendees.ATTENDEE_RELATIONSHIP,
1182                Attendees.RELATIONSHIP_ORGANIZER);
1183        organizerValues.put(Attendees.ATTENDEE_EMAIL, organizerEmail);
1184        entity.addSubValue(Attendees.CONTENT_URI, organizerValues);
1185
1186        // Create a message from the Entity we've built.  The message will have fields like
1187        // to, subject, date, and text filled in.  There will also be an "inline" attachment
1188        // which is in iCalendar format
1189        int flag;
1190        switch(response) {
1191            case EmailServiceConstants.MEETING_REQUEST_ACCEPTED:
1192                flag = Message.FLAG_OUTGOING_MEETING_ACCEPT;
1193                break;
1194            case EmailServiceConstants.MEETING_REQUEST_DECLINED:
1195                flag = Message.FLAG_OUTGOING_MEETING_DECLINE;
1196                break;
1197            case EmailServiceConstants.MEETING_REQUEST_TENTATIVE:
1198            default:
1199                flag = Message.FLAG_OUTGOING_MEETING_TENTATIVE;
1200                break;
1201        }
1202        Message outgoingMsg =
1203            CalendarUtilities.createMessageForEntity(mContext, entity, flag,
1204                    meetingInfo.get(MeetingInfo.MEETING_UID), mAccount);
1205        // Assuming we got a message back (we might not if the event has been deleted), send it
1206        if (outgoingMsg != null) {
1207            EasOutboxService.sendMessage(mContext, mAccount.mId, outgoingMsg);
1208        }
1209    }
1210
1211    /**
1212     * Responds to a move request.  The MessageMoveRequest is basically our
1213     * wrapper for the MoveItems service call
1214     * @param req the request (message id and "to" mailbox id)
1215     * @throws IOException
1216     */
1217    protected void messageMoveRequest(MessageMoveRequest req) throws IOException {
1218        // Retrieve the message and mailbox; punt if either are null
1219        Message msg = Message.restoreMessageWithId(mContext, req.mMessageId);
1220        if (msg == null) return;
1221        Cursor c = mContentResolver.query(ContentUris.withAppendedId(Message.UPDATED_CONTENT_URI,
1222                msg.mId), new String[] {MessageColumns.MAILBOX_KEY}, null, null, null);
1223        Mailbox srcMailbox = null;
1224        try {
1225            if (!c.moveToNext()) return;
1226            srcMailbox = Mailbox.restoreMailboxWithId(mContext, c.getLong(0));
1227        } finally {
1228            c.close();
1229        }
1230        if (srcMailbox == null) return;
1231        Mailbox dstMailbox = Mailbox.restoreMailboxWithId(mContext, req.mMailboxId);
1232        if (dstMailbox == null) return;
1233        Serializer s = new Serializer();
1234        s.start(Tags.MOVE_MOVE_ITEMS).start(Tags.MOVE_MOVE);
1235        s.data(Tags.MOVE_SRCMSGID, msg.mServerId);
1236        s.data(Tags.MOVE_SRCFLDID, srcMailbox.mServerId);
1237        s.data(Tags.MOVE_DSTFLDID, dstMailbox.mServerId);
1238        s.end().end().done();
1239        HttpResponse res = sendHttpClientPost("MoveItems", s.toByteArray());
1240        HttpEntity entity = res.getEntity();
1241        try {
1242            int status = res.getStatusLine().getStatusCode();
1243            if (status == HttpStatus.SC_OK) {
1244                    int len = (int) entity.getContentLength();
1245                    InputStream is = res.getEntity().getContent();
1246                    if (len != 0) {
1247                        MoveItemsParser p = new MoveItemsParser(is, this);
1248                        p.parse();
1249                        int statusCode = p.getStatusCode();
1250                        ContentValues cv = new ContentValues();
1251                        if (statusCode == MoveItemsParser.STATUS_CODE_REVERT) {
1252                            // Restore the old mailbox id
1253                            cv.put(MessageColumns.MAILBOX_KEY, srcMailbox.mServerId);
1254                            mContentResolver.update(
1255                                    ContentUris.withAppendedId(Message.CONTENT_URI, req.mMessageId),
1256                                    cv, null, null);
1257                        } else if (statusCode == MoveItemsParser.STATUS_CODE_SUCCESS) {
1258                            // Update with the new server id
1259                            cv.put(SyncColumns.SERVER_ID, p.getNewServerId());
1260                            cv.put(Message.FLAGS, msg.mFlags | MESSAGE_FLAG_MOVED_MESSAGE);
1261                            mContentResolver.update(
1262                                    ContentUris.withAppendedId(Message.CONTENT_URI, req.mMessageId),
1263                                    cv, null, null);
1264                        }
1265                        if (statusCode == MoveItemsParser.STATUS_CODE_SUCCESS
1266                                || statusCode == MoveItemsParser.STATUS_CODE_REVERT) {
1267                            // If we revert or succeed, we no longer need the update information
1268                            // OR the now-duplicate email (the new copy will be synced down)
1269                            mContentResolver.delete(ContentUris.withAppendedId(
1270                                    Message.UPDATED_CONTENT_URI, req.mMessageId), null, null);
1271                        } else {
1272                            // In this case, we're retrying, so do nothing.  The request will be
1273                            // handled next sync
1274                        }
1275                    }
1276            } else if (isAuthError(status)) {
1277                throw new EasAuthenticationException();
1278            } else {
1279                userLog("Move items request failed, code: " + status);
1280                throw new IOException();
1281            }
1282        } finally {
1283            if (entity != null) {
1284                entity.consumeContent();
1285            }
1286        }
1287    }
1288
1289    /**
1290     * Responds to a meeting request.  The MeetingResponseRequest is basically our
1291     * wrapper for the meetingResponse service call
1292     * @param req the request (message id and response code)
1293     * @throws IOException
1294     */
1295    protected void sendMeetingResponse(MeetingResponseRequest req) throws IOException {
1296        // Retrieve the message and mailbox; punt if either are null
1297        Message msg = Message.restoreMessageWithId(mContext, req.mMessageId);
1298        if (msg == null) return;
1299        Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, msg.mMailboxKey);
1300        if (mailbox == null) return;
1301        Serializer s = new Serializer();
1302        s.start(Tags.MREQ_MEETING_RESPONSE).start(Tags.MREQ_REQUEST);
1303        s.data(Tags.MREQ_USER_RESPONSE, Integer.toString(req.mResponse));
1304        s.data(Tags.MREQ_COLLECTION_ID, mailbox.mServerId);
1305        s.data(Tags.MREQ_REQ_ID, msg.mServerId);
1306        s.end().end().done();
1307        HttpResponse res = sendHttpClientPost("MeetingResponse", s.toByteArray());
1308        HttpEntity entity = res.getEntity();
1309        try {
1310            int status = res.getStatusLine().getStatusCode();
1311            if (status == HttpStatus.SC_OK) {
1312                int len = (int)entity.getContentLength();
1313                if (len != 0) {
1314                    InputStream is = res.getEntity().getContent();
1315                    new MeetingResponseParser(is, this).parse();
1316                    String meetingInfo = msg.mMeetingInfo;
1317                    if (meetingInfo != null) {
1318                        String responseRequested = new PackedString(meetingInfo).get(
1319                                MeetingInfo.MEETING_RESPONSE_REQUESTED);
1320                        // If there's no tag, or a non-zero tag, we send the response mail
1321                        if ("0".equals(responseRequested)) {
1322                            return;
1323                        }
1324                    }
1325                    sendMeetingResponseMail(msg, req.mResponse);
1326                }
1327            } else if (isAuthError(status)) {
1328                throw new EasAuthenticationException();
1329            } else {
1330                userLog("Meeting response request failed, code: " + status);
1331                throw new IOException();
1332            }
1333        } finally {
1334            if (entity != null) {
1335                entity.consumeContent();
1336            }
1337       }
1338    }
1339
1340    /**
1341     * Using mUserName and mPassword, create and cache mAuthString and mCacheString, which are used
1342     * in all HttpPost commands.  This should be called if these strings are null, or if mUserName
1343     * and/or mPassword are changed
1344     */
1345    private void cacheAuthAndCmdString() {
1346        String safeUserName = Uri.encode(mUserName);
1347        String cs = mUserName + ':' + mPassword;
1348        mAuthString = "Basic " + Base64.encodeToString(cs.getBytes(), Base64.NO_WRAP);
1349        mCmdString = "&User=" + safeUserName + "&DeviceId=" + mDeviceId +
1350            "&DeviceType=" + DEVICE_TYPE;
1351    }
1352
1353    /*package*/ String makeUriString(String cmd, String extra) throws IOException {
1354        // Cache the authentication string and the command string
1355        if (mAuthString == null || mCmdString == null) {
1356            cacheAuthAndCmdString();
1357        }
1358        String us = (mSsl ? (mTrustSsl ? "httpts" : "https") : "http") + "://" + mHostAddress +
1359            "/Microsoft-Server-ActiveSync";
1360        if (cmd != null) {
1361            us += "?Cmd=" + cmd + mCmdString;
1362        }
1363        if (extra != null) {
1364            us += extra;
1365        }
1366        return us;
1367    }
1368
1369    /**
1370     * Set standard HTTP headers, using a policy key if required
1371     * @param method the method we are going to send
1372     * @param usePolicyKey whether or not a policy key should be sent in the headers
1373     */
1374    /*package*/ void setHeaders(HttpRequestBase method, boolean usePolicyKey) {
1375        method.setHeader("Authorization", mAuthString);
1376        method.setHeader("MS-ASProtocolVersion", mProtocolVersion);
1377        method.setHeader("Connection", "keep-alive");
1378        method.setHeader("User-Agent", USER_AGENT);
1379        if (usePolicyKey) {
1380            // If there's an account in existence, use its key; otherwise (we're creating the
1381            // account), send "0".  The server will respond with code 449 if there are policies
1382            // to be enforced
1383            String key = "0";
1384            if (mAccount != null) {
1385                String accountKey = mAccount.mSecuritySyncKey;
1386                if (!TextUtils.isEmpty(accountKey)) {
1387                    key = accountKey;
1388                }
1389            }
1390            method.setHeader("X-MS-PolicyKey", key);
1391        }
1392    }
1393
1394    private ClientConnectionManager getClientConnectionManager() {
1395        return ExchangeService.getClientConnectionManager();
1396    }
1397
1398    private HttpClient getHttpClient(int timeout) {
1399        HttpParams params = new BasicHttpParams();
1400        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
1401        HttpConnectionParams.setSoTimeout(params, timeout);
1402        HttpConnectionParams.setSocketBufferSize(params, 8192);
1403        HttpClient client = new DefaultHttpClient(getClientConnectionManager(), params);
1404        return client;
1405    }
1406
1407    protected HttpResponse sendHttpClientPost(String cmd, byte[] bytes) throws IOException {
1408        return sendHttpClientPost(cmd, new ByteArrayEntity(bytes), COMMAND_TIMEOUT);
1409    }
1410
1411    protected HttpResponse sendHttpClientPost(String cmd, HttpEntity entity) throws IOException {
1412        return sendHttpClientPost(cmd, entity, COMMAND_TIMEOUT);
1413    }
1414
1415    protected HttpResponse sendPing(byte[] bytes, int heartbeat) throws IOException {
1416       Thread.currentThread().setName(mAccount.mDisplayName + ": Ping");
1417       if (Eas.USER_LOG) {
1418           userLog("Send ping, timeout: " + heartbeat + "s, high: " + mPingHighWaterMark + 's');
1419       }
1420       return sendHttpClientPost(PING_COMMAND, new ByteArrayEntity(bytes), (heartbeat+5)*SECONDS);
1421    }
1422
1423    /**
1424     * Convenience method for executePostWithTimeout for use other than with the Ping command
1425     */
1426    protected HttpResponse executePostWithTimeout(HttpClient client, HttpPost method, int timeout)
1427            throws IOException {
1428        return executePostWithTimeout(client, method, timeout, false);
1429    }
1430
1431    /**
1432     * Handle executing an HTTP POST command with proper timeout, watchdog, and ping behavior
1433     * @param client the HttpClient
1434     * @param method the HttpPost
1435     * @param timeout the timeout before failure, in ms
1436     * @param isPingCommand whether the POST is for the Ping command (requires wakelock logic)
1437     * @return the HttpResponse
1438     * @throws IOException
1439     */
1440    protected HttpResponse executePostWithTimeout(HttpClient client, HttpPost method, int timeout,
1441            boolean isPingCommand) throws IOException {
1442        synchronized(getSynchronizer()) {
1443            mPendingPost = method;
1444            long alarmTime = timeout + WATCHDOG_TIMEOUT_ALLOWANCE;
1445            if (isPingCommand) {
1446                ExchangeService.runAsleep(mMailboxId, alarmTime);
1447            } else {
1448                ExchangeService.setWatchdogAlarm(mMailboxId, alarmTime);
1449            }
1450        }
1451        try {
1452            return client.execute(method);
1453        } finally {
1454            synchronized(getSynchronizer()) {
1455                if (isPingCommand) {
1456                    ExchangeService.runAwake(mMailboxId);
1457                } else {
1458                    ExchangeService.clearWatchdogAlarm(mMailboxId);
1459                }
1460                mPendingPost = null;
1461            }
1462        }
1463    }
1464
1465    protected HttpResponse sendHttpClientPost(String cmd, HttpEntity entity, int timeout)
1466            throws IOException {
1467        HttpClient client = getHttpClient(timeout);
1468        boolean isPingCommand = cmd.equals(PING_COMMAND);
1469
1470        // Split the mail sending commands
1471        String extra = null;
1472        boolean msg = false;
1473        if (cmd.startsWith("SmartForward&") || cmd.startsWith("SmartReply&")) {
1474            int cmdLength = cmd.indexOf('&');
1475            extra = cmd.substring(cmdLength);
1476            cmd = cmd.substring(0, cmdLength);
1477            msg = true;
1478        } else if (cmd.startsWith("SendMail&")) {
1479            msg = true;
1480        }
1481
1482        String us = makeUriString(cmd, extra);
1483        HttpPost method = new HttpPost(URI.create(us));
1484        // Send the proper Content-Type header
1485        // If entity is null (e.g. for attachments), don't set this header
1486        if (msg) {
1487            method.setHeader("Content-Type", "message/rfc822");
1488        } else if (entity != null) {
1489            method.setHeader("Content-Type", "application/vnd.ms-sync.wbxml");
1490        }
1491        setHeaders(method, !cmd.equals(PING_COMMAND));
1492        method.setEntity(entity);
1493        return executePostWithTimeout(client, method, timeout, isPingCommand);
1494    }
1495
1496    protected HttpResponse sendHttpClientOptions() throws IOException {
1497        HttpClient client = getHttpClient(COMMAND_TIMEOUT);
1498        String us = makeUriString("OPTIONS", null);
1499        HttpOptions method = new HttpOptions(URI.create(us));
1500        setHeaders(method, false);
1501        return client.execute(method);
1502    }
1503
1504    String getTargetCollectionClassFromCursor(Cursor c) {
1505        int type = c.getInt(Mailbox.CONTENT_TYPE_COLUMN);
1506        if (type == Mailbox.TYPE_CONTACTS) {
1507            return "Contacts";
1508        } else if (type == Mailbox.TYPE_CALENDAR) {
1509            return "Calendar";
1510        } else {
1511            return "Email";
1512        }
1513    }
1514
1515    /**
1516     * Negotiate provisioning with the server.  First, get policies form the server and see if
1517     * the policies are supported by the device.  Then, write the policies to the account and
1518     * tell SecurityPolicy that we have policies in effect.  Finally, see if those policies are
1519     * active; if so, acknowledge the policies to the server and get a final policy key that we
1520     * use in future EAS commands and write this key to the account.
1521     * @return whether or not provisioning has been successful
1522     * @throws IOException
1523     */
1524    private boolean tryProvision() throws IOException {
1525        // First, see if provisioning is even possible, i.e. do we support the policies required
1526        // by the server
1527        ProvisionParser pp = canProvision();
1528        if (pp != null) {
1529            // Get the policies from ProvisionParser
1530            PolicySet ps = pp.getPolicySet();
1531            // Update the account with a null policyKey (the key we've gotten is
1532            // temporary and cannot be used for syncing)
1533            ps.writeAccount(mAccount, null, true, mContext);
1534            // Make sure that SecurityPolicy is up-to-date
1535            SecurityPolicyDelegate.updatePolicies(mContext, mAccount.mId);
1536            if (pp.getRemoteWipe()) {
1537                // We've gotten a remote wipe command
1538                ExchangeService.alwaysLog("!!! Remote wipe request received");
1539                // Start by setting the account to security hold
1540                SecurityPolicyDelegate.setAccountHoldFlag(mContext, mAccount, true);
1541                // Force a stop to any running syncs for this account (except this one)
1542                ExchangeService.stopNonAccountMailboxSyncsForAccount(mAccount.mId);
1543
1544                // If we're not the admin, we can't do the wipe, so just return
1545                if (!SecurityPolicyDelegate.isActiveAdmin(mContext)) {
1546                    ExchangeService.alwaysLog("!!! Not device admin; can't wipe");
1547                    return false;
1548                }
1549
1550                // First, we've got to acknowledge it, but wrap the wipe in try/catch so that
1551                // we wipe the device regardless of any errors in acknowledgment
1552                try {
1553                    ExchangeService.alwaysLog("!!! Acknowledging remote wipe to server");
1554                    acknowledgeRemoteWipe(pp.getPolicyKey());
1555                } catch (Exception e) {
1556                    // Because remote wipe is such a high priority task, we don't want to
1557                    // circumvent it if there's an exception in acknowledgment
1558                }
1559                // Then, tell SecurityPolicy to wipe the device
1560                ExchangeService.alwaysLog("!!! Executing remote wipe");
1561                SecurityPolicyDelegate.remoteWipe(mContext);
1562                return false;
1563            } else if (SecurityPolicyDelegate.isActive(mContext, ps)) {
1564                // See if the required policies are in force; if they are, acknowledge the policies
1565                // to the server and get the final policy key
1566                String policyKey = acknowledgeProvision(pp.getPolicyKey(), PROVISION_STATUS_OK);
1567                if (policyKey != null) {
1568                    // Write the final policy key to the Account and say we've been successful
1569                    ps.writeAccount(mAccount, policyKey, true, mContext);
1570                    // Release any mailboxes that might be in a security hold
1571                    ExchangeService.releaseSecurityHold(mAccount);
1572                    return true;
1573                }
1574            } else {
1575                // Notify that we are blocked because of policies
1576                SecurityPolicyDelegate.policiesRequired(mContext, mAccount.mId);
1577            }
1578        }
1579        return false;
1580    }
1581
1582    private String getPolicyType() {
1583        return (mProtocolVersionDouble >=
1584            Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) ? EAS_12_POLICY_TYPE : EAS_2_POLICY_TYPE;
1585    }
1586
1587    /**
1588     * Obtain a set of policies from the server and determine whether those policies are supported
1589     * by the device.
1590     * @return the ProvisionParser (holds policies and key) if we receive policies and they are
1591     * supported by the device; null otherwise
1592     * @throws IOException
1593     */
1594    private ProvisionParser canProvision() throws IOException {
1595        Serializer s = new Serializer();
1596        s.start(Tags.PROVISION_PROVISION).start(Tags.PROVISION_POLICIES);
1597        s.start(Tags.PROVISION_POLICY).data(Tags.PROVISION_POLICY_TYPE, getPolicyType())
1598            .end().end().end().done();
1599        HttpResponse resp = sendHttpClientPost("Provision", s.toByteArray());
1600        HttpEntity entity = resp.getEntity();
1601        try {
1602            int code = resp.getStatusLine().getStatusCode();
1603            if (code == HttpStatus.SC_OK) {
1604                InputStream is = entity.getContent();
1605                ProvisionParser pp = new ProvisionParser(is, this);
1606                if (pp.parse()) {
1607                    // The PolicySet in the ProvisionParser will have the requirements for all KNOWN
1608                    // policies.  If others are required, hasSupportablePolicySet will be false
1609                    if (pp.hasSupportablePolicySet()) {
1610                        // If the policies are supportable (in this context, meaning there are no
1611                        // completely unimplemented policies required), return the parser itself
1612                        return pp;
1613                    } else {
1614                        // Try to acknowledge using the "partial" status (i.e. we can partially
1615                        // accommodate the required policies).  The server will agree to this if the
1616                        // "allow non-provisionable devices" setting is enabled on the server
1617                        String policyKey = acknowledgeProvision(pp.getPolicyKey(),
1618                                PROVISION_STATUS_PARTIAL);
1619                        // Return either the parser (success) or null (failure)
1620                        if (policyKey != null) {
1621                            pp.clearUnsupportedPolicies();
1622                            return pp;
1623                        } else {
1624                            return null;
1625                        }
1626                    }
1627                }
1628            }
1629        } finally {
1630            if (entity != null) {
1631                entity.consumeContent();
1632            }
1633        }
1634        // On failures, simply return null
1635        return null;
1636    }
1637
1638    /**
1639     * Acknowledge that we support the policies provided by the server, and that these policies
1640     * are in force.
1641     * @param tempKey the initial (temporary) policy key sent by the server
1642     * @return the final policy key, which can be used for syncing
1643     * @throws IOException
1644     */
1645    private void acknowledgeRemoteWipe(String tempKey) throws IOException {
1646        acknowledgeProvisionImpl(tempKey, PROVISION_STATUS_OK, true);
1647    }
1648
1649    private String acknowledgeProvision(String tempKey, String result) throws IOException {
1650        return acknowledgeProvisionImpl(tempKey, result, false);
1651    }
1652
1653    private String acknowledgeProvisionImpl(String tempKey, String status,
1654            boolean remoteWipe) throws IOException {
1655        Serializer s = new Serializer();
1656        s.start(Tags.PROVISION_PROVISION).start(Tags.PROVISION_POLICIES);
1657        s.start(Tags.PROVISION_POLICY);
1658
1659        // Use the proper policy type, depending on EAS version
1660        s.data(Tags.PROVISION_POLICY_TYPE, getPolicyType());
1661
1662        s.data(Tags.PROVISION_POLICY_KEY, tempKey);
1663        s.data(Tags.PROVISION_STATUS, status);
1664        s.end().end(); // PROVISION_POLICY, PROVISION_POLICIES
1665        if (remoteWipe) {
1666            s.start(Tags.PROVISION_REMOTE_WIPE);
1667            s.data(Tags.PROVISION_STATUS, PROVISION_STATUS_OK);
1668            s.end();
1669        }
1670        s.end().done(); // PROVISION_PROVISION
1671        HttpResponse resp = sendHttpClientPost("Provision", s.toByteArray());
1672        HttpEntity entity = resp.getEntity();
1673        try {
1674            int code = resp.getStatusLine().getStatusCode();
1675            if (code == HttpStatus.SC_OK) {
1676                InputStream is = entity.getContent();
1677                ProvisionParser pp = new ProvisionParser(is, this);
1678                if (pp.parse()) {
1679                    // Return the final policy key from the ProvisionParser
1680                    return pp.getPolicyKey();
1681                }
1682            }
1683        } finally {
1684            if (entity != null) {
1685                entity.consumeContent();
1686            }
1687        }
1688        // On failures, return null
1689        return null;
1690    }
1691
1692    /**
1693     * Translate exit status code to service status code (used in callbacks)
1694     * @param exitStatus the service's exit status
1695     * @return the corresponding service status
1696     */
1697    private int exitStatusToServiceStatus(int exitStatus) {
1698        switch(exitStatus) {
1699            case EXIT_SECURITY_FAILURE:
1700                return EmailServiceStatus.SECURITY_FAILURE;
1701            case EXIT_LOGIN_FAILURE:
1702                return EmailServiceStatus.LOGIN_FAILED;
1703            default:
1704                return EmailServiceStatus.SUCCESS;
1705        }
1706    }
1707
1708    /**
1709     * Performs FolderSync
1710     *
1711     * @throws IOException
1712     * @throws EasParserException
1713     */
1714    public void runAccountMailbox() throws IOException, EasParserException {
1715        // Initialize exit status to success
1716        mExitStatus = EXIT_DONE;
1717        try {
1718            try {
1719                ExchangeService.callback()
1720                    .syncMailboxListStatus(mAccount.mId, EmailServiceStatus.IN_PROGRESS, 0);
1721            } catch (RemoteException e1) {
1722                // Don't care if this fails
1723            }
1724
1725            // Don't run if we're being held
1726            if ((mAccount.mFlags & Account.FLAGS_SECURITY_HOLD) != 0) {
1727                mExitStatus = EXIT_SECURITY_FAILURE;
1728                return;
1729            }
1730
1731            if (mAccount.mSyncKey == null) {
1732                mAccount.mSyncKey = "0";
1733                userLog("Account syncKey INIT to 0");
1734                ContentValues cv = new ContentValues();
1735                cv.put(AccountColumns.SYNC_KEY, mAccount.mSyncKey);
1736                mAccount.update(mContext, cv);
1737            }
1738
1739            boolean firstSync = mAccount.mSyncKey.equals("0");
1740            if (firstSync) {
1741                userLog("Initial FolderSync");
1742            }
1743
1744            // When we first start up, change all mailboxes to push.
1745            ContentValues cv = new ContentValues();
1746            cv.put(Mailbox.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PUSH);
1747            if (mContentResolver.update(Mailbox.CONTENT_URI, cv,
1748                    WHERE_ACCOUNT_AND_SYNC_INTERVAL_PING,
1749                    new String[] {Long.toString(mAccount.mId)}) > 0) {
1750                ExchangeService.kick("change ping boxes to push");
1751            }
1752
1753            // Determine our protocol version, if we haven't already and save it in the Account
1754            // Also re-check protocol version at least once a day (in case of upgrade)
1755            if (mAccount.mProtocolVersion == null ||
1756                    ((System.currentTimeMillis() - mMailbox.mSyncTime) > DAYS)) {
1757                userLog("Determine EAS protocol version");
1758                HttpResponse resp = sendHttpClientOptions();
1759                int code = resp.getStatusLine().getStatusCode();
1760                userLog("OPTIONS response: ", code);
1761                if (code == HttpStatus.SC_OK) {
1762                    Header header = resp.getFirstHeader("MS-ASProtocolCommands");
1763                    userLog(header.getValue());
1764                    header = resp.getFirstHeader("ms-asprotocolversions");
1765                    try {
1766                        setupProtocolVersion(this, header);
1767                    } catch (MessagingException e) {
1768                        // Since we've already validated, this can't really happen
1769                        // But if it does, we'll rethrow this...
1770                        throw new IOException();
1771                    }
1772                    // Save the protocol version
1773                    cv.clear();
1774                    // Save the protocol version in the account; if we're using 12.0 or greater,
1775                    // set the flag for support of SmartForward
1776                    cv.put(Account.PROTOCOL_VERSION, mProtocolVersion);
1777                    if (mProtocolVersionDouble >= 12.0) {
1778                        cv.put(Account.FLAGS,
1779                                mAccount.mFlags | Account.FLAGS_SUPPORTS_SMART_FORWARD);
1780                    }
1781                    mAccount.update(mContext, cv);
1782                    cv.clear();
1783                    // Save the sync time of the account mailbox to current time
1784                    cv.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
1785                    mMailbox.update(mContext, cv);
1786                 } else {
1787                    errorLog("OPTIONS command failed; throwing IOException");
1788                    throw new IOException();
1789                }
1790            }
1791
1792            // Change all pushable boxes to push when we start the account mailbox
1793            if (mAccount.mSyncInterval == Account.CHECK_INTERVAL_PUSH) {
1794                cv.clear();
1795                cv.put(Mailbox.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PUSH);
1796                if (mContentResolver.update(Mailbox.CONTENT_URI, cv,
1797                        ExchangeService.WHERE_IN_ACCOUNT_AND_PUSHABLE,
1798                        new String[] {Long.toString(mAccount.mId)}) > 0) {
1799                    userLog("Push account; set pushable boxes to push...");
1800                }
1801            }
1802
1803            while (!mStop) {
1804                // Check that the account's mailboxes are consistent
1805                MailboxUtilities.checkMailboxConsistency(mContext, mAccount.mId);
1806                userLog("Sending Account syncKey: ", mAccount.mSyncKey);
1807                Serializer s = new Serializer();
1808                s.start(Tags.FOLDER_FOLDER_SYNC).start(Tags.FOLDER_SYNC_KEY)
1809                    .text(mAccount.mSyncKey).end().end().done();
1810                HttpResponse resp = sendHttpClientPost("FolderSync", s.toByteArray());
1811                HttpEntity entity = resp.getEntity();
1812                try {
1813                    if (mStop) break;
1814                    int code = resp.getStatusLine().getStatusCode();
1815                    if (code == HttpStatus.SC_OK) {
1816                            int len = (int)entity.getContentLength();
1817                            if (len != 0) {
1818                                InputStream is = entity.getContent();
1819                                // Returns true if we need to sync again
1820                                if (new FolderSyncParser(is, new AccountSyncAdapter(this))
1821                                        .parse()) {
1822                                    continue;
1823                                }
1824                            }
1825                    } else if (isProvisionError(code)) {
1826                        // If the sync error is a provisioning failure (perhaps policies changed),
1827                        // let's try the provisioning procedure
1828                        // Provisioning must only be attempted for the account mailbox - trying to
1829                        // provision any other mailbox may result in race conditions and the
1830                        // creation of multiple policy keys.
1831                        if (!tryProvision()) {
1832                            // Set the appropriate failure status
1833                            mExitStatus = EXIT_SECURITY_FAILURE;
1834                            return;
1835                        } else {
1836                            // If we succeeded, try again...
1837                            continue;
1838                        }
1839                    } else if (isAuthError(code)) {
1840                        mExitStatus = EXIT_LOGIN_FAILURE;
1841                        return;
1842                    } else {
1843                        userLog("FolderSync response error: ", code);
1844                    }
1845                } finally {
1846                    if (entity != null) {
1847                        entity.consumeContent();
1848                    }
1849                }
1850
1851                // Change all push/hold boxes to push
1852                cv.clear();
1853                cv.put(Mailbox.SYNC_INTERVAL, Account.CHECK_INTERVAL_PUSH);
1854                if (mContentResolver.update(Mailbox.CONTENT_URI, cv,
1855                        WHERE_PUSH_HOLD_NOT_ACCOUNT_MAILBOX,
1856                        new String[] {Long.toString(mAccount.mId)}) > 0) {
1857                    userLog("Set push/hold boxes to push...");
1858                }
1859
1860                try {
1861                    ExchangeService.callback()
1862                        .syncMailboxListStatus(mAccount.mId, exitStatusToServiceStatus(mExitStatus),
1863                                0);
1864                } catch (RemoteException e1) {
1865                    // Don't care if this fails
1866                }
1867
1868                // Before each run of the pingLoop, if this Account has a PolicySet, make sure it's
1869                // active; otherwise, clear out the key/flag.  This should cause a provisioning
1870                // error on the next POST, and start the security sequence over again
1871                String key = mAccount.mSecuritySyncKey;
1872                if (!TextUtils.isEmpty(key)) {
1873                    PolicySet ps = new PolicySet(mAccount);
1874                    if (!SecurityPolicyDelegate.isActive(mContext, ps)) {
1875                        cv.clear();
1876                        cv.put(AccountColumns.SECURITY_FLAGS, 0);
1877                        cv.putNull(AccountColumns.SECURITY_SYNC_KEY);
1878                        long accountId = mAccount.mId;
1879                        mContentResolver.update(ContentUris.withAppendedId(
1880                                Account.CONTENT_URI, accountId), cv, null, null);
1881                        SecurityPolicyDelegate.policiesRequired(mContext, accountId);
1882                    }
1883                }
1884
1885                // Wait for push notifications.
1886                String threadName = Thread.currentThread().getName();
1887                try {
1888                    runPingLoop();
1889                } catch (StaleFolderListException e) {
1890                    // We break out if we get told about a stale folder list
1891                    userLog("Ping interrupted; folder list requires sync...");
1892                } catch (IllegalHeartbeatException e) {
1893                    // If we're sending an illegal heartbeat, reset either the min or the max to
1894                    // that heartbeat
1895                    resetHeartbeats(e.mLegalHeartbeat);
1896                } finally {
1897                    Thread.currentThread().setName(threadName);
1898                }
1899            }
1900         } catch (IOException e) {
1901            // We catch this here to send the folder sync status callback
1902            // A folder sync failed callback will get sent from run()
1903            try {
1904                if (!mStop) {
1905                    // NOTE: The correct status is CONNECTION_ERROR, but the UI displays this, and
1906                    // it's not really appropriate for EAS as this is not unexpected for a ping and
1907                    // connection errors are retried in any case
1908                    ExchangeService.callback()
1909                        .syncMailboxListStatus(mAccount.mId,
1910                                EmailServiceStatus.SUCCESS, 0);
1911                }
1912            } catch (RemoteException e1) {
1913                // Don't care if this fails
1914            }
1915            throw e;
1916        }
1917    }
1918
1919    /**
1920     * Reset either our minimum or maximum ping heartbeat to a heartbeat known to be legal
1921     * @param legalHeartbeat a known legal heartbeat (from the EAS server)
1922     */
1923    /*package*/ void resetHeartbeats(int legalHeartbeat) {
1924        userLog("Resetting min/max heartbeat, legal = " + legalHeartbeat);
1925        // We are here because the current heartbeat (mPingHeartbeat) is invalid.  Depending on
1926        // whether the argument is above or below the current heartbeat, we can infer the need to
1927        // change either the minimum or maximum heartbeat
1928        if (legalHeartbeat > mPingHeartbeat) {
1929            // The legal heartbeat is higher than the ping heartbeat; therefore, our minimum was
1930            // too low.  We respond by raising either or both of the minimum heartbeat or the
1931            // force heartbeat to the argument value
1932            if (mPingMinHeartbeat < legalHeartbeat) {
1933                mPingMinHeartbeat = legalHeartbeat;
1934            }
1935            if (mPingForceHeartbeat < legalHeartbeat) {
1936                mPingForceHeartbeat = legalHeartbeat;
1937            }
1938            // If our minimum is now greater than the max, bring them together
1939            if (mPingMinHeartbeat > mPingMaxHeartbeat) {
1940                mPingMaxHeartbeat = legalHeartbeat;
1941            }
1942        } else if (legalHeartbeat < mPingHeartbeat) {
1943            // The legal heartbeat is lower than the ping heartbeat; therefore, our maximum was
1944            // too high.  We respond by lowering the maximum to the argument value
1945            mPingMaxHeartbeat = legalHeartbeat;
1946            // If our maximum is now less than the minimum, bring them together
1947            if (mPingMaxHeartbeat < mPingMinHeartbeat) {
1948                mPingMinHeartbeat = legalHeartbeat;
1949            }
1950        }
1951        // Set current heartbeat to the legal heartbeat
1952        mPingHeartbeat = legalHeartbeat;
1953        // Allow the heartbeat logic to run
1954        mPingHeartbeatDropped = false;
1955    }
1956
1957    private void pushFallback(long mailboxId) {
1958        Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mailboxId);
1959        if (mailbox == null) {
1960            return;
1961        }
1962        ContentValues cv = new ContentValues();
1963        int mins = PING_FALLBACK_PIM;
1964        if (mailbox.mType == Mailbox.TYPE_INBOX) {
1965            mins = PING_FALLBACK_INBOX;
1966        }
1967        cv.put(Mailbox.SYNC_INTERVAL, mins);
1968        mContentResolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId),
1969                cv, null, null);
1970        errorLog("*** PING ERROR LOOP: Set " + mailbox.mDisplayName + " to " + mins + " min sync");
1971        ExchangeService.kick("push fallback");
1972    }
1973
1974    /**
1975     * Simplistic attempt to determine a NAT timeout, based on experience with various carriers
1976     * and networks.  The string "reset by peer" is very common in these situations, so we look for
1977     * that specifically.  We may add additional tests here as more is learned.
1978     * @param message
1979     * @return whether this message is likely associated with a NAT failure
1980     */
1981    private boolean isLikelyNatFailure(String message) {
1982        if (message == null) return false;
1983        if (message.contains("reset by peer")) {
1984            return true;
1985        }
1986        return false;
1987    }
1988
1989    private void runPingLoop() throws IOException, StaleFolderListException,
1990            IllegalHeartbeatException {
1991        int pingHeartbeat = mPingHeartbeat;
1992        userLog("runPingLoop");
1993        // Do push for all sync services here
1994        long endTime = System.currentTimeMillis() + (30*MINUTES);
1995        HashMap<String, Integer> pingErrorMap = new HashMap<String, Integer>();
1996        ArrayList<String> readyMailboxes = new ArrayList<String>();
1997        ArrayList<String> notReadyMailboxes = new ArrayList<String>();
1998        int pingWaitCount = 0;
1999        long inboxId = -1;
2000
2001        while ((System.currentTimeMillis() < endTime) && !mStop) {
2002            // Check that the account's mailboxes are consistent
2003            MailboxUtilities.checkMailboxConsistency(mContext, mAccount.mId);
2004            // Count of pushable mailboxes
2005            int pushCount = 0;
2006            // Count of mailboxes that can be pushed right now
2007            int canPushCount = 0;
2008            // Count of uninitialized boxes
2009            int uninitCount = 0;
2010
2011            Serializer s = new Serializer();
2012            Cursor c = mContentResolver.query(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION,
2013                    MailboxColumns.ACCOUNT_KEY + '=' + mAccount.mId +
2014                    AND_FREQUENCY_PING_PUSH_AND_NOT_ACCOUNT_MAILBOX, null, null);
2015            notReadyMailboxes.clear();
2016            readyMailboxes.clear();
2017            // Look for an inbox, and remember its id
2018            if (inboxId == -1) {
2019                inboxId = Mailbox.findMailboxOfType(mContext, mAccount.mId, Mailbox.TYPE_INBOX);
2020            }
2021            try {
2022                // Loop through our pushed boxes seeing what is available to push
2023                while (c.moveToNext()) {
2024                    pushCount++;
2025                    // Two requirements for push:
2026                    // 1) ExchangeService tells us the mailbox is syncable (not running/not stopped)
2027                    // 2) The syncKey isn't "0" (i.e. it's synced at least once)
2028                    long mailboxId = c.getLong(Mailbox.CONTENT_ID_COLUMN);
2029                    int pingStatus = ExchangeService.pingStatus(mailboxId);
2030                    String mailboxName = c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN);
2031                    if (pingStatus == ExchangeService.PING_STATUS_OK) {
2032                        String syncKey = c.getString(Mailbox.CONTENT_SYNC_KEY_COLUMN);
2033                        if ((syncKey == null) || syncKey.equals("0")) {
2034                            // We can't push until the initial sync is done
2035                            pushCount--;
2036                            uninitCount++;
2037                            continue;
2038                        }
2039
2040                        if (canPushCount++ == 0) {
2041                            // Initialize the Ping command
2042                            s.start(Tags.PING_PING)
2043                                .data(Tags.PING_HEARTBEAT_INTERVAL,
2044                                        Integer.toString(pingHeartbeat))
2045                                .start(Tags.PING_FOLDERS);
2046                        }
2047
2048                        String folderClass = getTargetCollectionClassFromCursor(c);
2049                        s.start(Tags.PING_FOLDER)
2050                            .data(Tags.PING_ID, c.getString(Mailbox.CONTENT_SERVER_ID_COLUMN))
2051                            .data(Tags.PING_CLASS, folderClass)
2052                            .end();
2053                        readyMailboxes.add(mailboxName);
2054                    } else if ((pingStatus == ExchangeService.PING_STATUS_RUNNING) ||
2055                            (pingStatus == ExchangeService.PING_STATUS_WAITING)) {
2056                        notReadyMailboxes.add(mailboxName);
2057                    } else if (pingStatus == ExchangeService.PING_STATUS_UNABLE) {
2058                        pushCount--;
2059                        userLog(mailboxName, " in error state; ignore");
2060                        continue;
2061                    }
2062                }
2063            } finally {
2064                c.close();
2065            }
2066
2067            if (Eas.USER_LOG) {
2068                if (!notReadyMailboxes.isEmpty()) {
2069                    userLog("Ping not ready for: " + notReadyMailboxes);
2070                }
2071                if (!readyMailboxes.isEmpty()) {
2072                    userLog("Ping ready for: " + readyMailboxes);
2073                }
2074            }
2075
2076            // If we've waited 10 seconds or more, just ping with whatever boxes are ready
2077            // But use a shorter than normal heartbeat
2078            boolean forcePing = !notReadyMailboxes.isEmpty() && (pingWaitCount > 5);
2079
2080            if ((canPushCount > 0) && ((canPushCount == pushCount) || forcePing)) {
2081                // If all pingable boxes are ready for push, send Ping to the server
2082                s.end().end().done();
2083                pingWaitCount = 0;
2084                mPostReset = false;
2085                mPostAborted = false;
2086
2087                // If we've been stopped, this is a good time to return
2088                if (mStop) return;
2089
2090                long pingTime = SystemClock.elapsedRealtime();
2091                try {
2092                    // Send the ping, wrapped by appropriate timeout/alarm
2093                    if (forcePing) {
2094                        userLog("Forcing ping after waiting for all boxes to be ready");
2095                    }
2096                    HttpResponse res =
2097                        sendPing(s.toByteArray(), forcePing ? mPingForceHeartbeat : pingHeartbeat);
2098                    HttpEntity entity = res.getEntity();
2099
2100                    try {
2101                        int code = res.getStatusLine().getStatusCode();
2102                        userLog("Ping response: ", code);
2103
2104                        // Return immediately if we've been asked to stop during the ping
2105                        if (mStop) {
2106                            userLog("Stopping pingLoop");
2107                            return;
2108                        }
2109
2110                        if (code == HttpStatus.SC_OK) {
2111                            // Make sure to clear out any pending sync errors
2112                            ExchangeService.removeFromSyncErrorMap(mMailboxId);
2113                            int len = (int)entity.getContentLength();
2114                            InputStream is = entity.getContent();
2115                            if (len != 0) {
2116                                int pingResult = parsePingResult(is, mContentResolver,
2117                                        pingErrorMap);
2118                                // If our ping completed (status = 1), and wasn't forced and we're
2119                                // not at the maximum, try increasing timeout by two minutes
2120                                if (pingResult == PROTOCOL_PING_STATUS_COMPLETED && !forcePing) {
2121                                    if (pingHeartbeat > mPingHighWaterMark) {
2122                                        mPingHighWaterMark = pingHeartbeat;
2123                                        userLog("Setting high water mark at: ", mPingHighWaterMark);
2124                                    }
2125                                    if ((pingHeartbeat < mPingMaxHeartbeat) &&
2126                                            !mPingHeartbeatDropped) {
2127                                        pingHeartbeat += PING_HEARTBEAT_INCREMENT;
2128                                        if (pingHeartbeat > mPingMaxHeartbeat) {
2129                                            pingHeartbeat = mPingMaxHeartbeat;
2130                                        }
2131                                        userLog("Increase ping heartbeat to ", pingHeartbeat, "s");
2132                                    }
2133                                }
2134                            } else {
2135                                userLog("Ping returned empty result; throwing IOException");
2136                                throw new IOException();
2137                            }
2138                        } else if (isAuthError(code)) {
2139                            mExitStatus = EXIT_LOGIN_FAILURE;
2140                            userLog("Authorization error during Ping: ", code);
2141                            throw new IOException();
2142                        }
2143                    } finally {
2144                        if (entity != null) {
2145                            entity.consumeContent();
2146                        }
2147                    }
2148                } catch (IOException e) {
2149                    String message = e.getMessage();
2150                    // If we get the exception that is indicative of a NAT timeout and if we
2151                    // haven't yet "fixed" the timeout, back off by two minutes and "fix" it
2152                    boolean hasMessage = message != null;
2153                    userLog("IOException runPingLoop: " + (hasMessage ? message : "[no message]"));
2154                    if (mPostReset) {
2155                        // Nothing to do in this case; this is ExchangeService telling us to try
2156                        // another ping.
2157                    } else if (mPostAborted || isLikelyNatFailure(message)) {
2158                        long pingLength = SystemClock.elapsedRealtime() - pingTime;
2159                        if ((pingHeartbeat > mPingMinHeartbeat) &&
2160                                (pingHeartbeat > mPingHighWaterMark)) {
2161                            pingHeartbeat -= PING_HEARTBEAT_INCREMENT;
2162                            mPingHeartbeatDropped = true;
2163                            if (pingHeartbeat < mPingMinHeartbeat) {
2164                                pingHeartbeat = mPingMinHeartbeat;
2165                            }
2166                            userLog("Decreased ping heartbeat to ", pingHeartbeat, "s");
2167                        } else if (mPostAborted) {
2168                            // There's no point in throwing here; this can happen in two cases
2169                            // 1) An alarm, which indicates minutes without activity; no sense
2170                            //    backing off
2171                            // 2) ExchangeService abort, due to sync of mailbox.  Again, we want to
2172                            //    keep on trying to ping
2173                            userLog("Ping aborted; retry");
2174                        } else if (pingLength < 2000) {
2175                            userLog("Abort or NAT type return < 2 seconds; throwing IOException");
2176                            throw e;
2177                        } else {
2178                            userLog("NAT type IOException");
2179                        }
2180                    } else if (hasMessage && message.contains("roken pipe")) {
2181                        // The "broken pipe" error (uppercase or lowercase "b") seems to be an
2182                        // internal error, so let's not throw an exception (which leads to delays)
2183                        // but rather simply run through the loop again
2184                    } else {
2185                        throw e;
2186                    }
2187                }
2188            } else if (forcePing) {
2189                // In this case, there aren't any boxes that are pingable, but there are boxes
2190                // waiting (for IOExceptions)
2191                userLog("pingLoop waiting 60s for any pingable boxes");
2192                sleep(60*SECONDS, true);
2193            } else if (pushCount > 0) {
2194                // If we want to Ping, but can't just yet, wait a little bit
2195                // TODO Change sleep to wait and use notify from ExchangeService when a sync ends
2196                sleep(2*SECONDS, false);
2197                pingWaitCount++;
2198                //userLog("pingLoop waited 2s for: ", (pushCount - canPushCount), " box(es)");
2199            } else if (uninitCount > 0) {
2200                // In this case, we're doing an initial sync of at least one mailbox.  Since this
2201                // is typically a one-time case, I'm ok with trying again every 10 seconds until
2202                // we're in one of the other possible states.
2203                userLog("pingLoop waiting for initial sync of ", uninitCount, " box(es)");
2204                sleep(10*SECONDS, true);
2205            } else if (inboxId == -1) {
2206                // In this case, we're still syncing mailboxes, so sleep for only a short time
2207                sleep(45*SECONDS, true);
2208            } else {
2209                // We've got nothing to do, so we'll check again in 20 minutes at which time
2210                // we'll update the folder list, check for policy changes and/or remote wipe, etc.
2211                // Let the device sleep in the meantime...
2212                userLog(ACCOUNT_MAILBOX_SLEEP_TEXT);
2213                sleep(ACCOUNT_MAILBOX_SLEEP_TIME, true);
2214            }
2215        }
2216
2217        // Save away the current heartbeat
2218        mPingHeartbeat = pingHeartbeat;
2219    }
2220
2221    private void sleep(long ms, boolean runAsleep) {
2222        if (runAsleep) {
2223            ExchangeService.runAsleep(mMailboxId, ms+(5*SECONDS));
2224        }
2225        try {
2226            Thread.sleep(ms);
2227        } catch (InterruptedException e) {
2228            // Doesn't matter whether we stop early; it's the thought that counts
2229        } finally {
2230            if (runAsleep) {
2231                ExchangeService.runAwake(mMailboxId);
2232            }
2233        }
2234    }
2235
2236    private int parsePingResult(InputStream is, ContentResolver cr,
2237            HashMap<String, Integer> errorMap)
2238            throws IOException, StaleFolderListException, IllegalHeartbeatException {
2239        PingParser pp = new PingParser(is, this);
2240        if (pp.parse()) {
2241            // True indicates some mailboxes need syncing...
2242            // syncList has the serverId's of the mailboxes...
2243            mBindArguments[0] = Long.toString(mAccount.mId);
2244            mPingChangeList = pp.getSyncList();
2245            for (String serverId: mPingChangeList) {
2246                mBindArguments[1] = serverId;
2247                Cursor c = cr.query(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION,
2248                        WHERE_ACCOUNT_KEY_AND_SERVER_ID, mBindArguments, null);
2249                try {
2250                    if (c.moveToFirst()) {
2251
2252                        /**
2253                         * Check the boxes reporting changes to see if there really were any...
2254                         * We do this because bugs in various Exchange servers can put us into a
2255                         * looping behavior by continually reporting changes in a mailbox, even when
2256                         * there aren't any.
2257                         *
2258                         * This behavior is seemingly random, and therefore we must code defensively
2259                         * by backing off of push behavior when it is detected.
2260                         *
2261                         * One known cause, on certain Exchange 2003 servers, is acknowledged by
2262                         * Microsoft, and the server hotfix for this case can be found at
2263                         * http://support.microsoft.com/kb/923282
2264                         */
2265
2266                        // Check the status of the last sync
2267                        String status = c.getString(Mailbox.CONTENT_SYNC_STATUS_COLUMN);
2268                        int type = ExchangeService.getStatusType(status);
2269                        // This check should always be true...
2270                        if (type == ExchangeService.SYNC_PING) {
2271                            int changeCount = ExchangeService.getStatusChangeCount(status);
2272                            if (changeCount > 0) {
2273                                errorMap.remove(serverId);
2274                            } else if (changeCount == 0) {
2275                                // This means that a ping reported changes in error; we keep a count
2276                                // of consecutive errors of this kind
2277                                String name = c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN);
2278                                Integer failures = errorMap.get(serverId);
2279                                if (failures == null) {
2280                                    userLog("Last ping reported changes in error for: ", name);
2281                                    errorMap.put(serverId, 1);
2282                                } else if (failures > MAX_PING_FAILURES) {
2283                                    // We'll back off of push for this box
2284                                    pushFallback(c.getLong(Mailbox.CONTENT_ID_COLUMN));
2285                                    continue;
2286                                } else {
2287                                    userLog("Last ping reported changes in error for: ", name);
2288                                    errorMap.put(serverId, failures + 1);
2289                                }
2290                            }
2291                        }
2292
2293                        // If there were no problems with previous sync, we'll start another one
2294                        ExchangeService.startManualSync(c.getLong(Mailbox.CONTENT_ID_COLUMN),
2295                                ExchangeService.SYNC_PING, null);
2296                    }
2297                } finally {
2298                    c.close();
2299                }
2300            }
2301        }
2302        return pp.getSyncStatus();
2303    }
2304
2305    /**
2306     * Common code to sync E+PIM data
2307     *
2308     * @param target an EasMailbox, EasContacts, or EasCalendar object
2309     */
2310    public void sync(AbstractSyncAdapter target) throws IOException {
2311        Mailbox mailbox = target.mMailbox;
2312
2313        boolean moreAvailable = true;
2314        int loopingCount = 0;
2315        while (!mStop && (moreAvailable || hasPendingRequests())) {
2316            // If we have no connectivity, just exit cleanly. ExchangeService will start us up again
2317            // when connectivity has returned
2318            if (!hasConnectivity()) {
2319                userLog("No connectivity in sync; finishing sync");
2320                mExitStatus = EXIT_DONE;
2321                return;
2322            }
2323
2324            // Every time through the loop we check to see if we're still syncable
2325            if (!target.isSyncable()) {
2326                mExitStatus = EXIT_DONE;
2327                return;
2328            }
2329
2330            // Now, handle various requests
2331            while (true) {
2332                Request req = null;
2333
2334                if (mRequestQueue.isEmpty()) {
2335                    break;
2336                } else {
2337                    req = mRequestQueue.peek();
2338                }
2339
2340                // Our two request types are PartRequest (loading attachment) and
2341                // MeetingResponseRequest (respond to a meeting request)
2342                if (req instanceof PartRequest) {
2343                    loadAttachment((PartRequest)req);
2344                } else if (req instanceof MeetingResponseRequest) {
2345                    sendMeetingResponse((MeetingResponseRequest)req);
2346                } else if (req instanceof MessageMoveRequest) {
2347                    messageMoveRequest((MessageMoveRequest)req);
2348                }
2349
2350                // If there's an exception handling the request, we'll throw it
2351                // Otherwise, we remove the request
2352                mRequestQueue.remove();
2353            }
2354
2355            // Don't sync if we've got nothing to do
2356            if (!moreAvailable) {
2357                continue;
2358            }
2359
2360            Serializer s = new Serializer();
2361
2362            String className = target.getCollectionName();
2363            String syncKey = target.getSyncKey();
2364            userLog("sync, sending ", className, " syncKey: ", syncKey);
2365            s.start(Tags.SYNC_SYNC)
2366                .start(Tags.SYNC_COLLECTIONS)
2367                .start(Tags.SYNC_COLLECTION);
2368            // The "Class" element is removed in EAS 12.1 and later versions
2369            if (mProtocolVersionDouble < Eas.SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE) {
2370                s.data(Tags.SYNC_CLASS, className);
2371            }
2372            s.data(Tags.SYNC_SYNC_KEY, syncKey)
2373                .data(Tags.SYNC_COLLECTION_ID, mailbox.mServerId);
2374
2375            // Start with the default timeout
2376            int timeout = COMMAND_TIMEOUT;
2377            if (!syncKey.equals("0")) {
2378                // EAS doesn't allow GetChanges in an initial sync; sending other options
2379                // appears to cause the server to delay its response in some cases, and this delay
2380                // can be long enough to result in an IOException and total failure to sync.
2381                // Therefore, we don't send any options with the initial sync.
2382                // Set the truncation amount, body preference, lookback, etc.
2383                target.sendSyncOptions(mProtocolVersionDouble, s);
2384            } else {
2385                // Use enormous timeout for initial sync, which empirically can take a while longer
2386                timeout = 120*SECONDS;
2387            }
2388            // Send our changes up to the server
2389            target.sendLocalChanges(s);
2390
2391            s.end().end().end().done();
2392            HttpResponse resp = sendHttpClientPost("Sync", new ByteArrayEntity(s.toByteArray()),
2393                    timeout);
2394            HttpEntity entity = resp.getEntity();
2395            try {
2396                int code = resp.getStatusLine().getStatusCode();
2397                if (code == HttpStatus.SC_OK) {
2398                    // In EAS 12.1, we can get "empty" sync responses, which indicate that there are
2399                    // no changes in the mailbox; handle that case here
2400                    Header header = resp.getFirstHeader("content-length");
2401                    if (header != null && header.getValue().equals("0")) {
2402                        // If this happens, exit cleanly, and change the interval from push to ping
2403                        // if necessary
2404                        userLog("Empty sync response; finishing");
2405                        if (mMailbox.mSyncInterval == Mailbox.CHECK_INTERVAL_PUSH) {
2406                            userLog("Changing mailbox from push to ping");
2407                            ContentValues cv = new ContentValues();
2408                            cv.put(Mailbox.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PING);
2409                            mContentResolver.update(
2410                                    ContentUris.withAppendedId(Mailbox.CONTENT_URI, mMailbox.mId),
2411                                    cv, null, null);
2412                        }
2413                        if (mRequestQueue.isEmpty()) {
2414                            mExitStatus = EXIT_DONE;
2415                            return;
2416                        } else {
2417                            continue;
2418                        }
2419                    }
2420                    InputStream is = entity.getContent();
2421                    if (is != null) {
2422                        moreAvailable = target.parse(is);
2423                        if (target.isLooping()) {
2424                            loopingCount++;
2425                            userLog("** Looping: " + loopingCount);
2426                            // After the maximum number of loops, we'll set moreAvailable to false
2427                            // and allow the sync loop to terminate
2428                            if (moreAvailable && (loopingCount > MAX_LOOPING_COUNT)) {
2429                                userLog("** Looping force stopped");
2430                                moreAvailable = false;
2431                            }
2432                        } else {
2433                            loopingCount = 0;
2434                        }
2435                        target.cleanup();
2436                    } else {
2437                        userLog("Empty input stream in sync command response");
2438                    }
2439                } else {
2440                    userLog("Sync response error: ", code);
2441                    if (isProvisionError(code)) {
2442                        mExitStatus = EXIT_SECURITY_FAILURE;
2443                    } else if (isAuthError(code)) {
2444                        mExitStatus = EXIT_LOGIN_FAILURE;
2445                    } else {
2446                        mExitStatus = EXIT_IO_ERROR;
2447                    }
2448                    return;
2449                }
2450            } finally {
2451                if (entity != null) {
2452                    entity.consumeContent();
2453                }
2454            }
2455        }
2456        mExitStatus = EXIT_DONE;
2457    }
2458
2459    protected boolean setupService() {
2460        synchronized(getSynchronizer()) {
2461            mThread = Thread.currentThread();
2462            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
2463            TAG = mThread.getName();
2464        }
2465        // Make sure account and mailbox are always the latest from the database
2466        mAccount = Account.restoreAccountWithId(mContext, mAccount.mId);
2467        if (mAccount == null) return false;
2468        mMailbox = Mailbox.restoreMailboxWithId(mContext, mMailbox.mId);
2469        if (mMailbox == null) return false;
2470        HostAuth ha = HostAuth.restoreHostAuthWithId(mContext, mAccount.mHostAuthKeyRecv);
2471        if (ha == null) return false;
2472        mHostAddress = ha.mAddress;
2473        mUserName = ha.mLogin;
2474        mPassword = ha.mPassword;
2475
2476        // Set up our protocol version from the Account
2477        mProtocolVersion = mAccount.mProtocolVersion;
2478        // If it hasn't been set up, start with default version
2479        if (mProtocolVersion == null) {
2480            mProtocolVersion = Eas.DEFAULT_PROTOCOL_VERSION;
2481        }
2482        mProtocolVersionDouble = Eas.getProtocolVersionDouble(mProtocolVersion);
2483        return true;
2484    }
2485
2486    /* (non-Javadoc)
2487     * @see java.lang.Runnable#run()
2488     */
2489    public void run() {
2490        // Make sure account and mailbox are still valid
2491        if (!setupService()) return;
2492        // If we've been stopped, we're done
2493        if (mStop) return;
2494        if (mSyncReason >= ExchangeService.SYNC_CALLBACK_START) {
2495            try {
2496                ExchangeService.callback().syncMailboxStatus(mMailboxId,
2497                        EmailServiceStatus.IN_PROGRESS, 0);
2498            } catch (RemoteException e1) {
2499                // Don't care if this fails
2500            }
2501        }
2502
2503        // Whether or not we're the account mailbox
2504        try {
2505            mDeviceId = ExchangeService.getDeviceId(mContext);
2506            if ((mMailbox == null) || (mAccount == null)) {
2507                return;
2508            } else if (mMailbox.mType == Mailbox.TYPE_EAS_ACCOUNT_MAILBOX) {
2509                runAccountMailbox();
2510            } else {
2511                AbstractSyncAdapter target;
2512                if (mMailbox.mType == Mailbox.TYPE_CONTACTS) {
2513                    target = new ContactsSyncAdapter( this);
2514                } else if (mMailbox.mType == Mailbox.TYPE_CALENDAR) {
2515                    target = new CalendarSyncAdapter(this);
2516                } else {
2517                    target = new EmailSyncAdapter(this);
2518                }
2519                // We loop here because someone might have put a request in while we were syncing
2520                // and we've missed that opportunity...
2521                do {
2522                    if (mRequestTime != 0) {
2523                        userLog("Looping for user request...");
2524                        mRequestTime = 0;
2525                    }
2526                    sync(target);
2527                } while (mRequestTime != 0);
2528            }
2529        } catch (EasAuthenticationException e) {
2530            userLog("Caught authentication error");
2531            mExitStatus = EXIT_LOGIN_FAILURE;
2532        } catch (IOException e) {
2533            String message = e.getMessage();
2534            userLog("Caught IOException: ", (message == null) ? "No message" : message);
2535            mExitStatus = EXIT_IO_ERROR;
2536        } catch (Exception e) {
2537            userLog("Uncaught exception in EasSyncService", e);
2538        } finally {
2539            int status;
2540
2541            if (!mStop) {
2542                userLog("Sync finished");
2543                ExchangeService.done(this);
2544                switch (mExitStatus) {
2545                    case EXIT_IO_ERROR:
2546                        status = EmailServiceStatus.CONNECTION_ERROR;
2547                        break;
2548                    case EXIT_DONE:
2549                        status = EmailServiceStatus.SUCCESS;
2550                        ContentValues cv = new ContentValues();
2551                        cv.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
2552                        String s = "S" + mSyncReason + ':' + status + ':' + mChangeCount;
2553                        cv.put(Mailbox.SYNC_STATUS, s);
2554                        mContentResolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI,
2555                                mMailboxId), cv, null, null);
2556                        break;
2557                    case EXIT_LOGIN_FAILURE:
2558                        status = EmailServiceStatus.LOGIN_FAILED;
2559                        break;
2560                    case EXIT_SECURITY_FAILURE:
2561                        status = EmailServiceStatus.SECURITY_FAILURE;
2562                        // Ask for a new folder list.  This should wake up the account mailbox; a
2563                        // security error in account mailbox should start the provisioning process
2564                        ExchangeService.reloadFolderList(mContext, mAccount.mId, true);
2565                        break;
2566                    default:
2567                        status = EmailServiceStatus.REMOTE_EXCEPTION;
2568                        errorLog("Sync ended due to an exception.");
2569                        break;
2570                }
2571            } else {
2572                userLog("Stopped sync finished.");
2573                status = EmailServiceStatus.SUCCESS;
2574            }
2575
2576            // Send a callback if this run was initiated by a service call
2577            if (mSyncReason >= ExchangeService.SYNC_CALLBACK_START) {
2578                try {
2579                    // Unless the user specifically asked for a sync, we really don't want to report
2580                    // connection issues, as they are likely to be transient.  In this case, we
2581                    // simply report success, so that the progress indicator terminates without
2582                    // putting up an error banner
2583                    if (mSyncReason != ExchangeService.SYNC_UI_REQUEST &&
2584                            status == EmailServiceStatus.CONNECTION_ERROR) {
2585                        status = EmailServiceStatus.SUCCESS;
2586                    }
2587                    ExchangeService.callback().syncMailboxStatus(mMailboxId, status, 0);
2588                } catch (RemoteException e1) {
2589                    // Don't care if this fails
2590                }
2591            }
2592
2593            // Make sure ExchangeService knows about this
2594            ExchangeService.kick("sync finished");
2595       }
2596    }
2597}
2598