Search in sources :

Example 66 with ContentProviderOperation

use of android.content.ContentProviderOperation 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 67 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Timber by naman14.

the class PlaylistSongLoader method cleanupPlaylist.

private static void cleanupPlaylist(final Context context, final long playlistId, final Cursor cursor) {
    final int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
    final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newDelete(uri).build());
    final int YIELD_FREQUENCY = 100;
    if (cursor.moveToFirst() && cursor.getCount() > 0) {
        do {
            final ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(uri).withValue(Playlists.Members.PLAY_ORDER, cursor.getPosition()).withValue(Playlists.Members.AUDIO_ID, cursor.getLong(idCol));
            if ((cursor.getPosition() + 1) % YIELD_FREQUENCY == 0) {
                builder.withYieldAllowed(true);
            }
            ops.add(builder.build());
        } while (cursor.moveToNext());
    }
    try {
        context.getContentResolver().applyBatch(MediaStore.AUTHORITY, ops);
    } catch (RemoteException e) {
    } catch (OperationApplicationException e) {
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) Uri(android.net.Uri) OperationApplicationException(android.content.OperationApplicationException)

Example 68 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EditEventFragment method saveReminders.

private void saveReminders() {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(3);
    boolean changed = EditEventHelper.saveReminders(ops, mModel.mId, mModel.mReminders, mOriginalModel.mReminders, false);
    if (!changed) {
        return;
    }
    AsyncQueryService service = new AsyncQueryService(getActivity());
    service.startBatch(0, null, Calendars.CONTENT_URI.getAuthority(), ops, 0);
    // Update the "hasAlarm" field for the event
    Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mModel.mId);
    int len = mModel.mReminders.size();
    boolean hasAlarm = len > 0;
    if (hasAlarm != mOriginalModel.mHasAlarm) {
        ContentValues values = new ContentValues();
        values.put(Events.HAS_ALARM, hasAlarm ? 1 : 0);
        service.startUpdate(0, null, uri, values, null, null, 0);
    }
    Toast.makeText(mContext, R.string.saving_event, Toast.LENGTH_SHORT).show();
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) AsyncQueryService(com.android.calendar.AsyncQueryService) ArrayList(java.util.ArrayList) Uri(android.net.Uri)

Example 69 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EditEventHelper method saveReminders.

/**
     * Saves the reminders, if they changed. Returns true if operations to
     * update the database were added.
     *
     * @param ops the array of ContentProviderOperations
     * @param eventId the id of the event whose reminders are being updated
     * @param reminders the array of reminders set by the user
     * @param originalReminders the original array of reminders
     * @param forceSave if true, then save the reminders even if they didn't change
     * @return true if operations to update the database were added
     */
public static boolean saveReminders(ArrayList<ContentProviderOperation> ops, long eventId, ArrayList<ReminderEntry> reminders, ArrayList<ReminderEntry> originalReminders, boolean forceSave) {
    // If the reminders have not changed, then don't update the database
    if (reminders.equals(originalReminders) && !forceSave) {
        return false;
    }
    // Delete all the existing reminders for this event
    String where = Reminders.EVENT_ID + "=?";
    String[] args = new String[] { Long.toString(eventId) };
    ContentProviderOperation.Builder b = ContentProviderOperation.newDelete(Reminders.CONTENT_URI);
    b.withSelection(where, args);
    ops.add(b.build());
    ContentValues values = new ContentValues();
    int len = reminders.size();
    // Insert the new reminders, if any
    for (int i = 0; i < len; i++) {
        ReminderEntry re = reminders.get(i);
        values.clear();
        values.put(Reminders.MINUTES, re.getMinutes());
        values.put(Reminders.METHOD, re.getMethod());
        values.put(Reminders.EVENT_ID, eventId);
        b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(values);
        ops.add(b.build());
    }
    return true;
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ReminderEntry(com.android.calendar.CalendarEventModel.ReminderEntry)

Example 70 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EventInfoFragment method createExceptionResponse.

/**
     * Creates an exception to a recurring event.  The only change we're making is to the
     * "self attendee status" value.  The provider will take care of updating the corresponding
     * Attendees.attendeeStatus entry.
     *
     * @param eventId The recurring event.
     * @param status The new value for selfAttendeeStatus.
     */
private void createExceptionResponse(long eventId, int status) {
    ContentValues values = new ContentValues();
    values.put(Events.ORIGINAL_INSTANCE_TIME, mStartMillis);
    values.put(Events.SELF_ATTENDEE_STATUS, status);
    values.put(Events.STATUS, Events.STATUS_CONFIRMED);
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    Uri exceptionUri = Uri.withAppendedPath(Events.CONTENT_EXCEPTION_URI, String.valueOf(eventId));
    ops.add(ContentProviderOperation.newInsert(exceptionUri).withValues(values).build());
    mHandler.startBatch(mHandler.getNextToken(), null, CalendarContract.AUTHORITY, ops, Utils.UNDO_DELAY);
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Uri(android.net.Uri)

Aggregations

ContentProviderOperation (android.content.ContentProviderOperation)75 ArrayList (java.util.ArrayList)53 OperationApplicationException (android.content.OperationApplicationException)34 Uri (android.net.Uri)27 ContentValues (android.content.ContentValues)25 RemoteException (android.os.RemoteException)22 Cursor (android.database.Cursor)14 ContentProviderResult (android.content.ContentProviderResult)13 ContentResolver (android.content.ContentResolver)9 LinkedList (java.util.LinkedList)8 IOException (java.io.IOException)7 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 Intent (android.content.Intent)5 List (java.util.List)5 SuppressLint (android.annotation.SuppressLint)4 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4