Example usage for android.content ContentProvider insert

List of usage examples for android.content ContentProvider insert

Introduction

In this page you can find the example usage for android.content ContentProvider insert.

Prototype

public abstract @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values);

Source Link

Document

Implement this to handle requests to insert a new row.

Usage

From source file:com.android.providers.contacts.ContactsSyncAdapter.java

protected static void updateProviderWithGroupEntry(String account, Long syncLocalId, GroupEntry entry,
        ContentProvider provider) throws ParseException {
    ContentValues values = new ContentValues();
    values.put(Groups.NAME, entry.getTitle());
    values.put(Groups.NOTES, entry.getContent());
    values.put(Groups.SYSTEM_ID, entry.getSystemGroup());
    values.put(Groups._SYNC_ACCOUNT, account);
    values.put(Groups._SYNC_ID, lastItemFromUri(entry.getId()));
    values.put(Groups._SYNC_DIRTY, 0);//from   www .j  a  v a2 s  .  c om
    values.put(Groups._SYNC_LOCAL_ID, syncLocalId);
    final String editUri = entry.getEditUri();
    final String syncVersion = editUri == null ? null : lastItemFromUri(editUri);
    values.put(Groups._SYNC_TIME, syncVersion);
    values.put(Groups._SYNC_VERSION, syncVersion);
    provider.insert(Groups.CONTENT_URI, values);
}

From source file:com.android.providers.contacts.ContactsSyncAdapter.java

protected static void updateProviderImpl(String account, Long syncLocalId, Entry entry,
        ContentProvider provider) throws ParseException {
    // If this is a deleted entry then add it to the DELETED_CONTENT_URI
    ContentValues deletedValues = null;//from w w w  .  j a  v a  2  s. c  o m
    if (entry.isDeleted()) {
        deletedValues = new ContentValues();
        deletedValues.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
        final String id = entry.getId();
        final String editUri = entry.getEditUri();
        if (!TextUtils.isEmpty(id)) {
            deletedValues.put(SyncConstValue._SYNC_ID, lastItemFromUri(id));
        }
        if (!TextUtils.isEmpty(editUri)) {
            deletedValues.put(SyncConstValue._SYNC_VERSION, lastItemFromUri(editUri));
        }
        deletedValues.put(SyncConstValue._SYNC_ACCOUNT, account);
    }

    if (entry instanceof ContactEntry) {
        if (deletedValues != null) {
            provider.insert(People.DELETED_CONTENT_URI, deletedValues);
            return;
        }
        updateProviderWithContactEntry(account, syncLocalId, (ContactEntry) entry, provider);
        return;
    }
    if (entry instanceof GroupEntry) {
        if (deletedValues != null) {
            provider.insert(Groups.DELETED_CONTENT_URI, deletedValues);
            return;
        }
        updateProviderWithGroupEntry(account, syncLocalId, (GroupEntry) entry, provider);
        return;
    }
    throw new IllegalArgumentException("unknown entry type, " + entry.getClass().getName());
}

From source file:com.android.providers.contacts.ContactsSyncAdapter.java

