]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Allow uploading of files on API >= 21. Fixes https://redmine.stoutner.com/issues/1
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 020df487fa5ee0627e350ff4d53251960bae73e5..0bf8d0d6e0929f9cc7efbe4e1fb468b54dba7f96 100644 (file)
@@ -88,6 +88,7 @@ import android.view.inputmethod.InputMethodManager;
 import android.webkit.CookieManager;
 import android.webkit.HttpAuthHandler;
 import android.webkit.SslErrorHandler;
+import android.webkit.ValueCallback;
 import android.webkit.WebBackForwardList;
 import android.webkit.WebChromeClient;
 import android.webkit.WebResourceResponse;
@@ -398,6 +399,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
     // `oldFolderNameString` is used in `onCreate()` and `onSaveEditBookmarkFolder()`.
     private String oldFolderNameString;
 
+    // `fileChooserCallback` is used in `onCreate()` and `onActivityResult()`.
+    private ValueCallback<Uri[]> fileChooserCallback;
+
     // The download strings are used in `onCreate()` and `onRequestPermissionResult()`.
     private String downloadUrl;
     private String downloadContentDisposition;
@@ -906,7 +910,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 webViewTitle = title;
             }
 
-            // Enter full screen video
+            // Enter full screen video.
             @Override
             public void onShowCustomView(View view, CustomViewCallback callback) {
                 // Pause the ad if this is the free flavor.
@@ -934,7 +938,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 fullScreenVideoFrameLayout.setVisibility(View.VISIBLE);
             }
 
-            // Exit full screen video
+            // Exit full screen video.
+            @Override
             public void onHideCustomView() {
                 // Hide `fullScreenVideoFrameLayout`.
                 fullScreenVideoFrameLayout.removeAllViews();
@@ -955,6 +960,23 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                     adView = findViewById(R.id.adview);
                 }
             }
+
+            // Upload files.
+            @Override
+            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
+                // Show the file chooser if the device is running API >= 21.
+                if (Build.VERSION.SDK_INT >= 21) {
+                    // Store the file path callback.
+                    fileChooserCallback = filePathCallback;
+
+                    // Create an intent to open a chooser based ont the file chooser parameters.
+                    Intent fileChooserIntent = fileChooserParams.createIntent();
+
+                    // Open the file chooser.  Currently only one `startActivityForResult` exists in this activity, so the request code, used to differentiate them, is simply `0`.
+                    startActivityForResult(fileChooserIntent, 0);
+                }
+                return true;
+            }
         });
 
         // Register `mainWebView` for a context menu.  This is used to see link targets and download images.
@@ -2960,6 +2982,16 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         }
     }
 
+    // Process the results of an upload file chooser.  Currently there is only one `startActivityForResult` in this activity, so the request code, used to differentiate them, is ignored.
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        // File uploads only work on API >= 21.
+        if (Build.VERSION.SDK_INT >= 21) {
+            // Pass the file to the WebView.
+            fileChooserCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
+        }
+    }
+
     private void loadUrlFromTextBox() throws UnsupportedEncodingException {
         // Get the text from urlTextBox and convert it to a string.  trim() removes white spaces from the beginning and end of the string.
         String unformattedUrlString = urlTextBox.getText().toString().trim();