Example usage for android.content ContentResolver applyBatch

List of usage examples for android.content ContentResolver applyBatch

Introduction

In this page you can find the example usage for android.content ContentResolver applyBatch.

Prototype

public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
        @NonNull ArrayList<ContentProviderOperation> operations)
        throws RemoteException, OperationApplicationException 

Source Link

Document

Applies each of the ContentProviderOperation objects and returns an array of their results.

Usage

From source file:saschpe.birthdays.service.CalendarSyncService.java

private static void applyBatchOperation(ContentResolver cr, ArrayList<ContentProviderOperation> operations) {
    try {//w w w  .  j a v a 2s. c o  m
        Log.d(TAG, "Applying calendar batch operation");
        cr.applyBatch(CalendarContract.AUTHORITY, operations);
        Log.d(TAG, "Successfully applied calendar batch operation");
    } catch (Exception e) {
        Log.e(TAG, "Failed to apply calendar batch operation");
    }
}

From source file:com.barak.pix.FeedsActivity.java

/**
 * This method remove IM all entries at default Contact application
 *
 * @param contentResolver/*www .j  av  a  2 s  .c o m*/
 *            content resolver
 */
public static void deleteAllIMContactField(ContentResolver contentResolver) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
            .withSelection(
                    ContactsContract.Data.MIMETYPE + "= ? and "
                            + ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL + "= ?",
                    new String[] { ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE, IM_LABEL })
            .build());

    try {
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        Log.d(LOG_TAG, "An exception occurred when deleting all IM field of Contact.");
    }
}

From source file:com.barak.pix.FeedsActivity.java

/**
 * This method remove IM entry at default Contact application.
 *
 * @param contentResolver/*from   ww w.  j av  a  2  s.com*/
 *            content resolver
 * @param uid
 *            User id from android
 * @param account
 *            account name
 */
public static void removeIMContactField(ContentResolver contentResolver, String uid, String account) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
            .withSelection(
                    ContactsContract.Data.RAW_CONTACT_ID + "=? and " + ContactsContract.Data.MIMETYPE
                            + "=? and " + ContactsContract.CommonDataKinds.Im.DATA + " = ?",
                    new String[] { String.valueOf(uid), ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE,
                            account })
            .build());

    try {
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        Log.d(LOG_TAG, "Can't delete Contact's IM field.");
    }
}

From source file:com.canelmas.let.sample.ContactsActivity.java

/**
 * Accesses the Contacts content provider directly to insert a new contact.
 * <p>//  w w w.j  ava 2s .c  o  m
 * The contact is called "__DUMMY ENTRY" and only contains a name.
 */
private void insertDummyContact() {
    // Two operations are needed to insert a new contact.
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(2);

    // First, set up a new raw contact.
    ContentProviderOperation.Builder op = ContentProviderOperation
            .newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null);
    operations.add(op.build());

    // Next, set the name for the contact.
    op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, DUMMY_CONTACT_NAME);
    operations.add(op.build());

    // Apply the operations.
    ContentResolver resolver = getContentResolver();
    try {
        resolver.applyBatch(ContactsContract.AUTHORITY, operations);
    } catch (RemoteException e) {
        Log.d(TAG, "Could not add a new contact: " + e.getMessage());
    } catch (OperationApplicationException e) {
        Log.d(TAG, "Could not add a new contact: " + e.getMessage());
    }
}

From source file:org.agilespain.kitaos.service.JsonHandler.java

/**
 * Parse the given {@link org.json}, turning into a series of
 * {@link android.content.ContentProviderOperation} that are immediately applied using the
 * given {@link android.content.ContentResolver}.
 *
 * @param jsonString/*w w  w. j  a  v  a  2s .c  o m*/
 * @param resolver
 * @throws HandlerException
 */
public void parseAndApply(String jsonString, ContentResolver resolver) throws HandlerException {
    try {
        final ArrayList<ContentProviderOperation> batch = parse(jsonString, resolver);
        resolver.applyBatch(mAuthority, batch);

    } catch (HandlerException e) {
        throw e;
    } catch (JSONException e) {
        e.printStackTrace();
        throw new HandlerException("Problem parsing JSON response", e);
    } catch (IOException e) {
        throw new HandlerException("Problem reading response", e);
    } catch (RemoteException e) {
        // Failed binder transactions aren't recoverable
        throw new RuntimeException("Problem applying batch operation", e);
    } catch (OperationApplicationException e) {
        // Failures like constraint violation aren't recoverable
        // TODO: write unit tests to exercise full provider
        // TODO: consider catching version checking asserts here, and then
        // wrapping around to retry parsing again.
        throw new RuntimeException("Problem applying batch operation", e);
    }
}

From source file:ca.mudar.parkcatcher.io.JsonHandler.java

