List of usage examples for android.content ContentProviderClient query
public @Nullable Cursor query(@NonNull Uri url, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) throws RemoteException
From source file:org.pixmob.droidlink.sync.SyncAdapter.java
private void doPerformSync(NetworkClient client, SharedPreferences prefs, ContentProviderClient provider, SyncResult syncResult, boolean fullSync) { // Prepare the query. final String selection = DEVICE_ID + "=? AND " + STATE + "=? OR " + STATE + "=?"; final String[] selectionArgs = { client.getDeviceId(), String.valueOf(EventsContract.PENDING_UPLOAD_STATE), String.valueOf(EventsContract.PENDING_DELETE_STATE) }; // Get local data to sync. final Map<String, JSONObject> eventsToUpload = new HashMap<String, JSONObject>(8); final Set<String> eventsToDelete = new HashSet<String>(4); Cursor c = null;//from w ww . j av a 2s. co m try { c = provider.query(EventsContract.CONTENT_URI, PROJECTION, selection, selectionArgs, null); final int idIdx = c.getColumnIndexOrThrow(_ID); final int typeIdx = c.getColumnIndexOrThrow(TYPE); final int createdIdx = c.getColumnIndexOrThrow(CREATED); final int numberIdx = c.getColumnIndexOrThrow(NUMBER); final int nameIdx = c.getColumnIndexOrThrow(NAME); final int messageIdx = c.getColumnIndexOrThrow(MESSAGE); final int stateIdx = c.getColumnIndexOrThrow(STATE); while (c.moveToNext()) { final String eventId = c.getString(idIdx); final int eventState = c.getInt(stateIdx); if (EventsContract.PENDING_UPLOAD_STATE == eventState) { // This is a newly created event. final JSONObject event = new JSONObject(); try { event.put("deviceId", client.getDeviceId()); event.put("created", c.getLong(createdIdx)); event.put("type", c.getInt(typeIdx)); event.put("number", c.getString(numberIdx)); event.put("name", c.getString(nameIdx)); event.put("message", c.getString(messageIdx)); } catch (JSONException e) { Log.w(TAG, "Invalid event " + eventId + ": cannot sync", e); syncResult.stats.numSkippedEntries++; continue; } eventsToUpload.put(eventId, event); } else if (EventsContract.PENDING_DELETE_STATE == eventState) { // The user wants this event to be deleted. eventsToDelete.add(eventId); } } } catch (RemoteException e) { Log.e(TAG, "Failed to get events: cannot sync", e); syncResult.stats.numIoExceptions++; } finally { if (c != null) { c.close(); c = null; } } final ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>(32); final ContentValues values = new ContentValues(8); if (eventsToDelete.isEmpty()) { Log.i(TAG, "No events to delete"); } else { Log.i(TAG, "Found " + eventsToDelete.size() + " event(s) to delete"); } // Delete events on the remote server. for (final String eventId : eventsToDelete) { if (DEVELOPER_MODE) { Log.d(TAG, "Deleting event: " + eventId); } try { client.delete("/events/" + eventId); if (DEVELOPER_MODE) { Log.d(TAG, "Deleting event in local database: " + eventId); } batch.add(ContentProviderOperation .newDelete(Uri.withAppendedPath(EventsContract.CONTENT_URI, eventId)).build()); syncResult.stats.numDeletes++; } catch (IOException e) { Log.e(TAG, "Event deletion error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } catch (AppEngineAuthenticationException e) { Log.e(TAG, "Authentication error: cannot sync", e); syncResult.stats.numAuthExceptions++; return; } } try { provider.applyBatch(batch); } catch (Exception e) { Log.w(TAG, "Database error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } batch.clear(); if (fullSync) { // Get all events from the remote server. final JSONArray events; if (DEVELOPER_MODE) { Log.d(TAG, "Fetching events from the remote server"); } try { events = client.getAsArray("/events"); } catch (IOException e) { Log.e(TAG, "Event listing error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } catch (AppEngineAuthenticationException e) { Log.e(TAG, "Authentication error: cannot sync", e); syncResult.stats.numAuthExceptions++; return; } final int eventsLen = events != null ? events.length() : 0; if (eventsLen == 0) { Log.i(TAG, "No events from the remote server"); } else { Log.i(TAG, "Found " + eventsLen + " event(s) from the remote server"); } // Build a collection with local event identifiers. // This collection will be used to identify which events have // been deleted on the remote server. final Set<String> localEventIds; try { c = provider.query(EventsContract.CONTENT_URI, PROJECTION_ID, STATE + "=?", new String[] { String.valueOf(EventsContract.UPLOADED_STATE) }, null); localEventIds = new HashSet<String>(c.getCount()); final int idIdx = c.getColumnIndexOrThrow(_ID); while (c.moveToNext()) { final String eventId = c.getString(idIdx); localEventIds.add(eventId); } } catch (RemoteException e) { Log.e(TAG, "Failed to get events from local database", e); syncResult.stats.numIoExceptions++; return; } finally { if (c != null) { c.close(); c = null; } } String newEventId = null; int newEventCount = 0; // Reconcile remote events with local events. for (int i = 0; i < eventsLen; ++i) { String eventId = null; try { final JSONObject event = events.getJSONObject(i); eventId = event.getString("id"); // Check if this event exists in the local database. if (localEventIds.contains(eventId)) { // Found the event: update it. values.clear(); values.put(NUMBER, trimToNull(event.getString("number"))); values.put(NAME, trimToNull(event.getString("name"))); values.put(MESSAGE, trimToNull(event.getString("message"))); if (DEVELOPER_MODE) { Log.d(TAG, "Updating event in local database: " + eventId); } batch.add(ContentProviderOperation .newUpdate(Uri.withAppendedPath(EventsContract.CONTENT_URI, eventId)) .withExpectedCount(1).withValues(values).build()); syncResult.stats.numUpdates++; } else { // The event was not found: insert it. values.clear(); values.put(_ID, eventId); values.put(DEVICE_ID, event.getString("deviceId")); values.put(CREATED, event.getLong("created")); values.put(TYPE, event.getInt("type")); values.put(NUMBER, trimToNull(event.getString("number"))); values.put(NAME, trimToNull(event.getString("name"))); values.put(MESSAGE, trimToNull(event.getString("message"))); values.put(STATE, EventsContract.UPLOADED_STATE); if (DEVELOPER_MODE) { Log.d(TAG, "Adding event to local database: " + eventId); } batch.add(ContentProviderOperation .newInsert(Uri.withAppendedPath(EventsContract.CONTENT_URI, eventId)) .withValues(values).build()); syncResult.stats.numInserts++; ++newEventCount; if (newEventId == null) { newEventId = eventId; } } // This event now exists in the local database: // remove its identifier from this collection as we // don't want to delete it. localEventIds.remove(eventId); } catch (JSONException e) { Log.w(TAG, "Invalid event at index " + i + ": cannot sync", e); syncResult.stats.numSkippedEntries++; continue; } } // The remaining event identifiers was removed on the remote // server: there are still present in the local database. These // events are now being deleted. for (final String eventId : localEventIds) { if (DEVELOPER_MODE) { Log.d(TAG, "Deleting event in local database: " + eventId); } batch.add(ContentProviderOperation .newDelete(Uri.withAppendedPath(EventsContract.CONTENT_URI, eventId)).build()); syncResult.stats.numDeletes++; } try { provider.applyBatch(batch); } catch (Exception e) { Log.e(TAG, "Database error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } batch.clear(); if (newEventCount > 1) { newEventId = null; } if (newEventCount != 0) { startSyncNotificationService(newEventCount, newEventId); } } final int numEventsToUpload = eventsToUpload.size(); if (numEventsToUpload == 0) { Log.i(TAG, "No events to upload"); } else { Log.i(TAG, "Found " + numEventsToUpload + " event(s) to upload"); } // Send local events to the remote server. for (final Map.Entry<String, JSONObject> entry : eventsToUpload.entrySet()) { final String eventId = entry.getKey(); if (DEVELOPER_MODE) { Log.d(TAG, "Uploading event: " + eventId); } final JSONObject event = entry.getValue(); try { client.put("/events/" + eventId, event); if (DEVELOPER_MODE) { Log.d(TAG, "Updating event state to UPLOADED: " + eventId); } values.clear(); values.put(STATE, EventsContract.UPLOADED_STATE); batch.add(ContentProviderOperation .newUpdate(Uri.withAppendedPath(EventsContract.CONTENT_URI, eventId)).withValues(values) .withExpectedCount(1).build()); syncResult.stats.numUpdates++; Log.i(TAG, "Event upload successful: " + eventId); } catch (NetworkClientException e) { if (e.getStatusCode() == 404) { Log.e(TAG, "Device not found: cannot sync", e); registerDevice(); } else { Log.e(TAG, "Network error: cannot sync", e); } syncResult.stats.numIoExceptions++; return; } catch (IOException e) { Log.e(TAG, "Event upload error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } catch (AppEngineAuthenticationException e) { Log.e(TAG, "Authentication error: cannot sync", e); syncResult.stats.numAuthExceptions++; return; } } try { provider.applyBatch(batch); } catch (Exception e) { Log.w(TAG, "Database error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } batch.clear(); final SharedPreferences.Editor prefsEditor = prefs.edit(); final boolean syncRequired = !eventsToDelete.isEmpty() || !eventsToUpload.isEmpty(); if (syncRequired) { // Generate an unique sync token: the server will send this token to // every devices. If this token is received on this device, the sync // will not start. final String syncToken = UUID.randomUUID().toString(); prefsEditor.putString(SP_KEY_SYNC_TOKEN, syncToken); Features.getFeature(SharedPreferencesSaverFeature.class).save(prefsEditor); // Sync user devices. try { final JSONObject data = new JSONObject(); data.put("token", syncToken); client.post("/devices/" + client.getDeviceId() + "/sync", data); } catch (NetworkClientException e) { if (e.getStatusCode() == 404) { registerDevice(); } } catch (IOException e) { Log.e(TAG, "Device sync error: cannot sync", e); syncResult.stats.numIoExceptions++; return; } catch (AppEngineAuthenticationException e) { Log.e(TAG, "Authentication error: cannot sync", e); syncResult.stats.numAuthExceptions++; return; } catch (JSONException e) { Log.w(TAG, "Invalid sync token " + syncToken + ": cannot sync", e); syncResult.stats.numIoExceptions++; return; } } // Store sync time. prefsEditor.putLong(SP_KEY_LAST_SYNC, System.currentTimeMillis()); Features.getFeature(SharedPreferencesSaverFeature.class).save(prefsEditor); }
From source file:org.ohmage.sync.ResponseSyncAdapter.java
@Override public synchronized void onPerformSync(Account account, Bundle extras, String authority, final ContentProviderClient provider, final SyncResult syncResult) { Log.d(TAG, "Start onPerformSync()"); // Check for authtoken String token = null;//from w w w. java2 s . c o m try { token = am.blockingGetAuthToken(account, AuthUtil.AUTHTOKEN_TYPE, true); } catch (OperationCanceledException e) { syncResult.stats.numSkippedEntries++; } catch (IOException e) { syncResult.stats.numIoExceptions++; } catch (AuthenticatorException e) { syncResult.stats.numAuthExceptions++; } // If the token wasn't found or there was a problem, we can stop now if (token == null || syncResult.stats.numSkippedEntries > 0 || syncResult.stats.numIoExceptions > 0 || syncResult.stats.numAuthExceptions > 0) { Log.d(TAG, "No token found or there was a problem."); return; } // Upload responses Observable<Long> toDelete = null; Observable<ResponseFiles> filesToDelete = null; Cursor cursor = null; try { cursor = provider.query(Responses.CONTENT_URI, new String[] { BaseColumns._ID, Responses.SURVEY_ID, Responses.SURVEY_VERSION, Responses.RESPONSE_DATA, Responses.RESPONSE_METADATA, Responses.RESPONSE_EXTRAS }, null, null, null); AppLogManager.getInstance().logInfo(mContext, "ResponsesSyncStarted", cursor.getCount() + " surveys to upload."); while (cursor.moveToNext()) { final ResponseFiles files = gson.fromJson(cursor.getString(5), ResponseFiles.class); try { // Make the call to upload responses Observable<Response> uploadResponse = null; if (Ohmage.USE_DSU_DATAPOINTS_API) { uploadResponse = uploadDatapoint(cursor, files); } else { uploadOhmagePoint(cursor, files); } // Map the data for the upload response to the local id in the db final long localResponseId = cursor.getLong(0); Observable<Long> responseId = uploadResponse.map(new Func1<Response, Long>() { @Override public Long call(Response response) { return localResponseId; } }); if (toDelete == null) { toDelete = responseId; } else { toDelete = Observable.mergeDelayError(responseId, toDelete); } // Map the data for the upload response to the files in the db Observable<ResponseFiles> responseFiles = uploadResponse .map(new Func1<Response, ResponseFiles>() { @Override public ResponseFiles call(Response response) { return files; } }); if (filesToDelete == null) { filesToDelete = responseFiles; } else { filesToDelete = Observable.mergeDelayError(responseFiles, filesToDelete); } } catch (AuthenticationException e) { Crashlytics.logException(e); Log.e(TAG, "Auth", e); syncResult.stats.numAuthExceptions++; } catch (Exception e) { Log.e(TAG, "Other uploading error", e); Crashlytics.logException(e); } } cursor.close(); } catch (RemoteException e) { Log.e(TAG, "Remote", e); Crashlytics.logException(e); syncResult.stats.numIoExceptions++; } finally { if (cursor != null) cursor.close(); } if (toDelete != null) { Log.d(TAG, "Start deleting responses."); toDelete.flatMap(new Func1<Long, Observable<ContentProviderOperation>>() { @Override public Observable<ContentProviderOperation> call(Long aLong) { return Observable.from(ContentProviderOperation.newDelete( appendSyncAdapterParam(ContentUris.withAppendedId(Responses.CONTENT_URI, aLong))) .build()); } }).subscribe(new Subscriber<ContentProviderOperation>() { ArrayList<ContentProviderOperation> toDelete = new ArrayList<ContentProviderOperation>(); @Override public void onCompleted() { try { getContext().getContentResolver().applyBatch(ResponseContract.CONTENT_AUTHORITY, toDelete); } catch (RemoteException e) { syncResult.stats.numIoExceptions++; } catch (OperationApplicationException e) { syncResult.stats.numIoExceptions++; } unsubscribe(); } @Override public void onError(Throwable e) { // Send error report Log.e(TAG, "Upload failed", e); Crashlytics.logException(e); onCompleted(); } @Override public void onNext(ContentProviderOperation args) { toDelete.add(args); } }); } if (filesToDelete != null) { filesToDelete.doOnNext(new Action1<ResponseFiles>() { @Override public void call(ResponseFiles responseFiles) { for (String s : responseFiles.getIds()) { responseFiles.getFile(s).delete(); } } }).subscribe(new Subscriber<ResponseFiles>() { @Override public void onCompleted() { unsubscribe(); } @Override public void onError(Throwable e) { Crashlytics.logException(e); } @Override public void onNext(ResponseFiles responseFiles) { } }); } }
From source file:org.gege.caldavsyncadapter.syncadapter.SyncAdapter.java
/** * checks the android events for the dirty flag. * the flag is set by android when the event has been changed. * the dirty flag is removed when an android event has been updated from calendar event * @param provider//from www . ja v a 2 s.c o m * @param account * @param calendarUri * @param facade * @param caldavCalendarUri * @param stats * @param notifyList * @return count of dirty events */ private int checkDirtyAndroidEvents(ContentProviderClient provider, Account account, Uri calendarUri, CaldavFacade facade, URI caldavCalendarUri, SyncStats stats, ArrayList<Uri> notifyList) { Cursor curEvent = null; Cursor curAttendee = null; Cursor curReminder = null; Long EventID; Long CalendarID; AndroidEvent androidEvent = null; int rowDirty = 0; int rowInsert = 0; int rowUpdate = 0; int rowDelete = 0; try { CalendarID = ContentUris.parseId(calendarUri); String selection = "(" + Events.DIRTY + " = ?) AND (" + Events.CALENDAR_ID + " = ?)"; String[] selectionArgs = new String[] { "1", CalendarID.toString() }; curEvent = provider.query(Events.CONTENT_URI, null, selection, selectionArgs, null); while (curEvent.moveToNext()) { EventID = curEvent.getLong(curEvent.getColumnIndex(Events._ID)); Uri returnedUri = ContentUris.withAppendedId(Events.CONTENT_URI, EventID); androidEvent = new AndroidEvent(account, provider, returnedUri, calendarUri); androidEvent.readContentValues(curEvent); selection = "(" + Attendees.EVENT_ID + " = ?)"; selectionArgs = new String[] { String.valueOf(EventID) }; curAttendee = provider.query(Attendees.CONTENT_URI, null, selection, selectionArgs, null); selection = "(" + Reminders.EVENT_ID + " = ?)"; selectionArgs = new String[] { String.valueOf(EventID) }; curReminder = provider.query(Reminders.CONTENT_URI, null, selection, selectionArgs, null); androidEvent.readAttendees(curAttendee); androidEvent.readReminder(curReminder); curAttendee.close(); curReminder.close(); String SyncID = androidEvent.ContentValues.getAsString(Events._SYNC_ID); boolean Deleted = false; int intDeleted = 0; intDeleted = curEvent.getInt(curEvent.getColumnIndex(Events.DELETED)); Deleted = (intDeleted == 1); if (SyncID == null) { // new Android event String newGUID = java.util.UUID.randomUUID().toString() + "-caldavsyncadapter"; String calendarPath = caldavCalendarUri.getPath(); if (!calendarPath.endsWith("/")) calendarPath += "/"; SyncID = calendarPath + newGUID + ".ics"; androidEvent.createIcs(newGUID); if (facade.createEvent(URI.create(SyncID), androidEvent.getIcsEvent().toString())) { //HINT: bugfix for google calendar if (SyncID.contains("@")) SyncID = SyncID.replace("@", "%40"); ContentValues values = new ContentValues(); values.put(Events._SYNC_ID, SyncID); //google doesn't send the etag after creation //HINT: my SabreDAV send always the same etag after putting a new event //String LastETag = facade.getLastETag(); //if (!LastETag.equals("")) { // values.put(Event.ETAG, LastETag); //} else { //so get the etag with a new REPORT CalendarEvent calendarEvent = new CalendarEvent(account, provider); calendarEvent.calendarURL = caldavCalendarUri.toURL(); URI SyncURI = new URI(SyncID); calendarEvent.setUri(SyncURI); CaldavFacade.getEvent(calendarEvent); values.put(Event.ETAG, calendarEvent.getETag()); //} values.put(Event.UID, newGUID); values.put(Events.DIRTY, 0); values.put(Event.RAWDATA, androidEvent.getIcsEvent().toString()); int rowCount = provider.update( asSyncAdapter(androidEvent.getUri(), account.name, account.type), values, null, null); if (rowCount == 1) { rowInsert += 1; notifyList.add(androidEvent.getUri()); } } } else if (Deleted) { // deleted Android event if (facade.deleteEvent(URI.create(SyncID), androidEvent.getETag())) { String mSelectionClause = "(" + Events._ID + "= ?)"; String[] mSelectionArgs = { String.valueOf(EventID) }; int countDeleted = provider.delete( asSyncAdapter(Events.CONTENT_URI, account.name, account.type), mSelectionClause, mSelectionArgs); if (countDeleted == 1) { rowDelete += 1; notifyList.add(androidEvent.getUri()); } } } else { //update the android event to the server String uid = androidEvent.getUID(); if ((uid == null) || (uid.equals(""))) { //COMPAT: this is needed because in the past, the UID was not stored in the android event CalendarEvent calendarEvent = new CalendarEvent(account, provider); URI syncURI = new URI(SyncID); calendarEvent.setUri(syncURI); calendarEvent.calendarURL = caldavCalendarUri.toURL(); if (calendarEvent.fetchBody()) { calendarEvent.readContentValues(); uid = calendarEvent.getUID(); } } if (uid != null) { androidEvent.createIcs(uid); if (facade.updateEvent(URI.create(SyncID), androidEvent.getIcsEvent().toString(), androidEvent.getETag())) { selection = "(" + Events._ID + "= ?)"; selectionArgs = new String[] { EventID.toString() }; androidEvent.ContentValues.put(Events.DIRTY, 0); //google doesn't send the etag after update String LastETag = facade.getLastETag(); if (!LastETag.equals("")) { androidEvent.ContentValues.put(Event.ETAG, LastETag); } else { //so get the etag with a new REPORT CalendarEvent calendarEvent = new CalendarEvent(account, provider); calendarEvent.calendarURL = caldavCalendarUri.toURL(); URI SyncURI = new URI(SyncID); calendarEvent.setUri(SyncURI); CaldavFacade.getEvent(calendarEvent); androidEvent.ContentValues.put(Event.ETAG, calendarEvent.getETag()); } androidEvent.ContentValues.put(Event.RAWDATA, androidEvent.getIcsEvent().toString()); int RowCount = provider.update( asSyncAdapter(androidEvent.getUri(), account.name, account.type), androidEvent.ContentValues, null, null); if (RowCount == 1) { rowUpdate += 1; notifyList.add(androidEvent.getUri()); } } else { rowDirty += 1; } } else { rowDirty += 1; } } } curEvent.close(); /*if ((rowInsert > 0) || (rowUpdate > 0) || (rowDelete > 0) || (rowDirty > 0)) { Log.i(TAG,"Android Rows inserted: " + String.valueOf(rowInsert)); Log.i(TAG,"Android Rows updated: " + String.valueOf(rowUpdate)); Log.i(TAG,"Android Rows deleted: " + String.valueOf(rowDelete)); Log.i(TAG,"Android Rows dirty: " + String.valueOf(rowDirty)); }*/ stats.numInserts += rowInsert; stats.numUpdates += rowUpdate; stats.numDeletes += rowDelete; stats.numSkippedEntries += rowDirty; stats.numEntries += rowInsert + rowUpdate + rowDelete; } catch (RemoteException e) { e.printStackTrace(); } catch (URISyntaxException e) { // TODO Automatisch generierter Erfassungsblock e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Automatisch generierter Erfassungsblock e.printStackTrace(); } catch (IOException e) { // TODO Automatisch generierter Erfassungsblock e.printStackTrace(); } catch (CaldavProtocolException e) { // TODO Automatisch generierter Erfassungsblock e.printStackTrace(); } catch (ParserException e) { // TODO Automatisch generierter Erfassungsblock e.printStackTrace(); } return rowDirty; }
From source file:info.dc585.hpt.sa.SyncAdapter.java
private void syncTopHackers(ContentProviderClient provider, SyncResult syncResult) { try {/*www. j av a2s. co m*/ Log.d(TAG, "Calling something to sync up hackerpoint db"); JSONArray hackers = NetworkUtilities.getTopHackers(); if (hackers == null) { Log.e(TAG, "somehow got null hackers, explode please"); syncResult.stats.numParseExceptions++; } if (hackers.length() == 0) { return; } Log.i(TAG, "Updateing content provider"); Log.i(TAG, "hackers.length():" + hackers.length()); for (int i = 0; i < hackers.length(); i++) { JSONObject jo = hackers.getJSONObject(i); String name = jo.getString("name"); String selection = TopHackerTable.COLUMN_NAME + " like ?"; String[] selectionArgs = { name }; String[] projection = { TopHackerTable.COLUMN_ID, TopHackerTable.COLUMN_NAME }; ContentValues values = new ContentValues(); // FIXME: Dear self, WTF would i trust network returned data without verifying limits ;) // TODO: See FIXME above. Cursor c = provider.query(HackerPointsContentProvider.HACKERS_URI, projection, selection, selectionArgs, null); if (c.getCount() == 0) { Log.d(TAG, "performing insert for new name:" + name); int points = jo.getInt("points"); values.put(TopHackerTable.COLUMN_POINTS, points); int pool = jo.getInt("pool"); values.put(TopHackerTable.COLUMN_POOL, pool); int internets = jo.getInt("internets"); values.put(TopHackerTable.COLUMN_INTERNETS, internets); String pictureURL = jo.getString("pictureURL"); values.put(TopHackerTable.COLUMN_PICTUREURL, pictureURL); values.put(TopHackerTable.COLUMN_NAME, name); values.put(TopHackerTable.COLUMN_UPDATE, System.currentTimeMillis()); provider.insert(HackerPointsContentProvider.HACKERS_URI, values); syncResult.stats.numInserts++; } else if (c.getCount() == 1) { Log.d(TAG, "performing update for name:" + name); if (name == null || name.isEmpty()) { Log.e(TAG, "null or empty name"); continue; } int points = jo.getInt("points"); values.put(TopHackerTable.COLUMN_POINTS, points); int pool = jo.getInt("pool"); values.put(TopHackerTable.COLUMN_POOL, pool); int internets = jo.getInt("internets"); values.put(TopHackerTable.COLUMN_INTERNETS, internets); String pictureURL = jo.getString("pictureURL"); values.put(TopHackerTable.COLUMN_PICTUREURL, pictureURL); int rows = provider.update(HackerPointsContentProvider.HACKERS_URI, values, selection, selectionArgs); Log.d(TAG, "Updated:" + rows + ": rows"); syncResult.stats.numUpdates += rows; } else { Log.e(TAG, "Cursor count was not 0 or 1 was:" + c.getCount()); continue; } } // Now run through both lists and figure out which one to delete. String[] projection = { TopHackerTable.COLUMN_NAME }; Cursor c = provider.query(HackerPointsContentProvider.HACKERS_URI, projection, null, null, null); if (c.getCount() == 0) { return; } ArrayList<String> goodNames = new ArrayList<String>(); ArrayList<String> rmNames = new ArrayList<String>(); for (int i = 0; i < hackers.length(); i++) { JSONObject jo = hackers.getJSONObject(i); String name = jo.getString("name"); goodNames.add(name); } while (c.moveToNext()) { int namec = c.getColumnIndex(TopHackerTable.COLUMN_NAME); String dbName = c.getString(namec); if (goodNames.contains(dbName) == false) { Log.d(TAG, "Adding name:" + dbName + ": to the delete list"); rmNames.add(dbName); } } for (String nextName : rmNames) { Log.d(TAG, "deleting name:" + nextName + ":"); // FIXME: use column name from Table class String where = " name = ? "; String[] whereArgs = { nextName }; int rows = provider.delete(HackerPointsContentProvider.HACKERS_URI, where, whereArgs); syncResult.stats.numDeletes += rows; } } catch (final AuthenticationException e) { Log.e(TAG, "AuthenticationException", e); } catch (final JSONException e) { Log.e(TAG, "JSONException", e); } catch (final IOException e) { Log.e(TAG, "IOException", e); } catch (final ParseException e) { Log.e(TAG, "ParseException", e); syncResult.stats.numParseExceptions++; } catch (final RemoteException e) { Log.e(TAG, "RemoteException", e); } }
From source file:edu.mit.mobile.android.locast.sync.SyncEngine.java
/** * Uploads any unpublished casts.// w ww . ja va2s .c om * * This is the method that does all the hard work. * * @param toSync * @param provider * @param syncMap * @param syncResult * @return the number of casts uploaded. * @throws JSONException * @throws NetworkProtocolException * @throws IOException * @throws NoPublicPath * @throws RemoteException * @throws OperationApplicationException * @throws SyncException * @throws InterruptedException */ private int uploadUnpublished(Uri toSync, Account account, ContentProviderClient provider, SyncMap syncMap, HashMap<String, SyncEngine.SyncStatus> syncStatuses, SyncResult syncResult) throws JSONException, NetworkProtocolException, IOException, NoPublicPath, RemoteException, OperationApplicationException, SyncException, InterruptedException { int count = 0; final String type = provider.getType(toSync); final boolean isDir = type.startsWith(CONTENT_TYPE_PREFIX_DIR); final Cursor uploadMe = provider.query(toSync, null, SELECTION_UNPUBLISHED, null, null); if (uploadMe == null) { throw new SyncException("could not query " + toSync); } final int idCol = uploadMe.getColumnIndex(JsonSyncableItem._ID); try { for (uploadMe.moveToFirst(); !uploadMe.isAfterLast(); uploadMe.moveToNext()) { if (Thread.interrupted()) { throw new InterruptedException(); } final long id = uploadMe.getLong(idCol); final Uri localUri = isDir ? ContentUris.withAppendedId(toSync, id) : toSync; final String postUri = MediaProvider.getPostPath(mContext, localUri); Intent intent = new Intent(SYNC_STATUS_CHANGED); intent.putExtra(EXTRA_SYNC_STATUS, "castBegin"); intent.putExtra(EXTRA_SYNC_ID, id); mContext.sendStickyBroadcast(intent); try { final JSONObject jo = JsonSyncableItem.toJSON(mContext, localUri, uploadMe, syncMap); if (DEBUG) { Log.d(TAG, "uploading " + localUri + " to " + postUri); } // Upload! Any non-successful responses are handled by // exceptions. final HttpResponse res = mNetworkClient.post(postUri, jo.toString()); long serverTime; try { serverTime = getServerTime(res); // We should never get a corrupted date from the server, // but if it does happen, // using the local time is a sane fallback. } catch (final DateParseException e) { serverTime = System.currentTimeMillis(); } // newly-created items return the JSON serialization of the // object as the server // knows it, so the local database needs to be updated to // reflect that. final JSONObject newJo = NetworkClient.toJsonObject(res); try { final SyncStatus ss = loadItemFromJsonObject(newJo, syncMap, serverTime); // update immediately, so that any cancellation or // interruption of the sync // keeps the local state in sync with what's on the // server final int updates = provider.update(localUri, ss.remoteCVs, null, null); final String locUriString = localUri.toString(); if (updates == 1) { ss.state = SyncState.NOW_UP_TO_DATE; ss.local = localUri; // ensure that it's findable by local URI too syncStatuses.put(locUriString, ss); syncMap.onPostSyncItem(mContext, account, ss.local, ss.remoteJson, true); count++; syncResult.stats.numUpdates++; } else { Log.e(TAG, "error updating " + locUriString); syncResult.stats.numSkippedEntries++; } syncResult.stats.numEntries++; } catch (final JSONException e) { if (DEBUG) { Log.e(TAG, "result was " + newJo.toString()); } throw e; } } finally { intent = new Intent(SYNC_STATUS_CHANGED); intent.putExtra(EXTRA_SYNC_STATUS, "castEnd"); intent.putExtra(EXTRA_SYNC_ID, id); mContext.sendStickyBroadcast(intent); } } } finally { uploadMe.close(); } return count; }
From source file:net.sf.diningout.content.SyncAdapter.java
/** * Insert new system contacts, delete orphaned app contacts, and synchronise any changes to * existing.//from w w w.ja v a 2 s. c o m */ private void refreshContacts(Context context, ContentProviderClient cp) throws RemoteException { /* get system contacts */ String[] proj = { Email.ADDRESS, ContactsContract.Contacts.LOOKUP_KEY, RawContacts.CONTACT_ID, ContactsContract.Contacts.DISPLAY_NAME }; String sel = Email.IN_VISIBLE_GROUP + " = 1 AND " + Email.ADDRESS + " <> ?"; String[] args = { Accounts.selected().name }; EasyCursor sys = new EasyCursor(cr().query(Email.CONTENT_URI, proj, sel, args, Email.ADDRESS)); /* get app contacts */ proj = new String[] { Contacts.EMAIL, Contacts.ANDROID_LOOKUP_KEY, Contacts.ANDROID_ID, Contacts.NAME, _ID, Contacts.FOLLOWING, Contacts.STATUS_ID }; sel = Contacts.EMAIL + " IS NOT NULL"; EasyCursor app = new EasyCursor(cp.query(CONTACTS_URI, proj, sel, null, Contacts.EMAIL)); /* compare and sync */ ContentValues vals = new ContentValues(); for (CursorJoiner.Result result : new CursorJoiner(sys, new String[] { Email.ADDRESS }, app, new String[] { Contacts.EMAIL })) { switch (result) { case LEFT: // new system contact, insert into app contacts String email = sys.getString(Email.ADDRESS); String hash = BaseEncoding.base64() .encode(Hashing.sha512().hashString(email.toLowerCase(ENGLISH), UTF_8).asBytes()); long id = Contacts.idForHash(hash); // do we have this contact and not know it? /* insert or update values */ vals.put(Contacts.ANDROID_LOOKUP_KEY, sys.getString(ContactsContract.Contacts.LOOKUP_KEY)); vals.put(Contacts.ANDROID_ID, sys.getLong(RawContacts.CONTACT_ID)); String name = sys.getString(ContactsContract.Contacts.DISPLAY_NAME); vals.put(Contacts.NAME, name); vals.put(Contacts.NORMALISED_NAME, SQLite.normalise(name)); vals.put(Contacts.EMAIL, email); if (id <= 0) { vals.put(Contacts.EMAIL_HASH, hash); vals.put(Contacts.COLOR, Contacts.defaultColor()); id = ContentUris.parseId(cp.insert(CONTACTS_URI, vals)); } else { cp.update(ContentUris.withAppendedId(CONTACTS_URI, id), vals, null, null); } if (id > 0) { context.startService(new Intent(context, FriendColorService.class) .putExtra(FriendColorService.EXTRA_ID, id)); } break; case RIGHT: // orphaned app contact, delete unless user is following if (app.getInt(Contacts.FOLLOWING) == 0 && app.getInt(Contacts.STATUS_ID) == ACTIVE.id) { vals.put(Contacts.STATUS_ID, DELETED.id); vals.put(Contacts.DIRTY, 1); cp.update(Uris.appendId(CONTACTS_URI, app), vals, null, null); } break; case BOTH: // matching contacts, update details in app if needed String s = sys.getString(ContactsContract.Contacts.LOOKUP_KEY); if (!s.equals(app.getString(Contacts.ANDROID_LOOKUP_KEY))) { vals.put(Contacts.ANDROID_LOOKUP_KEY, s); } long l = sys.getLong(RawContacts.CONTACT_ID); if (l != app.getLong(Contacts.ANDROID_ID)) { vals.put(Contacts.ANDROID_ID, l); } s = sys.getString(ContactsContract.Contacts.DISPLAY_NAME); if (!s.equals(app.getString(Contacts.NAME))) { vals.put(Contacts.NAME, s); vals.put(Contacts.NORMALISED_NAME, SQLite.normalise(s)); } if (app.getInt(Contacts.STATUS_ID) == DELETED.id) { vals.put(Contacts.STATUS_ID, ACTIVE.id); vals.put(Contacts.DIRTY, 1); } if (vals.size() > 0) { cp.update(Uris.appendId(CONTACTS_URI, app), vals, null, null); context.startService(new Intent(context, FriendColorService.class) .putExtra(FriendColorService.EXTRA_ID, app.getLong(_ID))); } break; } vals.clear(); } sys.close(); app.close(); }
From source file:net.sf.diningout.content.SyncAdapter.java
/** * Download and update the details of Google Places that haven't been refreshed recently. *///ww w .j a va 2 s .co m private void refreshRestaurants(Context context, ContentProviderClient cp) throws RemoteException { int rows = Content.getCount(context, RESTAURANTS_URI); int days = 30; // age when restaurants should be refreshed Uri uri = Uris.limit(RESTAURANTS_URI, rows / days + 1); String[] proj = { _ID, Restaurants.PLACE_ID }; String sel = Restaurants.PLACE_ID + " IS NOT NULL AND " + Restaurants.PLACE_ID + " NOT LIKE 'NOT_FOUND_%' AND (" + Restaurants.REFRESHED_ON + " IS NULL OR " + Restaurants.REFRESHED_ON + " <= datetime('now', '-" + days + " days')) AND " + Restaurants.STATUS_ID + " = ?"; String[] args = { String.valueOf(ACTIVE.id) }; String order = Restaurants.REFRESHED_ON + " IS NULL DESC, " + Restaurants.REFRESHED_ON + ", " + _ID; EasyCursor c = new EasyCursor(cp.query(uri, proj, sel, args, order)); Result[] results = null; try { results = RestaurantsRefreshService.refresh(c); } catch (IOException e) { Log.e(TAG, "refreshing restaurants", e); exception(e); } c.close(); if (results != null) { for (Result result : results) { if (result.newReviewTimes != null) { int size = result.newReviewTimes.size(); for (int i = 0; i < size; i++) { cp.insert(SYNCS_URI, Syncs.values(result.newReviewTimes.keyAt(i), result.newReviewTimes.valueAt(i))); } } } } }
From source file:org.totschnig.myexpenses.sync.SyncAdapter.java
private List<TransactionChange> getLocalChanges(ContentProviderClient provider, long accountId, long sequenceNumber) throws RemoteException { List<TransactionChange> result = new ArrayList<>(); Uri changesUri = buildChangesUri(sequenceNumber, accountId); boolean hasLocalChanges = hasLocalChanges(provider, changesUri); if (hasLocalChanges) { ContentValues currentSyncIncrease = new ContentValues(1); long nextSequence = sequenceNumber + 1; currentSyncIncrease.put(KEY_SYNC_SEQUENCE_LOCAL, nextSequence); //in case of failed syncs due to non-available backends, sequence number might already be higher than nextSequence //we must take care to not decrease it here provider.update(TransactionProvider.ACCOUNTS_URI, currentSyncIncrease, KEY_ROWID + " = ? AND " + KEY_SYNC_SEQUENCE_LOCAL + " < ?", new String[] { String.valueOf(accountId), String.valueOf(nextSequence) }); }// w w w . j av a2s .c o m if (hasLocalChanges) { Cursor c = provider.query(changesUri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { do { TransactionChange transactionChange = TransactionChange.create(c); result.add(transactionChange); } while (c.moveToNext()); } c.close(); } } return result; }
From source file:org.kontalk.sync.Syncer.java
/** * The actual sync procedure./* ww w. j a v a 2s.c o m*/ * This one uses the slowest method ever: it first checks for every phone * number in all contacts and it sends them to the server. Once a response * is received, it deletes all the raw contacts created by us and then * recreates only the ones the server has found a match for. */ public void performSync(Context context, Account account, String authority, ContentProviderClient provider, ContentProviderClient usersProvider, SyncResult syncResult) throws OperationCanceledException { final Map<String, RawPhoneNumberEntry> lookupNumbers = new HashMap<>(); final List<String> jidList = new ArrayList<>(); // resync users database Log.v(TAG, "resyncing users database"); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); // update users database Uri uri = Users.CONTENT_URI.buildUpon().appendQueryParameter(Users.RESYNC, "true").build(); try { int count = usersProvider.update(uri, new ContentValues(), null, null); Log.d(TAG, "users database resynced (" + count + ")"); } catch (Exception e) { Log.e(TAG, "error resyncing users database - aborting sync", e); syncResult.databaseError = true; return; } // query all contacts Cursor cursor; try { cursor = usersProvider.query(Users.CONTENT_URI_OFFLINE, new String[] { Users.JID, Users.NUMBER, Users.LOOKUP_KEY }, null, null, null); } catch (Exception e) { Log.e(TAG, "error querying users database - aborting sync", e); syncResult.databaseError = true; return; } while (cursor.moveToNext()) { if (mCanceled) { cursor.close(); throw new OperationCanceledException(); } String jid = cursor.getString(0); String number = cursor.getString(1); String lookupKey = cursor.getString(2); // avoid to send duplicates to the server if (lookupNumbers.put(XmppStringUtils.parseLocalpart(jid), new RawPhoneNumberEntry(lookupKey, number, jid)) == null) jidList.add(jid); } cursor.close(); if (mCanceled) throw new OperationCanceledException(); // empty contacts :-| if (jidList.size() == 0) { // delete all Kontalk raw contacts try { syncResult.stats.numDeletes += deleteAll(account, provider); } catch (Exception e) { Log.e(TAG, "contact delete error", e); syncResult.databaseError = true; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { try { syncResult.stats.numDeletes += deleteProfile(account, provider); } catch (Exception e) { Log.e(TAG, "profile delete error", e); syncResult.databaseError = true; } } commit(usersProvider, syncResult); } else { final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mContext); // register presence broadcast receiver PresenceBroadcastReceiver receiver = new PresenceBroadcastReceiver(jidList, this); IntentFilter f = new IntentFilter(); f.addAction(MessageCenterService.ACTION_PRESENCE); f.addAction(MessageCenterService.ACTION_ROSTER_MATCH); f.addAction(MessageCenterService.ACTION_PUBLICKEY); f.addAction(MessageCenterService.ACTION_BLOCKLIST); f.addAction(MessageCenterService.ACTION_LAST_ACTIVITY); f.addAction(MessageCenterService.ACTION_CONNECTED); lbm.registerReceiver(receiver, f); // request current connection status MessageCenterService.requestConnectionStatus(mContext); // wait for the service to complete its job synchronized (this) { // wait for connection try { wait(MAX_WAIT_TIME); } catch (InterruptedException e) { // simulate canceled operation mCanceled = true; } } lbm.unregisterReceiver(receiver); // last chance to quit if (mCanceled) throw new OperationCanceledException(); List<PresenceItem> res = receiver.getResponse(); if (res != null) { ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); // TODO operations.size() could be used instead (?) int op = 0; // this is the time - delete all Kontalk raw contacts try { syncResult.stats.numDeletes += deleteAll(account, provider); } catch (Exception e) { Log.e(TAG, "contact delete error", e); syncResult.databaseError = true; return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { try { syncResult.stats.numDeletes += deleteProfile(account, provider); } catch (Exception e) { Log.e(TAG, "profile delete error", e); syncResult.databaseError = true; } } ContentValues registeredValues = new ContentValues(); registeredValues.put(Users.REGISTERED, 1); for (int i = 0; i < res.size(); i++) { PresenceItem entry = res.get(i); if (entry.discarded) continue; final RawPhoneNumberEntry data = lookupNumbers.get(XmppStringUtils.parseLocalpart(entry.from)); if (data != null && data.lookupKey != null) { // add contact addContact(account, getDisplayName(provider, data.lookupKey, data.number), data.number, data.jid, operations, op++); } else { syncResult.stats.numSkippedEntries++; } // update fields try { String status = entry.status; if (!TextUtils.isEmpty(status)) registeredValues.put(Users.STATUS, status); else registeredValues.putNull(Users.STATUS); if (entry.timestamp >= 0) registeredValues.put(Users.LAST_SEEN, entry.timestamp); else registeredValues.putNull(Users.LAST_SEEN); if (entry.publicKey != null) { try { PGPPublicKey pubKey = PGP.getMasterKey(entry.publicKey); // trust our own key blindly int trustLevel = Authenticator.isSelfJID(mContext, entry.from) ? MyUsers.Keys.TRUST_VERIFIED : -1; // update keys table immediately Keyring.setKey(mContext, entry.from, entry.publicKey, trustLevel); // no data from system contacts, use name from public key if (data == null) { PGPUserID uid = PGP.parseUserId(pubKey, XmppStringUtils.parseDomain(entry.from)); if (uid != null) { registeredValues.put(Users.DISPLAY_NAME, uid.getName()); } } } catch (Exception e) { Log.w(TAG, "unable to parse public key", e); } } else { // use roster name if no contact data available if (data == null && entry.rosterName != null) { registeredValues.put(Users.DISPLAY_NAME, entry.rosterName); } } // blocked status registeredValues.put(Users.BLOCKED, entry.blocked); // user JID as reported by the server registeredValues.put(Users.JID, entry.from); /* * Since UsersProvider.resync inserted the user row * using our server name, it might have changed because * of what the server reported. We already put into the * values the new JID, but we need to use the old one * in the where condition so we will have a match. */ String origJid; if (data != null) origJid = XMPPUtils.createLocalJID(mContext, XmppStringUtils.parseLocalpart(entry.from)); else origJid = entry.from; usersProvider.update(Users.CONTENT_URI_OFFLINE, registeredValues, Users.JID + " = ?", new String[] { origJid }); // clear data registeredValues.remove(Users.DISPLAY_NAME); // if this is our own contact, trust our own key later if (Authenticator.isSelfJID(mContext, entry.from)) { // register our profile while we're at it if (data != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { // add contact addProfile(account, Authenticator.getDefaultDisplayName(mContext), data.number, data.jid, operations, op++); } } } catch (Exception e) { Log.e(TAG, "error updating users database", e); // we shall continue here... } } try { if (operations.size() > 0) provider.applyBatch(operations); syncResult.stats.numInserts += op; syncResult.stats.numEntries += op; } catch (Exception e) { Log.w(TAG, "contact write error", e); syncResult.stats.numSkippedEntries += op; /* * We do not consider system contacts failure a fatal error. * This is actually a workaround for systems with disabled permissions or * exotic firmwares. It can also protect against security 3rd party apps or * non-Android platforms, such as Jolla/Alien Dalvik. */ } commit(usersProvider, syncResult); } // timeout or error else { Log.w(TAG, "connection timeout - aborting sync"); syncResult.stats.numIoExceptions++; } } }
From source file:net.sf.diningout.content.SyncAdapter.java
/** * Refresh the list of restaurants that are geofenced. *//*from www . jav a 2 s . c o m*/ private void geofenceRestaurants(Context context, ContentProviderClient cp) throws RemoteException { GoogleApiClient client = Locations.client(context); if (client == null) { return; } /* remove existing geofences */ PendingIntent intent = PendingIntent.getService(context, 0, new Intent(context, RestaurantGeofencingEventService.class), 0); PendingResult<Status> result = GeofencingApi.removeGeofences(client, intent); Status status = result.await(); if (!status.isSuccess()) { String message = GeofenceStatusCodes.getStatusCodeString(status.getStatusCode()); Log.e(TAG, "remove geofences failed: " + message); event("gms", "remove geofences failed", message); } /* add geofences if user wants to be notified */ if (!Prefs.getStringSet(context, SHOW_NOTIFICATIONS) .contains(context.getString(R.string.at_restaurant_notifications_value))) { client.disconnect(); return; } GeofencingRequest.Builder request = new GeofencingRequest.Builder(); Uri uri = Uris.limit(Restaurants.CONTENT_URI, MAX_GEOFENCES); String[] proj = { _ID, Restaurants.LATITUDE, Restaurants.LONGITUDE }; String sel = Restaurants.LATITUDE + " IS NOT NULL AND " + Restaurants.LONGITUDE + " IS NOT NULL AND " + Restaurants.GEOFENCE_NOTIFICATIONS + " = 1 AND " + Restaurants.STATUS_ID + " = ?"; String[] args = { String.valueOf(ACTIVE.id) }; EasyCursor c = new EasyCursor(cp.query(uri, proj, sel, args, _ID + " DESC")); while (c.moveToNext()) { request.addGeofence(new Geofence.Builder().setRequestId(String.valueOf(c.getLong(_ID))) .setCircularRegion(c.getDouble(Restaurants.LATITUDE), c.getDouble(Restaurants.LONGITUDE), GEOFENCE_RADIUS) .setTransitionTypes(GEOFENCE_TRANSITION_ALL).setLoiteringDelay(15 * (int) MINUTE_IN_MILLIS) .setNotificationResponsiveness(15 * (int) MINUTE_IN_MILLIS).setExpirationDuration(NEVER_EXPIRE) .build()); } if (c.getCount() > 0) { result = GeofencingApi.addGeofences(client, request.build(), intent); status = result.await(); if (!status.isSuccess()) { String message = GeofenceStatusCodes.getStatusCodeString(status.getStatusCode()); Log.e(TAG, "add geofences failed: " + message); event("gms", "add geofences failed", message); } } c.close(); client.disconnect(); }