List of usage examples for android.content ContentProviderOperation newDelete
public static Builder newDelete(Uri uri)
From source file:org.muckebox.android.net.RefreshHelper.java
public static Integer refreshTracks(long albumId) { try {/*from ww w . j ava2 s. c om*/ ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(1); JSONArray json = ApiHelper.callApiForArray("tracks", null, new String[] { "album" }, new String[] { Long.toString(albumId) }); operations.ensureCapacity(operations.size() + json.length() + 1); operations.add(ContentProviderOperation .newDelete(Uri.withAppendedPath(MuckeboxProvider.URI_TRACKS_ALBUM, Long.toString(albumId))) .build()); for (int j = 0; j < json.length(); ++j) { JSONObject o = json.getJSONObject(j); ContentValues values = new ContentValues(); values.put(TrackEntry.SHORT_ID, o.getInt("id")); values.put(TrackEntry.SHORT_ARTIST_ID, o.getInt("artist_id")); values.put(TrackEntry.SHORT_ALBUM_ID, o.getInt("album_id")); values.put(TrackEntry.SHORT_TITLE, o.getString("title")); if (!o.isNull("tracknumber")) values.put(TrackEntry.SHORT_TRACKNUMBER, o.getInt("tracknumber")); if (!o.isNull("discnumber")) values.put(TrackEntry.SHORT_DISCNUMBER, o.getInt("discnumber")); values.put(TrackEntry.SHORT_LABEL, o.getString("label")); values.put(TrackEntry.SHORT_CATALOGNUMBER, o.getString("catalognumber")); values.put(TrackEntry.SHORT_LENGTH, o.getInt("length")); values.put(TrackEntry.SHORT_DATE, o.getString("date")); operations.add(ContentProviderOperation .newDelete( Uri.withAppendedPath(MuckeboxProvider.URI_TRACKS, Integer.toString(o.getInt("id")))) .build()); operations.add( ContentProviderOperation.newInsert(MuckeboxProvider.URI_TRACKS).withValues(values).build()); } Log.d(LOG_TAG, "Got " + json.length() + " Tracks"); Muckebox.getAppContext().getContentResolver().applyBatch(MuckeboxProvider.AUTHORITY, operations); } catch (AuthenticationException e) { return R.string.error_authentication; } catch (SSLException e) { return R.string.error_ssl; } catch (IOException e) { Log.d(LOG_TAG, "IOException: " + e.getMessage()); return R.string.error_reload_tracks; } catch (JSONException e) { return R.string.error_json; } catch (RemoteException e) { e.printStackTrace(); return R.string.error_reload_tracks; } catch (OperationApplicationException e) { e.printStackTrace(); return R.string.error_reload_tracks; } return null; }
From source file:com.conferenceengineer.android.iosched.io.SearchSuggestHandler.java
public ArrayList<ContentProviderOperation> parse(String json) throws IOException { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); try {/* www. ja v a2 s . c om*/ JSONObject root = new JSONObject(json); JSONArray suggestions = root.getJSONArray("words"); batch.add( ContentProviderOperation .newDelete(ScheduleContract .addCallerIsSyncAdapterParameter(ScheduleContract.SearchSuggest.CONTENT_URI)) .build()); for (int i = 0; i < suggestions.length(); i++) { batch.add(ContentProviderOperation .newInsert(ScheduleContract .addCallerIsSyncAdapterParameter(ScheduleContract.SearchSuggest.CONTENT_URI)) .withValue(SearchManager.SUGGEST_COLUMN_TEXT_1, suggestions.getString(i)).build()); } } catch (JSONException e) { Log.e(Config.LOG_TAG, "Problem building word list", e); } return batch; }
From source file:com.conferenceengineer.android.iosched.io.TracksHandler.java
@Override public ArrayList<ContentProviderOperation> parse(String json) throws IOException { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); batch.add(ContentProviderOperation .newDelete(ScheduleContract.addCallerIsSyncAdapterParameter(ScheduleContract.Tracks.CONTENT_URI)) .build());// w w w . ja v a 2s .c o m TracksResponse response = new TracksResponse(); try { response.fromJSON(new JSONObject(json)); } catch (JSONException e) { return batch; } for (TrackResponse track : response.getTracks()) { parseTrack(track, batch); } return batch; }
From source file:com.conferenceengineer.android.iosched.io.BlocksHandler.java
public ArrayList<ContentProviderOperation> parse(String json) throws IOException { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); // Clear out any existing common slots to avoid duplicates ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(ScheduleContract.addCallerIsSyncAdapterParameter(Blocks.CONTENT_URI)); builder.withSelection(Blocks.BLOCK_TYPE + " = ?", new String[] { Blocks.BLOCK_TYPE_GENERIC }); batch.add(builder.build());//from ww w.j av a 2 s. c o m builder = ContentProviderOperation .newDelete(ScheduleContract.addCallerIsSyncAdapterParameter(Blocks.CONTENT_URI)); builder.withSelection(Blocks.BLOCK_TYPE + " = ?", new String[] { Blocks.BLOCK_TYPE_FOOD }); batch.add(builder.build()); try { EventSlots eventSlots = new EventSlots(); eventSlots.fromJSON(new JSONObject(json)); for (Day day : eventSlots.getDays()) { String date = day.date; for (TimeSlot timeSlot : day.getSlots()) { parseSlot(date, timeSlot, batch); } } } catch (Throwable e) { LOGE(TAG, e.toString()); } return batch; }
From source file:com.manning.androidhacks.hack043.service.SQLContentProviderService.java
@Override protected void onHandleIntent(Intent intent) { Builder builder = null;// ww w.j a v a2s. c o m ContentResolver contentResolver = getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); builder = ContentProviderOperation.newDelete(MySQLContentProvider.CONTENT_URI); operations.add(builder.build()); for (int i = 1; i <= 100; i++) { ContentValues cv = new ContentValues(); cv.put(MySQLContentProvider.COLUMN_TEXT, "" + i); builder = ContentProviderOperation.newInsert(MySQLContentProvider.CONTENT_URI); builder.withValues(cv); operations.add(builder.build()); } try { contentResolver.applyBatch(MySQLContentProvider.AUTHORITY, operations); } catch (RemoteException e) { Log.e(TAG, "Couldn't apply batch: " + e.getMessage()); } catch (OperationApplicationException e) { Log.e(TAG, "Couldn't apply batch: " + e.getMessage()); } }
From source file:com.manning.androidhacks.hack043.service.BatchService.java
@Override protected void onHandleIntent(Intent intent) { Builder builder = null;/*from ww w .j a v a2s . c o m*/ ContentResolver contentResolver = getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); builder = ContentProviderOperation.newDelete(BatchNumbersContentProvider.CONTENT_URI); operations.add(builder.build()); for (int i = 1; i <= 100; i++) { ContentValues cv = new ContentValues(); cv.put(NoBatchNumbersContentProvider.COLUMN_TEXT, "" + i); builder = ContentProviderOperation.newInsert(BatchNumbersContentProvider.CONTENT_URI); builder.withValues(cv); operations.add(builder.build()); } try { contentResolver.applyBatch(BatchNumbersContentProvider.AUTHORITY, operations); } catch (RemoteException e) { Log.e(TAG, "Couldn't apply batch: " + e.getMessage()); } catch (OperationApplicationException e) { Log.e(TAG, "Couldn't apply batch: " + e.getMessage()); } }
From source file:at.bitfire.davdroid.resource.LocalGroup.java
@Override public void clearDirty(String eTag) throws ContactsStorageException { assertID();//from ww w . j a va2 s .c o m ContentValues values = new ContentValues(2); values.put(Groups.DIRTY, 0); values.put(COLUMN_ETAG, this.eTag = eTag); update(values); // update cached group memberships BatchOperation batch = new BatchOperation(addressBook.provider); // delete cached group memberships batch.enqueue(new BatchOperation.Operation(ContentProviderOperation .newDelete(addressBook.syncAdapterURI(ContactsContract.Data.CONTENT_URI)) .withSelection(CachedGroupMembership.MIMETYPE + "=? AND " + CachedGroupMembership.GROUP_ID + "=?", new String[] { CachedGroupMembership.CONTENT_ITEM_TYPE, String.valueOf(id) }))); // insert updated cached group memberships for (long member : getMembers()) batch.enqueue(new BatchOperation.Operation(ContentProviderOperation .newInsert(addressBook.syncAdapterURI(ContactsContract.Data.CONTENT_URI)) .withValue(CachedGroupMembership.MIMETYPE, CachedGroupMembership.CONTENT_ITEM_TYPE) .withValue(CachedGroupMembership.RAW_CONTACT_ID, member) .withValue(CachedGroupMembership.GROUP_ID, id).withYieldAllowed(true))); batch.commit(); }
From source file:org.xwalk.runtime.extension.api.contacts.Contacts.java
@Override public void onMessage(int instanceID, String message) { if (message.isEmpty()) return;/*from w ww . ja v a 2 s .c om*/ try { JSONObject jsonInput = new JSONObject(message); String cmd = jsonInput.getString("cmd"); if (cmd.equals("addEventListener")) { mObserver.startListening(); return; } JSONObject jsonOutput = new JSONObject(); jsonOutput.put("_promise_id", jsonInput.getString("_promise_id")); if (cmd.equals("save")) { ContactSaver saver = new ContactSaver(mResolver); jsonOutput.put("data", saver.save(jsonInput.getString("contact"))); } else if (cmd.equals("find")) { ContactFinder finder = new ContactFinder(mResolver); String options = jsonInput.has("options") ? jsonInput.getString("options") : null; JSONArray results = finder.find(options); jsonOutput.put("data", results); } else if (cmd.equals("remove")) { ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); String[] args = new String[] { jsonInput.getString("contactId") }; ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI) .withSelection(RawContacts.CONTACT_ID + "=?", args).build()); try { mResolver.applyBatch(ContactsContract.AUTHORITY, ops); } catch (Exception e) { if (e instanceof RemoteException || e instanceof OperationApplicationException || e instanceof SecurityException) { Log.e(TAG, "onMessage - Failed to apply batch: " + e.toString()); return; } else { throw new RuntimeException(e); } } } else if (cmd.equals("clear")) { handleClear(); } else { Log.e(TAG, "Unexpected message received: " + message); return; } this.postMessage(instanceID, jsonOutput.toString()); } catch (JSONException e) { Log.e(TAG, e.toString()); } }
From source file:com.goliathonline.android.kegbot.io.RemoteUserHandler.java
/** {@inheritDoc} */ @Override//w w w . j ava2 s. c o m public ArrayList<ContentProviderOperation> parse(JSONObject parser, ContentResolver resolver) throws JSONException, IOException { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); // Walk document, parsing any incoming entries if (!parser.has("result")) return batch; JSONObject result = parser.getJSONObject("result"); JSONObject keg = result.getJSONObject("user"); final String userId = sanitizeId(keg.getString("username")); final Uri userUri = Users.buildUserUri(userId); // Check for existing details, only update when changed final ContentValues values = queryUserDetails(userUri, resolver); final long localUpdated = values.getAsLong(SyncColumns.UPDATED); final long serverUpdated = 500; //entry.getUpdated(); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "found user " + userId); Log.v(TAG, "found localUpdated=" + localUpdated + ", server=" + serverUpdated); } // Clear any existing values for this session, treating the // incoming details as authoritative. batch.add(ContentProviderOperation.newDelete(userUri).build()); final ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Users.CONTENT_URI); builder.withValue(SyncColumns.UPDATED, serverUpdated); builder.withValue(Users.USER_ID, userId); if (keg.has("image")) { JSONObject image = keg.getJSONObject("image"); if (image.has("url")) builder.withValue(Users.USER_IMAGE_URL, image.getString("url")); } // Normal session details ready, write to provider batch.add(builder.build()); return batch; }
From source file:com.goliathonline.android.kegbot.io.RemoteKegHandler.java
/** {@inheritDoc} */ @Override/* w w w . ja v a 2 s . c om*/ public ArrayList<ContentProviderOperation> parse(JSONObject parser, ContentResolver resolver) throws JSONException, IOException { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); // Walk document, parsing any incoming entries JSONObject result = parser.getJSONObject("result"); JSONObject keg = result.getJSONObject("keg"); JSONObject type = result.getJSONObject("type"); JSONObject image = type.getJSONObject("image"); final String kegId = sanitizeId(keg.getString("id")); final Uri kegUri = Kegs.buildKegUri(kegId); // Check for existing details, only update when changed final ContentValues values = queryKegDetails(kegUri, resolver); final long localUpdated = values.getAsLong(SyncColumns.UPDATED); final long serverUpdated = 500; //entry.getUpdated(); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "found keg " + kegId); Log.v(TAG, "found localUpdated=" + localUpdated + ", server=" + serverUpdated); } // Clear any existing values for this session, treating the // incoming details as authoritative. batch.add(ContentProviderOperation.newDelete(kegUri).build()); final ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Kegs.CONTENT_URI); builder.withValue(SyncColumns.UPDATED, serverUpdated); builder.withValue(Kegs.KEG_ID, kegId); // Inherit starred value from previous row if (values.containsKey(Kegs.KEG_STARRED)) { builder.withValue(Kegs.KEG_STARRED, values.getAsInteger(Kegs.KEG_STARRED)); } if (keg.has("status")) builder.withValue(Kegs.STATUS, keg.getString("status")); if (keg.has("volume_ml_remain")) builder.withValue(Kegs.VOLUME_REMAIN, keg.getDouble("volume_ml_remain")); if (keg.has("description")) builder.withValue(Kegs.DESCRIPTION, keg.getString("description")); if (keg.has("type_id")) builder.withValue(Kegs.TYPE_ID, keg.getString("type_id")); if (keg.has("size_id")) builder.withValue(Kegs.SIZE_ID, keg.getInt("size_id")); if (keg.has("percent_full")) builder.withValue(Kegs.PERCENT_FULL, keg.getDouble("percent_full")); if (keg.has("size_name")) builder.withValue(Kegs.SIZE_NAME, keg.getString("size_name")); if (keg.has("spilled_ml")) builder.withValue(Kegs.VOLUME_SPILL, keg.getDouble("spilled_ml")); if (keg.has("size_volume_ml")) builder.withValue(Kegs.VOLUME_SIZE, keg.getDouble("size_volume_ml")); if (type.has("name")) builder.withValue(Kegs.KEG_NAME, type.getString("name")); if (type.has("abv")) builder.withValue(Kegs.KEG_ABV, type.getDouble("abv")); if (image.has("url")) builder.withValue(Kegs.IMAGE_URL, image.getString("url")); // Normal keg details ready, write to provider batch.add(builder.build()); return batch; }