Example usage for android.database CursorJoiner CursorJoiner

List of usage examples for android.database CursorJoiner CursorJoiner

Introduction

In this page you can find the example usage for android.database CursorJoiner CursorJoiner.

Prototype

public CursorJoiner(Cursor cursorLeft, String[] columnNamesLeft, Cursor cursorRight,
        String[] columnNamesRight) 

Source Link

Document

Initializes the CursorJoiner and resets the cursors to the first row.

Usage

From source file:net.sf.diningout.content.SyncAdapter.java

/**
 * Insert new system contacts, delete orphaned app contacts, and synchronise any changes to
 * existing./*from  w  ww. j a  va 2 s  .  c o  m*/
 */
private void refreshContacts(Context context, ContentProviderClient cp) throws RemoteException {
    /* get system contacts */
    String[] proj = { Email.ADDRESS, ContactsContract.Contacts.LOOKUP_KEY, RawContacts.CONTACT_ID,
            ContactsContract.Contacts.DISPLAY_NAME };
    String sel = Email.IN_VISIBLE_GROUP + " = 1 AND " + Email.ADDRESS + " <> ?";
    String[] args = { Accounts.selected().name };
    EasyCursor sys = new EasyCursor(cr().query(Email.CONTENT_URI, proj, sel, args, Email.ADDRESS));
    /* get app contacts */
    proj = new String[] { Contacts.EMAIL, Contacts.ANDROID_LOOKUP_KEY, Contacts.ANDROID_ID, Contacts.NAME, _ID,
            Contacts.FOLLOWING, Contacts.STATUS_ID };
    sel = Contacts.EMAIL + " IS NOT NULL";
    EasyCursor app = new EasyCursor(cp.query(CONTACTS_URI, proj, sel, null, Contacts.EMAIL));
    /* compare and sync */
    ContentValues vals = new ContentValues();
    for (CursorJoiner.Result result : new CursorJoiner(sys, new String[] { Email.ADDRESS }, app,
            new String[] { Contacts.EMAIL })) {
        switch (result) {
        case LEFT: // new system contact, insert into app contacts
            String email = sys.getString(Email.ADDRESS);
            String hash = BaseEncoding.base64()
                    .encode(Hashing.sha512().hashString(email.toLowerCase(ENGLISH), UTF_8).asBytes());
            long id = Contacts.idForHash(hash); // do we have this contact and not know it?
            /* insert or update values */
            vals.put(Contacts.ANDROID_LOOKUP_KEY, sys.getString(ContactsContract.Contacts.LOOKUP_KEY));
            vals.put(Contacts.ANDROID_ID, sys.getLong(RawContacts.CONTACT_ID));
            String name = sys.getString(ContactsContract.Contacts.DISPLAY_NAME);
            vals.put(Contacts.NAME, name);
            vals.put(Contacts.NORMALISED_NAME, SQLite.normalise(name));
            vals.put(Contacts.EMAIL, email);
            if (id <= 0) {
                vals.put(Contacts.EMAIL_HASH, hash);
                vals.put(Contacts.COLOR, Contacts.defaultColor());
                id = ContentUris.parseId(cp.insert(CONTACTS_URI, vals));
            } else {
                cp.update(ContentUris.withAppendedId(CONTACTS_URI, id), vals, null, null);
            }
            if (id > 0) {
                context.startService(new Intent(context, FriendColorService.class)
                        .putExtra(FriendColorService.EXTRA_ID, id));
            }
            break;
        case RIGHT: // orphaned app contact, delete unless user is following
            if (app.getInt(Contacts.FOLLOWING) == 0 && app.getInt(Contacts.STATUS_ID) == ACTIVE.id) {
                vals.put(Contacts.STATUS_ID, DELETED.id);
                vals.put(Contacts.DIRTY, 1);
                cp.update(Uris.appendId(CONTACTS_URI, app), vals, null, null);
            }
            break;
        case BOTH: // matching contacts, update details in app if needed
            String s = sys.getString(ContactsContract.Contacts.LOOKUP_KEY);
            if (!s.equals(app.getString(Contacts.ANDROID_LOOKUP_KEY))) {
                vals.put(Contacts.ANDROID_LOOKUP_KEY, s);
            }
            long l = sys.getLong(RawContacts.CONTACT_ID);
            if (l != app.getLong(Contacts.ANDROID_ID)) {
                vals.put(Contacts.ANDROID_ID, l);
            }
            s = sys.getString(ContactsContract.Contacts.DISPLAY_NAME);
            if (!s.equals(app.getString(Contacts.NAME))) {
                vals.put(Contacts.NAME, s);
                vals.put(Contacts.NORMALISED_NAME, SQLite.normalise(s));
            }
            if (app.getInt(Contacts.STATUS_ID) == DELETED.id) {
                vals.put(Contacts.STATUS_ID, ACTIVE.id);
                vals.put(Contacts.DIRTY, 1);
            }
            if (vals.size() > 0) {
                cp.update(Uris.appendId(CONTACTS_URI, app), vals, null, null);
                context.startService(new Intent(context, FriendColorService.class)
                        .putExtra(FriendColorService.EXTRA_ID, app.getLong(_ID)));
            }
            break;
        }
        vals.clear();
    }
    sys.close();
    app.close();
}