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