Example usage for android.content ContentProviderClient insert

List of usage examples for android.content ContentProviderClient insert

Introduction

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

Prototype

public @Nullable Uri insert(@NonNull Uri url, @Nullable ContentValues initialValues) throws RemoteException 

Source Link

Document

See ContentProvider#insert ContentProvider.insert

Usage

From source file:at.bitfire.ical4android.AndroidCalendar.java

@SuppressLint("InlinedApi")
public static Uri create(Account account, ContentProviderClient provider, ContentValues info)
        throws CalendarStorageException {
    info.put(Calendars.ACCOUNT_NAME, account.name);
    info.put(Calendars.ACCOUNT_TYPE, account.type);

    if (android.os.Build.VERSION.SDK_INT >= 15) {
        // these values are generated by ical4android
        info.put(Calendars.ALLOWED_AVAILABILITY, Events.AVAILABILITY_BUSY + "," + Events.AVAILABILITY_FREE + ","
                + Events.AVAILABILITY_TENTATIVE);
        info.put(Calendars.ALLOWED_ATTENDEE_TYPES, Attendees.TYPE_NONE + "," + Attendees.TYPE_OPTIONAL + ","
                + Attendees.TYPE_REQUIRED + "," + Attendees.TYPE_RESOURCE);
    }//from w w  w  .j a  va2  s.  c  om

    Log.i(TAG, "Creating local calendar: " + info.toString());
    try {
        return provider.insert(syncAdapterURI(Calendars.CONTENT_URI, account), info);
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't create calendar", e);
    }
}

From source file:com.granita.icloudcalsync.resource.LocalTaskList.java

