blob: 0f0ba705e77c651c1b9e6a5fe1cca74872602581 [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 Scroggins7c08a2b2010-11-15 17:49:10 -050020import android.content.ActivityNotFoundException;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050021import android.content.BroadcastReceiver;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050022import android.content.Context;
23import android.content.Intent;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050024import android.net.Uri;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050025
26/**
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -040027 * This {@link BroadcastReceiver} handles clicks to notifications that
28 * downloads from the browser are in progress/complete. Clicking on an
29 * in-progress or failed download will open the download manager. Clicking on
30 * a complete, successful download will open the file.
Leon Scrogginsfedc4932010-01-26 14:15:01 -050031 */
32public class OpenDownloadReceiver extends BroadcastReceiver {
Leon Scroggins09ccfc72010-10-28 16:22:24 -040033 @Override
Leon Scrogginsfedc4932010-01-26 14:15:01 -050034 public void onReceive(Context context, Intent intent) {
Leon Scroggins09ccfc72010-10-28 16:22:24 -040035 String action = intent.getAction();
36 if (!DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
37 openDownloadsPage(context);
38 return;
39 }
40 long ids[] = intent.getLongArrayExtra(
41 DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
42 if (ids == null || ids.length == 0) {
43 openDownloadsPage(context);
44 return;
45 }
46 long id = ids[0];
Leon Scroggins09ccfc72010-10-28 16:22:24 -040047 DownloadManager manager = (DownloadManager) context.getSystemService(
48 Context.DOWNLOAD_SERVICE);
Vasu Nori1d88aa02010-11-04 18:16:10 -070049 Uri uri = manager.getUriForDownloadedFile(id);
50 if (uri == null) {
51 // Open the downloads page
52 openDownloadsPage(context);
53 } else {
54 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
Vasu Nori628bc7e2010-11-16 18:01:56 -080055 launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id));
Vasu Nori1d88aa02010-11-04 18:16:10 -070056 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Leon Scroggins7c08a2b2010-11-15 17:49:10 -050057 try {
58 context.startActivity(launchIntent);
59 } catch (ActivityNotFoundException e) {
60 openDownloadsPage(context);
61 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050062 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050063 }
Leon Scroggins09ccfc72010-10-28 16:22:24 -040064
65 /**
66 * Open the Activity which shows a list of all downloads.
67 * @param context
68 */
69 private void openDownloadsPage(Context context) {
70 Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
71 pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
72 context.startActivity(pageView);
73 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050074}