// /////////////////////////////////////////////////////////////////////////// // LIFECYCLE // /////////////////////////////////////////////////////////////////////////// @Override protected LoaderResult<Void> doInBackground() { LoaderResult<Void> result = new LoaderResult<Void>(); try { // Delete All local files in sync folder File synchroFolder = SyncContentManager.getInstance(context).getSynchroFolder(accountUsername, accountUrl); if (synchroFolder != null && synchroFolder.exists()) { IOUtils.deleteContents(synchroFolder); } // For each sync row, reset status Cursor allFavoritesCursor = context .getContentResolver() .query( SyncContentProvider.CONTENT_URI, SyncContentSchema.COLUMN_ALL, SyncContentProvider.getAccountFilter(accountId), null, null); while (allFavoritesCursor.moveToNext()) { if (isDeletion) { // Update Sync Info context .getContentResolver() .delete( SyncContentManager.getUri( allFavoritesCursor.getLong(SyncContentSchema.COLUMN_ID_ID)), null, null); } else { // Update Sync Info ContentValues cValues = new ContentValues(); cValues.put(OperationSchema.COLUMN_STATUS, SyncContentStatus.STATUS_PENDING); context .getContentResolver() .update( SyncContentManager.getUri( allFavoritesCursor.getLong(SyncContentSchema.COLUMN_ID_ID)), cValues, null, null); } } } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); result.setException(e); } return result; }
// ///////////////////////////////////////////////////////////// // CURSOR ADAPTER // //////////////////////////////////////////////////////////// @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { if (args == null || !args.containsKey(ARGUMENT_HIDE)) { setListShown(false); } StringBuilder selection = new StringBuilder(); if (acc == null) { acc = getAccount(); } if (acc != null) { selection.append(SyncContentProvider.getAccountFilter(acc)); } if (selection.length() > 0) { selection.append(" AND "); } if (getFolderId() != null) { selection .append(SyncContentSchema.COLUMN_PARENT_ID) .append(" == '") .append(getFolderId()) .append("'"); } else { selection.append( SyncContentSchema.COLUMN_IS_SYNC_ROOT + " == '" + SyncContentProvider.FLAG_SYNC_SET + "'"); selection.append(" OR "); selection.append( SyncContentSchema.COLUMN_STATUS + " == '" + SyncContentStatus.STATUS_REQUEST_USER + "'"); } if (selection.length() > 0) { selection.append(" AND "); } selection.append( SyncContentSchema.COLUMN_STATUS + " NOT IN (" + SyncContentStatus.STATUS_HIDDEN + ")"); return new CursorLoader( getActivity(), SyncContentProvider.CONTENT_URI, SyncContentSchema.COLUMN_ALL, selection.toString(), null, SyncContentSchema.COLUMN_TITLE + " COLLATE NOCASE ASC"); }
@Subscribe public void onDocumentUpdated(UpdateNodeEvent event) { if (event.hasException) { return; } Node updatedNode = event.data; if (updatedNode == null) { return; } Cursor syncCursor = null; try { syncCursor = getActivity() .getContentResolver() .query( SyncContentProvider.CONTENT_URI, SyncContentSchema.COLUMN_ALL, SyncContentProvider.getAccountFilter(acc) + " AND " + SyncContentSchema.COLUMN_NODE_ID + " LIKE '" + NodeRefUtils.getCleanIdentifier(updatedNode.getIdentifier()) + "%'", null, null); boolean hasSynced = (syncCursor.getCount() == 1); if (hasSynced && !hasSynchroActive) { syncCursor.moveToFirst(); ContentValues cValues = new ContentValues(); cValues.put(SyncContentSchema.COLUMN_NODE_ID, updatedNode.getIdentifier()); cValues.put( SyncContentSchema.COLUMN_SERVER_MODIFICATION_TIMESTAMP, updatedNode.getModifiedAt().getTimeInMillis()); getActivity() .getContentResolver() .update( SyncContentManager.getUri(syncCursor.getLong(SyncContentSchema.COLUMN_ID_ID)), cValues, null, null); } } catch (Exception e) { // Do nothing } finally { CursorUtils.closeCursor(syncCursor); } }