public static long determineBestId(
        ContentResolver resolver, CapsuleOperations operations, Capsule capsule) {
      // Query
      Cursor c =
          resolver.query(
              CapsuleContract.Capsules.CONTENT_URI,
              new String[] {CapsuleContract.Capsules._ID},
              CapsuleContract.Capsules.TABLE_NAME
                  + "."
                  + CapsuleContract.Capsules.SYNC_ID
                  + " = ?"
                  + " AND "
                  + CapsuleContract.Capsules.TABLE_NAME
                  + "."
                  + CapsuleContract.Capsules.SYNC_ID
                  + " != ?",
              new String[] {String.valueOf(capsule.getSyncId()), "0"},
              null);
      // Check if there is a row
      long id;
      if (c.getCount() < 1) {
        // There is no existing row
        id = 0;
      } else if (c.getCount() > 1) {
        // Get the first matching row to keep
        c.moveToFirst();
        id = c.getLong(c.getColumnIndex(CapsuleContract.Capsules._ID));
        // Add any extra IDs to a collection
        List<Long> duplicateIds = new ArrayList<Long>();
        while (c.moveToNext()) {
          duplicateIds.add(c.getLong(c.getColumnIndex(CapsuleContract.Capsules._ID)));
        }
        // Build operations to remove the extra IDs
        operations.buildCapsuleCleanup(id, duplicateIds);
      } else {
        // There is only one row, so move to it and get the ID
        c.moveToFirst();
        id = c.getLong(c.getColumnIndex(CapsuleContract.Capsules._ID));
      }
      // Close the cursor
      c.close();

      // If a better ID was found (non-zero), reassign it
      if (id > 0) {
        capsule.setId(id);
      }

      return id;
    }
    public static boolean save(
        ContentResolver resolver, Capsule capsule, CapsuleContract.SyncStateAction syncAction) {
      // Build the ContentProviderOperations for the save
      CapsuleOperations operations = new CapsuleOperations(resolver);
      operations.buildOwnershipSave(capsule, syncAction);

      // Apply the batch operation
      ContentProviderResult[] results = operations.applyBatch();

      // Make sure the rows were properly updated
      boolean success = true;

      // Check the first result (Capsule operation)
      if (results[0].uri != null) {
        // The Capsule operation was an INSERT, so parse the ID from the URI
        capsule.setId(ContentUris.parseId(results[0].uri));
      } else {
        // The Capsule operation was an UPDATE, so make sure a row was updated
        if (results[0].count < 1) {
          success = false;
        }
      }

      // Check the second result (Ownership operation)
      if (results[1].uri != null) {
        // The Ownership operation was an INSERT, so parse the ID from the URI
        ((CapsuleOwnership) capsule).setOwnershipId(ContentUris.parseId(results[1].uri));
      } else {
        // The Ownership operation was an UPDATE, so make sure a row was updated
        if (results[1].count < 1) {
          success = false;
        }
      }

      return success;
    }