Search in sources :

Example 1 with Builder

use of android.content.ContentProviderOperation.Builder in project packages_apps_Contacts by AOKP.

the class ContactSaveService method joinContacts.

private void joinContacts(Intent intent) {
    long contactId1 = intent.getLongExtra(EXTRA_CONTACT_ID1, -1);
    long contactId2 = intent.getLongExtra(EXTRA_CONTACT_ID2, -1);
    // Load raw contact IDs for all raw contacts involved - currently edited and selected
    // in the join UIs.
    long[] rawContactIds = getRawContactIdsForAggregation(contactId1, contactId2);
    if (rawContactIds == null) {
        Log.e(TAG, "Invalid arguments for joinContacts request");
        return;
    }
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    // For each pair of raw contacts, insert an aggregation exception
    for (int i = 0; i < rawContactIds.length; i++) {
        for (int j = 0; j < rawContactIds.length; j++) {
            if (i != j) {
                buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]);
            }
        }
    }
    final ContentResolver resolver = getContentResolver();
    // Use the name for contactId1 as the name for the newly aggregated contact.
    final Uri contactId1Uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId1);
    final Uri entityUri = Uri.withAppendedPath(contactId1Uri, Contacts.Entity.CONTENT_DIRECTORY);
    Cursor c = resolver.query(entityUri, ContactEntityQuery.PROJECTION, ContactEntityQuery.SELECTION, null, null);
    if (c == null) {
        Log.e(TAG, "Unable to open Contacts DB cursor");
        showToast(R.string.contactSavedErrorToast);
        return;
    }
    long dataIdToAddSuperPrimary = -1;
    try {
        if (c.moveToFirst()) {
            dataIdToAddSuperPrimary = c.getLong(ContactEntityQuery.DATA_ID);
        }
    } finally {
        c.close();
    }
    // display name does not change as a result of the join.
    if (dataIdToAddSuperPrimary != -1) {
        Builder builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(Data.CONTENT_URI, dataIdToAddSuperPrimary));
        builder.withValue(Data.IS_SUPER_PRIMARY, 1);
        builder.withValue(Data.IS_PRIMARY, 1);
        operations.add(builder.build());
    }
    boolean success = false;
    // Apply all aggregation exceptions as one batch
    try {
        resolver.applyBatch(ContactsContract.AUTHORITY, operations);
        showToast(R.string.contactsJoinedMessage);
        success = true;
    } catch (RemoteException | OperationApplicationException e) {
        Log.e(TAG, "Failed to apply aggregation exception batch", e);
        showToast(R.string.contactSavedErrorToast);
    }
    Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT);
    if (success) {
        Uri uri = RawContacts.getContactLookupUri(resolver, ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactIds[0]));
        callbackIntent.setData(uri);
    }
    deliverCallback(callbackIntent);
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Builder(android.content.ContentProviderOperation.Builder) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Intent(android.content.Intent) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 2 with Builder

use of android.content.ContentProviderOperation.Builder in project packages_apps_Contacts by AOKP.

the class ContactSaveService method buildJoinContactDiff.

/**
 * Construct a {@link AggregationExceptions#TYPE_KEEP_TOGETHER} ContentProviderOperation.
 */
private void buildJoinContactDiff(ArrayList<ContentProviderOperation> operations, long rawContactId1, long rawContactId2) {
    Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI);
    builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
    builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
    builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
    operations.add(builder.build());
}
Also used : Builder(android.content.ContentProviderOperation.Builder)

Example 3 with Builder

use of android.content.ContentProviderOperation.Builder in project network-monitor by caarmen.

the class DBImport method buildInsertOperations.

/**
 * Read all cells from the given table from the dbImport database, and add corresponding insert operations to the operations parameter.
 *
 * @throws OperationApplicationException if the database couldn't insert the data.
 * @throws RemoteException if the database couldn't insert the data.
 */
