1/* Copyright (C) 2008-2009 Marc Blank
2 * Licensed to The Android Open Source Project.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.exchange.adapter;
18
19import com.android.exchange.EasSyncService;
20import com.android.exchange.IllegalHeartbeatException;
21import com.android.exchange.StaleFolderListException;
22
23import java.io.IOException;
24import java.io.InputStream;
25import java.util.ArrayList;
26
27/**
28 * Parse the result of a Ping command.
29 *
30 * If there are folders with changes, add the serverId of those folders to the syncList array.
31 * If the folder list needs to be reloaded, throw a StaleFolderListException, which will be caught
32 * by the sync server, which will sync the updated folder list.
33 */
34public class PingParser extends Parser {
35    private ArrayList<String> syncList = new ArrayList<String>();
36    private EasSyncService mService;
37    private int mSyncStatus = 0;
38
39    public ArrayList<String> getSyncList() {
40        return syncList;
41    }
42
43    public int getSyncStatus() {
44        return mSyncStatus;
45    }
46
47    public PingParser(InputStream in, EasSyncService service) throws IOException {
48        super(in);
49        mService = service;
50    }
51
52    public void parsePingFolders(ArrayList<String> syncList) throws IOException {
53        while (nextTag(Tags.PING_FOLDERS) != END) {
54            if (tag == Tags.PING_FOLDER) {
55                // Here we'll keep track of which mailboxes need syncing
56                String serverId = getValue();
57                syncList.add(serverId);
58                mService.userLog("Changes found in: ", serverId);
59            } else {
60                skipTag();
61            }
62        }
63    }
64
65    @Override
66    public boolean parse() throws IOException, StaleFolderListException, IllegalHeartbeatException {
67        boolean res = false;
68        if (nextTag(START_DOCUMENT) != Tags.PING_PING) {
69            throw new IOException();
70        }
71        while (nextTag(START_DOCUMENT) != END_DOCUMENT) {
72            if (tag == Tags.PING_STATUS) {
73                int status = getValueInt();
74                mSyncStatus = status;
75                mService.userLog("Ping completed, status = ", status);
76                if (status == 2) {
77                    res = true;
78                } else if (status == 7 || status == 4) {
79                    // Status of 7 or 4 indicate a stale folder list
80                    throw new StaleFolderListException();
81                } else if (status == 5) {
82                    // Status 5 means our heartbeat is beyond allowable limits
83                    // In this case, there will be a heartbeat interval set
84                }
85            } else if (tag == Tags.PING_FOLDERS) {
86                parsePingFolders(syncList);
87            } else if (tag == Tags.PING_HEARTBEAT_INTERVAL) {
88                // Throw an exception, saving away the legal heartbeat interval specified
89                throw new IllegalHeartbeatException(getValueInt());
90            } else {
91                skipTag();
92            }
93        }
94        return res;
95    }
96}
97
98