blob: 99e5f4106513d0d54b7f4d4889c76f370a8ece9b [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
Steve Howard5a862fc2010-09-28 12:52:06 -070019import android.app.DownloadManager;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050020import android.content.ActivityNotFoundException;
21import android.content.BroadcastReceiver;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.database.Cursor;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050026import android.net.Uri;
27import android.provider.Downloads;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050028import android.widget.Toast;
29
30import java.io.File;
31
32/**
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -040033 * This {@link BroadcastReceiver} handles clicks to notifications that
34 * downloads from the browser are in progress/complete. Clicking on an
35 * in-progress or failed download will open the download manager. Clicking on
36 * a complete, successful download will open the file.
Leon Scrogginsfedc4932010-01-26 14:15:01 -050037 */
38public class OpenDownloadReceiver extends BroadcastReceiver {
39 public void onReceive(Context context, Intent intent) {
40 ContentResolver cr = context.getContentResolver();
41 Uri data = intent.getData();
Leon Scrogginsa563d092010-04-19 16:53:49 -040042 Cursor cursor = null;
43 try {
44 cursor = cr.query(data,
45 new String[] { Downloads.Impl._ID, Downloads.Impl._DATA,
46 Downloads.Impl.COLUMN_MIME_TYPE, Downloads.COLUMN_STATUS },
47 null, null, null);
48 if (cursor.moveToFirst()) {
49 String filename = cursor.getString(1);
50 String mimetype = cursor.getString(2);
51 String action = intent.getAction();
52 if (Downloads.ACTION_NOTIFICATION_CLICKED.equals(action)) {
53 int status = cursor.getInt(3);
Leon Scrogginsfaea5642010-04-26 13:36:01 -040054 if (Downloads.isStatusCompleted(status)
55 && Downloads.isStatusSuccess(status)) {
Leon Scrogginsa563d092010-04-19 16:53:49 -040056 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
57 Uri path = Uri.parse(filename);
58 // If there is no scheme, then it must be a file
59 if (path.getScheme() == null) {
60 path = Uri.fromFile(new File(filename));
61 }
62 launchIntent.setDataAndType(path, mimetype);
63 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
64 try {
65 context.startActivity(launchIntent);
66 } catch (ActivityNotFoundException ex) {
67 Toast.makeText(context,
68 R.string.download_no_application_title,
69 Toast.LENGTH_LONG).show();
70 }
71 } else {
72 // Open the downloads page
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -040073 Intent pageView = new Intent(
74 DownloadManager.ACTION_VIEW_DOWNLOADS);
Leon Scrogginsa563d092010-04-19 16:53:49 -040075 pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
76 context.startActivity(pageView);
77 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050078 }
79 }
Leon Scrogginsa563d092010-04-19 16:53:49 -040080 } finally {
81 if (cursor != null) cursor.close();
Leon Scrogginsfedc4932010-01-26 14:15:01 -050082 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050083 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050084}