cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [2/2] android commit: CB-8017 Add support for `<input type=file>` for Lollipop
Date Mon, 19 Jan 2015 21:23:29 GMT
CB-8017 Add support for `<input type=file>` for Lollipop

Also refactors a bit to remove related special-case code from CordovaActivity


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/5538a231
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/5538a231
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/5538a231

Branch: refs/heads/3.7.x
Commit: 5538a231a8395a2512743d62785f022fb2711e42
Parents: 9be1106
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Mon Jan 19 16:15:25 2015 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Mon Jan 19 16:23:20 2015 -0500

----------------------------------------------------------------------
 .../src/org/apache/cordova/CordovaActivity.java | 17 +-----
 .../org/apache/cordova/CordovaChromeClient.java | 57 ++++++++++++++------
 2 files changed, 43 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/5538a231/framework/src/org/apache/cordova/CordovaActivity.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java
index 6a3c70b..69a96f0 100755
--- a/framework/src/org/apache/cordova/CordovaActivity.java
+++ b/framework/src/org/apache/cordova/CordovaActivity.java
@@ -36,7 +36,6 @@ import android.content.DialogInterface;
 import android.content.Intent;
 import android.graphics.Color;
 import android.media.AudioManager;
-import android.net.Uri;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Display;
@@ -48,7 +47,6 @@ import android.view.ViewGroup;
 import android.view.ViewParent;
 import android.view.Window;
 import android.view.WindowManager;
-import android.webkit.ValueCallback;
 import android.webkit.WebViewClient;
 import android.widget.LinearLayout;
 
@@ -697,23 +695,12 @@ public class CordovaActivity extends Activity implements CordovaInterface
{
      * @param requestCode       The request code originally supplied to startActivityForResult(),
      *                          allowing you to identify who this result came from.
      * @param resultCode        The integer result code returned by the child activity through
its setResult().
-     * @param data              An Intent, which can return result data to the caller (various
data can be attached to Intent "extras").
+     * @param intent            An Intent, which can return result data to the caller (various
data can be attached to Intent "extras").
      */
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
-        LOG.d(TAG, "Incoming Result");
+        LOG.d(TAG, "Incoming Result. Request code = " + requestCode);
         super.onActivityResult(requestCode, resultCode, intent);
-        Log.d(TAG, "Request code = " + requestCode);
-        if (appView != null && requestCode == CordovaChromeClient.FILECHOOSER_RESULTCODE)
{
-        	ValueCallback<Uri> mUploadMessage = this.appView.getWebChromeClient().getValueCallback();
-            Log.d(TAG, "did we get here?");
-            if (null == mUploadMessage)
-                return;
-            Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
-            Log.d(TAG, "result = " + result);
-            mUploadMessage.onReceiveValue(result);
-            mUploadMessage = null;
-        }
         CordovaPlugin callback = this.activityResultCallback;
         if(callback == null && initCallbackClass != null) {
             // The application was restarted, but had defined an initial callback

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/5538a231/framework/src/org/apache/cordova/CordovaChromeClient.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaChromeClient.java b/framework/src/org/apache/cordova/CordovaChromeClient.java
index ff0c0f2..31759d0 100755
--- a/framework/src/org/apache/cordova/CordovaChromeClient.java
+++ b/framework/src/org/apache/cordova/CordovaChromeClient.java
@@ -22,10 +22,14 @@ import org.apache.cordova.CordovaInterface;
 import org.apache.cordova.LOG;
 
 import android.annotation.TargetApi;
+import android.app.Activity;
 import android.app.AlertDialog;
+import android.content.ActivityNotFoundException;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.net.Uri;
+import android.os.Build;
+import android.util.Log;
 import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.View;
@@ -67,10 +71,7 @@ public class CordovaChromeClient extends WebChromeClient {
     
     //Keep track of last AlertDialog showed
     private AlertDialog lastHandledDialog;
-    
-    // File Chooser
-    public ValueCallback<Uri> mUploadMessage;
-    
+
     @Deprecated
     public CordovaChromeClient(CordovaInterface cordova) {
         this.cordova = cordova;
@@ -309,7 +310,10 @@ public class CordovaChromeClient extends WebChromeClient {
         }
     return mVideoProgressView; 
     }
-    
+
+    // <input type=file> support:
+    // openFileChooser() is for pre KitKat and in KitKat mr1 (it's known broken in KitKat).
+    // For Lollipop, we use onShowFileChooser().
     public void openFileChooser(ValueCallback<Uri> uploadMsg) {
         this.openFileChooser(uploadMsg, "*/*");
     }
@@ -318,20 +322,41 @@ public class CordovaChromeClient extends WebChromeClient {
         this.openFileChooser(uploadMsg, acceptType, null);
     }
     
-    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String
capture)
+    public void openFileChooser(final ValueCallback<Uri> uploadMsg, String acceptType,
String capture)
     {
-        mUploadMessage = uploadMsg;
-        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
-        i.addCategory(Intent.CATEGORY_OPENABLE);
-        i.setType("*/*");
-        this.cordova.getActivity().startActivityForResult(Intent.createChooser(i, "File Browser"),
-                FILECHOOSER_RESULTCODE);
+        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
+        intent.addCategory(Intent.CATEGORY_OPENABLE);
+        intent.setType("*/*");
+        cordova.startActivityForResult(new CordovaPlugin() {
+            @Override
+            public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
+                Uri result = intent == null || resultCode != Activity.RESULT_OK ? null :
intent.getData();
+                Log.d(TAG, "Receive file chooser URL: " + result);
+                uploadMsg.onReceiveValue(result);
+            }
+        }, intent, FILECHOOSER_RESULTCODE);
     }
-    
-    public ValueCallback<Uri> getValueCallback() {
-        return this.mUploadMessage;
+
+    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+    @Override
+    public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback,
final WebChromeClient.FileChooserParams fileChooserParams) {
+        Intent intent = fileChooserParams.createIntent();
+        try {
+            cordova.startActivityForResult(new CordovaPlugin() {
+                @Override
+                public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
+                    Uri[] result = WebChromeClient.FileChooserParams.parseResult(resultCode,
intent);
+                    Log.d(TAG, "Receive file chooser URL: " + result);
+                    filePathsCallback.onReceiveValue(result);
+                }
+            }, intent, FILECHOOSER_RESULTCODE);
+        } catch (ActivityNotFoundException e) {
+            Log.w("No activity found to handle file chooser intent.", e);
+            filePathsCallback.onReceiveValue(null);
+        }
+        return true;
     }
-    
+
     public void destroyLastDialog(){
         if(lastHandledDialog != null){
                 lastHandledDialog.cancel();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message