EasSyncService.java revision c171a2362e6db78385463e3b7b1bc66585fdcdfc
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; it's always wbxml except for messages when
1423        // the EAS protocol version is < 14.0
1424        // If entity is null (e.g. for attachments), don't set this header
1425        if (msg && (mProtocolVersionDouble < Eas.SUPPORTED_PROTOCOL_EX2010_DOUBLE)) {
1426            method.setHeader("Content-Type", "message/rfc822");
1427        } else if (entity != null) {
1428            method.setHeader("Content-Type", "application/vnd.ms-sync.wbxml");
1429        }
1430        setHeaders(method, !cmd.equals(PING_COMMAND));
1431        method.setEntity(entity);
1432        return executePostWithTimeout(client, method, timeout, isPingCommand);
1433    }
1434
1435    protected EasResponse sendHttpClientOptions() throws IOException {
1436        HttpClient client = getHttpClient(COMMAND_TIMEOUT);
1437        String us = makeUriString("OPTIONS", null);
1438        HttpOptions method = new HttpOptions(URI.create(us));
1439        setHeaders(method, false);
1440        return new EasResponse(client.execute(method));
1441    }
1442
1443    String getTargetCollectionClassFromCursor(Cursor c) {
1444        int type = c.getInt(Mailbox.CONTENT_TYPE_COLUMN);
1445        if (type == Mailbox.TYPE_CONTACTS) {
1446            return "Contacts";
1447        } else if (type == Mailbox.TYPE_CALENDAR) {
1448            return "Calendar";
1449        } else {
1450            return "Email";
1451        }
1452    }
1453
1454    /**
1455     * Negotiate provisioning with the server.  First, get policies form the server and see if
1456     * the policies are supported by the device.  Then, write the policies to the account and
1457     * tell SecurityPolicy that we have policies in effect.  Finally, see if those policies are
1458     * active; if so, acknowledge the policies to the server and get a final policy key that we
1459     * use in future EAS commands and write this key to the account.
1460     * @return whether or not provisioning has been successful
1461     * @throws IOException
1462     */
1463    private boolean tryProvision() throws IOException {
1464        // First, see if provisioning is even possible, i.e. do we support the policies required
1465        // by the server
1466        ProvisionParser pp = canProvision();
1467        if (pp != null) {
1468            // Get the policies from ProvisionParser
1469            Policy policy = pp.getPolicy();
1470            Policy oldPolicy = null;
1471            // Grab the old policy (if any)
1472            if (mAccount.mPolicyKey > 0) {
1473                oldPolicy = Policy.restorePolicyWithId(mContext, mAccount.mPolicyKey);
1474            }
1475            // Update the account with a null policyKey (the key we've gotten is
1476            // temporary and cannot be used for syncing)
1477            Policy.setAccountPolicy(mContext, mAccount, policy, null);
1478            // Make sure that SecurityPolicy is up-to-date
1479            SecurityPolicyDelegate.policiesUpdated(mContext, mAccount.mId);
1480            if (pp.getRemoteWipe()) {
1481                // We've gotten a remote wipe command
1482                ExchangeService.alwaysLog("!!! Remote wipe request received");
1483                // Start by setting the account to security hold
1484                SecurityPolicyDelegate.setAccountHoldFlag(mContext, mAccount, true);
1485                // Force a stop to any running syncs for this account (except this one)
1486                ExchangeService.stopNonAccountMailboxSyncsForAccount(mAccount.mId);
1487
1488                // If we're not the admin, we can't do the wipe, so just return
1489                if (!SecurityPolicyDelegate.isActiveAdmin(mContext)) {
1490                    ExchangeService.alwaysLog("!!! Not device admin; can't wipe");
1491                    return false;
1492                }
1493
1494                // First, we've got to acknowledge it, but wrap the wipe in try/catch so that
1495                // we wipe the device regardless of any errors in acknowledgment
1496                try {
1497                    ExchangeService.alwaysLog("!!! Acknowledging remote wipe to server");
1498                    acknowledgeRemoteWipe(pp.getSecuritySyncKey());
1499                } catch (Exception e) {
1500                    // Because remote wipe is such a high priority task, we don't want to
1501                    // circumvent it if there's an exception in acknowledgment
1502                }
1503                // Then, tell SecurityPolicy to wipe the device
1504                ExchangeService.alwaysLog("!!! Executing remote wipe");
1505                SecurityPolicyDelegate.remoteWipe(mContext);
1506                return false;
1507            } else if (SecurityPolicyDelegate.isActive(mContext, policy)) {
1508                // See if the required policies are in force; if they are, acknowledge the policies
1509                // to the server and get the final policy key
1510                String securitySyncKey = acknowledgeProvision(pp.getSecuritySyncKey(),
1511                        PROVISION_STATUS_OK);
1512                if (securitySyncKey != null) {
1513                    // If attachment policies have changed, fix up any affected attachment records
1514                    if (oldPolicy != null) {
1515                        if ((oldPolicy.mDontAllowAttachments != policy.mDontAllowAttachments) ||
1516                                (oldPolicy.mMaxAttachmentSize != policy.mMaxAttachmentSize)) {
1517                            Policy.setAttachmentFlagsForNewPolicy(mContext, mAccount, policy);
1518                        }
1519                    }
1520                    // Write the final policy key to the Account and say we've been successful
1521                    Policy.setAccountPolicy(mContext, mAccount, policy, securitySyncKey);
1522                    // Release any mailboxes that might be in a security hold
1523                    ExchangeService.releaseSecurityHold(mAccount);
1524                    return true;
1525                }
1526            } else {
1527                // Notify that we are blocked because of policies
1528                SecurityPolicyDelegate.policiesRequired(mContext, mAccount.mId);
1529            }
1530        }
1531        return false;
1532    }
1533
1534    private String getPolicyType() {
1535        return (mProtocolVersionDouble >=
1536            Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) ? EAS_12_POLICY_TYPE : EAS_2_POLICY_TYPE;
1537    }
1538
1539    /**
1540     * Obtain a set of policies from the server and determine whether those policies are supported
1541     * by the device.
1542     * @return the ProvisionParser (holds policies and key) if we receive policies; null otherwise
1543     * @throws IOException
1544     */
1545    private ProvisionParser canProvision() throws IOException {
1546        Serializer s = new Serializer();
1547        s.start(Tags.PROVISION_PROVISION);
1548        if (mProtocolVersionDouble >= Eas.SUPPORTED_PROTOCOL_EX2010_DOUBLE) {
1549            // Send settings information in 14.0 and greater
1550            s.start(Tags.SETTINGS_DEVICE_INFORMATION).start(Tags.SETTINGS_SET);
1551            s.data(Tags.SETTINGS_MODEL, Build.MODEL);
1552            //s.data(Tags.SETTINGS_IMEI, "");
1553            //s.data(Tags.SETTINGS_FRIENDLY_NAME, "Friendly Name");
1554            s.data(Tags.SETTINGS_OS, "Android " + Build.VERSION.RELEASE);
1555            //s.data(Tags.SETTINGS_OS_LANGUAGE, "");
1556            //s.data(Tags.SETTINGS_PHONE_NUMBER, "");
1557            //s.data(Tags.SETTINGS_MOBILE_OPERATOR, "");
1558            s.data(Tags.SETTINGS_USER_AGENT, USER_AGENT);
1559            s.end().end();  // SETTINGS_SET, SETTINGS_DEVICE_INFORMATION
1560        }
1561        s.start(Tags.PROVISION_POLICIES);
1562        s.start(Tags.PROVISION_POLICY).data(Tags.PROVISION_POLICY_TYPE, getPolicyType()).end();
1563        s.end();  // PROVISION_POLICIES
1564        s.end().done(); // PROVISION_PROVISION
1565        EasResponse resp = sendHttpClientPost("Provision", s.toByteArray());
1566        try {
1567            int code = resp.getStatus();
1568            if (code == HttpStatus.SC_OK) {
1569                InputStream is = resp.getInputStream();
1570                ProvisionParser pp = new ProvisionParser(is, this);
1571                if (pp.parse()) {
1572                    // The PolicySet in the ProvisionParser will have the requirements for all KNOWN
1573                    // policies.  If others are required, hasSupportablePolicySet will be false
1574                    if (!pp.hasSupportablePolicySet())  {
1575                        // Try to acknowledge using the "partial" status (i.e. we can partially
1576                        // accommodate the required policies).  The server will agree to this if the
1577                        // "allow non-provisionable devices" setting is enabled on the server
1578                        String policyKey = acknowledgeProvision(pp.getSecuritySyncKey(),
1579                                PROVISION_STATUS_PARTIAL);
1580                        // Return either the parser (success) or null (failure)
1581                        if (policyKey != null) {
1582                            pp.clearUnsupportedPolicies();
1583                        }
1584                    }
1585                    return pp;
1586                }
1587            }
1588        } finally {
1589            resp.close();
1590        }
1591        // On failures, simply return null
1592        return null;
1593    }
1594
1595    /**
1596     * Acknowledge that we support the policies provided by the server, and that these policies
1597     * are in force.
1598     * @param tempKey the initial (temporary) policy key sent by the server
1599     * @return the final policy key, which can be used for syncing
1600     * @throws IOException
1601     */
1602    private void acknowledgeRemoteWipe(String tempKey) throws IOException {
1603        acknowledgeProvisionImpl(tempKey, PROVISION_STATUS_OK, true);
1604    }
1605
1606    private String acknowledgeProvision(String tempKey, String result) throws IOException {
1607        return acknowledgeProvisionImpl(tempKey, result, false);
1608    }
1609
1610    private String acknowledgeProvisionImpl(String tempKey, String status,
1611            boolean remoteWipe) throws IOException {
1612        Serializer s = new Serializer();
1613        s.start(Tags.PROVISION_PROVISION).start(Tags.PROVISION_POLICIES);
1614        s.start(Tags.PROVISION_POLICY);
1615
1616        // Use the proper policy type, depending on EAS version
1617        s.data(Tags.PROVISION_POLICY_TYPE, getPolicyType());
1618
1619        s.data(Tags.PROVISION_POLICY_KEY, tempKey);
1620        s.data(Tags.PROVISION_STATUS, status);
1621        s.end().end(); // PROVISION_POLICY, PROVISION_POLICIES
1622        if (remoteWipe) {
1623            s.start(Tags.PROVISION_REMOTE_WIPE);
1624            s.data(Tags.PROVISION_STATUS, PROVISION_STATUS_OK);
1625            s.end();
1626        }
1627        s.end().done(); // PROVISION_PROVISION
1628        EasResponse resp = sendHttpClientPost("Provision", s.toByteArray());
1629        try {
1630            int code = resp.getStatus();
1631            if (code == HttpStatus.SC_OK) {
1632                InputStream is = resp.getInputStream();
1633                ProvisionParser pp = new ProvisionParser(is, this);
1634                if (pp.parse()) {
1635                    // Return the final policy key from the ProvisionParser
1636                    return pp.getSecuritySyncKey();
1637                }
1638            }
1639        } finally {
1640            resp.close();
1641        }
1642        // On failures, return null
1643        return null;
1644    }
1645
1646    /**
1647     * Translate exit status code to service status code (used in callbacks)
1648     * @param exitStatus the service's exit status
1649     * @return the corresponding service status
1650     */
1651    private int exitStatusToServiceStatus(int exitStatus) {
1652        switch(exitStatus) {
1653            case EXIT_SECURITY_FAILURE:
1654                return EmailServiceStatus.SECURITY_FAILURE;
1655            case EXIT_LOGIN_FAILURE:
1656                return EmailServiceStatus.LOGIN_FAILED;
1657            default:
1658                return EmailServiceStatus.SUCCESS;
1659        }
1660    }
1661
1662    /**
1663     * Performs FolderSync
1664     *
1665     * @throws IOException
1666     * @throws EasParserException
1667     */
1668    public void runAccountMailbox() throws IOException, EasParserException {
1669        // Check that the account's mailboxes are consistent
1670        MailboxUtilities.checkMailboxConsistency(mContext, mAccount.mId);
1671        // Initialize exit status to success
1672        mExitStatus = EXIT_DONE;
1673        try {
1674            try {
1675                ExchangeService.callback()
1676                    .syncMailboxListStatus(mAccount.mId, EmailServiceStatus.IN_PROGRESS, 0);
1677            } catch (RemoteException e1) {
1678                // Don't care if this fails
1679            }
1680
1681            if (mAccount.mSyncKey == null) {
1682                mAccount.mSyncKey = "0";
1683                userLog("Account syncKey INIT to 0");
1684                ContentValues cv = new ContentValues();
1685                cv.put(AccountColumns.SYNC_KEY, mAccount.mSyncKey);
1686                mAccount.update(mContext, cv);
1687            }
1688
1689            boolean firstSync = mAccount.mSyncKey.equals("0");
1690            if (firstSync) {
1691                userLog("Initial FolderSync");
1692            }
1693
1694            // When we first start up, change all mailboxes to push.
1695            ContentValues cv = new ContentValues();
1696            cv.put(Mailbox.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PUSH);
1697            if (mContentResolver.update(Mailbox.CONTENT_URI, cv,
1698                    WHERE_ACCOUNT_AND_SYNC_INTERVAL_PING,
1699                    new String[] {Long.toString(mAccount.mId)}) > 0) {
1700                ExchangeService.kick("change ping boxes to push");
1701            }
1702
1703            // Determine our protocol version, if we haven't already and save it in the Account
1704            // Also re-check protocol version at least once a day (in case of upgrade)
1705            if (mAccount.mProtocolVersion == null ||
1706                    ((System.currentTimeMillis() - mMailbox.mSyncTime) > DAYS)) {
1707                userLog("Determine EAS protocol version");
1708                EasResponse resp = sendHttpClientOptions();
1709                try {
1710                    int code = resp.getStatus();
1711                    userLog("OPTIONS response: ", code);
1712                    if (code == HttpStatus.SC_OK) {
1713                        Header header = resp.getHeader("MS-ASProtocolCommands");
1714                        userLog(header.getValue());
1715                        header = resp.getHeader("ms-asprotocolversions");
1716                        try {
1717                            setupProtocolVersion(this, header);
1718                        } catch (MessagingException e) {
1719                            // Since we've already validated, this can't really happen
1720                            // But if it does, we'll rethrow this...
1721                            throw new IOException();
1722                        }
1723                        // Save the protocol version
1724                        cv.clear();
1725                        // Save the protocol version in the account; if we're using 12.0 or greater,
1726                        // set the flag for support of SmartForward
1727                        cv.put(Account.PROTOCOL_VERSION, mProtocolVersion);
1728                        if (mProtocolVersionDouble >= 12.0) {
1729                            cv.put(Account.FLAGS,
1730                                    mAccount.mFlags | Account.FLAGS_SUPPORTS_SMART_FORWARD);
1731                        }
1732                        mAccount.update(mContext, cv);
1733                        cv.clear();
1734                        // Save the sync time of the account mailbox to current time
1735                        cv.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
1736                        mMailbox.update(mContext, cv);
1737                     } else {
1738                        errorLog("OPTIONS command failed; throwing IOException");
1739                        throw new IOException();
1740                    }
1741                } finally {
1742                    resp.close();
1743                }
1744            }
1745
1746            // Change all pushable boxes to push when we start the account mailbox
1747            if (mAccount.mSyncInterval == Account.CHECK_INTERVAL_PUSH) {
1748                cv.clear();
1749                cv.put(Mailbox.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PUSH);
1750                if (mContentResolver.update(Mailbox.CONTENT_URI, cv,
1751                        ExchangeService.WHERE_IN_ACCOUNT_AND_PUSHABLE,
1752                        new String[] {Long.toString(mAccount.mId)}) > 0) {
1753                    userLog("Push account; set pushable boxes to push...");
1754                }
1755            }
1756
1757            while (!mStop) {
1758                // If we're not allowed to sync (e.g. roaming policy), leave now
1759                if (!ExchangeService.canAutoSync(mAccount)) return;
1760                userLog("Sending Account syncKey: ", mAccount.mSyncKey);
1761                Serializer s = new Serializer();
1762                s.start(Tags.FOLDER_FOLDER_SYNC).start(Tags.FOLDER_SYNC_KEY)
1763                    .text(mAccount.mSyncKey).end().end().done();
1764                EasResponse resp = sendHttpClientPost("FolderSync", s.toByteArray());
1765                try {
1766                    if (mStop) break;
1767                    int code = resp.getStatus();
1768                    if (code == HttpStatus.SC_OK) {
1769                        if (!resp.isEmpty()) {
1770                            InputStream is = resp.getInputStream();
1771                            // Returns true if we need to sync again
1772                            if (new FolderSyncParser(is, new AccountSyncAdapter(this)).parse()) {
1773                                continue;
1774                            }
1775                        }
1776                    } else if (isProvisionError(code)) {
1777                        throw new CommandStatusException(CommandStatus.NEEDS_PROVISIONING);
1778                    } else if (isAuthError(code)) {
1779                        mExitStatus = EXIT_LOGIN_FAILURE;
1780                        return;
1781                    } else {
1782                        userLog("FolderSync response error: ", code);
1783                    }
1784                } finally {
1785                    resp.close();
1786                }
1787
1788                // Change all push/hold boxes to push
1789                cv.clear();
1790                cv.put(Mailbox.SYNC_INTERVAL, Account.CHECK_INTERVAL_PUSH);
1791                if (mContentResolver.update(Mailbox.CONTENT_URI, cv,
1792                        WHERE_PUSH_HOLD_NOT_ACCOUNT_MAILBOX,
1793                        new String[] {Long.toString(mAccount.mId)}) > 0) {
1794                    userLog("Set push/hold boxes to push...");
1795                }
1796
1797                try {
1798                    ExchangeService.callback()
1799                        .syncMailboxListStatus(mAccount.mId, exitStatusToServiceStatus(mExitStatus),
1800                                0);
1801                } catch (RemoteException e1) {
1802                    // Don't care if this fails
1803                }
1804
1805                // Before each run of the pingLoop, if this Account has a PolicySet, make sure it's
1806                // active; otherwise, clear out the key/flag.  This should cause a provisioning
1807                // error on the next POST, and start the security sequence over again
1808                String key = mAccount.mSecuritySyncKey;
1809                if (!TextUtils.isEmpty(key)) {
1810                    Policy policy = Policy.restorePolicyWithId(mContext, mAccount.mPolicyKey);
1811                    if (!SecurityPolicyDelegate.isActive(mContext, policy)) {
1812                        cv.clear();
1813                        cv.put(AccountColumns.SECURITY_FLAGS, 0);
1814                        cv.putNull(AccountColumns.SECURITY_SYNC_KEY);
1815                        long accountId = mAccount.mId;
1816                        mContentResolver.update(ContentUris.withAppendedId(
1817                                Account.CONTENT_URI, accountId), cv, null, null);
1818                        SecurityPolicyDelegate.policiesRequired(mContext, accountId);
1819                    }
1820                }
1821
1822                // Wait for push notifications.
1823                String threadName = Thread.currentThread().getName();
1824                try {
1825                    runPingLoop();
1826                } catch (StaleFolderListException e) {
1827                    // We break out if we get told about a stale folder list
1828                    userLog("Ping interrupted; folder list requires sync...");
1829                } catch (IllegalHeartbeatException e) {
1830                    // If we're sending an illegal heartbeat, reset either the min or the max to
1831                    // that heartbeat
1832                    resetHeartbeats(e.mLegalHeartbeat);
1833                } finally {
1834                    Thread.currentThread().setName(threadName);
1835                }
1836            }
1837        } catch (CommandStatusException e) {
1838            // If the sync error is a provisioning failure (perhaps policies changed),
1839            // let's try the provisioning procedure
1840            // Provisioning must only be attempted for the account mailbox - trying to
1841            // provision any other mailbox may result in race conditions and the
1842            // creation of multiple policy keys.
1843            int status = e.mStatus;
1844            if (CommandStatus.isNeedsProvisioning(status)) {
1845                if (!tryProvision()) {
1846                    // Set the appropriate failure status
1847                    mExitStatus = EXIT_SECURITY_FAILURE;
1848                    return;
1849                }
1850            } else if (CommandStatus.isDeniedAccess(status)) {
1851                mExitStatus = EXIT_ACCESS_DENIED;
1852                return;
1853            } else {
1854                userLog("Unexpected status: " + CommandStatus.toString(status));
1855                mExitStatus = EXIT_EXCEPTION;
1856            }
1857        } catch (IOException e) {
1858            // We catch this here to send the folder sync status callback
1859            // A folder sync failed callback will get sent from run()
1860            try {
1861                if (!mStop) {
1862                    // NOTE: The correct status is CONNECTION_ERROR, but the UI displays this, and
1863                    // it's not really appropriate for EAS as this is not unexpected for a ping and
1864                    // connection errors are retried in any case
1865                    ExchangeService.callback()
1866                        .syncMailboxListStatus(mAccount.mId,
1867                                EmailServiceStatus.SUCCESS, 0);
1868                }
1869            } catch (RemoteException e1) {
1870                // Don't care if this fails
1871            }
1872            throw e;
1873        }
1874    }
1875
1876    /**
1877     * Reset either our minimum or maximum ping heartbeat to a heartbeat known to be legal
1878     * @param legalHeartbeat a known legal heartbeat (from the EAS server)
1879     */
1880    /*package*/ void resetHeartbeats(int legalHeartbeat) {
1881        userLog("Resetting min/max heartbeat, legal = " + legalHeartbeat);
1882        // We are here because the current heartbeat (mPingHeartbeat) is invalid.  Depending on
1883        // whether the argument is above or below the current heartbeat, we can infer the need to
1884        // change either the minimum or maximum heartbeat
1885        if (legalHeartbeat > mPingHeartbeat) {
1886            // The legal heartbeat is higher than the ping heartbeat; therefore, our minimum was
1887            // too low.  We respond by raising either or both of the minimum heartbeat or the
1888            // force heartbeat to the argument value
1889            if (mPingMinHeartbeat < legalHeartbeat) {
1890                mPingMinHeartbeat = legalHeartbeat;
1891            }
1892            if (mPingForceHeartbeat < legalHeartbeat) {
1893                mPingForceHeartbeat = legalHeartbeat;
1894            }
1895            // If our minimum is now greater than the max, bring them together
1896            if (mPingMinHeartbeat > mPingMaxHeartbeat) {
1897                mPingMaxHeartbeat = legalHeartbeat;
1898            }
1899        } else if (legalHeartbeat < mPingHeartbeat) {
1900            // The legal heartbeat is lower than the ping heartbeat; therefore, our maximum was
1901            // too high.  We respond by lowering the maximum to the argument value
1902            mPingMaxHeartbeat = legalHeartbeat;
1903            // If our maximum is now less than the minimum, bring them together
1904            if (mPingMaxHeartbeat < mPingMinHeartbeat) {
1905                mPingMinHeartbeat = legalHeartbeat;
1906            }
1907        }
1908        // Set current heartbeat to the legal heartbeat
1909        mPingHeartbeat = legalHeartbeat;
1910        // Allow the heartbeat logic to run
1911        mPingHeartbeatDropped = false;
1912    }
1913
1914    private void pushFallback(long mailboxId) {
1915        Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mailboxId);
1916        if (mailbox == null) {
1917            return;
1918        }
1919        ContentValues cv = new ContentValues();
1920        int mins = PING_FALLBACK_PIM;
1921        if (mailbox.mType == Mailbox.TYPE_INBOX) {
1922            mins = PING_FALLBACK_INBOX;
1923        }
1924        cv.put(Mailbox.SYNC_INTERVAL, mins);
1925        mContentResolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId),
1926                cv, null, null);
1927        errorLog("*** PING ERROR LOOP: Set " + mailbox.mDisplayName + " to " + mins + " min sync");
1928        ExchangeService.kick("push fallback");
1929    }
1930
1931    /**
1932     * Simplistic attempt to determine a NAT timeout, based on experience with various carriers
1933     * and networks.  The string "reset by peer" is very common in these situations, so we look for
1934     * that specifically.  We may add additional tests here as more is learned.
1935     * @param message
1936     * @return whether this message is likely associated with a NAT failure
1937     */
1938    private boolean isLikelyNatFailure(String message) {
1939        if (message == null) return false;
1940        if (message.contains("reset by peer")) {
1941            return true;
1942        }
1943        return false;
1944    }
1945
1946    private void runPingLoop() throws IOException, StaleFolderListException,
1947            IllegalHeartbeatException, CommandStatusException {
1948        int pingHeartbeat = mPingHeartbeat;
1949        userLog("runPingLoop");
1950        // Do push for all sync services here
1951        long endTime = System.currentTimeMillis() + (30*MINUTES);
1952        HashMap<String, Integer> pingErrorMap = new HashMap<String, Integer>();
1953        ArrayList<String> readyMailboxes = new ArrayList<String>();
1954        ArrayList<String> notReadyMailboxes = new ArrayList<String>();
1955        int pingWaitCount = 0;
1956        long inboxId = -1;
1957
1958        while ((System.currentTimeMillis() < endTime) && !mStop) {
1959            // Count of pushable mailboxes
1960            int pushCount = 0;
1961            // Count of mailboxes that can be pushed right now
1962            int canPushCount = 0;
1963            // Count of uninitialized boxes
1964            int uninitCount = 0;
1965
1966            Serializer s = new Serializer();
1967            Cursor c = mContentResolver.query(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION,
1968                    MailboxColumns.ACCOUNT_KEY + '=' + mAccount.mId +
1969                    AND_FREQUENCY_PING_PUSH_AND_NOT_ACCOUNT_MAILBOX, null, null);
1970            notReadyMailboxes.clear();
1971            readyMailboxes.clear();
1972            // Look for an inbox, and remember its id
1973            if (inboxId == -1) {
1974                inboxId = Mailbox.findMailboxOfType(mContext, mAccount.mId, Mailbox.TYPE_INBOX);
1975            }
1976            try {
1977                // Loop through our pushed boxes seeing what is available to push
1978                while (c.moveToNext()) {
1979                    pushCount++;
1980                    // Two requirements for push:
1981                    // 1) ExchangeService tells us the mailbox is syncable (not running/not stopped)
1982                    // 2) The syncKey isn't "0" (i.e. it's synced at least once)
1983                    long mailboxId = c.getLong(Mailbox.CONTENT_ID_COLUMN);
1984                    int pingStatus = ExchangeService.pingStatus(mailboxId);
1985                    String mailboxName = c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN);
1986                    if (pingStatus == ExchangeService.PING_STATUS_OK) {
1987                        String syncKey = c.getString(Mailbox.CONTENT_SYNC_KEY_COLUMN);
1988                        if ((syncKey == null) || syncKey.equals("0")) {
1989                            // We can't push until the initial sync is done
1990                            pushCount--;
1991                            uninitCount++;
1992                            continue;
1993                        }
1994
1995                        if (canPushCount++ == 0) {
1996                            // Initialize the Ping command
1997                            s.start(Tags.PING_PING)
1998                                .data(Tags.PING_HEARTBEAT_INTERVAL,
1999                                        Integer.toString(pingHeartbeat))
2000                                .start(Tags.PING_FOLDERS);
2001                        }
2002
2003                        String folderClass = getTargetCollectionClassFromCursor(c);
2004                        s.start(Tags.PING_FOLDER)
2005                            .data(Tags.PING_ID, c.getString(Mailbox.CONTENT_SERVER_ID_COLUMN))
2006                            .data(Tags.PING_CLASS, folderClass)
2007                            .end();
2008                        readyMailboxes.add(mailboxName);
2009                    } else if ((pingStatus == ExchangeService.PING_STATUS_RUNNING) ||
2010                            (pingStatus == ExchangeService.PING_STATUS_WAITING)) {
2011                        notReadyMailboxes.add(mailboxName);
2012                    } else if (pingStatus == ExchangeService.PING_STATUS_UNABLE) {
2013                        pushCount--;
2014                        userLog(mailboxName, " in error state; ignore");
2015                        continue;
2016                    }
2017                }
2018            } finally {
2019                c.close();
2020            }
2021
2022            if (Eas.USER_LOG) {
2023                if (!notReadyMailboxes.isEmpty()) {
2024                    userLog("Ping not ready for: " + notReadyMailboxes);
2025                }
2026                if (!readyMailboxes.isEmpty()) {
2027                    userLog("Ping ready for: " + readyMailboxes);
2028                }
2029            }
2030
2031            // If we've waited 10 seconds or more, just ping with whatever boxes are ready
2032            // But use a shorter than normal heartbeat
2033            boolean forcePing = !notReadyMailboxes.isEmpty() && (pingWaitCount > 5);
2034
2035            if ((canPushCount > 0) && ((canPushCount == pushCount) || forcePing)) {
2036                // If all pingable boxes are ready for push, send Ping to the server
2037                s.end().end().done();
2038                pingWaitCount = 0;
2039                mPostReset = false;
2040                mPostAborted = false;
2041
2042                // If we've been stopped, this is a good time to return
2043                if (mStop) return;
2044
2045                long pingTime = SystemClock.elapsedRealtime();
2046                try {
2047                    // Send the ping, wrapped by appropriate timeout/alarm
2048                    if (forcePing) {
2049                        userLog("Forcing ping after waiting for all boxes to be ready");
2050                    }
2051                    EasResponse resp =
2052                        sendPing(s.toByteArray(), forcePing ? mPingForceHeartbeat : pingHeartbeat);
2053
2054                    try {
2055                        int code = resp.getStatus();
2056                        userLog("Ping response: ", code);
2057
2058                        // If we're not allowed to sync (e.g. roaming policy), terminate gracefully
2059                        // now; otherwise we might start a sync based on the response
2060                        if (!ExchangeService.canAutoSync(mAccount)) {
2061                            mStop = true;
2062                        }
2063
2064                        // Return immediately if we've been asked to stop during the ping
2065                        if (mStop) {
2066                            userLog("Stopping pingLoop");
2067                            return;
2068                        }
2069
2070                        if (code == HttpStatus.SC_OK) {
2071                            // Make sure to clear out any pending sync errors
2072                            ExchangeService.removeFromSyncErrorMap(mMailboxId);
2073                            if (!resp.isEmpty()) {
2074                                InputStream is = resp.getInputStream();
2075                                int pingResult = parsePingResult(is, mContentResolver,
2076                                        pingErrorMap);
2077                                // If our ping completed (status = 1), and wasn't forced and we're
2078                                // not at the maximum, try increasing timeout by two minutes
2079                                if (pingResult == PROTOCOL_PING_STATUS_COMPLETED && !forcePing) {
2080                                    if (pingHeartbeat > mPingHighWaterMark) {
2081                                        mPingHighWaterMark = pingHeartbeat;
2082                                        userLog("Setting high water mark at: ", mPingHighWaterMark);
2083                                    }
2084                                    if ((pingHeartbeat < mPingMaxHeartbeat) &&
2085                                            !mPingHeartbeatDropped) {
2086                                        pingHeartbeat += PING_HEARTBEAT_INCREMENT;
2087                                        if (pingHeartbeat > mPingMaxHeartbeat) {
2088                                            pingHeartbeat = mPingMaxHeartbeat;
2089                                        }
2090                                        userLog("Increase ping heartbeat to ", pingHeartbeat, "s");
2091                                    }
2092                                }
2093                            } else {
2094                                userLog("Ping returned empty result; throwing IOException");
2095                                throw new IOException();
2096                            }
2097                        } else if (isAuthError(code)) {
2098                            mExitStatus = EXIT_LOGIN_FAILURE;
2099                            userLog("Authorization error during Ping: ", code);
2100                            throw new IOException();
2101                        }
2102                    } finally {
2103                        resp.close();
2104                    }
2105                } catch (IOException e) {
2106                    String message = e.getMessage();
2107                    // If we get the exception that is indicative of a NAT timeout and if we
2108                    // haven't yet "fixed" the timeout, back off by two minutes and "fix" it
2109                    boolean hasMessage = message != null;
2110                    userLog("IOException runPingLoop: " + (hasMessage ? message : "[no message]"));
2111                    if (mPostReset) {
2112                        // Nothing to do in this case; this is ExchangeService telling us to try
2113                        // another ping.
2114                    } else if (mPostAborted || isLikelyNatFailure(message)) {
2115                        long pingLength = SystemClock.elapsedRealtime() - pingTime;
2116                        if ((pingHeartbeat > mPingMinHeartbeat) &&
2117                                (pingHeartbeat > mPingHighWaterMark)) {
2118                            pingHeartbeat -= PING_HEARTBEAT_INCREMENT;
2119                            mPingHeartbeatDropped = true;
2120                            if (pingHeartbeat < mPingMinHeartbeat) {
2121                                pingHeartbeat = mPingMinHeartbeat;
2122                            }
2123                            userLog("Decreased ping heartbeat to ", pingHeartbeat, "s");
2124                        } else if (mPostAborted) {
2125                            // There's no point in throwing here; this can happen in two cases
2126                            // 1) An alarm, which indicates minutes without activity; no sense
2127                            //    backing off
2128                            // 2) ExchangeService abort, due to sync of mailbox.  Again, we want to
2129                            //    keep on trying to ping
2130                            userLog("Ping aborted; retry");
2131                        } else if (pingLength < 2000) {
2132                            userLog("Abort or NAT type return < 2 seconds; throwing IOException");
2133                            throw e;
2134                        } else {
2135                            userLog("NAT type IOException");
2136                        }
2137                    } else if (hasMessage && message.contains("roken pipe")) {
2138                        // The "broken pipe" error (uppercase or lowercase "b") seems to be an
2139                        // internal error, so let's not throw an exception (which leads to delays)
2140                        // but rather simply run through the loop again
2141                    } else {
2142                        throw e;
2143                    }
2144                }
2145            } else if (forcePing) {
2146                // In this case, there aren't any boxes that are pingable, but there are boxes
2147                // waiting (for IOExceptions)
2148                userLog("pingLoop waiting 60s for any pingable boxes");
2149                sleep(60*SECONDS, true);
2150            } else if (pushCount > 0) {
2151                // If we want to Ping, but can't just yet, wait a little bit
2152                // TODO Change sleep to wait and use notify from ExchangeService when a sync ends
2153                sleep(2*SECONDS, false);
2154                pingWaitCount++;
2155                //userLog("pingLoop waited 2s for: ", (pushCount - canPushCount), " box(es)");
2156            } else if (uninitCount > 0) {
2157                // In this case, we're doing an initial sync of at least one mailbox.  Since this
2158                // is typically a one-time case, I'm ok with trying again every 10 seconds until
2159                // we're in one of the other possible states.
2160                userLog("pingLoop waiting for initial sync of ", uninitCount, " box(es)");
2161                sleep(10*SECONDS, true);
2162            } else if (inboxId == -1) {
2163                // In this case, we're still syncing mailboxes, so sleep for only a short time
2164                sleep(45*SECONDS, true);
2165            } else {
2166                // We've got nothing to do, so we'll check again in 20 minutes at which time
2167                // we'll update the folder list, check for policy changes and/or remote wipe, etc.
2168                // Let the device sleep in the meantime...
2169                userLog(ACCOUNT_MAILBOX_SLEEP_TEXT);
2170                sleep(ACCOUNT_MAILBOX_SLEEP_TIME, true);
2171            }
2172        }
2173
2174        // Save away the current heartbeat
2175        mPingHeartbeat = pingHeartbeat;
2176    }
2177
2178    private void sleep(long ms, boolean runAsleep) {
2179        if (runAsleep) {
2180            ExchangeService.runAsleep(mMailboxId, ms+(5*SECONDS));
2181        }
2182        try {
2183            Thread.sleep(ms);
2184        } catch (InterruptedException e) {
2185            // Doesn't matter whether we stop early; it's the thought that counts
2186        } finally {
2187            if (runAsleep) {
2188                ExchangeService.runAwake(mMailboxId);
2189            }
2190        }
2191    }
2192
2193    private int parsePingResult(InputStream is, ContentResolver cr,
2194            HashMap<String, Integer> errorMap)
2195            throws IOException, StaleFolderListException, IllegalHeartbeatException,
2196                CommandStatusException {
2197        PingParser pp = new PingParser(is, this);
2198        if (pp.parse()) {
2199            // True indicates some mailboxes need syncing...
2200            // syncList has the serverId's of the mailboxes...
2201            mBindArguments[0] = Long.toString(mAccount.mId);
2202            mPingChangeList = pp.getSyncList();
2203            for (String serverId: mPingChangeList) {
2204                mBindArguments[1] = serverId;
2205                Cursor c = cr.query(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION,
2206                        WHERE_ACCOUNT_KEY_AND_SERVER_ID, mBindArguments, null);
2207                try {
2208                    if (c.moveToFirst()) {
2209
2210                        /**
2211                         * Check the boxes reporting changes to see if there really were any...
2212                         * We do this because bugs in various Exchange servers can put us into a
2213                         * looping behavior by continually reporting changes in a mailbox, even when
2214                         * there aren't any.
2215                         *
2216                         * This behavior is seemingly random, and therefore we must code defensively
2217                         * by backing off of push behavior when it is detected.
2218                         *
2219                         * One known cause, on certain Exchange 2003 servers, is acknowledged by
2220                         * Microsoft, and the server hotfix for this case can be found at
2221                         * http://support.microsoft.com/kb/923282
2222                         */
2223
2224                        // Check the status of the last sync
2225                        String status = c.getString(Mailbox.CONTENT_SYNC_STATUS_COLUMN);
2226                        int type = ExchangeService.getStatusType(status);
2227                        // This check should always be true...
2228                        if (type == ExchangeService.SYNC_PING) {
2229                            int changeCount = ExchangeService.getStatusChangeCount(status);
2230                            if (changeCount > 0) {
2231                                errorMap.remove(serverId);
2232                            } else if (changeCount == 0) {
2233                                // This means that a ping reported changes in error; we keep a count
2234                                // of consecutive errors of this kind
2235                                String name = c.getString(Mailbox.CONTENT_DISPLAY_NAME_COLUMN);
2236                                Integer failures = errorMap.get(serverId);
2237                                if (failures == null) {
2238                                    userLog("Last ping reported changes in error for: ", name);
2239                                    errorMap.put(serverId, 1);
2240                                } else if (failures > MAX_PING_FAILURES) {
2241                                    // We'll back off of push for this box
2242                                    pushFallback(c.getLong(Mailbox.CONTENT_ID_COLUMN));
2243                                    continue;
2244                                } else {
2245                                    userLog("Last ping reported changes in error for: ", name);
2246                                    errorMap.put(serverId, failures + 1);
2247                                }
2248                            }
2249                        }
2250
2251                        // If there were no problems with previous sync, we'll start another one
2252                        ExchangeService.startManualSync(c.getLong(Mailbox.CONTENT_ID_COLUMN),
2253                                ExchangeService.SYNC_PING, null);
2254                    }
2255                } finally {
2256                    c.close();
2257                }
2258            }
2259        }
2260        return pp.getSyncStatus();
2261    }
2262
2263    /**
2264     * Common code to sync E+PIM data
2265     *
2266     * @param target an EasMailbox, EasContacts, or EasCalendar object
2267     */
2268    public void sync(AbstractSyncAdapter target) throws IOException {
2269        Mailbox mailbox = target.mMailbox;
2270
2271        boolean moreAvailable = true;
2272        int loopingCount = 0;
2273        while (!mStop && (moreAvailable || hasPendingRequests())) {
2274            // If we have no connectivity, just exit cleanly. ExchangeService will start us up again
2275            // when connectivity has returned
2276            if (!hasConnectivity()) {
2277                userLog("No connectivity in sync; finishing sync");
2278                mExitStatus = EXIT_DONE;
2279                return;
2280            }
2281
2282            // Every time through the loop we check to see if we're still syncable
2283            if (!target.isSyncable()) {
2284                mExitStatus = EXIT_DONE;
2285                return;
2286            }
2287
2288            // Now, handle various requests
2289            while (true) {
2290                Request req = null;
2291
2292                if (mRequestQueue.isEmpty()) {
2293                    break;
2294                } else {
2295                    req = mRequestQueue.peek();
2296                }
2297
2298                // Our two request types are PartRequest (loading attachment) and
2299                // MeetingResponseRequest (respond to a meeting request)
2300                if (req instanceof PartRequest) {
2301                    new AttachmentLoader(this, (PartRequest)req).loadAttachment();
2302                } else if (req instanceof MeetingResponseRequest) {
2303                    sendMeetingResponse((MeetingResponseRequest)req);
2304                } else if (req instanceof MessageMoveRequest) {
2305                    messageMoveRequest((MessageMoveRequest)req);
2306                }
2307
2308                // If there's an exception handling the request, we'll throw it
2309                // Otherwise, we remove the request
2310                mRequestQueue.remove();
2311            }
2312
2313            // Don't sync if we've got nothing to do
2314            if (!moreAvailable) {
2315                continue;
2316            }
2317
2318            Serializer s = new Serializer();
2319
2320            String className = target.getCollectionName();
2321            String syncKey = target.getSyncKey();
2322            userLog("sync, sending ", className, " syncKey: ", syncKey);
2323            s.start(Tags.SYNC_SYNC)
2324                .start(Tags.SYNC_COLLECTIONS)
2325                .start(Tags.SYNC_COLLECTION);
2326            // The "Class" element is removed in EAS 12.1 and later versions
2327            if (mProtocolVersionDouble < Eas.SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE) {
2328                s.data(Tags.SYNC_CLASS, className);
2329            }
2330            s.data(Tags.SYNC_SYNC_KEY, syncKey)
2331                .data(Tags.SYNC_COLLECTION_ID, mailbox.mServerId);
2332
2333            // Start with the default timeout
2334            int timeout = COMMAND_TIMEOUT;
2335            if (!syncKey.equals("0")) {
2336                // EAS doesn't allow GetChanges in an initial sync; sending other options
2337                // appears to cause the server to delay its response in some cases, and this delay
2338                // can be long enough to result in an IOException and total failure to sync.
2339                // Therefore, we don't send any options with the initial sync.
2340                // Set the truncation amount, body preference, lookback, etc.
2341                target.sendSyncOptions(mProtocolVersionDouble, s);
2342            } else {
2343                // Use enormous timeout for initial sync, which empirically can take a while longer
2344                timeout = 120*SECONDS;
2345            }
2346            // Send our changes up to the server
2347            target.sendLocalChanges(s);
2348
2349            s.end().end().end().done();
2350            EasResponse resp = sendHttpClientPost("Sync", new ByteArrayEntity(s.toByteArray()),
2351                    timeout);
2352            try {
2353                int code = resp.getStatus();
2354                if (code == HttpStatus.SC_OK) {
2355                    // In EAS 12.1, we can get "empty" sync responses, which indicate that there are
2356                    // no changes in the mailbox; handle that case here
2357                    // There are two cases here; if we get back a compressed stream (GZIP), we won't
2358                    // know until we try to parse it (and generate an EmptyStreamException). If we
2359                    // get uncompressed data, the response will be empty (i.e. have zero length)
2360                    boolean emptyStream = false;
2361                    if (!resp.isEmpty()) {
2362                        InputStream is = resp.getInputStream();
2363                        try {
2364                            moreAvailable = target.parse(is);
2365                            if (target.isLooping()) {
2366                                loopingCount++;
2367                                userLog("** Looping: " + loopingCount);
2368                                // After the maximum number of loops, we'll set moreAvailable to
2369                                // false and allow the sync loop to terminate
2370                                if (moreAvailable && (loopingCount > MAX_LOOPING_COUNT)) {
2371                                    userLog("** Looping force stopped");
2372                                    moreAvailable = false;
2373                                }
2374                            } else {
2375                                loopingCount = 0;
2376                            }
2377                            target.cleanup();
2378                        } catch (EmptyStreamException e) {
2379                            userLog("Empty stream detected in GZIP response");
2380                            emptyStream = true;
2381                        } catch (CommandStatusException e) {
2382                            // TODO 14.1
2383                            int status = e.mStatus;
2384                            if (CommandStatus.isNeedsProvisioning(status)) {
2385                                mExitStatus = EXIT_SECURITY_FAILURE;
2386                            } else if (CommandStatus.isDeniedAccess(status)) {
2387                                mExitStatus = EXIT_ACCESS_DENIED;
2388                            } else if (CommandStatus.isTransientError(status)) {
2389                                mExitStatus = EXIT_IO_ERROR;
2390                            } else {
2391                                mExitStatus = EXIT_EXCEPTION;
2392                            }
2393                            return;
2394                        }
2395                    } else {
2396                        emptyStream = true;
2397                    }
2398
2399                    if (emptyStream) {
2400                        // If this happens, exit cleanly, and change the interval from push to ping
2401                        // if necessary
2402                        userLog("Empty sync response; finishing");
2403                        if (mMailbox.mSyncInterval == Mailbox.CHECK_INTERVAL_PUSH) {
2404                            userLog("Changing mailbox from push to ping");
2405                            ContentValues cv = new ContentValues();
2406                            cv.put(Mailbox.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PING);
2407                            mContentResolver.update(
2408                                    ContentUris.withAppendedId(Mailbox.CONTENT_URI, mMailbox.mId),
2409                                    cv, null, null);
2410                        }
2411                        if (mRequestQueue.isEmpty()) {
2412                            mExitStatus = EXIT_DONE;
2413                            return;
2414                        } else {
2415                            continue;
2416                        }
2417                    }
2418                } else {
2419                    userLog("Sync response error: ", code);
2420                    if (isProvisionError(code)) {
2421                        mExitStatus = EXIT_SECURITY_FAILURE;
2422                    } else if (isAuthError(code)) {
2423                        mExitStatus = EXIT_LOGIN_FAILURE;
2424                    } else {
2425                        mExitStatus = EXIT_IO_ERROR;
2426                    }
2427                    return;
2428                }
2429            } finally {
2430                resp.close();
2431            }
2432        }
2433        mExitStatus = EXIT_DONE;
2434    }
2435
2436    protected boolean setupService() {
2437        synchronized(getSynchronizer()) {
2438            mThread = Thread.currentThread();
2439            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
2440            TAG = mThread.getName();
2441        }
2442        // Make sure account and mailbox are always the latest from the database
2443        mAccount = Account.restoreAccountWithId(mContext, mAccount.mId);
2444        if (mAccount == null) return false;
2445        mMailbox = Mailbox.restoreMailboxWithId(mContext, mMailbox.mId);
2446        if (mMailbox == null) return false;
2447        HostAuth ha = HostAuth.restoreHostAuthWithId(mContext, mAccount.mHostAuthKeyRecv);
2448        if (ha == null) return false;
2449        mHostAddress = ha.mAddress;
2450        mUserName = ha.mLogin;
2451        mPassword = ha.mPassword;
2452
2453        // Set up our protocol version from the Account
2454        mProtocolVersion = mAccount.mProtocolVersion;
2455        // If it hasn't been set up, start with default version
2456        if (mProtocolVersion == null) {
2457            mProtocolVersion = Eas.DEFAULT_PROTOCOL_VERSION;
2458        }
2459        mProtocolVersionDouble = Eas.getProtocolVersionDouble(mProtocolVersion);
2460        return true;
2461    }
2462
2463    /* (non-Javadoc)
2464     * @see java.lang.Runnable#run()
2465     */
2466    public void run() {
2467        // Make sure account and mailbox are still valid
2468        if (!setupService()) return;
2469        // If we've been stopped, we're done
2470        if (mStop) return;
2471        if (mSyncReason >= ExchangeService.SYNC_CALLBACK_START) {
2472            try {
2473                ExchangeService.callback().syncMailboxStatus(mMailboxId,
2474                        EmailServiceStatus.IN_PROGRESS, 0);
2475            } catch (RemoteException e1) {
2476                // Don't care if this fails
2477            }
2478        }
2479
2480        // Whether or not we're the account mailbox
2481        try {
2482            mDeviceId = ExchangeService.getDeviceId(mContext);
2483            if ((mMailbox == null) || (mAccount == null)) {
2484                return;
2485            } else if (mMailbox.mType == Mailbox.TYPE_EAS_ACCOUNT_MAILBOX) {
2486                runAccountMailbox();
2487            } else {
2488                AbstractSyncAdapter target;
2489                if (mMailbox.mType == Mailbox.TYPE_CONTACTS) {
2490                    target = new ContactsSyncAdapter( this);
2491                } else if (mMailbox.mType == Mailbox.TYPE_CALENDAR) {
2492                    target = new CalendarSyncAdapter(this);
2493                } else {
2494                    target = new EmailSyncAdapter(this);
2495                }
2496                // We loop here because someone might have put a request in while we were syncing
2497                // and we've missed that opportunity...
2498                do {
2499                    if (mRequestTime != 0) {
2500                        userLog("Looping for user request...");
2501                        mRequestTime = 0;
2502                    }
2503                    sync(target);
2504                } while (mRequestTime != 0);
2505            }
2506        } catch (EasAuthenticationException e) {
2507            userLog("Caught authentication error");
2508            mExitStatus = EXIT_LOGIN_FAILURE;
2509        } catch (IOException e) {
2510            String message = e.getMessage();
2511            userLog("Caught IOException: ", (message == null) ? "No message" : message);
2512            mExitStatus = EXIT_IO_ERROR;
2513        } catch (Exception e) {
2514            userLog("Uncaught exception in EasSyncService", e);
2515        } finally {
2516            int status;
2517
2518            if (!mStop) {
2519                userLog("Sync finished");
2520                ExchangeService.done(this);
2521                switch (mExitStatus) {
2522                    case EXIT_IO_ERROR:
2523                        status = EmailServiceStatus.CONNECTION_ERROR;
2524                        break;
2525                    case EXIT_DONE:
2526                        status = EmailServiceStatus.SUCCESS;
2527                        ContentValues cv = new ContentValues();
2528                        cv.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
2529                        String s = "S" + mSyncReason + ':' + status + ':' + mChangeCount;
2530                        cv.put(Mailbox.SYNC_STATUS, s);
2531                        mContentResolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI,
2532                                mMailboxId), cv, null, null);
2533                        break;
2534                    case EXIT_LOGIN_FAILURE:
2535                        status = EmailServiceStatus.LOGIN_FAILED;
2536                        break;
2537                    case EXIT_SECURITY_FAILURE:
2538                        status = EmailServiceStatus.SECURITY_FAILURE;
2539                        // Ask for a new folder list.  This should wake up the account mailbox; a
2540                        // security error in account mailbox should start the provisioning process
2541                        ExchangeService.reloadFolderList(mContext, mAccount.mId, true);
2542                        break;
2543                    case EXIT_ACCESS_DENIED:
2544                        status = EmailServiceStatus.ACCESS_DENIED;
2545                        break;
2546                    default:
2547                        status = EmailServiceStatus.REMOTE_EXCEPTION;
2548                        errorLog("Sync ended due to an exception.");
2549                        break;
2550                }
2551            } else {
2552                userLog("Stopped sync finished.");
2553                status = EmailServiceStatus.SUCCESS;
2554            }
2555
2556            // Send a callback if this run was initiated by a service call
2557            if (mSyncReason >= ExchangeService.SYNC_CALLBACK_START) {
2558                try {
2559                    // Unless the user specifically asked for a sync, we really don't want to report
2560                    // connection issues, as they are likely to be transient.  In this case, we
2561                    // simply report success, so that the progress indicator terminates without
2562                    // putting up an error banner
2563                    if (mSyncReason != ExchangeService.SYNC_UI_REQUEST &&
2564                            status == EmailServiceStatus.CONNECTION_ERROR) {
2565                        status = EmailServiceStatus.SUCCESS;
2566                    }
2567                    ExchangeService.callback().syncMailboxStatus(mMailboxId, status, 0);
2568                } catch (RemoteException e1) {
2569                    // Don't care if this fails
2570                }
2571            }
2572
2573            // Make sure ExchangeService knows about this
2574            ExchangeService.kick("sync finished");
2575       }
2576    }
2577}
2578