示例#1
0
  public static Contact getMergedContact(Context c, long contactID, Account account) {

    Uri ContactUri =
        RawContacts.CONTENT_URI
            .buildUpon()
            .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
            .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type)
            .build();

    Cursor cursor =
        c.getContentResolver()
            .query(
                ContactUri,
                new String[] {BaseColumns._ID, RawContacts.DISPLAY_NAME_PRIMARY},
                RawContacts.CONTACT_ID + " = '" + contactID + "'",
                null,
                null);
    if (cursor.getCount() > 0) {
      cursor.moveToFirst();
      Contact contact = new Contact();
      contact.ID = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
      contact.name = cursor.getString(cursor.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY));
      cursor.close();
      return contact;
    }
    cursor.close();
    return null;
  }
  private static void moveContactsData(
      final ContentResolver resolver,
      final String name,
      final String oldType,
      final String newType) {
    final Uri oldContacts =
        RawContacts.CONTENT_URI
            .buildUpon()
            .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(RawContacts.ACCOUNT_NAME, name)
            .appendQueryParameter(RawContacts.ACCOUNT_TYPE, oldType)
            .build();

    // Update this calendar to have the new account type.
    final ContentValues values = new ContentValues();
    values.put(CalendarContract.Calendars.ACCOUNT_TYPE, newType);
    resolver.update(oldContacts, values, null, null);
  }
  private static void performSync(
      Context context,
      Account account,
      Bundle extras,
      String authority,
      ContentProviderClient provider,
      SyncResult syncResult)
      throws OperationCanceledException {
    HashMap<String, Long> localContacts = new HashMap<String, Long>();
    mContentResolver = context.getContentResolver();

    // Load the local contacts
    Uri rawContactUri =
        RawContacts.CONTENT_URI
            .buildUpon()
            .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
            .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type)
            .build();
    Cursor c1 =
        mContentResolver.query(
            rawContactUri, new String[] {BaseColumns._ID, RawContacts.SYNC1}, null, null, null);
    while (c1.moveToNext()) {
      localContacts.put(c1.getString(1), c1.getLong(0));
    }

    final String jsonStr = Utils.getJSON(context, "http://api.juick.com/users/friends");
    if (jsonStr != null && jsonStr.length() > 4) {
      try {
        JSONArray json = new JSONArray(jsonStr);
        int cnt = json.length();
        for (int i = 0; i < cnt; i++) {
          JuickUser user = JuickUser.parseJSON(json.getJSONObject(i));
          if (!localContacts.containsKey(user.UName)) {
            addContact(context, account, user);
          }
        }
      } catch (JSONException e) {
      }
    }
  }