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