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.adapter;
19
20import com.android.email.Email;
21import com.android.email.provider.EmailContent.Account;
22import com.android.email.provider.EmailContent.Mailbox;
23import com.android.exchange.EasSyncService;
24
25import android.content.Context;
26
27import java.io.IOException;
28import java.io.InputStream;
29
30/**
31 * Parent class of all sync adapters (EasMailbox, EasCalendar, and EasContacts)
32 *
33 */
34public abstract class AbstractSyncAdapter {
35
36    public static final int SECONDS = 1000;
37    public static final int MINUTES = SECONDS*60;
38    public static final int HOURS = MINUTES*60;
39    public static final int DAYS = HOURS*24;
40    public static final int WEEKS = DAYS*7;
41
42    public Mailbox mMailbox;
43    public EasSyncService mService;
44    public Context mContext;
45    public Account mAccount;
46    public final android.accounts.Account mAccountManagerAccount;
47
48    // Create the data for local changes that need to be sent up to the server
49    public abstract boolean sendLocalChanges(Serializer s)
50        throws IOException;
51    // Parse incoming data from the EAS server, creating, modifying, and deleting objects as
52    // required through the EmailProvider
53    public abstract boolean parse(InputStream is)
54        throws IOException;
55    // The name used to specify the collection type of the target (Email, Calendar, or Contacts)
56    public abstract String getCollectionName();
57    public abstract void cleanup();
58    public abstract boolean isSyncable();
59
60    public boolean isLooping() {
61        return false;
62    }
63
64    public AbstractSyncAdapter(Mailbox mailbox, EasSyncService service) {
65        mMailbox = mailbox;
66        mService = service;
67        mContext = service.mContext;
68        mAccount = service.mAccount;
69        mAccountManagerAccount = new android.accounts.Account(mAccount.mEmailAddress,
70                Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
71    }
72
73    public void userLog(String ...strings) {
74        mService.userLog(strings);
75    }
76
77    public void incrementChangeCount() {
78        mService.mChangeCount++;
79    }
80
81    /**
82     * Returns the current SyncKey; override if the SyncKey is stored elsewhere (as for Contacts)
83     * @return the current SyncKey for the Mailbox
84     * @throws IOException
85     */
86    public String getSyncKey() throws IOException {
87        if (mMailbox.mSyncKey == null) {
88            userLog("Reset SyncKey to 0");
89            mMailbox.mSyncKey = "0";
90        }
91        return mMailbox.mSyncKey;
92    }
93
94    public void setSyncKey(String syncKey, boolean inCommands) throws IOException {
95        mMailbox.mSyncKey = syncKey;
96    }
97}
98
99