From 8ec87ea5a5c191554d6205d4c779768664b1724b Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Wed, 18 Sep 2013 12:53:39 -0700 Subject: Unified handling of errors around opening. Handle both missing downloads and missing activities. Bug: 10799449, 10713636 Change-Id: I592b07fc5cf530526803379d7f7a99e8a6b207c4 --- .../providers/downloads/DownloadReceiver.java | 10 ++---- .../android/providers/downloads/OpenHelper.java | 36 +++++++++++++++++----- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/com/android/providers/downloads/DownloadReceiver.java b/src/com/android/providers/downloads/DownloadReceiver.java index 42f029a3..f3d23766 100644 --- a/src/com/android/providers/downloads/DownloadReceiver.java +++ b/src/com/android/providers/downloads/DownloadReceiver.java @@ -20,7 +20,6 @@ import static android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMP import static android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION; import android.app.DownloadManager; -import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.ContentUris; import android.content.ContentValues; @@ -162,13 +161,8 @@ public class DownloadReceiver extends BroadcastReceiver { * {@link DownloadManager#COLUMN_ID}. */ private void openDownload(Context context, long id) { - final Intent intent = OpenHelper.buildViewIntent(context, id); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - context.startActivity(intent); - } catch (ActivityNotFoundException ex) { - Log.d(Constants.TAG, "no activity for " + intent, ex); - Toast.makeText(context, R.string.download_no_application_title, Toast.LENGTH_LONG) + if (!OpenHelper.startViewIntent(context, id, Intent.FLAG_ACTIVITY_NEW_TASK)) { + Toast.makeText(context, R.string.download_no_application_title, Toast.LENGTH_SHORT) .show(); } } diff --git a/src/com/android/providers/downloads/OpenHelper.java b/src/com/android/providers/downloads/OpenHelper.java index 0d5f5e92..af7a37f2 100644 --- a/src/com/android/providers/downloads/OpenHelper.java +++ b/src/com/android/providers/downloads/OpenHelper.java @@ -21,23 +21,47 @@ import static android.app.DownloadManager.COLUMN_LOCAL_URI; import static android.app.DownloadManager.COLUMN_MEDIA_TYPE; import static android.app.DownloadManager.COLUMN_URI; import static android.provider.Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI; +import static com.android.providers.downloads.Constants.TAG; import android.app.DownloadManager; +import android.content.ActivityNotFoundException; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.provider.Downloads.Impl.RequestHeaders; +import android.util.Log; import java.io.File; public class OpenHelper { /** - * Build an {@link Intent} to view the download at current {@link Cursor} - * position, handling subtleties around installing packages. + * Build and start an {@link Intent} to view the download with given ID, + * handling subtleties around installing packages. */ - public static Intent buildViewIntent(Context context, long id) { + public static boolean startViewIntent(Context context, long id, int intentFlags) { + final Intent intent = OpenHelper.buildViewIntent(context, id); + if (intent == null) { + Log.w(TAG, "No intent built for " + id); + return false; + } + + intent.addFlags(intentFlags); + try { + context.startActivity(intent); + return true; + } catch (ActivityNotFoundException e) { + Log.w(TAG, "Failed to start " + intent + ": " + e); + return false; + } + } + + /** + * Build an {@link Intent} to view the download with given ID, handling + * subtleties around installing packages. + */ + private static Intent buildViewIntent(Context context, long id) { final DownloadManager downManager = (DownloadManager) context.getSystemService( Context.DOWNLOAD_SERVICE); downManager.setAccessAllDownloads(true); @@ -45,7 +69,7 @@ public class OpenHelper { final Cursor cursor = downManager.query(new DownloadManager.Query().setFilterById(id)); try { if (!cursor.moveToFirst()) { - throw new IllegalArgumentException("Missing download " + id); + return null; } final Uri localUri = getCursorUri(cursor, COLUMN_LOCAL_URI); @@ -121,10 +145,6 @@ public class OpenHelper { return Uri.parse(getCursorString(cursor, column)); } - private static long getCursorLong(Cursor cursor, String column) { - return cursor.getLong(cursor.getColumnIndexOrThrow(column)); - } - private static File getCursorFile(Cursor cursor, String column) { return new File(cursor.getString(cursor.getColumnIndexOrThrow(column))); } -- cgit v1.2.3