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.ActivityNotFoundException; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.database.Cursor; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.provider.Downloads; |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 28 | import android.widget.Toast; |
| 29 | |
| 30 | import java.io.File; |
| 31 | |
| 32 | /** |
Leon Scroggins III | a682a3c | 2010-09-27 12:32:40 -0400 | [diff] [blame] | 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. |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 37 | */ |
| 38 | public class OpenDownloadReceiver extends BroadcastReceiver { |
| 39 | public void onReceive(Context context, Intent intent) { |
| 40 | ContentResolver cr = context.getContentResolver(); |
| 41 | Uri data = intent.getData(); |
Leon Scroggins | a563d09 | 2010-04-19 16:53:49 -0400 | [diff] [blame] | 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.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.ACTION_NOTIFICATION_CLICKED.equals(action)) { |
| 53 | int status = cursor.getInt(3); |
Leon Scroggins | faea564 | 2010-04-26 13:36:01 -0400 | [diff] [blame] | 54 | if (Downloads.isStatusCompleted(status) |
| 55 | && Downloads.isStatusSuccess(status)) { |
Leon Scroggins | a563d09 | 2010-04-19 16:53:49 -0400 | [diff] [blame] | 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 |
Leon Scroggins III | a682a3c | 2010-09-27 12:32:40 -0400 | [diff] [blame] | 73 | Intent pageView = new Intent( |
| 74 | DownloadManager.ACTION_VIEW_DOWNLOADS); |
Leon Scroggins | a563d09 | 2010-04-19 16:53:49 -0400 | [diff] [blame] | 75 | pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 76 | context.startActivity(pageView); |
| 77 | } |
Leon Scroggins | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
Leon Scroggins | a563d09 | 2010-04-19 16:53:49 -0400 | [diff] [blame] | 80 | } finally { |
| 81 | if (cursor != null) cursor.close(); |
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 | fedc493 | 2010-01-26 14:15:01 -0500 | [diff] [blame] | 84 | } |