protected static void updateProviderWithContactEntry(String account, Long syncLocalId, ContactEntry entry,
        ContentProvider provider) throws ParseException {
    final String name = entry.getTitle();
    final String notes = entry.getContent();
    final String yomiName = entry.getYomiName();
    final String personSyncId = lastItemFromUri(entry.getId());
    final String personSyncVersion = lastItemFromUri(entry.getEditUri());

    // Store the info about the person
    ContentValues values = new ContentValues();
    values.put(People.NAME, name);// w  w w.  j  a va  2s .c  om
    values.put(People.NOTES, notes);
    values.put(People.PHONETIC_NAME, yomiName);
    values.put(SyncConstValue._SYNC_ACCOUNT, account);
    values.put(SyncConstValue._SYNC_ID, personSyncId);
    values.put(SyncConstValue._SYNC_DIRTY, "0");
    values.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
    values.put(SyncConstValue._SYNC_TIME, personSyncVersion);
    values.put(SyncConstValue._SYNC_VERSION, personSyncVersion);
    Uri personUri = provider.insert(People.CONTENT_URI, values);

    // Store the photo information
    final boolean photoExistsOnServer = !TextUtils.isEmpty(entry.getLinkPhotoHref());
    final String photoVersion = lastItemFromUri(entry.getLinkEditPhotoHref());
    values.clear();
    values.put(Photos.PERSON_ID, ContentUris.parseId(personUri));
    values.put(Photos.EXISTS_ON_SERVER, photoExistsOnServer ? 1 : 0);
    values.put(SyncConstValue._SYNC_ACCOUNT, account);
    values.put(SyncConstValue._SYNC_ID, personSyncId);
    values.put(SyncConstValue._SYNC_DIRTY, 0);
    values.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
    values.put(SyncConstValue._SYNC_TIME, photoVersion);
    values.put(SyncConstValue._SYNC_VERSION, photoVersion);
    if (provider.insert(Photos.CONTENT_URI, values) == null) {
        Log.e(TAG, "error inserting photo row, " + values);
    }

    // Store each email address
    for (Object object : entry.getEmailAddresses()) {
        EmailAddress email = (EmailAddress) object;
        values.clear();
        contactsElementToValues(values, email, ENTRY_TYPE_TO_PROVIDER_EMAIL);
        values.put(ContactMethods.DATA, email.getAddress());
        values.put(ContactMethods.KIND, Contacts.KIND_EMAIL);
        Uri uri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
        provider.insert(uri, values);
    }

    // Store each postal address
    for (Object object : entry.getPostalAddresses()) {
        PostalAddress address = (PostalAddress) object;
        values.clear();
        contactsElementToValues(values, address, ENTRY_TYPE_TO_PROVIDER_POSTAL);
        values.put(ContactMethods.DATA, address.getValue());
        values.put(ContactMethods.KIND, Contacts.KIND_POSTAL);
        Uri uri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
        provider.insert(uri, values);
    }

    // Store each im address
    for (Object object : entry.getImAddresses()) {
        ImAddress address = (ImAddress) object;
        values.clear();
        contactsElementToValues(values, address, ENTRY_TYPE_TO_PROVIDER_IM);
        values.put(ContactMethods.DATA, address.getAddress());
        values.put(ContactMethods.KIND, Contacts.KIND_IM);
        final byte protocolType = address.getProtocolPredefined();
        if (protocolType == ImAddress.PROTOCOL_NONE) {
            // don't add anything
        } else if (protocolType == ImAddress.PROTOCOL_CUSTOM) {
            values.put(ContactMethods.AUX_DATA,
                    ContactMethods.encodeCustomImProtocol(address.getProtocolCustom()));
        } else {
            Integer providerProtocolType = ENTRY_IM_PROTOCOL_TO_PROVIDER_PROTOCOL.get(protocolType);
            if (providerProtocolType == null) {
                throw new IllegalArgumentException("unknown protocol type, " + protocolType);
            }
            values.put(ContactMethods.AUX_DATA,
                    ContactMethods.encodePredefinedImProtocol(providerProtocolType));
        }
        Uri uri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
        provider.insert(uri, values);
    }

    // Store each organization
    for (Object object : entry.getOrganizations()) {
        Organization organization = (Organization) object;
        values.clear();
        contactsElementToValues(values, organization, ENTRY_TYPE_TO_PROVIDER_ORGANIZATION);
        values.put(Organizations.COMPANY, organization.getName());
        values.put(Organizations.TITLE, organization.getTitle());
        values.put(Organizations.COMPANY, organization.getName());
        Uri uri = Uri.withAppendedPath(personUri, Organizations.CONTENT_DIRECTORY);
        provider.insert(uri, values);
    }

    // Store each group
    for (Object object : entry.getGroups()) {
        GroupMembershipInfo groupMembershipInfo = (GroupMembershipInfo) object;
        if (groupMembershipInfo.isDeleted()) {
            continue;
        }
        values.clear();
        values.put(GroupMembership.GROUP_SYNC_ACCOUNT, account);
        values.put(GroupMembership.GROUP_SYNC_ID, lastItemFromUri(groupMembershipInfo.getGroup()));
        Uri uri = Uri.withAppendedPath(personUri, GroupMembership.CONTENT_DIRECTORY);
        provider.insert(uri, values);
    }

    // Store each phone number
    for (Object object : entry.getPhoneNumbers()) {
        PhoneNumber phone = (PhoneNumber) object;
        values.clear();
        contactsElementToValues(values, phone, ENTRY_TYPE_TO_PROVIDER_PHONE);
        values.put(People.Phones.NUMBER, phone.getPhoneNumber());
        values.put(People.Phones.LABEL, phone.getLabel());
        Uri uri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
        provider.insert(uri, values);
    }

    // Store the extended properties
    for (Object object : entry.getExtendedProperties()) {
        ExtendedProperty extendedProperty = (ExtendedProperty) object;
        if (!"android".equals(extendedProperty.getName())) {
            continue;
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(extendedProperty.getXmlBlob());
        } catch (JSONException e) {
            Log.w(TAG, "error parsing the android extended property, dropping, entry is " + entry.toString());
            continue;
        }
        Iterator jsonIterator = jsonObject.keys();
        while (jsonIterator.hasNext()) {
            String key = (String) jsonIterator.next();
            values.clear();
            values.put(Extensions.NAME, key);
            try {
                values.put(Extensions.VALUE, jsonObject.getString(key));
            } catch (JSONException e) {
                // this should never happen, since we just got the key from the iterator
            }
            Uri uri = Uri.withAppendedPath(personUri, People.Extensions.CONTENT_DIRECTORY);
            if (null == provider.insert(uri, values)) {
                Log.e(TAG, "Error inserting extension into provider, uri " + uri + ", values " + values);
            }
        }
        break;
    }
}