Merge branch 'froyo' into froyo-release
diff --git a/res/layout/geolocation_permissions_prompt.xml b/res/layout/geolocation_permissions_prompt.xml
index cdb25d2..babde3a 100755
--- a/res/layout/geolocation_permissions_prompt.xml
+++ b/res/layout/geolocation_permissions_prompt.xml
@@ -16,7 +16,8 @@
      This is the layout for the Geolocation permissions prompt.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.android.browser.GeolocationPermissionsPrompt
+    xmlns:android="http://schemas.android.com/apk/res/android"
     android:fitsSystemWindows="true"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
@@ -87,4 +88,4 @@
 
         </LinearLayout>
     </LinearLayout>
-</LinearLayout>
+</com.android.browser.GeolocationPermissionsPrompt>
diff --git a/res/layout/tab.xml b/res/layout/tab.xml
index abef51d..69baf56 100755
--- a/res/layout/tab.xml
+++ b/res/layout/tab.xml
@@ -32,8 +32,8 @@
         android:layout_weight="1" />
 
     <!-- Geolocation permissions prompt -->
-    <com.android.browser.GeolocationPermissionsPrompt
-        android:id="@+id/geolocation_permissions_prompt"
+    <ViewStub android:id="@+id/geolocation_permissions_prompt"
+        android:layout="@layout/geolocation_permissions_prompt"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
 
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/BrowserBookmarksPage.java b/src/com/android/browser/BrowserBookmarksPage.java
index a824a84..7560c78 100644
--- a/src/com/android/browser/BrowserBookmarksPage.java
+++ b/src/com/android/browser/BrowserBookmarksPage.java
@@ -679,6 +679,7 @@
      *  Refresh the shown list after the database has changed.
      */
     private void refreshList() {
+        if (mBookmarksAdapter == null) return;
         mBookmarksAdapter.refreshList();
     }
 
diff --git a/src/com/android/browser/GeolocationPermissionsPrompt.java b/src/com/android/browser/GeolocationPermissionsPrompt.java
index 982aa89..95c5415 100755
--- a/src/com/android/browser/GeolocationPermissionsPrompt.java
+++ b/src/com/android/browser/GeolocationPermissionsPrompt.java
@@ -46,15 +46,26 @@
 
     public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
         super(context, attrs);
-        LayoutInflater factory = LayoutInflater.from(context);
-        factory.inflate(R.layout.geolocation_permissions_prompt, this);
+    }
 
+    void init() {
         mInner = (LinearLayout) findViewById(R.id.inner);
         mMessage = (TextView) findViewById(R.id.message);
         mShareButton = (Button) findViewById(R.id.share_button);
         mDontShareButton = (Button) findViewById(R.id.dont_share_button);
         mRemember = (CheckBox) findViewById(R.id.remember);
-        setButtonClickListeners();
+
+        final GeolocationPermissionsPrompt me = this;
+        mShareButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                me.handleButtonClick(true);
+            }
+        });
+        mDontShareButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                me.handleButtonClick(false);
+            }
+        });
     }
 
     /**
@@ -79,23 +90,6 @@
     }
 
     /**
-     * Sets the on click listeners for the buttons.
-     */
-    private void setButtonClickListeners() {
-        final GeolocationPermissionsPrompt me = this;
-        mShareButton.setOnClickListener(new View.OnClickListener() {
-            public void onClick(View v) {
-                me.handleButtonClick(true);
-            }
-        });
-        mDontShareButton.setOnClickListener(new View.OnClickListener() {
-            public void onClick(View v) {
-                me.handleButtonClick(false);
-            }
-        });
-    }
-
-    /**
      * Handles a click on one the buttons by invoking the callback.
      */
     private void handleButtonClick(boolean allow) {
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();
     }
 
     /**
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 8c2371a..01424e2 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -48,6 +48,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.ViewStub;
 import android.view.View.OnClickListener;
 import android.webkit.ConsoleMessage;
 import android.webkit.CookieSyncManager;
@@ -1097,7 +1098,7 @@
         public void onGeolocationPermissionsShowPrompt(String origin,
                 GeolocationPermissions.Callback callback) {
             if (mInForeground) {
-                mGeolocationPermissionsPrompt.show(origin, callback);
+                getGeolocationPermissionsPrompt().show(origin, callback);
             }
         }
 
@@ -1106,7 +1107,7 @@
          */
         @Override
         public void onGeolocationPermissionsHidePrompt() {
-            if (mInForeground) {
+            if (mInForeground && mGeolocationPermissionsPrompt != null) {
                 mGeolocationPermissionsPrompt.hide();
             }
         }
@@ -1309,10 +1310,6 @@
         // WebView, as well as any other UI elements associated with the tab.
         mContainer = mInflateService.inflate(R.layout.tab, null);
 
-        mGeolocationPermissionsPrompt =
-            (GeolocationPermissionsPrompt) mContainer.findViewById(
-                R.id.geolocation_permissions_prompt);
-
         mDownloadListener = new DownloadListener() {
             public void onDownloadStart(String url, String userAgent,
                     String contentDisposition, String mimetype,
@@ -1362,7 +1359,9 @@
         }
         // If the WebView is changing, the page will be reloaded, so any ongoing
         // Geolocation permission requests are void.
-        mGeolocationPermissionsPrompt.hide();
+        if (mGeolocationPermissionsPrompt != null) {
+            mGeolocationPermissionsPrompt.hide();
+        }
 
         // Just remove the old one.
         FrameLayout wrapper =
@@ -1661,6 +1660,13 @@
      * @return The geolocation permissions prompt for this tab.
      */
     GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
+        if (mGeolocationPermissionsPrompt == null) {
+            ViewStub stub = (ViewStub) mContainer
+                    .findViewById(R.id.geolocation_permissions_prompt);
+            mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
+                    .inflate();
+            mGeolocationPermissionsPrompt.init();
+        }
         return mGeolocationPermissionsPrompt;
     }