Merge "Fix a wrong call to cancel downloads"

main
Jean Chalard 2013-07-16 05:40:16 +00:00 committed by Android (Google) Code Review
commit 4d4310df75
2 changed files with 22 additions and 19 deletions

View File

@ -199,6 +199,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
final ContentValues defaultMetadataValues = new ContentValues(); final ContentValues defaultMetadataValues = new ContentValues();
defaultMetadataValues.put(CLIENT_CLIENT_ID_COLUMN, ""); defaultMetadataValues.put(CLIENT_CLIENT_ID_COLUMN, "");
defaultMetadataValues.put(CLIENT_METADATA_URI_COLUMN, defaultMetadataUri); defaultMetadataValues.put(CLIENT_METADATA_URI_COLUMN, defaultMetadataUri);
defaultMetadataValues.put(CLIENT_PENDINGID_COLUMN, UpdateHandler.NOT_AN_ID);
db.insert(CLIENT_TABLE_NAME, null, defaultMetadataValues); db.insert(CLIENT_TABLE_NAME, null, defaultMetadataValues);
} }
} }
@ -358,21 +359,21 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
} }
/** /**
* Get the metadata download ID for a client ID. * Get the metadata download ID for a metadata URI.
* *
* This will retrieve the download ID for the metadata file associated with a client ID. * This will retrieve the download ID for the metadata file that has the passed URI.
* If there is no metadata download in progress for this client, it will return NOT_AN_ID. * If this URI is not being downloaded right now, it will return NOT_AN_ID.
* *
* @param context a context instance to open the database on * @param context a context instance to open the database on
* @param clientId the client ID to retrieve the metadata download ID of * @param uri the URI to retrieve the metadata download ID of
* @return the metadata download ID, or NOT_AN_ID if no download is in progress * @return the metadata download ID, or NOT_AN_ID if no download is in progress
*/ */
public static long getMetadataDownloadIdForClient(final Context context, public static long getMetadataDownloadIdForURI(final Context context,
final String clientId) { final String uri) {
SQLiteDatabase defaultDb = getDb(context, null); SQLiteDatabase defaultDb = getDb(context, null);
final Cursor cursor = defaultDb.query(CLIENT_TABLE_NAME, final Cursor cursor = defaultDb.query(CLIENT_TABLE_NAME,
new String[] { CLIENT_PENDINGID_COLUMN }, new String[] { CLIENT_PENDINGID_COLUMN },
CLIENT_CLIENT_ID_COLUMN + " = ?", new String[] { clientId }, CLIENT_METADATA_URI_COLUMN + " = ?", new String[] { uri },
null, null, null, null); null, null, null, null);
try { try {
if (!cursor.moveToFirst()) return UpdateHandler.NOT_AN_ID; if (!cursor.moveToFirst()) return UpdateHandler.NOT_AN_ID;
@ -782,6 +783,8 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
" but the values " + "contain a different ID : ", valuesClientId); " but the values " + "contain a different ID : ", valuesClientId);
return; return;
} }
// Default value for a pending ID is NOT_AN_ID
values.put(CLIENT_PENDINGID_COLUMN, UpdateHandler.NOT_AN_ID);
final SQLiteDatabase defaultDb = getDb(context, ""); final SQLiteDatabase defaultDb = getDb(context, "");
if (-1 == defaultDb.insert(CLIENT_TABLE_NAME, null, values)) { if (-1 == defaultDb.insert(CLIENT_TABLE_NAME, null, values)) {
defaultDb.update(CLIENT_TABLE_NAME, values, defaultDb.update(CLIENT_TABLE_NAME, values,

View File

@ -272,23 +272,22 @@ public final class UpdateHandler {
} }
/** /**
* Cancels a pending update, if there is one. * Cancels downloading a file, if there is one for this URI.
* *
* If none, this is a no-op. * If we are not currently downloading the file at this URI, this is a no-op.
* *
* @param context the context to open the database on * @param context the context to open the database on
* @param clientId the id of the client * @param metadataUri the URI to cancel
* @param manager an instance of DownloadManager * @param manager an instance of DownloadManager
*/ */
private static void cancelUpdateWithDownloadManager(final Context context, private static void cancelUpdateWithDownloadManager(final Context context,
final String clientId, final DownloadManager manager) { final String metadataUri, final DownloadManager manager) {
synchronized (sSharedIdProtector) { synchronized (sSharedIdProtector) {
final long metadataDownloadId = final long metadataDownloadId =
MetadataDbHelper.getMetadataDownloadIdForClient(context, clientId); MetadataDbHelper.getMetadataDownloadIdForURI(context, metadataUri);
if (NOT_AN_ID == metadataDownloadId) return; if (NOT_AN_ID == metadataDownloadId) return;
manager.remove(metadataDownloadId); manager.remove(metadataDownloadId);
writeMetadataDownloadId(context, writeMetadataDownloadId(context, metadataUri, NOT_AN_ID);
MetadataDbHelper.getMetadataUriAsString(context, clientId), NOT_AN_ID);
} }
// Consider a cancellation as a failure. As such, inform listeners that the download // Consider a cancellation as a failure. As such, inform listeners that the download
// has failed. // has failed.
@ -298,10 +297,10 @@ public final class UpdateHandler {
} }
/** /**
* Cancels a pending update, if there is one. * Cancels a pending update for this client, if there is one.
* *
* If there is none, this is a no-op. This is a helper method that gets the * If we are not currently updating metadata for this client, this is a no-op. This is a helper
* download manager service. * method that gets the download manager service and the metadata URI for this client.
* *
* @param context the context, to get an instance of DownloadManager * @param context the context, to get an instance of DownloadManager
* @param clientId the ID of the client we want to cancel the update of * @param clientId the ID of the client we want to cancel the update of
@ -309,7 +308,8 @@ public final class UpdateHandler {
public static void cancelUpdate(final Context context, final String clientId) { public static void cancelUpdate(final Context context, final String clientId) {
final DownloadManager manager = final DownloadManager manager =
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
if (null != manager) cancelUpdateWithDownloadManager(context, clientId, manager); final String metadataUri = MetadataDbHelper.getMetadataUriAsString(context, clientId);
if (null != manager) cancelUpdateWithDownloadManager(context, metadataUri, manager);
} }
/** /**