Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 18 | |
Steve Howard | 5a862fc | 2010-09-28 12:52:06 -0700 | [diff] [blame] | 19 | import android.app.DownloadManager; |
Leon Scroggins | 7c08a2b | 2010-11-15 17:49:10 -0500 | [diff] [blame] | 20 | import android.content.ActivityNotFoundException; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 24 | import android.net.Uri; |
Leon Scroggins | 2d8a51d | 2010-11-17 10:13:29 -0500 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.HandlerThread; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 27 | |
| 28 | /** |
Leon Scroggins III | a682a3c | 2010-09-27 12:32:40 -0400 | [diff] [blame] | 29 | * 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 Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 33 | */ |
| 34 | public class OpenDownloadReceiver extends BroadcastReceiver { |
Leon Scroggins | 2d8a51d | 2010-11-17 10:13:29 -0500 | [diff] [blame] | 35 | 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 Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 41 | @Override |
Leon Scroggins | 2d8a51d | 2010-11-17 10:13:29 -0500 | [diff] [blame] | 42 | public void onReceive(final Context context, Intent intent) { |
Leon Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 43 | 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 Scroggins | 2d8a51d | 2010-11-17 10:13:29 -0500 | [diff] [blame] | 54 | 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 Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 67 | DownloadManager manager = (DownloadManager) context.getSystemService( |
| 68 | Context.DOWNLOAD_SERVICE); |
Vasu Nori | 1d88aa0 | 2010-11-04 18:16:10 -0700 | [diff] [blame] | 69 | 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 Nori | 628bc7e | 2010-11-16 18:01:56 -0800 | [diff] [blame] | 75 | launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id)); |
Vasu Nori | 1d88aa0 | 2010-11-04 18:16:10 -0700 | [diff] [blame] | 76 | launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Leon Scroggins | 7c08a2b | 2010-11-15 17:49:10 -0500 | [diff] [blame] | 77 | try { |
| 78 | context.startActivity(launchIntent); |
| 79 | } catch (ActivityNotFoundException e) { |
| 80 | openDownloadsPage(context); |
| 81 | } |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 82 | } |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 83 | } |
Leon Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 84 | |
| 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 Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 94 | } |