Fix file upload handling after ActivityNotFoundException.

When we launch the file picker for uploads, if nothing can handle
the type of file then we catch the ActivityNotFoundException so
that we do not crash and launch a default picker instead. However
we still will receive a callback for the intent that failed so in
this case we must skip processing it.

Bug:3447924
Change-Id: Ic309f4ba1a6feda981fcce5231b9aed9be70eef2
diff --git a/src/com/android/browser/UploadHandler.java b/src/com/android/browser/UploadHandler.java
index ee578fa..5947e4a 100644
--- a/src/com/android/browser/UploadHandler.java
+++ b/src/com/android/browser/UploadHandler.java
@@ -39,6 +39,9 @@
     private ValueCallback<Uri> mUploadMessage;
     private String mCameraFilePath;
 
+    private boolean mHandled;
+    private boolean mCaughtActivityNotFoundException;
+
     private Controller mController;
 
     public UploadHandler(Controller controller) {
@@ -49,7 +52,19 @@
         return mCameraFilePath;
     }
 
+    boolean handled() {
+        return mHandled;
+    }
+
     void onResult(int resultCode, Intent intent) {
+
+        if (resultCode == Activity.RESULT_CANCELED && mCaughtActivityNotFoundException) {
+            // Couldn't resolve an activity, we are going to try again so skip
+            // this result.
+            mCaughtActivityNotFoundException = false;
+            return;
+        }
+
         Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
                 : intent.getData();
 
@@ -71,6 +86,8 @@
         }
 
         mUploadMessage.onReceiveValue(result);
+        mHandled = true;
+        mCaughtActivityNotFoundException = false;
     }
 
     void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
@@ -183,6 +200,7 @@
             // No installed app was able to handle the intent that
             // we sent, so fallback to the default file upload control.
             try {
+                mCaughtActivityNotFoundException = true;
                 mController.getActivity().startActivityForResult(createDefaultOpenableIntent(),
                         Controller.FILE_SELECTED);
             } catch (ActivityNotFoundException e2) {