public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    final ContentProviderClient client = resolver.acquireContentProviderClient(TASKS_AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No tasks provider found");

    ContentValues values = new ContentValues();
    values.put(TaskContract.TaskLists.ACCOUNT_NAME, account.name);
    values.put(TaskContract.TaskLists.ACCOUNT_TYPE, account.type);
    values.put(TaskContract.TaskLists._SYNC_ID, info.getURL());
    values.put(TaskContract.TaskLists.LIST_NAME, info.getTitle());
    values.put(TaskContract.TaskLists.LIST_COLOR, DAVUtils.CalDAVtoARGBColor(info.getColor()));
    values.put(TaskContract.TaskLists.OWNER, account.name);
    values.put(TaskContract.TaskLists.ACCESS_LEVEL, 0);
    values.put(TaskContract.TaskLists.SYNC_ENABLED, 1);
    values.put(TaskContract.TaskLists.VISIBLE, 1);

    Log.i(TAG, "Inserting task list: " + values.toString());
    try {/*from  ww w .ja va 2 s.c o  m*/
        return client.insert(taskListsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:fr.free.nrw.commons.contributions.ContributionDao.java

public void save(Contribution contribution) {
    ContentProviderClient db = clientProvider.get();
    try {//from www.  j a  v a2  s . c  om
        if (contribution.getContentUri() == null) {
            contribution.setContentUri(db.insert(BASE_URI, toContentValues(contribution)));
        } else {
            db.update(contribution.getContentUri(), toContentValues(contribution), null, null);
        }
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    } finally {
        db.release();
    }
}

From source file:at.bitfire.davdroid.resource.LocalTaskList.java

public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    @Cleanup("release")
    final ContentProviderClient client = resolver.acquireContentProviderClient(TASKS_AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No tasks provider found");

    ContentValues values = new ContentValues();
    values.put(TaskContract.TaskLists.ACCOUNT_NAME, account.name);
    values.put(TaskContract.TaskLists.ACCOUNT_TYPE, account.type);
    values.put(TaskContract.TaskLists._SYNC_ID, info.getURL());
    values.put(TaskContract.TaskLists.LIST_NAME, info.getTitle());
    values.put(TaskContract.TaskLists.LIST_COLOR,
            info.getColor() != null ? info.getColor() : DAVUtils.calendarGreen);
    values.put(TaskContract.TaskLists.OWNER, account.name);
    values.put(TaskContract.TaskLists.ACCESS_LEVEL, 0);
    values.put(TaskContract.TaskLists.SYNC_ENABLED, 1);
    values.put(TaskContract.TaskLists.VISIBLE, 1);

    Log.i(TAG, "Inserting task list: " + values.toString());
    try {//  w  w  w . jav a  2s  .  c o  m
        return client.insert(taskListsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:net.sf.diningout.content.SyncAdapter.java

/**
 * Sync remote changes to a review./*from w  w  w.j  a  v a  2s  .  com*/
 */
private void syncReview(ContentProviderClient cp, Sync<Review> sync) throws RemoteException {
    Review review = sync.object;
    switch (sync.action) {
    case INSERT:
        ReviewsService.add(review);
        if (review.localId > 0 && review.userId > 0) { // friend's review
            cp.insert(SYNCS_URI, Syncs.values(sync));
        }
        break;
    case UPDATE:
        ContentValues vals = new ContentValues(3);
        vals.put(Reviews.COMMENTS, review.comments);
        vals.put(Reviews.RATING, review.rating);
        vals.put(Reviews.STATUS_ID, review.status.id);
        String sel = Reviews.GLOBAL_ID + " = ?";
        String[] args = { String.valueOf(review.globalId) };
        cp.update(REVIEWS_URI, vals, sel, args);
        break;
    }
    /* update restaurant rating and last visit if own review */
    String id = String.valueOf(Restaurants.idForGlobalId(review.restaurantId));
    if (sync.action != INSERT) { // already called in add
        cr().call(AUTHORITY_URI, CALL_UPDATE_RESTAURANT_RATING, id, null);
    }
    if (review.userId == 0 && sync.action != UPDATE) {
        cr().call(AUTHORITY_URI, CALL_UPDATE_RESTAURANT_LAST_VISIT, id, null);
    }
}

From source file:net.sf.diningout.content.SyncAdapter.java

/**
 * Sync remote changes to a restaurant.//from   ww  w . j  a v  a2  s .  c  o m
 */
private void syncRestaurant(ContentProviderClient cp, Sync<Restaurant> sync) throws RemoteException {
    Restaurant restaurant = sync.object;
    switch (sync.action) {
    case INSERT:
        ContentValues vals = Restaurants.values(restaurant);
        vals.put(Restaurants.COLOR, Restaurants.defaultColor());
        restaurant.localId = ContentUris.parseId(cp.insert(RESTAURANTS_URI, vals));
        if (restaurant.localId > 0 && restaurant.status == ACTIVE) {
            RestaurantService.download(restaurant.localId);
            try {
                RestaurantService.photo(restaurant.localId, vals);
            } catch (IOException e) {
                Log.e(TAG, "downloading Street View image", e);
                exception(e);
            }
        }
        break;
    case UPDATE:
        restaurant.localId = Restaurants.idForGlobalId(restaurant.globalId);
        if (restaurant.localId > 0) {
            vals = Restaurants.values(restaurant);
            try { // while place_id has UNIQUE constraint
                cp.update(ContentUris.withAppendedId(RESTAURANTS_URI, restaurant.localId), vals, null, null);
            } catch (SQLiteConstraintException e) {
                Log.e(TAG, "updating restaurant from sync", e);
                exception(e);
            }
            try {
                RestaurantService.photo(restaurant.localId, vals);
            } catch (IOException e) {
                Log.e(TAG, "downloading Street View image", e);
                exception(e);
            }
        } else { // re-added on other device
            sync.action = INSERT;
            syncRestaurant(cp, sync);
        }
        break;
    }
}

From source file:com.renard.ocr.OCRActivity.java

private Uri saveDocumentToDB(File imageFile, String hocr, String plainText) throws RemoteException {
    ContentProviderClient client = null;
    try {/*  w w  w  .j a  va 2  s  . c o  m*/
        ContentValues v = null;
        if (imageFile != null) {
            v = new ContentValues();
            v.put(com.renard.ocr.DocumentContentProvider.Columns.PHOTO_PATH, imageFile.getPath());
        }
        if (hocr != null) {
            v.put(Columns.HOCR_TEXT, hocr);
        }
        if (plainText != null) {
            v.put(Columns.OCR_TEXT, plainText);
        }

        if (mParentId > -1) {
            v.put(Columns.PARENT_ID, mParentId);
        }
        client = getContentResolver().acquireContentProviderClient(DocumentContentProvider.CONTENT_URI);
        return client.insert(DocumentContentProvider.CONTENT_URI, v);
    } finally {
        if (client != null) {
            client.release();
        }
    }
}

From source file:net.sf.diningout.content.SyncAdapter.java

/**
 * Convert a contact when they become a user and sync remote changes to a followed user.
 *///from w  w  w.  j  a v  a2  s . c o  m
private void syncUser(ContentProviderClient cp, Sync<User> sync) throws RemoteException {
    User user = sync.object;
    ContentValues vals = new ContentValues(1);
    switch (sync.action) {
    case INSERT:
        user.localId = Contacts.idForHash(user.emailHash);
        if (user.localId > 0) {
            vals.put(Contacts.GLOBAL_ID, user.globalId);
            cp.update(ContentUris.withAppendedId(CONTACTS_URI, user.localId), vals, null, null);
            cp.insert(SYNCS_URI, Syncs.values(sync));
        }
        break;
    case UPDATE:
        int following = user.isFollowing ? 1 : 0;
        vals.put(Contacts.FOLLOWING, following);
        String sel = Contacts.GLOBAL_ID + " = ? AND " + Contacts.FOLLOWING + " <> ?";
        String[] args = Elements.toStrings(user.globalId, following);
        if (cp.update(CONTACTS_URI, vals, sel, args) > 0 && user.isFollowing) {
            ReviewsService.download(user.globalId);
        }
        break;
    }
}

From source file:net.sf.diningout.content.SyncAdapter.java

/**
 * Sync remote changes to a review draft.
 */// ww w  . java  2  s  .  c  o  m
private void syncReviewDraft(ContentProviderClient cp, Sync<Review> sync) throws RemoteException {
    ContentValues vals = ReviewDrafts.values(sync.object);
    /* get current version and increment it */
    Uri uri = ContentUris.withAppendedId(REVIEW_DRAFTS_URI, vals.getAsLong(ReviewDrafts.RESTAURANT_ID));
    String[] proj = { ReviewDrafts.VERSION };
    long version = Cursors.firstLong(cp.query(uri, proj, null, null, null));
    if (version >= 0) {
        vals.put(ReviewDrafts.VERSION, version + 1);
        cp.update(uri, vals, null, null);
    } else {
        cp.insert(REVIEW_DRAFTS_URI, vals);
    }
}

From source file:net.sf.diningout.content.SyncAdapter.java

/**
 * Log the user into the server and restore any contacts or restaurants.
 *//*w  w  w .  j  av a  2s.co  m*/
private void initUser(Context context, ContentProviderClient cp) throws RemoteException {
    Init init = Server.init();
    Intent intent = new Intent(ACTION_USER_LOGGED_IN);
    intent.putExtra(EXTRA_HAS_RESTAURANTS, init != null && init.restaurants != null);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    if (init != null) {
        Prefs.edit(context, APP).putBoolean(ACCOUNT_INITIALISED, true).putLong(INSTALL_ID, init.installId)
                .apply();
        if (init.users != null) {
            ContentValues vals = new ContentValues(6);
            int size = init.users.size();
            for (int i = 0; i < size; i++) {
                cp.insert(CONTACTS_URI, Contacts.values(vals, init.users.get(i)));
            }
        }
        if (init.restaurants != null) {
            Prefs.putBoolean(context, APP, ONBOARDED, true);
            int size = init.restaurants.size();
            for (int i = 0; i < size; i++) {
                Restaurant restaurant = init.restaurants.get(i);
                restaurant.localId = Restaurants.add(restaurant.globalId);
                if (restaurant.localId > 0) {
                    RestaurantService.download(restaurant.localId);
                }
            }
        }
    }
}