Example usage for android.content ContentProviderClient query

List of usage examples for android.content ContentProviderClient query

Introduction

In this page you can find the example usage for android.content ContentProviderClient query.

Prototype

public @Nullable Cursor query(@NonNull Uri url, @Nullable String[] projection, @Nullable String selection,
        @Nullable String[] selectionArgs, @Nullable String sortOrder) throws RemoteException 

Source Link

Document

See ContentProvider#query ContentProvider.query

Usage

From source file:com.clearcenter.mobile_demo.mdSyncAdapter.java

public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {//from ww w .  java  2s  .  c  om
    String authtoken = null;
    Log.i(TAG, "Starting sync operation...");

    try {
        mdSSLUtil.DisableSecurity();

        // Use the account manager to request the AuthToken we'll need
        // to talk to our sample server.  If we don't have an AuthToken
        // yet, this could involve a round-trip to the server to request
        // an AuthToken.
        authtoken = account_manager.blockingGetAuthToken(account, mdConstants.AUTHTOKEN_TYPE,
                NOTIFY_AUTH_FAILURE);

        final String hostname = account_manager.getUserData(account, "hostname");

        long _last_sample = -1;
        if (last_sample.containsKey(account.name))
            _last_sample = last_sample.get(account.name);

        // Get sync data from server...
        final String data = mdRest.GetSystemInfo(hostname, authtoken, _last_sample);

        // Something went wrong :^(
        if (TextUtils.isEmpty(data))
            return;

        if (_last_sample < 0) {
            int count = provider.delete(mdDeviceSamples.CONTENT_URI, mdDeviceSamples.NICKNAME + " = ?",
                    new String[] { account.name });

            Log.d(TAG, "Reset database, purged " + count + " samples.");
        }

        try {
            JSONObject json_data = new JSONObject(data);
            if (json_data.has("time"))
                _last_sample = Long.valueOf(json_data.getString("time"));
            Log.d(TAG, account.name + ": last sample time-stamp: " + _last_sample);
        } catch (JSONException e) {
            Log.e(TAG, "JSONException", e);
            return;
        }

        last_sample.put(account.name, _last_sample);

        ContentValues values = new ContentValues();
        values.put(mdDeviceSamples.NICKNAME, account.name);
        values.put(mdDeviceSamples.DATA, data);

        provider.insert(mdDeviceSamples.CONTENT_URI, values);

        String projection[] = new String[] { mdDeviceSamples.SAMPLE_ID };

        Cursor cursor = provider.query(mdDeviceSamples.CONTENT_URI, projection,
                mdDeviceSamples.NICKNAME + " = ?", new String[] { account.name }, mdDeviceSamples.SAMPLE_ID);

        int rows = cursor.getCount();
        Log.d(TAG, "Rows: " + rows);
        if (rows <= mdConstants.MAX_SAMPLES) {
            cursor.close();
            return;
        }

        Log.d(TAG, "Samples to purge: " + (rows - mdConstants.MAX_SAMPLES));
        cursor.move(rows - mdConstants.MAX_SAMPLES);

        int top_id = cursor.getInt(cursor.getColumnIndex(mdDeviceSamples.SAMPLE_ID));
        Log.d(TAG, "Top ID: " + top_id);

        cursor.close();

        int count = provider.delete(mdDeviceSamples.CONTENT_URI,
                mdDeviceSamples.SAMPLE_ID + " <= ? AND " + mdDeviceSamples.NICKNAME + " = ?",
                new String[] { String.valueOf(top_id), account.name });

        Log.d(TAG, "Purged " + count + " samples.");

    } catch (final RemoteException e) {
        Log.e(TAG, "RemoteException", e);
        syncResult.stats.numParseExceptions++;
    } catch (final AuthenticatorException e) {
        Log.e(TAG, "AuthenticatorException", e);
        syncResult.stats.numParseExceptions++;
    } catch (final OperationCanceledException e) {
        Log.e(TAG, "OperationCanceledExcetpion", e);
    } catch (final IOException e) {
        Log.e(TAG, "IOException", e);
        syncResult.stats.numIoExceptions++;
    } catch (final ParseException e) {
        Log.e(TAG, "ParseException", e);
        syncResult.stats.numParseExceptions++;
    } catch (final AuthenticationException e) {
        Log.e(TAG, "AuthenticationException", e);
        syncResult.stats.numAuthExceptions++;
        account_manager.invalidateAuthToken(mdConstants.AUTHTOKEN_TYPE, authtoken);
    } catch (GeneralSecurityException e) {
        Log.e(TAG, "GeneralSecurityException", e);
    } catch (final JSONException e) {
        Log.e(TAG, "JSONException", e);
        syncResult.stats.numParseExceptions++;
    }
}

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  w w.  j ava  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();
}

