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