blob: 02dba51db9d865515310d10815a7efc7a985880a [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.BroadcastReceiver;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050021import android.content.Context;
22import android.content.Intent;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050023import android.net.Uri;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050024
25/**
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -040026 * This {@link BroadcastReceiver} handles clicks to notifications that
27 * downloads from the browser are in progress/complete. Clicking on an
28 * in-progress or failed download will open the download manager. Clicking on
29 * a complete, successful download will open the file.
Leon Scrogginsfedc4932010-01-26 14:15:01 -050030 */
31public class OpenDownloadReceiver extends BroadcastReceiver {
Leon Scroggins09ccfc72010-10-28 16:22:24 -040032 @Override
Leon Scrogginsfedc4932010-01-26 14:15:01 -050033 public void onReceive(Context context, Intent intent) {
Leon Scroggins09ccfc72010-10-28 16:22:24 -040034 String action = intent.getAction();
35 if (!DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
36 openDownloadsPage(context);
37 return;
38 }
39 long ids[] = intent.getLongArrayExtra(
40 DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
41 if (ids == null || ids.length == 0) {
42 openDownloadsPage(context);
43 return;
44 }
45 long id = ids[0];
Leon Scroggins09ccfc72010-10-28 16:22:24 -040046 DownloadManager manager = (DownloadManager) context.getSystemService(
47 Context.DOWNLOAD_SERVICE);
Vasu Nori1d88aa02010-11-04 18:16:10 -070048 Uri uri = manager.getUriForDownloadedFile(id);
49 if (uri == null) {
50 // Open the downloads page
51 openDownloadsPage(context);
52 } else {
53 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
54 launchIntent.setDataAndType(uri, context.getContentResolver().getType(uri));
55 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
56 context.startActivity(launchIntent);
Leon Scrogginsfedc4932010-01-26 14:15:01 -050057 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050058 }
Leon Scroggins09ccfc72010-10-28 16:22:24 -040059
60 /**
61 * Open the Activity which shows a list of all downloads.
62 * @param context
63 */
64 private void openDownloadsPage(Context context) {
65 Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
66 pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67 context.startActivity(pageView);
68 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050069}