OSDN Git Service

am 9df17a63: am 066e5308: reconcile main tree with open-source eclair
[android-x86/packages-apps-Browser.git] / src / com / android / browser / OpenDownloadReceiver.java
1 /*
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
17 package com.android.browser;
18
19 import android.content.ActivityNotFoundException;
20 import android.content.BroadcastReceiver;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.database.DatabaseUtils;
26 import android.net.Uri;
27 import android.provider.Downloads;
28 import android.provider.MediaStore;
29 import android.widget.Toast;
30
31 import java.io.File;
32
33 /**
34  * This {@link BroadcastReceiver} handles {@link Intent}s to open and delete
35  * files downloaded by the Browser.
36  */
37 public class OpenDownloadReceiver extends BroadcastReceiver {
38     public void onReceive(Context context, Intent intent) {
39         ContentResolver cr = context.getContentResolver();
40         Uri data = intent.getData();
41         Cursor cursor = cr.query(data,
42                 new String[] { Downloads.Impl._ID, Downloads.Impl._DATA,
43                 Downloads.Impl.COLUMN_MIME_TYPE }, null, null, null);
44         if (cursor.moveToFirst()) {
45             String filename = cursor.getString(1);
46             String mimetype = cursor.getString(2);
47             String action = intent.getAction();
48             if (Downloads.ACTION_NOTIFICATION_CLICKED.equals(action)) {
49                 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
50                 Uri path = Uri.parse(filename);
51                 // If there is no scheme, then it must be a file
52                 if (path.getScheme() == null) {
53                     path = Uri.fromFile(new File(filename));
54                 }
55                 launchIntent.setDataAndType(path, mimetype);
56                 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
57                 try {
58                     context.startActivity(launchIntent);
59                 } catch (ActivityNotFoundException ex) {
60                     Toast.makeText(context,
61                             R.string.download_no_application_title,
62                             Toast.LENGTH_LONG).show();
63                 }
64             } else if (Intent.ACTION_DELETE.equals(action)) {
65                 if (deleteFile(cr, filename, mimetype)) {
66                     cr.delete(data, null, null);
67                 }
68             }
69         }
70         cursor.close();
71     }
72
73     /**
74      * Remove the file from the SD card
75      * @param cr ContentResolver used to delete the file.
76      * @param filename Name of the file to delete.
77      * @param mimetype Mimetype of the file to delete.
78      * @return boolean True on success, false on failure.
79      */
80     // FIXME: Once there are receivers in other packages to delete downloaded
81     // files, this should be moved to a common place so mutiple packages can
82     // share the code.
83     private boolean deleteFile(ContentResolver cr, String filename,
84             String mimetype) {
85         Uri uri;
86         if (mimetype.startsWith("image")) {
87             uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
88         } else if (mimetype.startsWith("audio")) {
89             uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
90         } else if (mimetype.startsWith("video")) {
91             uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
92         } else {
93             File file = new File(filename);
94             return file.delete();
95         }
96         return cr.delete(uri, MediaStore.MediaColumns.DATA + " = "
97                 + DatabaseUtils.sqlEscapeString(filename), null) > 0;
98     }
99 }