Home | History | Annotate | Download | only in browser
      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 
     19 import android.app.DownloadManager;
     20 import android.content.ActivityNotFoundException;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.net.Uri;
     25 import android.os.Handler;
     26 import android.os.HandlerThread;
     27 
     28 /**
     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.
     33  */
     34 public class OpenDownloadReceiver extends BroadcastReceiver {
     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     }
     41     @Override
     42     public void onReceive(final Context context, Intent intent) {
     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         }
     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) {
     67         DownloadManager manager = (DownloadManager) context.getSystemService(
     68                 Context.DOWNLOAD_SERVICE);
     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);
     75             launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id));
     76             launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     77             try {
     78                 context.startActivity(launchIntent);
     79             } catch (ActivityNotFoundException e) {
     80                 openDownloadsPage(context);
     81             }
     82         }
     83     }
     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     }
     94 }
     95