blob: da53fb28e166ec469cbfdc3e13662b66c7d1e4a5 [file] [log] [blame]
Leon Scrogginsfedc4932010-01-26 14:15:01 -05001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.content.ActivityNotFoundException;
20import android.content.BroadcastReceiver;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.database.DatabaseUtils;
26import android.net.Uri;
27import android.provider.Downloads;
28import android.provider.MediaStore;
29import android.widget.Toast;
30
31import java.io.File;
32
33/**
34 * This {@link BroadcastReceiver} handles {@link Intent}s to open and delete
35 * files downloaded by the Browser.
36 */
37public class OpenDownloadReceiver extends BroadcastReceiver {
38 public void onReceive(Context context, Intent intent) {
39 ContentResolver cr = context.getContentResolver();
40 Uri data = intent.getData();
Leon Scrogginsa563d092010-04-19 16:53:49 -040041 Cursor cursor = null;
42 try {
43 cursor = cr.query(data,
44 new String[] { Downloads.Impl._ID, Downloads.Impl._DATA,
45 Downloads.Impl.COLUMN_MIME_TYPE, Downloads.COLUMN_STATUS },
46 null, null, null);
47 if (cursor.moveToFirst()) {
48 String filename = cursor.getString(1);
49 String mimetype = cursor.getString(2);
50 String action = intent.getAction();
51 if (Downloads.ACTION_NOTIFICATION_CLICKED.equals(action)) {
52 int status = cursor.getInt(3);
53 if (Downloads.isStatusCompleted(status)) {
54 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
55 Uri path = Uri.parse(filename);
56 // If there is no scheme, then it must be a file
57 if (path.getScheme() == null) {
58 path = Uri.fromFile(new File(filename));
59 }
60 launchIntent.setDataAndType(path, mimetype);
61 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
62 try {
63 context.startActivity(launchIntent);
64 } catch (ActivityNotFoundException ex) {
65 Toast.makeText(context,
66 R.string.download_no_application_title,
67 Toast.LENGTH_LONG).show();
68 }
69 } else {
70 // Open the downloads page
71 Intent pageView = new Intent(context,
72 BrowserDownloadPage.class);
73 pageView.setData(data);
74 pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
75 context.startActivity(pageView);
76 }
77 } else if (Intent.ACTION_DELETE.equals(action)) {
78 if (deleteFile(cr, filename, mimetype)) {
79 cr.delete(data, null, null);
80 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050081 }
82 }
Leon Scrogginsa563d092010-04-19 16:53:49 -040083 } finally {
84 if (cursor != null) cursor.close();
Leon Scrogginsfedc4932010-01-26 14:15:01 -050085 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050086 }
87
88 /**
89 * Remove the file from the SD card
90 * @param cr ContentResolver used to delete the file.
91 * @param filename Name of the file to delete.
92 * @param mimetype Mimetype of the file to delete.
93 * @return boolean True on success, false on failure.
94 */
Leon Scrogginsfedc4932010-01-26 14:15:01 -050095 private boolean deleteFile(ContentResolver cr, String filename,
96 String mimetype) {
97 Uri uri;
98 if (mimetype.startsWith("image")) {
99 uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
100 } else if (mimetype.startsWith("audio")) {
101 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
102 } else if (mimetype.startsWith("video")) {
103 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
104 } else {
Leon Scroggins93926e42010-03-17 09:07:05 -0400105 uri = null;
Leon Scrogginsfedc4932010-01-26 14:15:01 -0500106 }
Leon Scroggins93926e42010-03-17 09:07:05 -0400107 return (uri != null && cr.delete(uri, MediaStore.MediaColumns.DATA
108 + " = " + DatabaseUtils.sqlEscapeString(filename), null) > 0)
109 || new File(filename).delete();
Leon Scrogginsfedc4932010-01-26 14:15:01 -0500110 }
111}