1package com.android.exchange.eas;
2
3import android.content.Context;
4
5import com.android.emailcommon.provider.Account;
6import com.android.emailcommon.provider.Mailbox;
7import com.android.exchange.Eas;
8import com.android.exchange.adapter.AbstractSyncParser;
9import com.android.exchange.adapter.Serializer;
10import com.android.exchange.adapter.Tags;
11
12import java.io.IOException;
13import java.io.InputStream;
14
15/**
16 * Abstract base class that handles the details of syncing a specific collection type.
17 * These details include:
18 * - Forming the request options. Contacts, Calendar, and Mail set this up differently.
19 * - Getting the appropriate parser for this collection type.
20 */
21public abstract class EasSyncCollectionTypeBase {
22
23    public static final int MAX_WINDOW_SIZE = 512;
24
25    /**
26     * Get the flag for traffic bookkeeping for this sync type.
27     * @return The appropriate value from {@link com.android.emailcommon.TrafficFlags} for this
28     *         sync.
29     */
30    public abstract int getTrafficFlag();
31
32    /**
33     * Write the contents of a Collection node in an EAS sync request appropriate for our mailbox.
34     * See http://msdn.microsoft.com/en-us/library/gg650891(v=exchg.80).aspx for documentation on
35     * the contents of this sync request element.
36     * @param context
37     * @param s The {@link Serializer} for the current request. This should be within a
38     *          {@link com.android.exchange.adapter.Tags#SYNC_COLLECTION} element.
39     * @param protocolVersion
40     * @param account
41     * @param mailbox
42     * @param isInitialSync
43     * @param numWindows
44     * @throws IOException
45     */
46    public abstract void setSyncOptions(final Context context, final Serializer s,
47            final double protocolVersion, final Account account, final Mailbox mailbox,
48            final boolean isInitialSync, final int numWindows) throws IOException;
49
50    /**
51     * Create a parser for the current response data, appropriate for this collection type.
52     * @param context
53     * @param account
54     * @param mailbox
55     * @param is The {@link InputStream} for the server response we're processing.
56     * @return An appropriate parser for this input.
57     * @throws IOException
58     */
59    public abstract AbstractSyncParser getParser(final Context context, final Account account,
60            final Mailbox mailbox, final InputStream is) throws IOException;
61
62    /**
63     * After every successful sync iteration, this function gets called to cleanup any state to
64     * match the sync result (e.g., to clean up an external ContentProvider for PIM data).
65     * @param context
66     * @param account
67     */
68    public void cleanup(final Context context, final Account account) {}
69
70    /**
71     * Shared non-initial sync options for PIM (contacts & calendar) objects.
72     *
73     * @param s The {@link com.android.exchange.adapter.Serializer} for this sync request.
74     * @param filter The lookback to use, or null if no lookback is desired.
75     * @param protocolVersion The EAS protocol version for this request, as a double.
76     * @param windowSize
77     * @throws IOException
78     */
79    protected static void setPimSyncOptions(final Serializer s, final String filter,
80            final double protocolVersion, int windowSize) throws IOException {
81        s.tag(Tags.SYNC_DELETES_AS_MOVES);
82        s.tag(Tags.SYNC_GET_CHANGES);
83        s.data(Tags.SYNC_WINDOW_SIZE, String.valueOf(windowSize));
84        s.start(Tags.SYNC_OPTIONS);
85        // Set the filter (lookback), if provided
86        if (filter != null) {
87            s.data(Tags.SYNC_FILTER_TYPE, filter);
88        }
89        // Set the truncation amount and body type
90        if (protocolVersion >= Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) {
91            s.start(Tags.BASE_BODY_PREFERENCE);
92            // Plain text
93            s.data(Tags.BASE_TYPE, Eas.BODY_PREFERENCE_TEXT);
94            s.data(Tags.BASE_TRUNCATION_SIZE, Eas.EAS12_TRUNCATION_SIZE);
95            s.end();
96        } else {
97            s.data(Tags.SYNC_TRUNCATION, Eas.EAS2_5_TRUNCATION_SIZE);
98        }
99        s.end();
100    }
101}
102