List of usage examples for android.content ContentProviderOperation newInsert
public static Builder newInsert(Uri uri)
From source file:org.mythtv.android.db.dvr.ProgramHelperV27.java
public void processProgram(final Context context, final LocationProfile locationProfile, Uri uri, String table, ArrayList<ContentProviderOperation> ops, Program program, String tag) { // Log.d( TAG, "processProgram : enter" ); ContentValues programValues = convertProgramToContentValues(locationProfile, program, tag); if (table.equals(ProgramConstants.TABLE_NAME_RECORDED) || table.equals(ProgramConstants.TABLE_NAME_UPCOMING) || table.equals(ProgramConstants.TABLE_NAME_GUIDE)) { //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table ); ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true) .build());/* w w w .ja va 2s. c om*/ } else { String programSelection = table + "." + ProgramConstants.FIELD_CHANNEL_ID + " = ? AND " + table + "." + ProgramConstants.FIELD_START_TIME + " = ?"; String[] programSelectionArgs = new String[] { String.valueOf(program.getChannel().getChanId()), String.valueOf(program.getStartTime().getMillis()) }; programSelection = appendLocationHostname(context, locationProfile, programSelection, table); Cursor programCursor = context.getContentResolver().query(uri, programProjection, programSelection, programSelectionArgs, null); if (programCursor.moveToFirst()) { Log.v(TAG, "processProgram : UPDATE PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table); Long id = programCursor.getLong(programCursor.getColumnIndexOrThrow(ProgramConstants._ID)); ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(uri, id)) .withValues(programValues).withYieldAllowed(true).build()); } else { //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table ); ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true) .build()); } programCursor.close(); } // Log.d( TAG, "processProgram : exit" ); }
From source file:jp.ac.tokushima_u.is.ll.io.LanguageJsonHandler.java
public ArrayList<ContentProviderOperation> parse(ContentResolver resolver) { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); Uri uri = Languages.CONTENT_URI; String sortOrder = SyncColumns.UPDATED + " desc"; Cursor cursor = resolver.query(uri, LanguagesQuery.PROJECTION, null, null, sortOrder); try {//from ww w .j av a2 s. c om if (cursor.moveToFirst()) { Log.d(TAG, "Language has been inserted"); int count = cursor.getCount(); if (count > 3) { return batch; } } } finally { cursor.close(); } HttpPost httpPost = new HttpPost(this.systemUrl + this.syncServiceUrl + this.languageSearchUrl); try { DefaultHttpClient client = HttpClientFactory.createHttpClient(); MultipartEntity params = new MultipartEntity(); httpPost.setEntity(params); HttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); JSONObject json = new JSONObject(result); if (json != null) { JSONArray array = json.getJSONArray("languages"); if (array != null) { for (int i = 0; i < array.length(); i++) { JSONObject o = array.getJSONObject(i); String languageId = o.getString("id"); if (languageId == null || languageId.length() <= 0) continue; ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(Languages.CONTENT_URI); builder.withValue(Languages.LANGUAGE_ID, languageId); if (o.getString("code") != null && o.getString("code").length() > 0 && !"null".equals(o.getString("code"))) builder.withValue(Languages.CODE, o.getString("code")); if (o.getString("name") != null && o.getString("name").length() > 0 && !"null".equals(o.getString("name"))) builder.withValue(Languages.NAME, o.getString("name")); builder.withValue(SyncColumns.UPDATED, Calendar.getInstance().getTimeInMillis()); batch.add(builder.build()); } } } } } catch (Exception e) { Log.d(TAG, "exception occured", e); } return batch; }
From source file:org.mythtv.service.dvr.v26.ProgramHelperV26.java
public void processProgram(final Context context, final LocationProfile locationProfile, Uri uri, String table, ArrayList<ContentProviderOperation> ops, Program program, String tag) { // Log.d( TAG, "processProgram : enter" ); ContentValues programValues = convertProgramToContentValues(locationProfile, program, tag); if (table.equals(ProgramConstants.TABLE_NAME_RECORDED) || table.equals(ProgramConstants.TABLE_NAME_UPCOMING) || table.equals(ProgramConstants.TABLE_NAME_GUIDE)) { //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannelInfo().getChannelId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostname() + ":" + table ); ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true) .build());//from w w w .j a v a 2 s .c o m } else { String programSelection = table + "." + ProgramConstants.FIELD_CHANNEL_ID + " = ? AND " + table + "." + ProgramConstants.FIELD_START_TIME + " = ?"; String[] programSelectionArgs = new String[] { String.valueOf(program.getChannel().getChanId()), String.valueOf(program.getStartTime().getMillis()) }; programSelection = appendLocationHostname(context, locationProfile, programSelection, table); Cursor programCursor = context.getContentResolver().query(uri, programProjection, programSelection, programSelectionArgs, null); if (programCursor.moveToFirst()) { Log.v(TAG, "processProgram : UPDATE PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table); Long id = programCursor.getLong(programCursor.getColumnIndexOrThrow(ProgramConstants._ID)); ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(uri, id)) .withValues(programValues).withYieldAllowed(true).build()); } else { //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table ); ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true) .build()); } programCursor.close(); } // Log.d( TAG, "processProgram : exit" ); }
From source file:com.goliathonline.android.kegbot.io.RemoteDrinksHandler.java
/** {@inheritDoc} */ @Override/*ww w . ja v a 2 s .co m*/ public ArrayList<ContentProviderOperation> parse(JSONObject parser, ContentResolver resolver) throws JSONException, IOException { final ArrayList<ContentProviderOperation> batch = Lists.newArrayList(); // Walk document, parsing any incoming entries int drink_id = 0; JSONObject result = parser.getJSONObject("result"); JSONArray drinks = result.getJSONArray("drinks"); JSONObject drink; for (int i = 0; i < drinks.length(); i++) { if (drink_id == 0) { // && ENTRY.equals(parser.getName() // Process single spreadsheet row at a time drink = drinks.getJSONObject(i); final String drinkId = sanitizeId(drink.getString("id")); final Uri drinkUri = Drinks.buildDrinkUri(drinkId); // Check for existing details, only update when changed final ContentValues values = queryDrinkDetails(drinkUri, resolver); final long localUpdated = values.getAsLong(SyncColumns.UPDATED); final long serverUpdated = 500; //entry.getUpdated(); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "found drink " + drinkId); Log.v(TAG, "found localUpdated=" + localUpdated + ", server=" + serverUpdated); } if (localUpdated != KegbotContract.UPDATED_NEVER) continue; final Uri drinkKegUri = Drinks.buildKegUri(drinkId); final Uri drinkUserUri = Drinks.buildUserUri(drinkId); // Clear any existing values for this session, treating the // incoming details as authoritative. batch.add(ContentProviderOperation.newDelete(drinkUri).build()); batch.add(ContentProviderOperation.newDelete(drinkKegUri).build()); final ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(Drinks.CONTENT_URI); builder.withValue(SyncColumns.UPDATED, serverUpdated); builder.withValue(Drinks.DRINK_ID, drinkId); // Inherit starred value from previous row if (values.containsKey(Drinks.DRINK_STARRED)) { builder.withValue(Drinks.DRINK_STARRED, values.getAsInteger(Drinks.DRINK_STARRED)); } if (drink.has("session_id")) builder.withValue(Drinks.SESSION_ID, drink.getInt("session_id")); if (drink.has("status")) builder.withValue(Drinks.STATUS, drink.getString("status")); if (drink.has("user_id")) builder.withValue(Drinks.USER_ID, drink.getString("user_id")); if (drink.has("keg_id")) builder.withValue(Drinks.KEG_ID, drink.getInt("keg_id")); if (drink.has("volume_ml")) builder.withValue(Drinks.VOLUME, drink.getDouble("volume_ml")); if (drink.has("pour_time")) builder.withValue(Drinks.POUR_TIME, drink.getString("pour_time")); // Normal session details ready, write to provider batch.add(builder.build()); // Assign kegs final int kegId = drink.getInt("keg_id"); batch.add(ContentProviderOperation.newInsert(drinkKegUri).withValue(DrinksKeg.DRINK_ID, drinkId) .withValue(DrinksKeg.KEG_ID, kegId).build()); // Assign users if (drink.has("user_id")) { final String userId = drink.getString("user_id"); batch.add(ContentProviderOperation.newInsert(drinkUserUri) .withValue(DrinksUser.DRINK_ID, drinkId).withValue(DrinksUser.USER_ID, userId).build()); } } } return batch; }
From source file:org.codarama.haxsync.services.ContactsSyncAdapterService.java
private static void addContact(Account account, String name, String username) { ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI); builder.withValue(RawContacts.ACCOUNT_NAME, account.name); builder.withValue(RawContacts.ACCOUNT_TYPE, account.type); builder.withValue(RawContacts.SYNC1, username); operationList.add(builder.build());/*from w w w . j a v a 2 s. c om*/ builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name); operationList.add(builder.build()); builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.org.codarama.haxsync.profile"); builder.withValue(ContactsContract.Data.DATA1, username); builder.withValue(ContactsContract.Data.DATA2, "Facebook Profile"); builder.withValue(ContactsContract.Data.DATA3, "View profile"); operationList.add(builder.build()); try { mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList); } catch (Exception e) { Log.e("Error", e.getLocalizedMessage()); } }
From source file:com.canelmas.let.sample.ContactsActivity.java
/** * Accesses the Contacts content provider directly to insert a new contact. * <p>/*from w w w . j av a2 s . co m*/ * The contact is called "__DUMMY ENTRY" and only contains a name. */ private void insertDummyContact() { // Two operations are needed to insert a new contact. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(2); // First, set up a new raw contact. ContentProviderOperation.Builder op = ContentProviderOperation .newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null); operations.add(op.build()); // Next, set the name for the contact. op = 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, DUMMY_CONTACT_NAME); operations.add(op.build()); // Apply the operations. ContentResolver resolver = getContentResolver(); try { resolver.applyBatch(ContactsContract.AUTHORITY, operations); } catch (RemoteException e) { Log.d(TAG, "Could not add a new contact: " + e.getMessage()); } catch (OperationApplicationException e) { Log.d(TAG, "Could not add a new contact: " + e.getMessage()); } }
From source file:fr.mixit.android.io.JsonHandlerApplyTalks.java
@Override public boolean parseItem(JSONObject item, ContentResolver resolver) throws JSONException { final String id = item.getString(TAG_ID); mItemIds.add(id);/*from w w w. j a v a 2 s . c o m*/ final Uri itemUri = MixItContract.Sessions.buildSessionUri(id); boolean tagUpdated = false; boolean newItem = false; boolean build = false; ContentProviderOperation.Builder builder; if (ProviderParsingUtils.isRowExisting(itemUri, MixItContract.Sessions.PROJ_DETAIL.PROJECTION, resolver)) { builder = ContentProviderOperation.newUpdate(itemUri); tagUpdated = isItemUpdated(itemUri, item, resolver); } else { newItem = true; builder = ContentProviderOperation .newInsert(mIsLightningTalks ? MixItContract.Sessions.CONTENT_URI_LIGNTHNING : MixItContract.Sessions.CONTENT_URI); builder.withValue(MixItContract.Sessions.SESSION_ID, id); build = true; } if (newItem || tagUpdated) { if (item.has(TAG_TITLE)) { builder.withValue(MixItContract.Sessions.TITLE, item.getString(TAG_TITLE)); } if (item.has(TAG_SUMMARY)) { builder.withValue(MixItContract.Sessions.SUMMARY, item.getString(TAG_SUMMARY)); } if (item.has(TAG_DESC)) { builder.withValue(MixItContract.Sessions.DESC, item.getString(TAG_DESC)); } if (item.has(TAG_FORMAT)) { builder.withValue(MixItContract.Sessions.FORMAT, mIsLightningTalks ? MixItContract.Sessions.FORMAT_LIGHTNING_TALK : item.getString(TAG_FORMAT)); } else { builder.withValue(MixItContract.Sessions.FORMAT, mIsLightningTalks ? MixItContract.Sessions.FORMAT_LIGHTNING_TALK : MixItContract.Sessions.FORMAT_TALK); } if (item.has(TAG_LEVEL)) { builder.withValue(MixItContract.Sessions.LEVEL, item.getString(TAG_LEVEL)); } if (item.has(TAG_LANG)) { builder.withValue(MixItContract.Sessions.LANG, item.getString(TAG_LANG)); } if (item.has(TAG_START)) { final String start = item.getString(TAG_START); builder.withValue(MixItContract.Sessions.START, DateUtils.parseISO8601(start)); } if (item.has(TAG_END)) { final String end = item.getString(TAG_END); builder.withValue(MixItContract.Sessions.END, DateUtils.parseISO8601(end)); } if (item.has(TAG_ROOM)) { builder.withValue(MixItContract.Sessions.ROOM_ID, item.getString(TAG_ROOM)); } if (item.has(TAG_NB_VOTES)) { builder.withValue(MixItContract.Sessions.NB_VOTES, item.getString(TAG_NB_VOTES)); } build = true; } if (build) { ProviderParsingUtils.addOpeAndApplyBatch(mAuthority, resolver, mBatch, false, builder.build()); } if (mIsFullParsing && item.has(TAG_INTERESTS)) { final JSONArray interests = item.getJSONArray(TAG_INTERESTS); parseLinkedInterests(id, interests, resolver); } if (item.has(TAG_SPEAKERS)) { final JSONArray speakers = item.getJSONArray(TAG_SPEAKERS); parseLinkedSpeakers(id, speakers, resolver); } if (!mIsParsingList) { deleteItemsDataNotFound(resolver); return ProviderParsingUtils.applyBatch(mAuthority, resolver, mBatch, true); } return true; }
From source file:de.kollode.redminebrowser.sync.SyncHelper.java
void performSync(SyncResult syncResult, Account account) throws IOException { NotificationManager mNotificationManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); android.support.v4.app.NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext); mBuilder.setContentTitle("Projects").setContentText("Syncing in progress") .setSmallIcon(R.drawable.ic_launcher); final ContentResolver resolver = mContext.getContentResolver(); ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>(); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); AccountManager accMgr = AccountManager.get(this.mContext); String serverUrl = accMgr.getUserData(account, "serverUrl"); if (isOnline()) { final long startRemote = System.currentTimeMillis(); int syncedProjects = 0; Log.i(sTag, "Remote syncing speakers"); Log.i(sTag, serverUrl);//from ww w .j a va2s . c om try { int offset = 0; int numberOfProjects; int limit = 100; boolean loadMore = true; do { String restQuery = "sort=updated_on:desc&limit=" + limit + "&offset=" + offset + "&key=" + accMgr.getPassword(account); Log.d(sTag, "REST URL: " + serverUrl + "/projects.json?" + restQuery); JSONObject projectsJson = getJsonFromUrl(serverUrl + "/projects.json?" + restQuery); numberOfProjects = projectsJson.getInt("total_count"); mBuilder.setProgress(numberOfProjects, syncedProjects, false); mNotificationManager.notify(0, mBuilder.build()); if (numberOfProjects < limit + offset) { Log.d(sTag, "Enough Projects"); loadMore = false; } else { Log.d(sTag, "More Projects"); offset += limit; } JSONArray projects = projectsJson.getJSONArray("projects"); for (int i = 0; i < projects.length(); i++) { JSONObject project = projects.getJSONObject(i); Builder projectBuilder = ContentProviderOperation .newInsert(Project.buildProjectsUri(account.name)); Log.d(sTag, project.toString()); try { projectBuilder.withValue(Project.PARENT, project.getJSONObject("parent").getInt("id")); } catch (Exception e) { } batch.add(projectBuilder.withValue(BaseColumns._ID, project.getInt("id")) .withValue(Project.NAME, project.getString("name")) .withValue(Project.IDENTIFIER, project.getString("identifier")) .withValue(Project.UPDATED, System.currentTimeMillis()) .withValue(Project.CREATED, System.currentTimeMillis()) .withValue(Project.CREATED_ON, project.getString("created_on")) .withValue(Project.UPDATED_ON, project.getString("updated_on")).build()); mBuilder.setProgress(numberOfProjects, syncedProjects++, false); mNotificationManager.notify(0, mBuilder.build()); } } while (loadMore && !this.isCanceled()); try { // Apply all queued up batch operations for local data. resolver.applyBatch(RedmineTables.CONTENT_AUTHORITY, batch); } catch (RemoteException e) { throw new RuntimeException("Problem applying batch operation", e); } catch (OperationApplicationException e) { throw new RuntimeException("Problem applying batch operation", e); } } catch (Exception e) { e.printStackTrace(); } if (this.isCanceled()) { mBuilder.setContentText("Sync was canceled").setProgress(0, 0, false); } else { mBuilder.setContentText("Sync complete").setProgress(0, 0, false); } mNotificationManager.notify(0, mBuilder.build()); syncResult.delayUntil = ((long) 60 * 60); Log.d(sTag, "Remote sync took " + (System.currentTimeMillis() - startRemote) + "ms"); Log.d(sTag, "Number of projects: " + syncedProjects); } }
From source file:com.sudhirkhanger.andpress.ui.MainActivity.java
public void insertData(ArrayList<Post> postArrayList) { Log.d(LOG_TAG, "insert"); ArrayList<ContentProviderOperation> batchOperations = new ArrayList<>(10); for (Post post : postArrayList) { ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(PostProvider.Posts.CONTENT_URI); builder.withValue(PostColumns.POST_ID, post.getId()); builder.withValue(PostColumns.TITLE, post.getTitle()); builder.withValue(PostColumns.IMAGE_URL, post.getImage_url()); builder.withValue(PostColumns.CONTENT, post.getContent()); batchOperations.add(builder.build()); }/* www . j ava2 s . com*/ try { getContentResolver().applyBatch(PostProvider.AUTHORITY, batchOperations); } catch (RemoteException | OperationApplicationException e) { Log.e(LOG_TAG, "Error applying batch insert", e); } }
From source file:com.taxicop.sync.SyncAdapter.java
private static ContentProviderOperation insert(ContentValues buff) { final ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(PlateContentProvider.URI_REPORT); builder.withValue(Fields.RANKING, buff.get(Fields.RANKING)); builder.withValue(Fields.DESCRIPTION, buff.get(Fields.DESCRIPTION)); builder.withValue(Fields.DATE_REPORT, buff.get(Fields.DATE_REPORT)); builder.withValue(Fields.CAR_PLATE, buff.get(Fields.CAR_PLATE)); return builder.build(); }