/**
  * Queries all the records which have the given type.
  *
  * @param TransferType The type of transfers to query for.
  * @return A Cursor pointing to records in the database with the given type.
  */
 public Cursor queryAllTransfersWithType(TransferType type) {
   if (type == TransferType.ANY) {
     return transferDBBase.query(transferDBBase.getContentUri(), null, null, null, null);
   } else {
     return transferDBBase.query(
         transferDBBase.getContentUri(),
         null,
         TransferTable.COLUMN_TYPE + "=?",
         new String[] {type.toString()},
         null);
   }
 }
 /**
  * Updates states of all transfer records with the specified which are "running" and "waiting" to
  * "pending cancel"
  *
  * @param TransferType The type of transfers to cancel
  * @return Number of rows updated.
  */
 public int cancelAllWithType(TransferType type) {
   ContentValues values = new ContentValues();
   values.put(TransferTable.COLUMN_STATE, TransferState.PENDING_CANCEL.toString());
   String selection = null;
   String[] selectionArgs = null;
   if (type == TransferType.ANY) {
     selection = TransferTable.COLUMN_STATE + " in (?,?,?,?,?)";
     selectionArgs =
         new String[] {
           TransferState.IN_PROGRESS.toString(),
           TransferState.RESUMED_WAITING.toString(),
           TransferState.WAITING.toString(),
           TransferState.PAUSED.toString(),
           TransferState.WAITING_FOR_NETWORK.toString()
         };
   } else {
     selection =
         TransferTable.COLUMN_STATE + " in (?,?,?,?,?) and " + TransferTable.COLUMN_TYPE + "=?";
     selectionArgs =
         new String[] {
           TransferState.IN_PROGRESS.toString(),
           TransferState.RESUMED_WAITING.toString(),
           TransferState.WAITING.toString(),
           TransferState.PAUSED.toString(),
           TransferState.WAITING_FOR_NETWORK.toString(),
           type.toString()
         };
   }
   return transferDBBase.update(transferDBBase.getContentUri(), values, selection, selectionArgs);
 }
 /**
  * Updates states of all transfer records which are "waiting for network" to "waiting to resume"
  *
  * @return Number of rows updated.
  */
 public int updateNetworkConnected() {
   ContentValues values = new ContentValues();
   values.put(TransferTable.COLUMN_STATE, TransferState.RESUMED_WAITING.toString());
   return transferDBBase.update(
       transferDBBase.getContentUri(),
       values,
       TransferTable.COLUMN_STATE + " in (?,?)",
       new String[] {
         TransferState.PENDING_NETWORK_DISCONNECT.toString(),
         TransferState.WAITING_FOR_NETWORK.toString()
       });
 }
 /**
  * Updates states of all transfer records which are "running" and "waiting" to "paused"
  *
  * @return Number of rows updated.
  */
 public int setAllRunningRecordsToPausedBeforeShutdownService() {
   ContentValues values = new ContentValues();
   values.put(TransferTable.COLUMN_STATE, TransferState.PAUSED.toString());
   return transferDBBase.update(
       transferDBBase.getContentUri(),
       values,
       TransferTable.COLUMN_STATE + " in (?,?,?,?)",
       new String[] {
         TransferState.IN_PROGRESS.toString(),
         TransferState.PENDING_PAUSE.toString(),
         TransferState.RESUMED_WAITING.toString(),
         TransferState.WAITING.toString()
       });
 }
 /**
  * Inserts a part upload record into database with the given values.
  *
  * @param bucket The name of the bucket to upload to.
  * @param key The key in the specified bucket by which to store the new object.
  * @param file The file to upload.
  * @param fileOffset The byte offset for the file to upload.
  * @param partNumber The part number of this part.
  * @param uploadId The multipart upload id of the upload.
  * @param bytesTotal The Total bytes of the file.
  * @param isLastPart Whether this part is the last part of the upload.
  * @return An Uri of the record inserted.
  */
 public Uri insertMultipartUploadRecord(
     String bucket,
     String key,
     File file,
     long fileOffset,
     int partNumber,
     String uploadId,
     long bytesTotal,
     int isLastPart) {
   ContentValues values =
       generateContentValuesForMultiPartUpload(
           bucket,
           key,
           file,
           fileOffset,
           partNumber,
           uploadId,
           bytesTotal,
           isLastPart,
           new ObjectMetadata());
   return transferDBBase.insert(transferDBBase.getContentUri(), values);
 }
 /**
  * Gets the Uri of the records that have the given state.
  *
  * @param state The state of transfers
  * @return The Uri that is used to query transfer records with the given state.
  */
 public Uri getStateUri(TransferState state) {
   return Uri.parse(transferDBBase.getContentUri() + "/state/" + state.toString());
 }
 /**
  * Gets the Uri of part records of a multipart upload.
  *
  * @param mainUploadId The main upload id of the transfer.
  * @return The Uri of the part upload records that have the given mainUploadId value.
  */
 public Uri getPartUri(int mainUploadId) {
   return Uri.parse(transferDBBase.getContentUri() + "/part/" + mainUploadId);
 }
 /**
  * Gets the Uri of a record.
  *
  * @param id The id of the transfer.
  * @return The Uri of the record specified by the id.
  */
 public Uri getRecordUri(int id) {
   return Uri.parse(transferDBBase.getContentUri() + "/" + id);
 }
 /**
  * Gets the Uri of the transfer record table.
  *
  * @return The Uri of a table.
  */
 public Uri getContentUri() {
   return transferDBBase.getContentUri();
 }
 /**
  * Updates the state and also notify TransferService to refresh its transfer record list. The
  * method is called by TransferUtility, more typically, by applications to perform "pause" or
  * "resume" actions, so it needs to explicitly notify the Service after updating the database.
  *
  * @param id The id of the transfer.
  * @param state The new state of the transfer.
  * @return Number of rows updated.
  */
 public int updateStateAndNotifyUpdate(int id, TransferState state) {
   ContentValues values = new ContentValues();
   values.put(TransferTable.COLUMN_STATE, state.toString());
   return transferDBBase.update(
       transferDBBase.getContentUri(), values, TransferTable.COLUMN_ID + "=" + id, null);
 }
 /**
  * Inserts multiple records at a time.
  *
  * @param valuesArray An array of values to insert.
  * @return The mainUploadId of the multipart records
  */
 public int bulkInsertTransferRecords(ContentValues[] valuesArray) {
   return transferDBBase.bulkInsert(transferDBBase.getContentUri(), valuesArray);
 }
 /**
  * Inserts a transfer record into database with the given values.
  *
  * @param type The type of the transfer, can be "upload" or "download".
  * @param bucket The name of the bucket to upload to.
  * @param key The key in the specified bucket by which to store the new object.
  * @param file The file to upload.
  * @param metadata The S3 Object metadata associated with this object
  * @return An Uri of the record inserted.
  */
 public Uri insertSingleTransferRecord(
     TransferType type, String bucket, String key, File file, ObjectMetadata metadata) {
   ContentValues values =
       generateContentValuesForSinglePartTransfer(type, bucket, key, file, metadata);
   return transferDBBase.insert(transferDBBase.getContentUri(), values);
 }