Search in sources :

Example 36 with ContentProviderResult

use of android.content.ContentProviderResult in project packages_apps_Contacts by AOKP.

the class ContactSaveService method applyDiffSubset.

/**
 * Splits "diff" into subsets based on "MAX_CONTACTS_PROVIDER_BATCH_SIZE", applies each of the
 * subsets, adds the returned array to "results".
 *
 * @return the size of the array, if not null; -1 when the array is null.
 */
private int applyDiffSubset(ArrayList<ContentProviderOperation> diff, int offset, ContentProviderResult[] results, ContentResolver resolver) throws RemoteException, OperationApplicationException {
    final int subsetCount = Math.min(diff.size() - offset, MAX_CONTACTS_PROVIDER_BATCH_SIZE);
    final ArrayList<ContentProviderOperation> subset = new ArrayList<>();
    subset.addAll(diff.subList(offset, offset + subsetCount));
    final ContentProviderResult[] subsetResult = resolver.applyBatch(ContactsContract.AUTHORITY, subset);
    if (subsetResult == null || (offset + subsetResult.length) > results.length) {
        return -1;
    }
    for (ContentProviderResult c : subsetResult) {
        results[offset++] = c;
    }
    return subsetResult.length;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 37 with ContentProviderResult

use of android.content.ContentProviderResult in project steps by linroid.

the class DataProvider method applyBatch.

@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    Timber.i("applyBatch");
    SQLiteDatabase db = openHelper.getWritableDatabase();
    ContentProviderResult[] results = new ContentProviderResult[operations.size()];
    try {
        db.beginTransaction();
        for (int i = 0; i < operations.size(); i++) {
            ContentProviderOperation operation = operations.get(i);
            results[i] = operation.apply(this, results, i);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    return results;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 38 with ContentProviderResult

use of android.content.ContentProviderResult in project cordova-android-chromeview by thedracle.

the class ContactAccessorSdk5 method createNewContact.

/**
 * Creates a new contact and stores it in the database
 *
 * @param contact the contact to be saved
 * @param account the account to be saved under
 */
private String createNewContact(JSONObject contact, String accountType, String accountName) {
    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    // Add contact type
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build());
    // Add name
    try {
        JSONObject name = contact.optJSONObject("name");
        String displayName = contact.getString("displayName");
        if (displayName != null || name != null) {
            ops.add(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, displayName).withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName")).withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName")).withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName")).withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(name, "honorificPrefix")).withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(name, "honorificSuffix")).build());
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get name object");
    }
    // Add phone numbers
    JSONArray phones = null;
    try {
        phones = contact.getJSONArray("phoneNumbers");
        if (phones != null) {
            for (int i = 0; i < phones.length(); i++) {
                JSONObject phone = (JSONObject) phones.get(i);
                insertPhone(ops, phone);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }
    // Add emails
    JSONArray emails = null;
    try {
        emails = contact.getJSONArray("emails");
        if (emails != null) {
            for (int i = 0; i < emails.length(); i++) {
                JSONObject email = (JSONObject) emails.get(i);
                insertEmail(ops, email);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }
    // Add addresses
    JSONArray addresses = null;
    try {
        addresses = contact.getJSONArray("addresses");
        if (addresses != null) {
            for (int i = 0; i < addresses.length(); i++) {
                JSONObject address = (JSONObject) addresses.get(i);
                insertAddress(ops, address);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }
    // Add organizations
    JSONArray organizations = null;
    try {
        organizations = contact.getJSONArray("organizations");
        if (organizations != null) {
            for (int i = 0; i < organizations.length(); i++) {
                JSONObject org = (JSONObject) organizations.get(i);
                insertOrganization(ops, org);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }
    // Add IMs
    JSONArray ims = null;
    try {
        ims = contact.getJSONArray("ims");
        if (ims != null) {
            for (int i = 0; i < ims.length(); i++) {
                JSONObject im = (JSONObject) ims.get(i);
                insertIm(ops, im);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }
    // Add note
    String note = getJsonString(contact, "note");
    if (note != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());
    }
    // Add nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }
    // Add urls
    JSONArray websites = null;
    try {
        websites = contact.getJSONArray("urls");
        if (websites != null) {
            for (int i = 0; i < websites.length(); i++) {
                JSONObject website = (JSONObject) websites.get(i);
                insertWebsite(ops, website);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }
    // Add birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY).withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }
    // Add photos
    JSONArray photos = null;
    try {
        photos = contact.getJSONArray("photos");
        if (photos != null) {
            for (int i = 0; i < photos.length(); i++) {
                JSONObject photo = (JSONObject) photos.get(i);
                insertPhoto(ops, photo);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }
    String newId = null;
    // Add contact
    try {
        ContentProviderResult[] cpResults = mApp.getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        if (cpResults.length >= 0) {
            newId = cpResults[0].uri.getLastPathSegment();
        }
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return newId;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 39 with ContentProviderResult

use of android.content.ContentProviderResult in project robolectric by robolectric.

the class ShadowContentProviderResultTest method count.

@Test
public void count() {
    ContentProviderResult result = new ContentProviderResult(5);
    assertThat(result.count).isEqualTo(5);
}
Also used : ContentProviderResult(android.content.ContentProviderResult) Test(org.junit.Test)

Aggregations

ContentProviderResult (android.content.ContentProviderResult)39 ContentProviderOperation (android.content.ContentProviderOperation)30 OperationApplicationException (android.content.OperationApplicationException)22 ArrayList (java.util.ArrayList)20 RemoteException (android.os.RemoteException)17 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)12 ContentValues (android.content.ContentValues)9 Uri (android.net.Uri)9 ContentResolver (android.content.ContentResolver)6 Intent (android.content.Intent)6 NonNull (android.support.annotation.NonNull)6 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4 PackageMonitor (com.android.internal.content.PackageMonitor)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 Test (org.junit.Test)3 ContentProvider (android.content.ContentProvider)2 OperationInfo (com.android.calendar.AsyncQueryServiceHelper.OperationInfo)2 OrgProperties (com.orgzly.org.OrgProperties)2