private void buildInsertOperations(SQLiteDatabase dbImport, Uri uri, ArrayList<ContentProviderOperation> operations, ProgressListener listener) throws RemoteException, OperationApplicationException {
    Log.v(TAG, "buildInsertOperations: uri = " + uri);
    try {
        Cursor c = dbImport.query(false, NetMonColumns.TABLE_NAME, null, null, null, null, null, null, null);
        if (c != null) {
            try {
                int count = c.getCount();
                if (c.moveToFirst()) {
                    int columnCount = c.getColumnCount();
                    do {
                        Builder builder = ContentProviderOperation.newInsert(uri);
                        for (int i = 0; i < columnCount; i++) {
                            String columnName = c.getColumnName(i);
                            Object value = c.getString(i);
                            builder.withValue(columnName, value);
                        }
                        operations.add(builder.build());
                        if (operations.size() >= 100) {
                            mContext.getContentResolver().applyBatch(NetMonProvider.AUTHORITY, operations);
                            operations.clear();
                        }
                        if (listener != null)
                            listener.onProgress(c.getPosition(), count);
                    } while (c.moveToNext() && !mIsCanceled.get());
                    if (operations.size() > 0 && !mIsCanceled.get())
                        mContext.getContentResolver().applyBatch(NetMonProvider.AUTHORITY, operations);
                }
                if (listener != null) {
                    if (mIsCanceled.get())
                        listener.onError(mContext.getString(R.string.import_notif_canceled_content));
                    else
                        listener.onComplete(mContext.getString(R.string.import_notif_complete_content, Share.readDisplayName(mContext, mUri)));
                }
                return;
            } finally {
                c.close();
            }
        }
    } catch (SQLiteException e) {
        Log.w(TAG, "Couldn't import database " + mUri, e);
    }
    if (listener != null)
        listener.onError(mContext.getString(R.string.import_notif_error_content, Share.readDisplayName(mContext, uri)));
}
Also used : Builder(android.content.ContentProviderOperation.Builder) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 4 with Builder

use of android.content.ContentProviderOperation.Builder in project AsmackService by rtreffer.

the class ContactDataMapper method persist.

/**
 * Append all operations needed to store the current contact to a set of
 * operations.
 * @param contact The current contact with metadata.
 * @param operations A set of operations to be extended.
 */
public void persist(RawContact contact, ArrayList<ContentProviderOperation> operations) {
    int operationsStart = operations.size();
    Builder operation;
    if (contact.getID() == -1) {
        operation = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    } else {
        operation = ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI);
        operation.withSelection(RawContacts._ID + "=?", new String[] { Long.toString(contact.getID()) });
    }
    ContentValues values = new ContentValues();
    put(values, contact);
    operation.withValues(values);
    operations.add(operation.build());
    for (Metadata data : contact.getMetadata().values()) {
        values.clear();
        put(values, data);
        if (data instanceof DeletedMetadata) {
            operation = ContentProviderOperation.newDelete(Data.CONTENT_URI);
            operation.withValues(values);
            operation.withSelection(Data._ID + "=?", new String[] { Long.toString(contact.getID()) });
            operations.add(operation.build());
            continue;
        }
        if (data.getID() == -1) {
            operation = ContentProviderOperation.newInsert(Data.CONTENT_URI);
        } else {
            operation = ContentProviderOperation.newUpdate(Data.CONTENT_URI);
            operation.withSelection(Data._ID + "=?", new String[] { Long.toString(data.getID()) });
        }
        if (contact.getID() == -1) {
            operation.withValueBackReference(Data.RAW_CONTACT_ID, operationsStart);
            values.remove(Data.RAW_CONTACT_ID);
        } else {
            values.put(Data.RAW_CONTACT_ID, contact.getID());
        }
        operation.withValues(values);
        operations.add(operation.build());
    }
}
Also used : ContentValues(android.content.ContentValues) Builder(android.content.ContentProviderOperation.Builder)

Aggregations

Builder (android.content.ContentProviderOperation.Builder)4 Cursor (android.database.Cursor)2 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentResolver (android.content.ContentResolver)1 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 OperationApplicationException (android.content.OperationApplicationException)1 SQLiteException (android.database.sqlite.SQLiteException)1 Uri (android.net.Uri)1 RemoteException (android.os.RemoteException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1