Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to modern sites #22

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
habilitar download de conteúdo
  • Loading branch information
Alzemand committed Sep 21, 2024
commit abf4b236d92dacfb3c5c906833dda0ad804e1946
46 changes: 41 additions & 5 deletions app/src/main/java/com/example/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.DownloadListener;
import android.widget.Toast;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

private WebView mWebView;
private ValueCallback<Uri[]> mFilePathCallback;
private final static int FILECHOOSER_RESULTCODE = 1;

@Override
@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -36,6 +40,26 @@ protected void onCreate(Bundle savedInstanceState) {
// Definindo o WebViewClient para garantir que os links sejam abertos na WebView
mWebView.setWebViewClient(new MyWebViewClient());

// Definindo o WebChromeClient para lidar com uploads de arquivos
mWebView.setWebChromeClient(new WebChromeClient() {
// For Android 5.0+
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, FILECHOOSER_RESULTCODE);
} catch (Exception e) {
mFilePathCallback = null;
return false;
}
return true;
}
});

// Habilitando o download de arquivos
mWebView.setDownloadListener(new DownloadListener() {
@Override
Expand All @@ -57,11 +81,23 @@ public void onDownloadStart(String url, String userAgent, String contentDisposit
}

@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (mFilePathCallback != null) {
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
}
} else {
super.onBackPressed();
super.onActivityResult(requestCode, resultCode, data);
}
}

Expand Down