/**
 * Parse the given {@link JSONTokener}, turning into a series of
 * {@link ContentProviderOperation} that are immediately applied using the
 * given {@link ContentResolver}./*from ww w .j  a v  a 2  s .  c o m*/
 */
public void parseAndApply(JSONTokener jsonTokener, ContentResolver resolver) throws HandlerException {
    try {
        final ArrayList<ContentProviderOperation> batch = parse(jsonTokener, resolver);
        resolver.applyBatch(mAuthority, batch);
    } catch (IOException e) {
        throw new HandlerException("Problem reading response", e);
    } catch (JSONException e) {
        throw new HandlerException("Problem parsing JSON response", e);
    } catch (RemoteException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    } catch (OperationApplicationException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    }
}

From source file:cn.pjt.rxjava.contacts.ContactsFragment.java

/**
 * Accesses the Contacts content provider directly to insert a new contact.
 * <p>//from ww  w. j av a 2 s .  c  o m
 * The contact is called "__DUMMY ENTRY" and only contains a name.
 */
private void insertDummyContact() {
    // Two operations are needed to insert a new contact.
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(2);

    // First, set up a new raw contact.
    ContentProviderOperation.Builder op = ContentProviderOperation
            .newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null);
    operations.add(op.build());

    // Next, set the name for the contact.
    op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, DUMMY_CONTACT_NAME);
    operations.add(op.build());

    // Apply the operations.
    ContentResolver resolver = getActivity().getContentResolver();
    try {
        resolver.applyBatch(ContactsContract.AUTHORITY, operations);
    } catch (RemoteException e) {
        Log.d(TAG, "Could not add a new contact: " + e.getMessage());
    } catch (OperationApplicationException e) {
        Log.d(TAG, "Could not add a new contact: " + e.getMessage());
    }
}

From source file:fr.mixit.android.io.JSONHandler.java

/**
  * Parse the given {@link org.json.JSONArray}, turning into a series of
  * {@link android.content.ContentProviderOperation} that are immediately applied using the
  * given {@link android.content.ContentResolver}.
  *//*from w  w w . j  a v a2 s  .c o  m*/
public void parseAndApply(ArrayList<JSONArray> entries, ContentResolver resolver) throws JSONHandlerException {
    try {
        final ArrayList<ContentProviderOperation> batch = parse(entries, resolver);
        resolver.applyBatch(getAuthority(), batch);
    } catch (JSONException e) {
        throw new JSONHandlerException("Problem parsing JSON response", e);
    } catch (RemoteException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    } catch (OperationApplicationException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    }
}

From source file:com.grokkingandroid.sampleapp.samples.data.contentprovider.lentitems.LentItemDisplayFragment.java

/**
 * The dummyApplyBatch method is only used to show a sample batch. 
 * It's not used within this sample app.
 *///from w w w  .  ja  v  a2  s .c o  m
@SuppressWarnings("unused")
private void dummyApplyBatch() {

    String borrower = "John Doe";
    String item = "Anhalter";
    boolean hasPic = true;
    Context ctx = getActivity();
    String somePath = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(Items.CONTENT_URI).withValue(Items.NAME, item)
            .withValue(Items.BORROWER, borrower).build());
    if (hasPic) {
        ops.add(ContentProviderOperation.newInsert(Photos.CONTENT_URI).withValue(Photos._DATA, somePath)
                .withValueBackReference(Photos.ITEMS_ID, 0).build());
    }
    try {
        ContentResolver resolver = getActivity().getContentResolver();
        resolver.applyBatch(LentItemsContract.AUTHORITY, ops);
    } catch (OperationApplicationException e) {
        Log.e("wemonit", "cannot apply batch: " + e.getLocalizedMessage(), e);
    } catch (RemoteException e) {
        Log.e("wemonit", "cannot apply batch: " + e.getLocalizedMessage(), e);
    }

    // EventBus.getDefault().post(whatever);

}

From source file:com.barak.pix.FeedsActivity.java

/**
 * This method add my account under IM field at default Contact
 * application/*from w  ww. j  a  v a2 s. c  o  m*/
 *
 * Labeled with my custom protocol.
 *
 * @param contentResolver
 *            content resolver
 * @param uid
 *            User id from android
 * @param account
 *            account name
 */
public static void updateIMContactField(ContentResolver contentResolver, String uid, String account) {

    ContentValues contentValues = new ContentValues();

    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, Integer.parseInt(uid));
    contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
    contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE,
            ContactsContract.CommonDataKinds.Im.TYPE_CUSTOM);
    contentValues.put(ContactsContract.CommonDataKinds.Im.LABEL, IM_LABEL);
    contentValues.put(ContactsContract.CommonDataKinds.Im.PROTOCOL,
            ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM);
    contentValues.put(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL, IM_LABEL);

    contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, account);

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValues(contentValues)
            .build());

    try {
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        Log.d(LOG_TAG, "Can't update Contact's IM field.");
    }
}