summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/OpenDownloadReceiver.java
diff options
context:
space:
mode:
authorLeon Scroggins <scroggo@google.com>2010-01-26 14:15:01 -0500
committerLeon Scroggins <scroggo@google.com>2010-02-04 16:37:55 -0500
commitfedc493cd810fbd4385efbd647ee70852870988f (patch)
tree7a32e6e925b30efe6c440f3f4b82c1bccd914888 /src/com/android/browser/OpenDownloadReceiver.java
parentc38909b6156331f8d739e0d6d7a9f7a0fae3d17c (diff)
downloadandroid_packages_apps_Gello-fedc493cd810fbd4385efbd647ee70852870988f.tar.gz
android_packages_apps_Gello-fedc493cd810fbd4385efbd647ee70852870988f.tar.bz2
android_packages_apps_Gello-fedc493cd810fbd4385efbd647ee70852870988f.zip
Launch intents to open/delete downloads, and handle them in the browser.
Initial work for http://b/issue?id=2384554 : showing all downloads in the browser. On the BrowserDownloadPage, no longer rely on the filename, since once the Browser shows downloads from other applications, the filenames for those downloads will not be available, and this way all downloads are handled the same. Other applications which download files will need to handle the same Intents as OpenDownloadReceiver in order to open/delete them from the BrowserDownloadPage. OpenDownloadReceiver is also a necessary step towards moving the BrowserDownloadPage into its own application, if we ultimately decide to do that.
Diffstat (limited to 'src/com/android/browser/OpenDownloadReceiver.java')
-rw-r--r--src/com/android/browser/OpenDownloadReceiver.java99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/com/android/browser/OpenDownloadReceiver.java b/src/com/android/browser/OpenDownloadReceiver.java
new file mode 100644
index 00000000..498afc00
--- /dev/null
+++ b/src/com/android/browser/OpenDownloadReceiver.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.browser;
+
+import android.content.ActivityNotFoundException;
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.database.Cursor;
+import android.database.DatabaseUtils;
+import android.net.Uri;
+import android.provider.Downloads;
+import android.provider.MediaStore;
+import android.widget.Toast;
+
+import java.io.File;
+
+/**
+ * This {@link BroadcastReceiver} handles {@link Intent}s to open and delete
+ * files downloaded by the Browser.
+ */
+public class OpenDownloadReceiver extends BroadcastReceiver {
+ public void onReceive(Context context, Intent intent) {
+ ContentResolver cr = context.getContentResolver();
+ Uri data = intent.getData();
+ Cursor cursor = cr.query(data,
+ new String[] { Downloads.Impl._ID, Downloads.Impl._DATA,
+ Downloads.Impl.COLUMN_MIME_TYPE }, null, null, null);
+ if (cursor.moveToFirst()) {
+ String filename = cursor.getString(1);
+ String mimetype = cursor.getString(2);
+ String action = intent.getAction();
+ if (Downloads.ACTION_NOTIFICATION_CLICKED.equals(action)) {
+ Intent launchIntent = new Intent(Intent.ACTION_VIEW);
+ Uri path = Uri.parse(filename);
+ // If there is no scheme, then it must be a file
+ if (path.getScheme() == null) {
+ path = Uri.fromFile(new File(filename));
+ }
+ launchIntent.setDataAndType(path, mimetype);
+ launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ try {
+ context.startActivity(launchIntent);
+ } catch (ActivityNotFoundException ex) {
+ Toast.makeText(context,
+ R.string.download_no_application_title,
+ Toast.LENGTH_LONG).show();
+ }
+ } else if (Intent.ACTION_DELETE.equals(action)) {
+ if (deleteFile(cr, filename, mimetype)) {
+ cr.delete(data, null, null);
+ }
+ }
+ }
+ cursor.close();
+ }
+
+ /**
+ * Remove the file from the SD card
+ * @param cr ContentResolver used to delete the file.
+ * @param filename Name of the file to delete.
+ * @param mimetype Mimetype of the file to delete.
+ * @return boolean True on success, false on failure.
+ */
+ // FIXME: Once there are receivers in other packages to delete downloaded
+ // files, this should be moved to a common place so mutiple packages can
+ // share the code.
+ private boolean deleteFile(ContentResolver cr, String filename,
+ String mimetype) {
+ Uri uri;
+ if (mimetype.startsWith("image")) {
+ uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+ } else if (mimetype.startsWith("audio")) {
+ uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
+ } else if (mimetype.startsWith("video")) {
+ uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
+ } else {
+ File file = new File(filename);
+ return file.delete();
+ }
+ return cr.delete(uri, MediaStore.MediaColumns.DATA + " = "
+ + DatabaseUtils.sqlEscapeString(filename), null) > 0;
+ }
+}