Home | History | Annotate | Download | only in adapter
      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 
     18 package com.android.exchange.adapter;
     19 
     20 import com.android.emailcommon.provider.Account;
     21 import com.android.emailcommon.provider.Mailbox;
     22 import com.android.exchange.CommandStatusException;
     23 import com.android.exchange.Eas;
     24 import com.android.exchange.EasSyncService;
     25 
     26 import android.content.ContentResolver;
     27 import android.content.Context;
     28 
     29 import java.io.IOException;
     30 import java.io.InputStream;
     31 
     32 /**
     33  * Parent class of all sync adapters (EasMailbox, EasCalendar, and EasContacts)
     34  *
     35  */
     36 public abstract class AbstractSyncAdapter {
     37 
     38     public static final int SECONDS = 1000;
     39     public static final int MINUTES = SECONDS*60;
     40     public static final int HOURS = MINUTES*60;
     41     public static final int DAYS = HOURS*24;
     42     public static final int WEEKS = DAYS*7;
     43 
     44     protected static final String PIM_WINDOW_SIZE = "4";
     45 
     46     public Mailbox mMailbox;
     47     public EasSyncService mService;
     48     public Context mContext;
     49     public Account mAccount;
     50     public final ContentResolver mContentResolver;
     51     public final android.accounts.Account mAccountManagerAccount;
     52 
     53     // Create the data for local changes that need to be sent up to the server
     54     public abstract boolean sendLocalChanges(Serializer s) throws IOException;
     55     // Parse incoming data from the EAS server, creating, modifying, and deleting objects as
     56     // required through the EmailProvider
     57     public abstract boolean parse(InputStream is) throws IOException, CommandStatusException;
     58     // The name used to specify the collection type of the target (Email, Calendar, or Contacts)
     59     public abstract String getCollectionName();
     60     public abstract void cleanup();
     61     public abstract boolean isSyncable();
     62     // Add sync options (filter, body type - html vs plain, and truncation)
     63     public abstract void sendSyncOptions(Double protocolVersion, Serializer s) throws IOException;
     64     /**
     65      * Delete all records of this class in this account
     66      */
     67     public abstract void wipe();
     68 
     69     public boolean isLooping() {
     70         return false;
     71     }
     72 
     73     public AbstractSyncAdapter(EasSyncService service) {
     74         mService = service;
     75         mMailbox = service.mMailbox;
     76         mContext = service.mContext;
     77         mAccount = service.mAccount;
     78         mAccountManagerAccount = new android.accounts.Account(mAccount.mEmailAddress,
     79                 Eas.EXCHANGE_ACCOUNT_MANAGER_TYPE);
     80         mContentResolver = mContext.getContentResolver();
     81     }
     82 
     83     public void userLog(String ...strings) {
     84         mService.userLog(strings);
     85     }
     86 
     87     public void incrementChangeCount() {
     88         mService.mChangeCount++;
     89     }
     90 
     91     /**
     92      * Set sync options common to PIM's (contacts and calendar)
     93      * @param protocolVersion the protocol version under which we're syncing
     94      * @param the filter to use (or null)
     95      * @param s the Serializer
     96      * @throws IOException
     97      */
     98     protected void setPimSyncOptions(Double protocolVersion, String filter, Serializer s)
     99             throws IOException {
    100         s.tag(Tags.SYNC_DELETES_AS_MOVES);
    101         s.tag(Tags.SYNC_GET_CHANGES);
    102         s.data(Tags.SYNC_WINDOW_SIZE, PIM_WINDOW_SIZE);
    103         s.start(Tags.SYNC_OPTIONS);
    104         // Set the filter (lookback), if provided
    105         if (filter != null) {
    106             s.data(Tags.SYNC_FILTER_TYPE, filter);
    107         }
    108         // Set the truncation amount and body type
    109         if (protocolVersion >= Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) {
    110             s.start(Tags.BASE_BODY_PREFERENCE);
    111             // Plain text
    112             s.data(Tags.BASE_TYPE, Eas.BODY_PREFERENCE_TEXT);
    113             s.data(Tags.BASE_TRUNCATION_SIZE, Eas.EAS12_TRUNCATION_SIZE);
    114             s.end();
    115         } else {
    116             s.data(Tags.SYNC_TRUNCATION, Eas.EAS2_5_TRUNCATION_SIZE);
    117         }
    118         s.end();
    119     }
    120 
    121     /**
    122      * Returns the current SyncKey; override if the SyncKey is stored elsewhere (as for Contacts)
    123      * @return the current SyncKey for the Mailbox
    124      * @throws IOException
    125      */
    126     public String getSyncKey() throws IOException {
    127         if (mMailbox.mSyncKey == null) {
    128             userLog("Reset SyncKey to 0");
    129             mMailbox.mSyncKey = "0";
    130         }
    131         return mMailbox.mSyncKey;
    132     }
    133 
    134     public void setSyncKey(String syncKey, boolean inCommands) throws IOException {
    135         mMailbox.mSyncKey = syncKey;
    136     }
    137 }
    138 
    139