Fix attaching files (aka WebView file picking) on Android.

This commit is contained in:
Cory McWilliams 2024-04-26 18:10:22 -04:00
parent 6cd9dea186
commit b5ed7014f6
6 changed files with 21 additions and 18 deletions

View File

@ -57,11 +57,11 @@ CFLAGS += \
-fno-exceptions \
-g
ANDROID_BUILD_TOOLS := $(ANDROID_SDK)/build-tools/34.0.0
ANDROID_PLATFORM := $(ANDROID_SDK)/platforms/android-34
ANDROID_NDK ?= $(ANDROID_SDK)/ndk/26.2.11394342
ANDROID_MIN_SDK_VERSION := 24
ANDROID_TARGET_SDK_VERSION := 34
ANDROID_BUILD_TOOLS := $(ANDROID_SDK)/build-tools/34.0.0
ANDROID_PLATFORM := $(ANDROID_SDK)/platforms/android-$(ANDROID_TARGET_SDK_VERSION)
ANDROID_NDK ?= $(ANDROID_SDK)/ndk/26.2.11394342
ANDROID_ARMV7A_TARGETS := \
out/androiddebug-armv7a/tildefriends \
@ -693,7 +693,7 @@ CLASS_FILES := $(foreach src,$(JAVA_FILES),out/classes/com/unprompted/tildefrien
$(CLASS_FILES) &: $(JAVA_FILES)
@echo "[javac] $(CLASS_FILES)"
@javac --release 8 -Xlint:deprecation -classpath $(ANDROID_PLATFORM)/android.jar -d out/classes $(JAVA_FILES)
@javac --release 8 -encoding UTF-8 -Xlint:deprecation -XDuseUnsharedTable=true -classpath $(ANDROID_PLATFORM)/android.jar:$(ANDROID_BUILD_TOOLS)/core-lambda-stubs.jar -d out/classes $(JAVA_FILES)
out/apk/classes.dex: $(CLASS_FILES)
@mkdir -p $(dir $@)
@ -759,7 +759,7 @@ release-apk: out/TildeFriends-arm-release.zopfli.apk out/TildeFriends-x86-releas
releaseapkgo: out/TildeFriends-arm-release.apk
@adb install -r $<
@adb shell am start com.unprompted.tildefriends/.MainActivity
@adb shell am start com.unprompted.tildefriends/.TildeFriendsActivity
.PHONY: releaseapkgo
# iOS Support

View File

@ -243,7 +243,6 @@ class TfComposeElement extends LitElement {
attach() {
let self = this;
let edit = this.renderRoot.getElementById('edit');
let input = document.createElement('input');
input.type = 'file';
input.onchange = function (event) {

View File

@ -12,7 +12,7 @@
android:extractNativeLibs="true">
<meta-data android:name="android.max_aspect" android:value="2.1"/>
<activity
android:name=".MainActivity"
android:name=".TildeFriendsActivity"
android:icon="@drawable/icon"
android:configChanges="orientation|screenSize"
android:exported="true">

View File

@ -21,12 +21,13 @@ import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
//import android.webkit.WebView;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
@ -49,8 +50,8 @@ import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity {
WebView web_view;
public class TildeFriendsActivity extends Activity {
TildeFriendsWebView web_view;
String base_url;
Process process;
Thread thread;
@ -69,7 +70,7 @@ public class MainActivity extends Activity {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
web_view = (WebView)findViewById(R.id.web);
web_view = (TildeFriendsWebView)findViewById(R.id.web);
set_status("Extracting executable...");
Log.w("tildefriends", String.format("getFilesDir() is %s", getFilesDir().toString()));
Log.w("tildefriends", String.format("getPackageResourcePath() is %s", getPackageResourcePath().toString()));
@ -79,7 +80,7 @@ public class MainActivity extends Activity {
new File(port_file_path).delete();
base_url = "http://127.0.0.1:12345/";
MainActivity activity = this;
TildeFriendsActivity activity = this;
thread = new Thread(new Runnable() {
@Override
@ -182,7 +183,8 @@ public class MainActivity extends Activity {
});
web_view.setWebChromeClient(new WebChromeClient() {
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
new AlertDialog.Builder(view.getContext())
.setTitle("Tilde Friends")
.setMessage(message)
@ -210,15 +212,17 @@ public class MainActivity extends Activity {
** https://stackoverflow.com/questions/8586691/how-to-open-file-save-dialog-in-android
*/
@Override
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> message, WebChromeClient.FileChooserParams params) {
upload_message = message;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
MainActivity.this.startActivityForResult(Intent.createChooser(intent, "File Chooser"), MainActivity.FILECHOOSER_RESULT);
TildeFriendsActivity.this.startActivityForResult(Intent.createChooser(intent, "File Chooser"), TildeFriendsActivity.FILECHOOSER_RESULT);
return true;
}
@Override
public boolean onConsoleMessage(android.webkit.ConsoleMessage consoleMessage) {
Log.d("tildefriends", consoleMessage.message() + " -- From line " + consoleMessage.lineNumber() + " of " + consoleMessage.sourceId());
return true;

View File

@ -4,14 +4,14 @@ import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
public class WebView extends android.webkit.WebView {
public class TildeFriendsWebView extends android.webkit.WebView {
boolean overscrolledY = false;
public WebView(final Context context) {
public TildeFriendsWebView(final Context context) {
super(context);
}
public WebView(final Context context, final AttributeSet attrs) {
public TildeFriendsWebView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}

View File

@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.unprompted.tildefriends.WebView
<com.unprompted.tildefriends.TildeFriendsWebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>