EasProvision.java revision 110837ebff288a75f9bda067c38e2c46797d99b5
1328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu/*
2328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * Copyright (C) 2013 The Android Open Source Project
3328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu *
4328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * Licensed under the Apache License, Version 2.0 (the "License");
5328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * you may not use this file except in compliance with the License.
6328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * You may obtain a copy of the License at
7328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu *
8328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu *      http://www.apache.org/licenses/LICENSE-2.0
9328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu *
10328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * Unless required by applicable law or agreed to in writing, software
11328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * distributed under the License is distributed on an "AS IS" BASIS,
12328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * See the License for the specific language governing permissions and
14328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * limitations under the License.
15328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu */
16328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
17328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hupackage com.android.exchange.eas;
18328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
19328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport android.content.SyncResult;
20328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
21328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.emailcommon.provider.Policy;
22328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.emailcommon.service.PolicyServiceProxy;
23328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.exchange.Eas;
24328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.exchange.EasResponse;
25328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.exchange.adapter.ProvisionParser;
26328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.exchange.adapter.Serializer;
27328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.exchange.adapter.Tags;
28328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport com.android.mail.utils.LogUtils;
29328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
30328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport org.apache.http.HttpEntity;
31328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
32328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Huimport java.io.IOException;
33328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
34328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu/**
35328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * Implements the EAS Provision protocol.
36328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu *
37328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * Provisioning actually consists of two server interactions:
38328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * 1) Ask the server for the required policies.
39328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * 2) Acknowledge our disposition for enforcing those policies.
40328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu *
41328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * The structure of the requests and response are essentially the same for both, so we use the
42328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * same code and vary slightly based on which one we're doing. Also, provisioning responses can tell
43328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * us to wipe the device, so we need to handle that too.
44328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * TODO: Make it possible to ack separately, possibly by splitting into separate operations.
45328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu * See http://msdn.microsoft.com/en-us/library/ee203567(v=exchg.80).aspx for more details.
46328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu */
47328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hupublic class EasProvision extends EasOperation {
48328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
49110837ebff288a75f9bda067c38e2c46797d99b5Alon Albert    private static final String LOG_TAG = Eas.LOG_TAG;
50328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
51328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** The policy type for versions of EAS prior to 2007. */
52be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu    public static final String EAS_2_POLICY_TYPE = "MS-WAP-Provisioning-XML";
53328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** The policy type for versions of EAS starting with 2007. */
54be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu    public static final String EAS_12_POLICY_TYPE = "MS-EAS-Provisioning-WBXML";
55328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
56328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** The EAS protocol Provision status for "we implement all of the policies" */
57328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final String PROVISION_STATUS_OK = "1";
58328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** The EAS protocol Provision status meaning "we partially implement the policies" */
59328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final String PROVISION_STATUS_PARTIAL = "2";
60328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
61328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** Value for {@link #mPhase} indicating we're performing the initial request. */
62328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final int PHASE_INITIAL = 0;
63328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** Value for {@link #mPhase} indicating we're performing the acknowledgement request. */
64328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final int PHASE_ACKNOWLEDGE = 1;
65328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** Value for {@link #mPhase} indicating we're performing the acknowledgement for a wipe. */
66328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final int PHASE_WIPE = 2;
67328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
68328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /**
69328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * This operation doesn't use public result codes because ultimately the operation answers
70328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * a yes/no question. These result codes are used internally only to communicate from
71328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * {@link #handleResponse}.
72328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     */
73328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
74328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** Result code indicating the server's policy can be fully supported. */
75328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final int RESULT_POLICY_SUPPORTED = 1;
76328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** Result code indicating the server's policy cannot be fully supported. */
77328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final int RESULT_POLICY_UNSUPPORTED = 2;
78328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /** Result code indicating the server sent a remote wipe directive. */
79328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private static final int RESULT_REMOTE_WIPE = 3;
80328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
81328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private Policy mPolicy;
82328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private String mPolicyKey;
83328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private String mStatus;
84328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
85328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /**
86328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * Because this operation supports variants of the request and parsing, and {@link EasOperation}
87328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * has no way to communicate this into {@link #performOperation}, we use this member variable
88328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * to vary how {@link #getRequestEntity} and {@link #handleResponse} work.
89328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     */
90328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private int mPhase;
91328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
92b6ef78e8c4112d47d965b5b274d52fe8885f58faYu Ping Hu    public EasProvision(final EasOperation parentOperation) {
93b6ef78e8c4112d47d965b5b274d52fe8885f58faYu Ping Hu        super(parentOperation);
94328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPolicy = null;
95328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPolicyKey = null;
96328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mStatus = null;
97328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPhase = 0;
98328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
99328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
100328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private int performInitialRequest(final SyncResult syncResult) {
101328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPhase = PHASE_INITIAL;
102328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return performOperation(syncResult);
103328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
104328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
105328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private void performAckRequestForWipe(final SyncResult syncResult) {
106328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPhase = PHASE_WIPE;
107328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        performOperation(syncResult);
108328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
109328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
110328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private int performAckRequest(final SyncResult syncResult, final boolean isPartial) {
111328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPhase = PHASE_ACKNOWLEDGE;
112328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mStatus = isPartial ? PROVISION_STATUS_PARTIAL : PROVISION_STATUS_OK;
113328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return performOperation(syncResult);
114328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
115328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
116328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /**
117328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * Make the provisioning calls to determine if we can handle the required policy.
118b6ef78e8c4112d47d965b5b274d52fe8885f58faYu Ping Hu     * @return The {@link Policy} if we support it, or null otherwise.
119328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     */
120b6ef78e8c4112d47d965b5b274d52fe8885f58faYu Ping Hu    public final Policy test() {
121328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        int result = performInitialRequest(null);
122328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (result == RESULT_POLICY_UNSUPPORTED) {
123328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            // Check if the server will permit partial policies.
124328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            result = performAckRequest(null, true);
125328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
126b6ef78e8c4112d47d965b5b274d52fe8885f58faYu Ping Hu        if (result == RESULT_POLICY_SUPPORTED) {
127be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu            // The server is ok with us not supporting everything, so clear the unsupported ones.
128be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu            mPolicy.mProtocolPoliciesUnsupported = null;
129b6ef78e8c4112d47d965b5b274d52fe8885f58faYu Ping Hu        }
130be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        return (result == RESULT_POLICY_SUPPORTED || result == RESULT_POLICY_UNSUPPORTED)
131be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu                ? mPolicy : null;
132328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
133328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
134328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /**
135328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * Get the required policy from the server and enforce it.
136328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * @param syncResult The {@link SyncResult}, if anym for this operation.
137328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * @param accountId The id for the account for this request.
138328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * @return Whether we succeeded in provisioning this account.
139328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     */
140328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    public final boolean provision(final SyncResult syncResult, final long accountId) {
141328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        final int result = performInitialRequest(syncResult);
142328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
143328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (result < 0) {
144328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return false;
145328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
146328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
147328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (result == RESULT_REMOTE_WIPE) {
148328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            performAckRequestForWipe(syncResult);
149328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            LogUtils.i(LOG_TAG, "Executing remote wipe");
150328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            PolicyServiceProxy.remoteWipe(mContext);
151328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return false;
152328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
153328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
154328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // Apply the policies (that we support) with the temporary key.
155328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        mPolicy.mProtocolPoliciesUnsupported = null;
156328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        PolicyServiceProxy.setAccountPolicy(mContext, accountId, mPolicy, null);
157328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (!PolicyServiceProxy.isActive(mContext, mPolicy)) {
158328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return false;
159328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
160328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
161328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // Acknowledge to the server and make sure all's well.
162328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (performAckRequest(syncResult, result == RESULT_POLICY_UNSUPPORTED) ==
163328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu                RESULT_POLICY_UNSUPPORTED) {
164328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return false;
165328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
166328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
167328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // Write the final policy key to the Account.
168328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        PolicyServiceProxy.setAccountPolicy(mContext, accountId, mPolicy, mPolicyKey);
169be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu
170be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        // For 12.1 and 14.0, after provisioning we need to also send the device information via
171be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        // the Settings command.
172be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        // See the comments for EasSettings for more details.
173be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        final double version = getProtocolVersion();
174be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        if (version == Eas.SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE
175be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu                || version == Eas.SUPPORTED_PROTOCOL_EX2010_DOUBLE) {
176be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu            final EasSettings settingsOperation = new EasSettings(this);
177be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu            if (!settingsOperation.sendDeviceInformation(syncResult)) {
178be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu                // If the Settings command failed, so do we.
179be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu                return false;
180be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu            }
181be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu        }
182be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu
183328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return true;
184328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
185328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
186328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    @Override
187328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    protected String getCommand() {
188328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return "Provision";
189328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
190328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
191328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    @Override
192328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    protected HttpEntity getRequestEntity() throws IOException {
193328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        final Serializer s = new Serializer();
194328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        s.start(Tags.PROVISION_PROVISION);
195328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
196328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // When requesting the policy in 14.1, we also need to send device information.
197328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (mPhase == PHASE_INITIAL &&
198328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu                getProtocolVersion() >= Eas.SUPPORTED_PROTOCOL_EX2010_SP1_DOUBLE) {
199be22ff8b8b3aa6057de165b76da433cabad4dcfdYu Ping Hu            addDeviceInformationToSerlializer(s);
200328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
201328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        s.start(Tags.PROVISION_POLICIES);
202328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        s.start(Tags.PROVISION_POLICY);
203328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        s.data(Tags.PROVISION_POLICY_TYPE, getPolicyType());
204328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
205328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // When acknowledging a policy, we tell the server whether we applied the policy.
206328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (mPhase == PHASE_ACKNOWLEDGE) {
207328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            s.data(Tags.PROVISION_POLICY_KEY, mPolicyKey);
208328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            s.data(Tags.PROVISION_STATUS, mStatus);
209328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
210328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (mPhase == PHASE_WIPE) {
211328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            s.start(Tags.PROVISION_REMOTE_WIPE);
212328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            s.data(Tags.PROVISION_STATUS, PROVISION_STATUS_OK);
213328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            s.end();
214328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
215328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        s.end().end().end().done(); // PROVISION_POLICY, PROVISION_POLICIES, PROVISION_PROVISION
216328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
217328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return makeEntity(s);
218328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
219328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
220328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    @Override
221328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    protected int handleResponse(final EasResponse response, final SyncResult syncResult)
222328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            throws IOException {
223328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        final ProvisionParser pp = new ProvisionParser(mContext, response.getInputStream());
224328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // If this is the response for a remote wipe ack, it doesn't have anything useful in it.
225328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // Just go ahead and return now.
226328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (mPhase == PHASE_WIPE) {
227328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return RESULT_REMOTE_WIPE;
228328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
229328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
230328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (!pp.parse()) {
231328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            throw new IOException("Error while parsing response");
232328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
233328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
234328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // What we care about in the response depends on what phase we're in.
235328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (mPhase == PHASE_INITIAL) {
236328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            if (pp.getRemoteWipe()) {
237328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu                return RESULT_REMOTE_WIPE;
238328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            }
239328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            mPolicy = pp.getPolicy();
240328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            mPolicyKey = pp.getSecuritySyncKey();
241328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
242328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return (pp.hasSupportablePolicySet()
243328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu                    ? RESULT_POLICY_SUPPORTED : RESULT_POLICY_UNSUPPORTED);
244328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
245328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
246328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        if (mPhase == PHASE_ACKNOWLEDGE) {
247328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            mPolicyKey = pp.getSecuritySyncKey();
248328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu            return (mPolicyKey != null ? RESULT_POLICY_SUPPORTED : RESULT_POLICY_UNSUPPORTED);
249328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        }
250328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
251328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // Note: this should be unreachable, but the compiler doesn't know it.
252328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // If we somehow get here, act like we can't do anything.
253328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return RESULT_POLICY_UNSUPPORTED;
254328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
255328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
256328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    @Override
257328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    protected boolean handleProvisionError(final SyncResult syncResult, final long accountId) {
258328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        // If we get a provisioning error while doing provisioning, we should not recurse.
259328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return false;
260328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
261328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu
262328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    /**
263328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     * @return The policy type for this connection.
264328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu     */
265328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    private final String getPolicyType() {
266328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu        return (getProtocolVersion() >= Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) ?
267328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu                EAS_12_POLICY_TYPE : EAS_2_POLICY_TYPE;
268328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu    }
269328ca0d959f7e729e96f19538c8f3af8fe782a09Yu Ping Hu}
270