diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-02-28 22:26:39 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2017-02-28 22:26:39 +1300 |
commit | 1637915c21ce99fc1ea21d4549d28dbd86adfca8 (patch) | |
tree | 0b9cd72c64e13042a318202098b1ac30e5fc581c | |
parent | 48240b261a170a958758320c52c6f8ce79e0e3a8 (diff) | |
download | etbsa-traccar-manager-android-1637915c21ce99fc1ea21d4549d28dbd86adfca8.tar.gz etbsa-traccar-manager-android-1637915c21ce99fc1ea21d4549d28dbd86adfca8.tar.bz2 etbsa-traccar-manager-android-1637915c21ce99fc1ea21d4549d28dbd86adfca8.zip |
Implement file upload (fix #42)
-rw-r--r-- | app/src/main/java/org/traccar/manager/MainFragment.java | 69 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
2 files changed, 69 insertions, 1 deletions
diff --git a/app/src/main/java/org/traccar/manager/MainFragment.java b/app/src/main/java/org/traccar/manager/MainFragment.java index 7816ce8..4d71e25 100644 --- a/app/src/main/java/org/traccar/manager/MainFragment.java +++ b/app/src/main/java/org/traccar/manager/MainFragment.java @@ -15,15 +15,19 @@ */ package org.traccar.manager; +import android.app.Activity; +import android.content.ActivityNotFoundException; +import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.res.AssetManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; -import android.view.MotionEvent; import android.view.View; import android.webkit.MimeTypeMap; +import android.webkit.ValueCallback; +import android.webkit.WebChromeClient; import android.webkit.WebResourceResponse; import android.webkit.WebSettings; import android.webkit.WebView; @@ -38,6 +42,8 @@ import java.util.Map; public class MainFragment extends WebViewFragment { + private final static int REQUEST_FILE_CHOOSER = 1; + private AssetManager assetManager; @Override @@ -53,6 +59,7 @@ public class MainFragment extends WebViewFragment { } getWebView().setWebViewClient(webViewClient); + getWebView().setWebChromeClient(webChromeClient); WebSettings webSettings = getWebView().getSettings(); webSettings.setJavaScriptEnabled(true); @@ -124,4 +131,64 @@ public class MainFragment extends WebViewFragment { }; + private ValueCallback<Uri> openFileCallback; + private ValueCallback<Uri[]> openFileCallback2; + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_FILE_CHOOSER) { + Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); + if (openFileCallback != null) { + openFileCallback.onReceiveValue(result); + openFileCallback = null; + } + if (openFileCallback2 != null) { + openFileCallback2.onReceiveValue(new Uri[] { result }); + openFileCallback2 = null; + } + } + } + + private WebChromeClient webChromeClient = new WebChromeClient() { + + // Android 3.0+ + public void openFileChooser(ValueCallback uploadMessage, String acceptType) { + openFileChooser(uploadMessage); + } + + // Android 4.1+ + protected void openFileChooser(ValueCallback<Uri> uploadMessage, String acceptType, String capture) { + openFileChooser(uploadMessage); + } + + protected void openFileChooser(ValueCallback<Uri> uploadMessage) { + MainFragment.this.openFileCallback = uploadMessage; + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType("*/*"); + startActivityForResult(Intent.createChooser(intent, getString(R.string.file_browser)), REQUEST_FILE_CHOOSER); + } + + // Android 5.0+ + public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { + if (openFileCallback2 != null) { + openFileCallback2.onReceiveValue(null); + openFileCallback2 = null; + } + + openFileCallback2 = filePathCallback; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Intent intent = fileChooserParams.createIntent(); + try { + startActivityForResult(intent, REQUEST_FILE_CHOOSER); + } catch (ActivityNotFoundException e) { + openFileCallback2 = null; + return false; + } + } + return true; + } + + }; + } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e421fd4..0f91e69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,4 +4,5 @@ <string name="label_info">This screen will be shown only once. After you click the button, server address will be saved in preferences. To change it, clear app data from application manager.</string> <string name="button_start">Start</string> <string name="error_connection">Server connection failed</string> + <string name="file_browser">File Browser</string> </resources> |