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 | |
| 17 | package com.android.browser; |
| 18 | |
Steve Howard | 5a862fc | 2010-09-28 12:52:06 -0700 | [diff] [blame] | 19 | import android.app.DownloadManager; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 23 | import android.net.Uri; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 24 | |
| 25 | /** |
Leon Scroggins III | a682a3c | 2010-09-27 12:32:40 -0400 | [diff] [blame] | 26 | * 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 Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 30 | */ |
| 31 | public class OpenDownloadReceiver extends BroadcastReceiver { |
Leon Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 32 | @Override |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 33 | public void onReceive(Context context, Intent intent) { |
Leon Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 34 | 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 Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 46 | DownloadManager manager = (DownloadManager) context.getSystemService( |
| 47 | Context.DOWNLOAD_SERVICE); |
Vasu Nori | 1d88aa0 | 2010-11-04 18:16:10 -0700 | [diff] [blame] | 48 | 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 Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 57 | } |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 58 | } |
Leon Scroggins | 09ccfc7 | 2010-10-28 16:22:24 -0400 | [diff] [blame] | 59 | |
| 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 Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 69 | } |