OpenDownloadReceiver.java revision 93926e4f464989b14bc320291af66faf327395bc
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
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();
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    private boolean deleteFile(ContentResolver cr, String filename,
81            String mimetype) {
82        Uri uri;
83        if (mimetype.startsWith("image")) {
84            uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
85        } else if (mimetype.startsWith("audio")) {
86            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
87        } else if (mimetype.startsWith("video")) {
88            uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
89        } else {
90            uri = null;
91        }
92        return (uri != null && cr.delete(uri, MediaStore.MediaColumns.DATA
93                + " = " + DatabaseUtils.sqlEscapeString(filename), null) > 0)
94                || new File(filename).delete();
95    }
96}
97