From source file:edu.mit.mobile.android.locast.sync.SyncEngine.java

/**
 * @param toSync//from w w  w . j  av a2s  . co  m
 * @param account
 * @param extras
 * @param provider
 * @param syncResult
 * @return true if the item was sync'd successfully. Soft errors will cause this to return
 *         false.
 * @throws RemoteException
 * @throws SyncException
 * @throws JSONException
 * @throws IOException
 * @throws NetworkProtocolException
 * @throws NoPublicPath
 * @throws OperationApplicationException
 * @throws InterruptedException
 */
public boolean sync(Uri toSync, Account account, Bundle extras, ContentProviderClient provider,
        SyncResult syncResult) throws RemoteException, SyncException, JSONException, IOException,
        NetworkProtocolException, NoPublicPath, OperationApplicationException, InterruptedException {

    String pubPath = null;

    //
    // Handle http or https uris separately. These require the
    // destination uri.
    //
    if ("http".equals(toSync.getScheme()) || "https".equals(toSync.getScheme())) {
        pubPath = toSync.toString();

        if (!extras.containsKey(EXTRA_DESTINATION_URI)) {
            throw new IllegalArgumentException("missing EXTRA_DESTINATION_URI when syncing HTTP URIs");
        }
        toSync = Uri.parse(extras.getString(EXTRA_DESTINATION_URI));
    }

    final String type = provider.getType(toSync);
    final boolean isDir = type.startsWith(CONTENT_TYPE_PREFIX_DIR);

    final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);

    // skip any items already sync'd
    if (!manualSync && mLastUpdated.isUpdatedRecently(toSync)) {
        if (DEBUG) {
            Log.d(TAG, "not syncing " + toSync + " as it's been updated recently");
        }
        syncResult.stats.numSkippedEntries++;
        return false;
    }

    // the sync map will convert the json data to ContentValues
    final SyncMap syncMap = MediaProvider.getSyncMap(provider, toSync);

    final Uri toSyncWithoutQuerystring = toSync.buildUpon().query(null).build();

    final HashMap<String, SyncStatus> syncStatuses = new HashMap<String, SyncEngine.SyncStatus>();
    final ArrayList<ContentProviderOperation> cpo = new ArrayList<ContentProviderOperation>();
    final LinkedList<String> cpoPubUris = new LinkedList<String>();

    //
    // first things first, upload any content that needs to be
    // uploaded.
    //

    try {
        uploadUnpublished(toSync, account, provider, syncMap, syncStatuses, syncResult);

        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        // this should ensure that all items have a pubPath when we
        // query it below.

        if (pubPath == null) {
            // we should avoid calling this too much as it
            // can be expensive
            pubPath = MediaProvider.getPublicPath(mContext, toSync);
        }
    } catch (final NoPublicPath e) {
        // TODO this is a special case and this is probably not the best place to handle this.
        // Ideally, this should be done in such a way as to reduce any extra DB queries -
        // perhaps by doing a join with the parent.
        if (syncMap.isFlagSet(SyncMap.FLAG_PARENT_MUST_SYNC_FIRST)) {
            if (DEBUG) {
                Log.d(TAG, "skipping " + toSync + " whose parent hasn't been sync'd first");
            }
            syncResult.stats.numSkippedEntries++;
            return false;
        }

        // if it's an item, we can handle it.
        if (isDir) {
            throw e;
        }
    }

    if (pubPath == null) {

        // this should have been updated already by the initial
        // upload, so something must be wrong
        throw new SyncException("never got a public path for " + toSync);
    }

    if (DEBUG) {
        Log.d(TAG, "sync(toSync=" + toSync + ", account=" + account + ", extras=" + extras + ", manualSync="
                + manualSync + ",...)");
        Log.d(TAG, "pubPath: " + pubPath);
    }

    final long request_time = System.currentTimeMillis();

    HttpResponse hr = mNetworkClient.get(pubPath);

    final long response_time = System.currentTimeMillis();

    // the time compensation below allows a time-based synchronization to function even if the
    // local clock is entirely wrong. The server's time is extracted using the Date header and
    // all are compared relative to the respective clock reference. Any data that's stored on
    // the mobile should be stored relative to the local clock and the server will respect the
    // same.
    long serverTime;

    try {
        serverTime = getServerTime(hr);
    } catch (final DateParseException e) {
        Log.w(TAG, "could not retrieve date from server. Using local time, which may be incorrect.", e);
        serverTime = System.currentTimeMillis();
    }

    // TODO check out
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
    final long response_delay = response_time - request_time;
    if (DEBUG) {
        Log.d(TAG, "request took " + response_delay + "ms");
    }
    final long localTime = request_time;

    // add this to the server time to get the local time
    final long localOffset = (localTime - serverTime);

    if (Math.abs(localOffset) > 30 * 60 * 1000) {
        Log.w(TAG, "local clock is off by " + localOffset + "ms");
    }

    if (Thread.interrupted()) {
        throw new InterruptedException();
    }

    final HttpEntity ent = hr.getEntity();

    String selection;
    String selectionInverse;
    String[] selectionArgs;

    if (isDir) {

        final JSONArray ja = new JSONArray(StreamUtils.inputStreamToString(ent.getContent()));
        ent.consumeContent();

        final int len = ja.length();
        selectionArgs = new String[len];

        // build the query to see which items are already in the
        // database
        final StringBuilder sb = new StringBuilder();

        sb.append("(");

        for (int i = 0; i < len; i++) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }

            final SyncStatus syncStatus = loadItemFromJsonObject(ja.getJSONObject(i), syncMap, serverTime);

            syncStatuses.put(syncStatus.remote, syncStatus);

            selectionArgs[i] = syncStatus.remote;

            // add in a placeholder for the query
            sb.append('?');
            if (i != (len - 1)) {
                sb.append(',');
            }

        }
        sb.append(")");

        final String placeholders = sb.toString();
        selection = JsonSyncableItem._PUBLIC_URI + " IN " + placeholders;
        selectionInverse = JsonSyncableItem._PUBLIC_URI + " NOT IN " + placeholders;
    } else {

        final JSONObject jo = new JSONObject(StreamUtils.inputStreamToString(ent.getContent()));
        ent.consumeContent();
        final SyncStatus syncStatus = loadItemFromJsonObject(jo, syncMap, serverTime);

        syncStatuses.put(syncStatus.remote, syncStatus);

        selection = JsonSyncableItem._PUBLIC_URI + "=?";
        selectionInverse = JsonSyncableItem._PUBLIC_URI + "!=?";
        selectionArgs = new String[] { syncStatus.remote };
    }

    // first check without the querystring. This will ensure that we
    // properly mark things that we already have in the database.
    final Cursor check = provider.query(toSyncWithoutQuerystring, SYNC_PROJECTION, selection, selectionArgs,
            null);

    // these items are on both sides
    try {
        final int pubUriCol = check.getColumnIndex(JsonSyncableItem._PUBLIC_URI);
        final int idCol = check.getColumnIndex(JsonSyncableItem._ID);

        // All the items in this cursor should be found on both
        // the client and the server.
        for (check.moveToFirst(); !check.isAfterLast(); check.moveToNext()) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }

            final long id = check.getLong(idCol);
            final Uri localUri = ContentUris.withAppendedId(toSync, id);

            final String pubUri = check.getString(pubUriCol);

            final SyncStatus itemStatus = syncStatuses.get(pubUri);

            itemStatus.state = SyncState.BOTH_UNKNOWN;

            itemStatus.local = localUri;

            // make the status searchable by both remote and
            // local uri
            syncStatuses.put(localUri.toString(), itemStatus);
        }
    } finally {
        check.close();
    }

    Cursor c = provider.query(toSync, SYNC_PROJECTION, selection, selectionArgs, null);

    // these items are on both sides
    try {
        final int pubUriCol = c.getColumnIndex(JsonSyncableItem._PUBLIC_URI);
        final int localModifiedCol = c.getColumnIndex(JsonSyncableItem._MODIFIED_DATE);
        final int serverModifiedCol = c.getColumnIndex(JsonSyncableItem._SERVER_MODIFIED_DATE);
        final int idCol = c.getColumnIndex(JsonSyncableItem._ID);

        // All the items in this cursor should be found on both
        // the client and the server.
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }

            final long id = c.getLong(idCol);
            final Uri localUri = ContentUris.withAppendedId(toSync, id);

            final String pubUri = c.getString(pubUriCol);

            final SyncStatus itemStatus = syncStatuses.get(pubUri);

            if (itemStatus.state == SyncState.ALREADY_UP_TO_DATE
                    || itemStatus.state == SyncState.NOW_UP_TO_DATE) {
                if (DEBUG) {
                    Log.d(TAG, localUri + "(" + pubUri + ")" + " is already up to date.");
                }
                continue;
            }

            itemStatus.local = localUri;

            // make the status searchable by both remote and local uri
            syncStatuses.put(localUri.toString(), itemStatus);

            // last modified as stored in the DB, in phone time
            final long itemLocalModified = c.getLong(localModifiedCol);

            // last modified as stored in the DB, in server time
            final long itemServerModified = c.getLong(serverModifiedCol);
            final long localAge = localTime - itemLocalModified;

            final long remoteAge = serverTime - itemStatus.remoteModifiedTime;

            final long ageDifference = Math.abs(localAge - remoteAge);

            // up to date, as far remote -> local goes
            if (itemServerModified == itemStatus.remoteModifiedTime) {
                itemStatus.state = SyncState.ALREADY_UP_TO_DATE;
                if (DEBUG) {
                    Log.d(TAG, pubUri + " is up to date.");
                }

                // need to download
            } else if (localAge > remoteAge) {
                if (DEBUG) {
                    final long serverModified = itemStatus.remoteModifiedTime;

                    Log.d(TAG,
                            pubUri + " : local is " + ageDifference + "ms older ("
                                    + android.text.format.DateUtils.formatDateTime(mContext, itemLocalModified,
                                            FORMAT_ARGS_DEBUG)
                                    + ") than remote (" + android.text.format.DateUtils.formatDateTime(mContext,
                                            serverModified, FORMAT_ARGS_DEBUG)
                                    + "); updating local copy...");
                }

                itemStatus.state = SyncState.REMOTE_DIRTY;

                final ContentProviderOperation.Builder b = ContentProviderOperation.newUpdate(localUri);

                // update this so it's in the local timescale
                correctServerOffset(itemStatus.remoteCVs, JsonSyncableItem._CREATED_DATE,
                        JsonSyncableItem._CREATED_DATE, localOffset);
                correctServerOffset(itemStatus.remoteCVs, JsonSyncableItem._SERVER_MODIFIED_DATE,
                        JsonSyncableItem._MODIFIED_DATE, localOffset);

                b.withValues(itemStatus.remoteCVs);
                b.withExpectedCount(1);

                cpo.add(b.build());
                cpoPubUris.add(pubUri);

                syncResult.stats.numUpdates++;

                // need to upload
            } else if (localAge < remoteAge) {
                if (DEBUG) {
                    final long serverModified = itemStatus.remoteModifiedTime;

                    Log.d(TAG,
                            pubUri + " : local is " + ageDifference + "ms newer ("
                                    + android.text.format.DateUtils.formatDateTime(mContext, itemLocalModified,
                                            FORMAT_ARGS_DEBUG)
                                    + ") than remote (" + android.text.format.DateUtils.formatDateTime(mContext,
                                            serverModified, FORMAT_ARGS_DEBUG)
                                    + "); publishing to server...");
                }
                itemStatus.state = SyncState.LOCAL_DIRTY;

                mNetworkClient.putJson(pubPath, JsonSyncableItem.toJSON(mContext, localUri, c, syncMap));
            }

            mLastUpdated.markUpdated(localUri);

            syncResult.stats.numEntries++;
        } // end for
    } finally {

        c.close();
    }

    /*
     * Apply updates in bulk
     */
    if (cpo.size() > 0) {
        if (DEBUG) {
            Log.d(TAG, "applying " + cpo.size() + " bulk updates...");
        }

        final ContentProviderResult[] r = provider.applyBatch(cpo);
        if (DEBUG) {
            Log.d(TAG, "Done applying updates. Running postSync handler...");
        }

        for (int i = 0; i < r.length; i++) {
            final ContentProviderResult res = r[i];
            final SyncStatus ss = syncStatuses.get(cpoPubUris.get(i));
            if (ss == null) {
                Log.e(TAG, "can't get sync status for " + res.uri);
                continue;
            }
            syncMap.onPostSyncItem(mContext, account, ss.local, ss.remoteJson,
                    res.count != null ? res.count == 1 : true);

            ss.state = SyncState.NOW_UP_TO_DATE;
        }

        if (DEBUG) {
            Log.d(TAG, "done running postSync handler.");
        }

        cpo.clear();
        cpoPubUris.clear();
    }

    if (Thread.interrupted()) {
        throw new InterruptedException();
    }

    /*
     * Look through the SyncState.state values and find ones that need to be stored.
     */

    for (final Map.Entry<String, SyncStatus> entry : syncStatuses.entrySet()) {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        final String pubUri = entry.getKey();
        final SyncStatus status = entry.getValue();
        if (status.state == SyncState.REMOTE_ONLY) {
            if (DEBUG) {
                Log.d(TAG, pubUri + " is not yet stored locally, adding...");
            }

            // update this so it's in the local timescale
            correctServerOffset(status.remoteCVs, JsonSyncableItem._CREATED_DATE,
                    JsonSyncableItem._CREATED_DATE, localOffset);
            correctServerOffset(status.remoteCVs, JsonSyncableItem._SERVER_MODIFIED_DATE,
                    JsonSyncableItem._MODIFIED_DATE, localOffset);

            final ContentProviderOperation.Builder b = ContentProviderOperation.newInsert(toSync);
            b.withValues(status.remoteCVs);

            cpo.add(b.build());
            cpoPubUris.add(pubUri);
            syncResult.stats.numInserts++;

        }
    }

    /*
     * Execute the content provider operations in bulk.
     */
    if (cpo.size() > 0) {
        if (DEBUG) {
            Log.d(TAG, "bulk inserting " + cpo.size() + " items...");
        }
        final ContentProviderResult[] r = provider.applyBatch(cpo);
        if (DEBUG) {
            Log.d(TAG, "applyBatch completed. Processing results...");
        }

        int successful = 0;
        for (int i = 0; i < r.length; i++) {
            final ContentProviderResult res = r[i];
            if (res.uri == null) {
                syncResult.stats.numSkippedEntries++;
                Log.e(TAG, "result from content provider bulk operation returned null");
                continue;
            }
            final String pubUri = cpoPubUris.get(i);
            final SyncStatus ss = syncStatuses.get(pubUri);

            if (ss == null) {
                syncResult.stats.numSkippedEntries++;
                Log.e(TAG, "could not find sync status for " + cpoPubUris.get(i));
                continue;
            }

            ss.local = res.uri;
            if (DEBUG) {
                Log.d(TAG, "onPostSyncItem(" + res.uri + ", ...); pubUri: " + pubUri);
            }

            syncMap.onPostSyncItem(mContext, account, res.uri, ss.remoteJson,
                    res.count != null ? res.count == 1 : true);

            ss.state = SyncState.NOW_UP_TO_DATE;
            successful++;
        }
        if (DEBUG) {
            Log.d(TAG, successful + " batch inserts successfully applied.");
        }
    } else {
        if (DEBUG) {
            Log.d(TAG, "no updates to perform.");
        }
    }

    /**
     * Look through all the items that we didn't already find on the server side, but which
     * still have a public uri. They should be checked to make sure they're not deleted.
     */
    c = provider.query(toSync, SYNC_PROJECTION,
            ProviderUtils.addExtraWhere(selectionInverse, JsonSyncableItem._PUBLIC_URI + " NOT NULL"),
            selectionArgs, null);

    try {
        final int idCol = c.getColumnIndex(JsonSyncableItem._ID);
        final int pubUriCol = c.getColumnIndex(JsonSyncableItem._PUBLIC_URI);

        cpo.clear();

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            final String pubUri = c.getString(pubUriCol);
            SyncStatus ss = syncStatuses.get(pubUri);

            final Uri item = isDir ? ContentUris.withAppendedId(toSyncWithoutQuerystring, c.getLong(idCol))
                    : toSync;

            if (ss == null) {
                ss = syncStatuses.get(item.toString());
            }

            if (DEBUG) {
                Log.d(TAG, item + " was not found in the main list of items on the server (" + pubPath
                        + "), but appears to be a child of " + toSync);

                if (ss != null) {
                    Log.d(TAG, "found sync status for " + item + ": " + ss);
                }
            }

            if (ss != null) {
                switch (ss.state) {
                case ALREADY_UP_TO_DATE:
                case NOW_UP_TO_DATE:
                    if (DEBUG) {
                        Log.d(TAG, item + " is already up to date. No need to see if it was deleted.");
                    }
                    continue;

                case BOTH_UNKNOWN:
                    if (DEBUG) {
                        Log.d(TAG,
                                item + " was found on both sides, but has an unknown sync status. Skipping...");
                    }
                    continue;

                default:

                    Log.w(TAG, "got an unexpected state for " + item + ": " + ss);
                }

            } else {
                ss = new SyncStatus(pubUri, SyncState.LOCAL_ONLY);
                ss.local = item;

                hr = mNetworkClient.head(pubUri);

                switch (hr.getStatusLine().getStatusCode()) {
                case 200:
                    if (DEBUG) {
                        Log.d(TAG, "HEAD " + pubUri + " returned 200");
                    }
                    ss.state = SyncState.BOTH_UNKNOWN;
                    break;

                case 404:
                    if (DEBUG) {
                        Log.d(TAG, "HEAD " + pubUri + " returned 404. Deleting locally...");
                    }
                    ss.state = SyncState.DELETED_REMOTELY;
                    final ContentProviderOperation deleteOp = ContentProviderOperation
                            .newDelete(ContentUris.withAppendedId(toSyncWithoutQuerystring, c.getLong(idCol)))
                            .build();
                    cpo.add(deleteOp);

                    break;

                default:
                    syncResult.stats.numIoExceptions++;
                    Log.w(TAG, "HEAD " + pubUri + " got unhandled result: " + hr.getStatusLine());
                }
            }
            syncStatuses.put(pubUri, ss);
        } // for cursor

        if (cpo.size() > 0) {
            final ContentProviderResult[] results = provider.applyBatch(cpo);

            for (final ContentProviderResult result : results) {
                if (result.count != 1) {
                    throw new SyncException("Error deleting item");
                }
            }
        }

    } finally {
        c.close();
    }

    syncStatuses.clear();

    mLastUpdated.markUpdated(toSync);

    return true;
}