Java tutorial
/* * Copyright 2010-2016 Frans-Willem Hardijzer * * This file is part of BitonSync. * * BitonSync is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * BitonSync is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with BitonSync. If not, see <http://www.gnu.org/licenses/>. */ package nl.hardijzer.bitonsync.syncadapter; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.Context; import android.content.SyncResult; import android.os.Bundle; import android.util.Log; import nl.hardijzer.bitonsync.Constants; import nl.hardijzer.bitonsync.client.NetworkUtilities; import nl.hardijzer.bitonsync.client.User; import nl.hardijzer.bitonsync.platform.ContactManager; import org.apache.http.ParseException; import org.apache.http.auth.AuthenticationException; import org.json.JSONException; import java.io.IOException; import java.util.Date; import java.util.List; /** * SyncAdapter implementation for syncing sample SyncAdapter contacts to the * platform ContactOperations provider. */ public class SyncAdapter extends AbstractThreadedSyncAdapter { private static final String TAG = "SyncAdapter"; private final AccountManager mAccountManager; private final Context mContext; public SyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); mContext = context; mAccountManager = AccountManager.get(context); } @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { List<User> users; String authtoken = null; try { // use the account manager to request the credentials authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, true /* notifyAuthFailure */); // fetch updates from the sample service over the cloud users = NetworkUtilities.fetchFriendUpdates(account, authtoken); // update platform contacts. Log.d(TAG, "Calling contactManager's sync contacts"); ContactManager.syncContacts(mContext, account.name, users); } catch (final AuthenticatorException e) { syncResult.stats.numParseExceptions++; Log.e(TAG, "AuthenticatorException", e); } catch (final OperationCanceledException e) { Log.e(TAG, "OperationCanceledExcetpion", e); } catch (final IOException e) { Log.e(TAG, "IOException", e); syncResult.stats.numIoExceptions++; } catch (final AuthenticationException e) { mAccountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authtoken); syncResult.stats.numAuthExceptions++; Log.e(TAG, "AuthenticationException", e); } catch (final ParseException e) { syncResult.stats.numParseExceptions++; Log.e(TAG, "ParseException", e); } } }