Example usage for android.content ContentProviderOperation newInsert

List of usage examples for android.content ContentProviderOperation newInsert

Introduction

In this page you can find the example usage for android.content ContentProviderOperation newInsert.

Prototype

public static Builder newInsert(Uri uri) 

Source Link

Document

Create a Builder suitable for building an insert ContentProviderOperation .

Usage

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Add an im to a list of database actions to be performed
 * /*from w  w w  . j  av a  2  s.  com*/
 * @param ops the list of database actions
 * @param im the item to be inserted
 */
private void insertIm(ArrayList<ContentProviderOperation> ops, JSONObject im) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
            .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type")))
            .build());
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Add an organization to a list of database actions to be performed
 * /*from  w w w. ja  va2 s .  c  om*/
 * @param ops the list of database actions
 * @param org the item to be inserted
 */
private void insertOrganization(ArrayList<ContentProviderOperation> ops, JSONObject org) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
                    getJsonString(org, "department"))
            .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
            .build());
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Add an address to a list of database actions to be performed
 * //w w w  .j  a  v a 2  s.c  om
 * @param ops the list of database actions
 * @param address the item to be inserted
 */
private void insertAddress(ArrayList<ContentProviderOperation> ops, JSONObject address) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                    getJsonString(address, "formatted"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET,
                    getJsonString(address, "streetAddress"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY,
                    getJsonString(address, "locality"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION,
                    getJsonString(address, "region"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
                    getJsonString(address, "postalCode"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
                    getJsonString(address, "country"))
            .build());
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Add an email to a list of database actions to be performed
 * //from   ww  w .j  a v  a2  s  . c om
 * @param ops the list of database actions
 * @param email the item to be inserted
 */
private void insertEmail(ArrayList<ContentProviderOperation> ops, JSONObject email) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getPhoneType(getJsonString(email, "type")))
            .build());
}

From source file:com.owncloud.android.datamodel.FileDataStorageManager.java

/**
 * Prepare operations to insert or update files to save in the given folder
 * @param shares        List of shares to insert
 * @param operations    List of operations
 * @return//from   w ww. j  av  a2s . com
 */
private ArrayList<ContentProviderOperation> prepareInsertShares(ArrayList<OCShare> shares,
        ArrayList<ContentProviderOperation> operations) {

    if (shares != null) {
        // prepare operations to insert or update files to save in the given folder
        for (OCShare share : shares) {
            ContentValues cv = new ContentValues();
            cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
            cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
            cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
            cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
            cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
            cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
            cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
            cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
            cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
            cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
            cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
            cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
            cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
            cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
            cv.put(ProviderTableMeta.OCSHARES_NAME, share.getName());
            cv.put(ProviderTableMeta.OCSHARES_URL, share.getShareLink());

            // adding a new share resource
            operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE)
                    .withValues(cv).build());
        }
    }
    return operations;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 * //  ww  w. j  a  va  2  s.c o  m
 * @param contact the contact to be saved
 * @param account the account to be saved under
 */
private boolean createNewContact(JSONObject contact, Account account) {
    // 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, account.type)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name).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("websites");
        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");
    }

    boolean retVal = true;
    //Add contact
    try {
        mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        retVal = false;
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        retVal = false;
    }

    return retVal;
}

From source file:com.android.calendar.EventInfoFragment.java

/**
 * 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.//from w w w  .  j  ava2 s. c o m
 *
 * @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);
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add a website to a list of database actions to be performed
 * /*  w  ww.j  av a  2 s . c om*/
 * @param ops
 *            the list of database actions
 * @param website
 *            the item to be inserted
 */
private void insertWebsite(ArrayList<ContentProviderOperation> ops, JSONObject website) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value"))
            .withValue(ContactsContract.CommonDataKinds.Website.TYPE,
                    getContactType(getJsonString(website, "type")))
            .build());
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add an im to a list of database actions to be performed
 * /*from  w ww  .j a va  2  s .co  m*/
 * @param ops
 *            the list of database actions
 * @param im
 *            the item to be inserted
 */
private void insertIm(ArrayList<ContentProviderOperation> ops, JSONObject im) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
            .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getImType(getJsonString(im, "type"))).build());
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add an organization to a list of database actions to be performed
 * /*w w  w .  ja v a2 s .c  o m*/
 * @param ops
 *            the list of database actions
 * @param org
 *            the item to be inserted
 */
private void insertOrganization(ArrayList<ContentProviderOperation> ops, JSONObject org) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Organization.TYPE,
                    getOrgType(getJsonString(org, "type")))
            .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
                    getJsonString(org, "department"))
            .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name"))
            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title"))
            .build());
}