OSDN Git Service

merge from open-source master
[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 = 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                             && Downloads.isStatusSuccess(status)) {
55                         Intent launchIntent = new Intent(Intent.ACTION_VIEW);
56                         Uri path = Uri.parse(filename);
57                         // If there is no scheme, then it must be a file
58                         if (path.getScheme() == null) {
59                             path = Uri.fromFile(new File(filename));
60                         }
61                         launchIntent.setDataAndType(path, mimetype);
62                         launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
63                         try {
64                             context.startActivity(launchIntent);
65                         } catch (ActivityNotFoundException ex) {
66                             Toast.makeText(context,
67                                     R.string.download_no_application_title,
68                                     Toast.LENGTH_LONG).show();
69                         }
70                     } else {
71                         // Open the downloads page
72                         Intent pageView = new Intent(context,
73                                 BrowserDownloadPage.class);
74                         pageView.setData(data);
75                         pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
76                         context.startActivity(pageView);
77                     }
78                 } else if (Intent.ACTION_DELETE.equals(action)) {
79                     if (deleteFile(cr, filename, mimetype)) {
80                         cr.delete(data, null, null);
81                     }
82                 }
83             }
84         } finally {
85             if (cursor != null) cursor.close();
86         }
87     }
88
89     /**
90      * Remove the file from the SD card
91      * @param cr ContentResolver used to delete the file.
92      * @param filename Name of the file to delete.
93      * @param mimetype Mimetype of the file to delete.
94      * @return boolean True on success, false on failure.
95      */
96     private boolean deleteFile(ContentResolver cr, String filename,
97             String mimetype) {
98         Uri uri;
99         if (mimetype.startsWith("image")) {
100             uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
101         } else if (mimetype.startsWith("audio")) {
102             uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
103         } else if (mimetype.startsWith("video")) {
104             uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
105         } else {
106             uri = null;
107         }
108         return (uri != null && cr.delete(uri, MediaStore.MediaColumns.DATA
109                 + " = " + DatabaseUtils.sqlEscapeString(filename), null) > 0)
110                 || new File(filename).delete();
111     }
112 }