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