1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.example.android.samplesync.syncadapter; 18 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.accounts.AuthenticatorException; 22 import android.accounts.OperationCanceledException; 23 import android.content.AbstractThreadedSyncAdapter; 24 import android.content.ContentProviderClient; 25 import android.content.Context; 26 import android.content.SyncResult; 27 import android.os.Bundle; 28 import android.util.Log; 29 30 import com.example.android.samplesync.Constants; 31 import com.example.android.samplesync.client.NetworkUtilities; 32 import com.example.android.samplesync.client.User; 33 import com.example.android.samplesync.client.User.Status; 34 import com.example.android.samplesync.platform.ContactManager; 35 36 import org.apache.http.ParseException; 37 import org.apache.http.auth.AuthenticationException; 38 import org.json.JSONException; 39 40 import java.io.IOException; 41 import java.util.Date; 42 import java.util.List; 43 44 /** 45 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the 46 * platform ContactOperations provider. 47 */ 48 public class SyncAdapter extends AbstractThreadedSyncAdapter { 49 private static final String TAG = "SyncAdapter"; 50 51 private final AccountManager mAccountManager; 52 private final Context mContext; 53 54 private Date mLastUpdated; 55 SyncAdapter(Context context, boolean autoInitialize)56 public SyncAdapter(Context context, boolean autoInitialize) { 57 super(context, autoInitialize); 58 mContext = context; 59 mAccountManager = AccountManager.get(context); 60 } 61 62 @Override onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)63 public void onPerformSync(Account account, Bundle extras, String authority, 64 ContentProviderClient provider, SyncResult syncResult) { 65 List<User> users; 66 List<Status> statuses; 67 String authtoken = null; 68 try { 69 // use the account manager to request the credentials 70 authtoken = 71 mAccountManager.blockingGetAuthToken(account, 72 Constants.AUTHTOKEN_TYPE, true /* notifyAuthFailure */); 73 // fetch updates from the sample service over the cloud 74 users = 75 NetworkUtilities.fetchFriendUpdates(account, authtoken, 76 mLastUpdated); 77 // update the last synced date. 78 mLastUpdated = new Date(); 79 // update platform contacts. 80 Log.d(TAG, "Calling contactManager's sync contacts"); 81 ContactManager.syncContacts(mContext, account.name, users); 82 // fetch and update status messages for all the synced users. 83 statuses = NetworkUtilities.fetchFriendStatuses(account, authtoken); 84 ContactManager.insertStatuses(mContext, account.name, statuses); 85 } catch (final AuthenticatorException e) { 86 syncResult.stats.numParseExceptions++; 87 Log.e(TAG, "AuthenticatorException", e); 88 } catch (final OperationCanceledException e) { 89 Log.e(TAG, "OperationCanceledExcetpion", e); 90 } catch (final IOException e) { 91 Log.e(TAG, "IOException", e); 92 syncResult.stats.numIoExceptions++; 93 } catch (final AuthenticationException e) { 94 mAccountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, 95 authtoken); 96 syncResult.stats.numAuthExceptions++; 97 Log.e(TAG, "AuthenticationException", e); 98 } catch (final ParseException e) { 99 syncResult.stats.numParseExceptions++; 100 Log.e(TAG, "ParseException", e); 101 } catch (final JSONException e) { 102 syncResult.stats.numParseExceptions++; 103 Log.e(TAG, "JSONException", e); 104 } 105 } 106 } 107