Android Open Source - Facebook-Contact-Sync Sync Adapter






From Project

Back to project page Facebook-Contact-Sync.

License

The source code is released under:

GNU General Public License

If you think the Android project Facebook-Contact-Sync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012 Danut Chereches/*  w  w w . j  a va 2  s  . c o  m*/
 *
 * Contact: Danut Chereches <admin@weednet.ro>
 *
 * This file is part of Facebook Contact Sync.
 * 
 * Facebook Contact Sync 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.
 * 
 * This program 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 Facebook Contact Sync.
 * If not, see <http://www.gnu.org/licenses/>.
 *
 */
package ro.weednet.contactssync.syncadapter;

import ro.weednet.ContactsSync;
import ro.weednet.contactssync.Constants;
import ro.weednet.contactssync.R;
import ro.weednet.contactssync.activities.Preferences;
import ro.weednet.contactssync.client.NetworkUtilities;
import ro.weednet.contactssync.client.RawContact;
import ro.weednet.contactssync.platform.ContactManager;

import org.apache.http.ParseException;
import org.apache.http.auth.AuthenticationException;
import org.json.JSONException;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;

import java.io.IOException;
import java.util.List;

public class SyncAdapter extends AbstractThreadedSyncAdapter {
  private static final String TAG = "SyncAdapter";
  private static final boolean NOTIFY_AUTH_FAILURE = true;
  
  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) {
    String authtoken = null;
    try {
    //  ContactManager.setAccountContactsVisibility(getContext(), account, true);
      ContactsSync app = ContactsSync.getInstance();
      
      if (app.getSyncWifiOnly() && !app.wifiConnected()) {
        throw new OperationCanceledException("not on wifi");
      }
      
      authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE);
      if (authtoken == null) {
        throw new AuthenticationException();
      }
      
      final long groupId = ContactManager.ensureGroupExists(mContext, account);
      final Uri rawContactsUri = RawContacts.CONTENT_URI.buildUpon()
        .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
        .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type)
        .build();
      
      List<RawContact> localContacts = ContactManager.getLocalContacts(mContext, rawContactsUri);
      
      if (app.getFullSync()) {
        ContactManager.deleteContacts(mContext, localContacts);
        localContacts.clear();
        app.clearFullSync();
      }
      
      NetworkUtilities nu = new NetworkUtilities(authtoken, mContext);
      List<RawContact> rawContacts = nu.getContacts(account);
      
      List<RawContact> syncedContacts = ContactManager.updateContacts(
        mContext, account, rawContacts, groupId,
        app.getJoinById(), app.getSyncAllContacts());
      
      ContactManager.deleteMissingContacts(mContext, localContacts, syncedContacts);
      
      if (app.getJoinById()) {
        ContactManager.addJoins(mContext, account, rawContacts);
      }
      
      localContacts = ContactManager.getLocalContacts(mContext, rawContactsUri);
      
      if (app.getSyncType() == ContactsSync.SyncType.HARD) {
        ContactManager.updateContactDetails(mContext, localContacts, nu);
      } else if (app.getSyncType() == ContactsSync.SyncType.MEDIUM) {
        List<RawContact> starredContacts = ContactManager.getStarredContacts(mContext, rawContactsUri);
        ContactManager.updateContactDetails(mContext, starredContacts, nu);
      }
      
      NotificationManager mNotificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.cancelAll();
      
    } catch (final AuthenticatorException e) {
      Log.e(TAG, "AuthenticatorException", e);
      syncResult.stats.numParseExceptions++;
      showNotificationError("Error connecting to facebook");
    } catch (final OperationCanceledException e) {
      Log.e(TAG, "OperationCanceledExcetpion", e);
    } catch (final IOException e) {
      Log.e(TAG, "IOException", e);
      syncResult.stats.numIoExceptions++;
      showNotificationError("Error connecting to facebook");
    } catch (final AuthenticationException e) {
      Log.e(TAG, "AuthenticationException", e);
      syncResult.stats.numAuthExceptions++;
      if (authtoken != null) {
        mAccountManager.invalidateAuthToken(account.type, authtoken);
      }
      try {
        authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE);
      } catch (OperationCanceledException e1) {
        Log.e(TAG, "OperationCanceledExcetpion", e);
      } catch (AuthenticatorException e1) {
        Log.e(TAG, "AuthenticatorException", e);
        syncResult.stats.numParseExceptions++;
      } catch (IOException e1) {
        Log.e(TAG, "IOException", e);
        syncResult.stats.numIoExceptions++;
      }
    } catch (final ParseException e) {
      Log.e(TAG, "ParseException", e);
      syncResult.stats.numParseExceptions++;
      showNotificationError("Error parsing the information from facebook");
    } catch (final JSONException e) {
      Log.e(TAG, "JSONException", e);
      syncResult.stats.numParseExceptions++;
      showNotificationError("Error parsing the information from facebook");
    } catch (final Exception e) {
      Log.e(TAG, "Unknown exception", e);
    }
  }
  
  public void showNotificationError(String message) {
    showNotificationMessage(message, message, "Select to test connection or change settings");
  }
  
  @SuppressWarnings("deprecation")
  public void showNotificationMessage(String tickerText, String title, String desc) {
    if (!ContactsSync.getInstance().getShowNotifications()) {
      return;
    }
    
    NotificationManager mNotificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
    
    Notification notification = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());
    Context context = ContactsSync.getInstance().getApplicationContext();
    Intent notificationIntent = new Intent(context, Preferences.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    
    notification.setLatestEventInfo(context, title, desc, contentIntent);
    mNotificationManager.notify(1, notification);
  }
}




Java Source Code List

ro.weednet.ContactsSync.java
ro.weednet.contactssync.Constants.java
ro.weednet.contactssync.activities.Preferences.java
ro.weednet.contactssync.activities.Profile.java
ro.weednet.contactssync.activities.TestFacebookApi.java
ro.weednet.contactssync.authenticator.AuthenticationService.java
ro.weednet.contactssync.authenticator.AuthenticatorActivity.java
ro.weednet.contactssync.authenticator.Authenticator.java
ro.weednet.contactssync.client.ContactPhoto.java
ro.weednet.contactssync.client.ContactStreamItem.java
ro.weednet.contactssync.client.NetworkUtilities.java
ro.weednet.contactssync.client.RawContact.java
ro.weednet.contactssync.iap.Base64DecoderException.java
ro.weednet.contactssync.iap.Base64.java
ro.weednet.contactssync.iap.IabException.java
ro.weednet.contactssync.iap.IabHelper.java
ro.weednet.contactssync.iap.IabResult.java
ro.weednet.contactssync.iap.Inventory.java
ro.weednet.contactssync.iap.Purchase.java
ro.weednet.contactssync.iap.Security.java
ro.weednet.contactssync.iap.SkuDetails.java
ro.weednet.contactssync.notifier.NotifierService.java
ro.weednet.contactssync.platform.BatchOperation.java
ro.weednet.contactssync.platform.ContactManager.java
ro.weednet.contactssync.platform.ContactOperations.java
ro.weednet.contactssync.platform.SyncAdapterColumns.java
ro.weednet.contactssync.preferences.GlobalFragment.java
ro.weednet.contactssync.syncadapter.SyncAdapter.java
ro.weednet.contactssync.syncadapter.SyncService.java