List of usage examples for android.content ContentProviderClient delete
public int delete(@NonNull Uri url, @Nullable String selection, @Nullable String[] selectionArgs) throws RemoteException
From source file:org.totschnig.myexpenses.sync.SyncAdapter.java
@Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {//from w ww . j a v a 2 s. co m categoryToId = new HashMap<>(); payeeToId = new HashMap<>(); methodToId = new HashMap<>(); accountUuidToId = new HashMap<>(); String uuidFromExtras = extras.getString(KEY_UUID); Timber.i("onPerformSync " + extras.toString()); AccountManager accountManager = AccountManager.get(getContext()); Exceptional<SyncBackendProvider> backendProviderExceptional = SyncBackendProviderFactory.get(getContext(), account); SyncBackendProvider backend; try { backend = backendProviderExceptional.getOrThrow(); } catch (Throwable throwable) { syncResult.databaseError = true; AcraHelper.report(throwable instanceof Exception ? ((Exception) throwable) : new Exception(throwable)); GenericAccountService.deactivateSync(account); accountManager.setUserData(account, GenericAccountService.KEY_BROKEN, "1"); String content = String.format(Locale.ROOT, "The backend could not be instantiated.Reason: %s. Please try to delete and recreate it.", throwable.getMessage()); Intent manageIntent = new Intent(getContext(), ManageSyncBackends.class); NotificationBuilderWrapper builder = NotificationBuilderWrapper .defaultBigTextStyleBuilder(getContext(), "Synchronization backend deactivated", content) .setContentIntent(PendingIntent.getActivity(getContext(), 0, manageIntent, PendingIntent.FLAG_CANCEL_CURRENT)); Notification notification = builder.build(); notification.flags = Notification.FLAG_AUTO_CANCEL; ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification); return; } if (!backend.setUp()) { syncResult.stats.numIoExceptions++; syncResult.delayUntil = 300; return; } String autoBackupFileUri = extras.getString(KEY_UPLOAD_AUTO_BACKUP); if (autoBackupFileUri != null) { try { backend.storeBackup(Uri.parse(autoBackupFileUri)); } catch (IOException e) { String content = getContext().getString(R.string.auto_backup_cloud_failure, autoBackupFileUri, account.name) + " " + e.getMessage(); Notification notification = NotificationBuilderWrapper.defaultBigTextStyleBuilder(getContext(), getContext().getString(R.string.pref_auto_backup_title), content).build(); notification.flags = Notification.FLAG_AUTO_CANCEL; ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification); } return; } Cursor c; String[] selectionArgs; String selection = KEY_SYNC_ACCOUNT_NAME + " = ?"; if (uuidFromExtras != null) { selection += " AND " + KEY_UUID + " = ?"; selectionArgs = new String[] { account.name, uuidFromExtras }; } else { selectionArgs = new String[] { account.name }; } String[] projection = { KEY_ROWID }; try { c = provider.query(TransactionProvider.ACCOUNTS_URI, projection, selection + " AND " + KEY_SYNC_SEQUENCE_LOCAL + " = 0", selectionArgs, null); } catch (RemoteException e) { syncResult.databaseError = true; AcraHelper.report(e); return; } if (c == null) { syncResult.databaseError = true; AcraHelper.report("Cursor is null"); return; } if (c.moveToFirst()) { do { long accountId = c.getLong(0); try { provider.update(buildInitializationUri(accountId), new ContentValues(0), null, null); } catch (RemoteException e) { syncResult.databaseError = true; AcraHelper.report(e); return; } } while (c.moveToNext()); } try { c = provider.query(TransactionProvider.ACCOUNTS_URI, projection, selection, selectionArgs, null); } catch (RemoteException e) { syncResult.databaseError = true; AcraHelper.report(e); return; } if (c != null) { if (c.moveToFirst()) { do { long accountId = c.getLong(0); String lastLocalSyncKey = KEY_LAST_SYNCED_LOCAL(accountId); String lastRemoteSyncKey = KEY_LAST_SYNCED_REMOTE(accountId); long lastSyncedLocal = Long .parseLong(getUserDataWithDefault(accountManager, account, lastLocalSyncKey, "0")); long lastSyncedRemote = Long .parseLong(getUserDataWithDefault(accountManager, account, lastRemoteSyncKey, "0")); dbAccount.set(org.totschnig.myexpenses.model.Account.getInstanceFromDb(accountId)); Timber.i("now syncing " + dbAccount.get().label); if (uuidFromExtras != null && extras.getBoolean(KEY_RESET_REMOTE_ACCOUNT)) { if (!backend.resetAccountData(uuidFromExtras)) { syncResult.stats.numIoExceptions++; Timber.e("error resetting account data"); } continue; } if (!backend.withAccount(dbAccount.get())) { syncResult.stats.numIoExceptions++; Timber.e("error withAccount"); continue; } if (backend.lock()) { try { ChangeSet changeSetSince = backend.getChangeSetSince(lastSyncedRemote, getContext()); if (changeSetSince.isFailed()) { syncResult.stats.numIoExceptions++; Timber.e("error getting changeset"); continue; } List<TransactionChange> remoteChanges; lastSyncedRemote = changeSetSince.sequenceNumber; remoteChanges = changeSetSince.changes; List<TransactionChange> localChanges = new ArrayList<>(); long sequenceToTest = lastSyncedLocal + 1; while (true) { List<TransactionChange> nextChanges = getLocalChanges(provider, accountId, sequenceToTest); if (nextChanges.size() > 0) { localChanges.addAll( Stream.of(nextChanges).filter(change -> !change.isEmpty()).toList()); lastSyncedLocal = sequenceToTest; sequenceToTest++; } else { break; } } if (localChanges.size() == 0 && remoteChanges.size() == 0) { continue; } if (localChanges.size() > 0) { localChanges = collectSplits(localChanges); } Pair<List<TransactionChange>, List<TransactionChange>> mergeResult = mergeChangeSets( localChanges, remoteChanges); localChanges = mergeResult.first; remoteChanges = mergeResult.second; if (remoteChanges.size() > 0) { writeRemoteChangesToDb(provider, Stream.of(remoteChanges) .filter(change -> !(change.isCreate() && uuidExists(change.uuid()))) .toList(), accountId); accountManager.setUserData(account, lastRemoteSyncKey, String.valueOf(lastSyncedRemote)); } if (localChanges.size() > 0) { lastSyncedRemote = backend.writeChangeSet(localChanges, getContext()); if (lastSyncedRemote != ChangeSet.FAILED) { if (!BuildConfig.DEBUG) { // on debug build for auditing purposes, we keep changes in the table provider.delete(TransactionProvider.CHANGES_URI, KEY_ACCOUNTID + " = ? AND " + KEY_SYNC_SEQUENCE_LOCAL + " <= ?", new String[] { String.valueOf(accountId), String.valueOf(lastSyncedLocal) }); } accountManager.setUserData(account, lastLocalSyncKey, String.valueOf(lastSyncedLocal)); accountManager.setUserData(account, lastRemoteSyncKey, String.valueOf(lastSyncedRemote)); } } } catch (IOException e) { Timber.e(e, "Error while syncing "); syncResult.stats.numIoExceptions++; } catch (RemoteException | OperationApplicationException | SQLiteException e) { Timber.e(e, "Error while syncing "); syncResult.databaseError = true; AcraHelper.report(e); } finally { if (!backend.unlock()) { Timber.e("Unlocking backend failed"); syncResult.stats.numIoExceptions++; } } } else { //TODO syncResult.delayUntil = ??? syncResult.stats.numIoExceptions++; } } while (c.moveToNext()); } c.close(); } backend.tearDown(); }