blob: 4277ff493ca1c4be90e93e8aaa6661a59f3df237 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050018
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 Scroggins2d8a51d2010-11-17 10:13:29 -050025import android.os.Handler;
26import android.os.HandlerThread;
Leon Scrogginsfedc4932010-01-26 14:15:01 -050027
28/**
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -040029 * This {@link BroadcastReceiver} handles clicks to notifications that
30 * downloads from the browser are in progress/complete. Clicking on an
31 * in-progress or failed download will open the download manager. Clicking on
32 * a complete, successful download will open the file.
Leon Scrogginsfedc4932010-01-26 14:15:01 -050033 */
34public class OpenDownloadReceiver extends BroadcastReceiver {
Leon Scroggins2d8a51d2010-11-17 10:13:29 -050035 private static Handler sAsyncHandler;
36 static {
37 HandlerThread thr = new HandlerThread("Open browser download async");
38 thr.start();
39 sAsyncHandler = new Handler(thr.getLooper());
40 }
Leon Scroggins09ccfc72010-10-28 16:22:24 -040041 @Override
Leon Scroggins2d8a51d2010-11-17 10:13:29 -050042 public void onReceive(final Context context, Intent intent) {
Leon Scroggins09ccfc72010-10-28 16:22:24 -040043 String action = intent.getAction();
44 if (!DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
45 openDownloadsPage(context);
46 return;
47 }
48 long ids[] = intent.getLongArrayExtra(
49 DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
50 if (ids == null || ids.length == 0) {
51 openDownloadsPage(context);
52 return;
53 }
Leon Scroggins2d8a51d2010-11-17 10:13:29 -050054 final long id = ids[0];
55 final PendingResult result = goAsync();
56 Runnable worker = new Runnable() {
57 @Override
58 public void run() {
59 onReceiveAsync(context, id);
60 result.finish();
61 }
62 };
63 sAsyncHandler.post(worker);
64 }
65
66 private void onReceiveAsync(Context context, long id) {
Leon Scroggins09ccfc72010-10-28 16:22:24 -040067 DownloadManager manager = (DownloadManager) context.getSystemService(
68 Context.DOWNLOAD_SERVICE);
Vasu Nori1d88aa02010-11-04 18:16:10 -070069 Uri uri = manager.getUriForDownloadedFile(id);
70 if (uri == null) {
71 // Open the downloads page
72 openDownloadsPage(context);
73 } else {
74 Intent launchIntent = new Intent(Intent.ACTION_VIEW);
Vasu Nori628bc7e2010-11-16 18:01:56 -080075 launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id));
Vasu Nori1d88aa02010-11-04 18:16:10 -070076 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Leon Scroggins7c08a2b2010-11-15 17:49:10 -050077 try {
78 context.startActivity(launchIntent);
79 } catch (ActivityNotFoundException e) {
80 openDownloadsPage(context);
81 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050082 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050083 }
Leon Scroggins09ccfc72010-10-28 16:22:24 -040084
85 /**
86 * Open the Activity which shows a list of all downloads.
87 * @param context
88 */
89 private void openDownloadsPage(Context context) {
90 Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
91 pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
92 context.startActivity(pageView);
93 }
Leon Scrogginsfedc4932010-01-26 14:15:01 -050094}