Click on notification for unfinished download, open downloads page.

BrowserActivity:
Use OpenDownloadReceiver as class name, so that it will handle the
notification from download manager.

OpenDownloadReceiver:
If the download is not complete, show the downloads page.

Bug 2606772

Change-Id: Ifeeac0943650552c6da232cf98088bbf958fd403
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 607dc38..5e55789 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -2901,7 +2901,7 @@
         values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
                 getPackageName());
         values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
-                BrowserDownloadPage.class.getCanonicalName());
+                OpenDownloadReceiver.class.getCanonicalName());
         values.put(Downloads.Impl.COLUMN_VISIBILITY,
                 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
         values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
diff --git a/src/com/android/browser/OpenDownloadReceiver.java b/src/com/android/browser/OpenDownloadReceiver.java
index ad66c03..da53fb2 100644
--- a/src/com/android/browser/OpenDownloadReceiver.java
+++ b/src/com/android/browser/OpenDownloadReceiver.java
@@ -38,36 +38,51 @@
     public void onReceive(Context context, Intent intent) {
         ContentResolver cr = context.getContentResolver();
         Uri data = intent.getData();
-        Cursor cursor = cr.query(data,
-                new String[] { Downloads.Impl._ID, Downloads.Impl._DATA,
-                Downloads.Impl.COLUMN_MIME_TYPE }, null, null, null);
-        if (cursor.moveToFirst()) {
-            String filename = cursor.getString(1);
-            String mimetype = cursor.getString(2);
-            String action = intent.getAction();
-            if (Downloads.ACTION_NOTIFICATION_CLICKED.equals(action)) {
-                Intent launchIntent = new Intent(Intent.ACTION_VIEW);
-                Uri path = Uri.parse(filename);
-                // If there is no scheme, then it must be a file
-                if (path.getScheme() == null) {
-                    path = Uri.fromFile(new File(filename));
-                }
-                launchIntent.setDataAndType(path, mimetype);
-                launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                try {
-                    context.startActivity(launchIntent);
-                } catch (ActivityNotFoundException ex) {
-                    Toast.makeText(context,
-                            R.string.download_no_application_title,
-                            Toast.LENGTH_LONG).show();
-                }
-            } else if (Intent.ACTION_DELETE.equals(action)) {
-                if (deleteFile(cr, filename, mimetype)) {
-                    cr.delete(data, null, null);
+        Cursor cursor = null;
+        try {
+            cursor = cr.query(data,
+                    new String[] { Downloads.Impl._ID, Downloads.Impl._DATA,
+                    Downloads.Impl.COLUMN_MIME_TYPE, Downloads.COLUMN_STATUS },
+                    null, null, null);
+            if (cursor.moveToFirst()) {
+                String filename = cursor.getString(1);
+                String mimetype = cursor.getString(2);
+                String action = intent.getAction();
+                if (Downloads.ACTION_NOTIFICATION_CLICKED.equals(action)) {
+                    int status = cursor.getInt(3);
+                    if (Downloads.isStatusCompleted(status)) {
+                        Intent launchIntent = new Intent(Intent.ACTION_VIEW);
+                        Uri path = Uri.parse(filename);
+                        // If there is no scheme, then it must be a file
+                        if (path.getScheme() == null) {
+                            path = Uri.fromFile(new File(filename));
+                        }
+                        launchIntent.setDataAndType(path, mimetype);
+                        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                        try {
+                            context.startActivity(launchIntent);
+                        } catch (ActivityNotFoundException ex) {
+                            Toast.makeText(context,
+                                    R.string.download_no_application_title,
+                                    Toast.LENGTH_LONG).show();
+                        }
+                    } else {
+                        // Open the downloads page
+                        Intent pageView = new Intent(context,
+                                BrowserDownloadPage.class);
+                        pageView.setData(data);
+                        pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                        context.startActivity(pageView);
+                    }
+                } else if (Intent.ACTION_DELETE.equals(action)) {
+                    if (deleteFile(cr, filename, mimetype)) {
+                        cr.delete(data, null, null);
+                    }
                 }
             }
+        } finally {
+            if (cursor != null) cursor.close();
         }
-        cursor.close();
     }
 
     /**