Fix file uploading
This commit is contained in:
parent
16fd228b3b
commit
924ba2b74b
2 changed files with 69 additions and 13 deletions
|
@ -1,18 +1,23 @@
|
|||
package dev.vendicated.vencord;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.StrictMode;
|
||||
import android.view.KeyEvent;
|
||||
import android.webkit.ValueCallback;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
public static final int FILECHOOSER_RESULTCODE = 8485;
|
||||
private WebView wv;
|
||||
|
||||
public ValueCallback<Uri[]> filePathCallback;
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled") // mad? watch this swag
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -28,7 +33,7 @@ public class MainActivity extends Activity {
|
|||
explodeAndroid();
|
||||
|
||||
wv.setWebViewClient(new VWebviewClient());
|
||||
wv.setWebChromeClient(new VChromeClient());
|
||||
wv.setWebChromeClient(new VChromeClient(this));
|
||||
|
||||
var s = wv.getSettings();
|
||||
s.setJavaScriptEnabled(true);
|
||||
|
@ -42,20 +47,45 @@ public class MainActivity extends Activity {
|
|||
return;
|
||||
}
|
||||
|
||||
if (savedInstanceState == null)
|
||||
wv.loadUrl("https://discord.com/app");
|
||||
wv.loadUrl("https://discord.com/app");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(@NonNull Bundle state) {
|
||||
super.onSaveInstanceState(state);
|
||||
wv.saveState(state);
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK && wv != null && wv.canGoBack()) {
|
||||
wv.goBack();
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Bundle state) {
|
||||
super.onRestoreInstanceState(state);
|
||||
wv.restoreState(state);
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||
if (requestCode != FILECHOOSER_RESULTCODE || filePathCallback == null)
|
||||
return;
|
||||
|
||||
if (resultCode != RESULT_OK || intent == null) {
|
||||
filePathCallback.onReceiveValue(null);
|
||||
return;
|
||||
} else {
|
||||
Uri[] uris;
|
||||
try {
|
||||
var clipData = intent.getClipData();
|
||||
if (clipData != null) { // multiple items
|
||||
uris = new Uri[clipData.getItemCount()];
|
||||
for (int i = 0; i < clipData.getItemCount(); i++) {
|
||||
uris[i] = clipData.getItemAt(i).getUri();
|
||||
}
|
||||
} else { // single item
|
||||
uris = new Uri[] { intent.getData() };
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.e("Error during file upload", ex);
|
||||
uris = null;
|
||||
}
|
||||
filePathCallback.onReceiveValue(uris);
|
||||
}
|
||||
filePathCallback = null;
|
||||
}
|
||||
|
||||
private void explodeAndroid() {
|
||||
|
|
|
@ -1,11 +1,19 @@
|
|||
package dev.vendicated.vencord;
|
||||
|
||||
import android.webkit.ConsoleMessage;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.net.Uri;
|
||||
import android.webkit.*;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class VChromeClient extends WebChromeClient {
|
||||
private final MainActivity activity;
|
||||
|
||||
public VChromeClient(MainActivity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConsoleMessage(ConsoleMessage msg) {
|
||||
var m = String.format(Locale.ENGLISH, "[Javascript] %s @ %d: %s", msg.message(), msg.lineNumber(), msg.sourceId());
|
||||
|
@ -25,4 +33,22 @@ public class VChromeClient extends WebChromeClient {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
|
||||
if (activity.filePathCallback != null)
|
||||
activity.filePathCallback.onReceiveValue(null);
|
||||
|
||||
activity.filePathCallback = filePathCallback;
|
||||
|
||||
var i = fileChooserParams.createIntent();
|
||||
try {
|
||||
activity.startActivityForResult(i, MainActivity.FILECHOOSER_RESULTCODE);
|
||||
} catch (ActivityNotFoundException ex) {
|
||||
activity.filePathCallback = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue