OSDN Git Service

Merge "Using ViewStub to defer the inflation of GeolocationPermissionsPrompt until...
[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                         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                     }
81                 }
82             }
83         } finally {
84             if (cursor != null) cursor.close();
85         }
86     }
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      */
95     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 {
105             uri = null;
106         }
107         return (uri != null && cr.delete(uri, MediaStore.MediaColumns.DATA
108                 + " = " + DatabaseUtils.sqlEscapeString(filename), null) > 0)
109                 || new File(filename).delete();
110     }
111 }