OpenDownloadReceiver.java revision b39546fe994ff9946d387377117ce384704cab13
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.ContentResolver; 23import android.content.Context; 24import android.content.Intent; 25import android.database.Cursor; 26import android.net.Uri; 27import android.provider.Downloads; 28import android.widget.Toast; 29 30import java.io.File; 31 32/** 33 * This {@link BroadcastReceiver} handles clicks to notifications that 34 * downloads from the browser are in progress/complete. Clicking on an 35 * in-progress or failed download will open the download manager. Clicking on 36 * a complete, successful download will open the file. 37 */ 38public class OpenDownloadReceiver extends BroadcastReceiver { 39 public void onReceive(Context context, Intent intent) { 40 ContentResolver cr = context.getContentResolver(); 41 Uri data = intent.getData(); 42 Cursor cursor = null; 43 try { 44 cursor = cr.query(data, 45 new String[] { Downloads.Impl._ID, Downloads.Impl._DATA, 46 Downloads.Impl.COLUMN_MIME_TYPE, Downloads.Impl.COLUMN_STATUS }, 47 null, null, null); 48 if (cursor.moveToFirst()) { 49 String filename = cursor.getString(1); 50 String mimetype = cursor.getString(2); 51 String action = intent.getAction(); 52 if (Downloads.Impl.ACTION_NOTIFICATION_CLICKED.equals(action)) { 53 int status = cursor.getInt(3); 54 if (Downloads.Impl.isStatusCompleted(status) 55 && Downloads.Impl.isStatusSuccess(status)) { 56 Intent launchIntent = new Intent(Intent.ACTION_VIEW); 57 Uri path = Uri.parse(filename); 58 // If there is no scheme, then it must be a file 59 if (path.getScheme() == null) { 60 path = Uri.fromFile(new File(filename)); 61 } 62 launchIntent.setDataAndType(path, mimetype); 63 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 64 try { 65 context.startActivity(launchIntent); 66 } catch (ActivityNotFoundException ex) { 67 Toast.makeText(context, 68 R.string.download_no_application_title, 69 Toast.LENGTH_LONG).show(); 70 } 71 } else { 72 // Open the downloads page 73 Intent pageView = new Intent( 74 DownloadManager.ACTION_VIEW_DOWNLOADS); 75 pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 76 context.startActivity(pageView); 77 } 78 } 79 } 80 } finally { 81 if (cursor != null) cursor.close(); 82 } 83 } 84} 85