OpenDownloadReceiver.java revision 7c08a2b624566e731b4f23235d05de6eb2859235
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 17package com.android.browser; 18 19import android.app.DownloadManager; 20import android.content.ActivityNotFoundException; 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.net.Uri; 25 26/** 27 * 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. 31 */ 32public class OpenDownloadReceiver extends BroadcastReceiver { 33 @Override 34 public void onReceive(Context context, Intent intent) { 35 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]; 47 DownloadManager manager = (DownloadManager) context.getSystemService( 48 Context.DOWNLOAD_SERVICE); 49 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); 55 launchIntent.setDataAndType(uri, context.getContentResolver().getType(uri)); 56 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 57 try { 58 context.startActivity(launchIntent); 59 } catch (ActivityNotFoundException e) { 60 openDownloadsPage(context); 61 } 62 } 63 } 64 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 } 74} 75