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