Guest User

Untitled

a guest
May 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. diff --git a/main/src/com/joelapenna/foursquared/ContactsSyncAdapter.java b/main/src/com/joelapenna/foursquared/ContactsSyncAdapter.java
  2. index 098e9d4..943665a 100644
  3. --- a/main/src/com/joelapenna/foursquared/ContactsSyncAdapter.java
  4. +++ b/main/src/com/joelapenna/foursquared/ContactsSyncAdapter.java
  5. @@ -14,12 +14,17 @@ import android.accounts.AuthenticatorException;
  6. import android.accounts.OperationCanceledException;
  7. import android.content.AbstractThreadedSyncAdapter;
  8. import android.content.ContentProviderClient;
  9. +import android.content.ContentProviderOperation;
  10. +import android.content.ContentResolver;
  11. import android.content.Context;
  12. import android.content.SyncResult;
  13. import android.os.Bundle;
  14. +import android.provider.ContactsContract;
  15. +import android.provider.ContactsContract.RawContacts;
  16. import android.util.Log;
  17.  
  18. import java.io.IOException;
  19. +import java.util.ArrayList;
  20.  
  21. public class ContactsSyncAdapter extends AbstractThreadedSyncAdapter {
  22.  
  23. @@ -27,11 +32,13 @@ public class ContactsSyncAdapter extends AbstractThreadedSyncAdapter {
  24.  
  25. final private Foursquared mFoursquared;
  26. final private AccountManager mAccountManager;
  27. + final private ContentResolver mContentResolver;
  28.  
  29. public ContactsSyncAdapter(Foursquared foursquared, Context context, boolean autoInitialize) {
  30. super(context, autoInitialize);
  31. mFoursquared = foursquared;
  32. mAccountManager = AccountManager.get(context);
  33. + mContentResolver = context.getContentResolver();
  34. }
  35.  
  36. @Override
  37. @@ -50,8 +57,10 @@ public class ContactsSyncAdapter extends AbstractThreadedSyncAdapter {
  38. Log.e(TAG, "ioexception while getting auth token", e);
  39. }
  40.  
  41. + final Group<User> friends = new Group<User>();
  42. +
  43. try {
  44. - Group<User> friends = mFoursquared.getFoursquare().friends(mFoursquared.getUserId(), LocationUtils.createFoursquareLocation(mFoursquared.getLastKnownLocation()));
  45. + friends.addAll(mFoursquared.getFoursquare().friends(mFoursquared.getUserId(), LocationUtils.createFoursquareLocation(mFoursquared.getLastKnownLocation())));
  46. } catch (FoursquareError e) {
  47. Log.e(TAG, "error fetching friends", e);
  48. } catch (FoursquareException e) {
  49. @@ -59,6 +68,47 @@ public class ContactsSyncAdapter extends AbstractThreadedSyncAdapter {
  50. } catch (IOException e) {
  51. Log.e(TAG, "ioexception fetching friends", e);
  52. }
  53. +
  54. + Log.i(TAG, "got " + friends.size() + " friends from server");
  55. +
  56. + for ( User friend : friends ) {
  57. + Log.i(TAG, "adding friend " + friend.getId() + " (" + friend.getFirstname() + " " + friend.getLastname() + ")");
  58. + addContact(account, friend);
  59. + }
  60. +
  61. + }
  62. +
  63. + private void addContact(Account account, User friend) {
  64. + ArrayList<ContentProviderOperation> opList = new ArrayList<ContentProviderOperation>();
  65. +
  66. + ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
  67. + builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
  68. + builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
  69. + builder.withValue(RawContacts.SYNC1, friend.getId());
  70. + opList.add(builder.build());
  71. +
  72. + builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
  73. + builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
  74. + builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
  75. + builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, friend.getFirstname()+" "+friend.getLastname());
  76. + opList.add(builder.build());
  77. +
  78. + // create a Data record with custom type to point at Foursquare profile
  79. + builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
  80. + builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
  81. + builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/com.joelapenna.foursquared.profile");
  82. + builder.withValue(ContactsContract.Data.DATA1, friend.getId());
  83. + builder.withValue(ContactsContract.Data.DATA2, "Foursquare Profile");
  84. + builder.withValue(ContactsContract.Data.DATA3, "View profile");
  85. + opList.add(builder.build());
  86. +
  87. + try {
  88. + mContentResolver.applyBatch(ContactsContract.AUTHORITY, opList);
  89. + } catch (Exception e) {
  90. + Log.e(TAG, "Something went wrong during creation! " + e);
  91. + e.printStackTrace();
  92. + }
  93. +
  94. }
  95.  
  96. }
Add Comment
Please, Sign In to add comment