android: Guard aganst ANRs harder?
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 31m26s
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 31m26s
This commit is contained in:
@@ -50,6 +50,7 @@ public class TildeFriendsActivity extends Activity {
|
||||
TildeFriendsWebView web_view;
|
||||
String base_url;
|
||||
String port_file_path;
|
||||
Thread create_thread;
|
||||
Thread server_thread;
|
||||
ServiceConnection service_connection;
|
||||
FileObserver observer;
|
||||
@@ -67,17 +68,7 @@ public class TildeFriendsActivity extends Activity {
|
||||
public static native int tf_server_main(String files_dir, String apk_path, String out_port_file_path, ConnectivityManager connectivity_manager);
|
||||
public static native int tf_sandbox_main(int pipe_fd);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
s_activity = this;
|
||||
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
|
||||
.detectLeakedClosableObjects()
|
||||
.penaltyLog()
|
||||
.build());
|
||||
super.onCreate(savedInstanceState);
|
||||
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
private void createThread() {
|
||||
web_view = (TildeFriendsWebView)findViewById(R.id.web);
|
||||
set_status("Extracting executable...");
|
||||
Log.w("tildefriends", String.format("getFilesDir() is %s", getFilesDir().toString()));
|
||||
@@ -109,152 +100,184 @@ public class TildeFriendsActivity extends Activity {
|
||||
});
|
||||
server_thread.start();
|
||||
|
||||
web_view.getSettings().setJavaScriptEnabled(true);
|
||||
web_view.getSettings().setDomStorageEnabled(true);
|
||||
runOnUiThread(() -> {
|
||||
web_view.getSettings().setJavaScriptEnabled(true);
|
||||
web_view.getSettings().setDomStorageEnabled(true);
|
||||
|
||||
set_database_enabled();
|
||||
set_database_path();
|
||||
set_database_enabled();
|
||||
set_database_path();
|
||||
|
||||
web_view.setDownloadListener(new DownloadListener() {
|
||||
public void onDownloadStart(String url, String userAgent, String content_disposition, String mime_type, long content_length) {
|
||||
Log.w("tildefriends", "Let's download: " + url + " (" + content_disposition + ")");
|
||||
String file_name = URLUtil.guessFileName(url, content_disposition, mime_type);
|
||||
if (url.startsWith("data:") && url.indexOf(',') != -1) {
|
||||
String b64 = url.substring(url.indexOf(',') + 1);
|
||||
byte[] data = Base64.decode(b64, Base64.DEFAULT);
|
||||
Log.w("tildefriends", "Downloaded " + data.length + " bytes.");
|
||||
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||
try (OutputStream stream = new FileOutputStream(new File(path, file_name))) {
|
||||
stream.write(data);
|
||||
} catch (java.io.IOException e) {
|
||||
Log.w("tildefriends", "IOException: " + e.toString());
|
||||
web_view.setDownloadListener(new DownloadListener() {
|
||||
public void onDownloadStart(String url, String userAgent, String content_disposition, String mime_type, long content_length) {
|
||||
Log.w("tildefriends", "Let's download: " + url + " (" + content_disposition + ")");
|
||||
String file_name = URLUtil.guessFileName(url, content_disposition, mime_type);
|
||||
if (url.startsWith("data:") && url.indexOf(',') != -1) {
|
||||
String b64 = url.substring(url.indexOf(',') + 1);
|
||||
byte[] data = Base64.decode(b64, Base64.DEFAULT);
|
||||
Log.w("tildefriends", "Downloaded " + data.length + " bytes.");
|
||||
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||
try (OutputStream stream = new FileOutputStream(new File(path, file_name))) {
|
||||
stream.write(data);
|
||||
} catch (java.io.IOException e) {
|
||||
Log.w("tildefriends", "IOException: " + e.toString());
|
||||
}
|
||||
Toast.makeText(getApplicationContext(), "Downloaded File", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
||||
request.setMimeType(mime_type);
|
||||
String cookies = CookieManager.getInstance().getCookie(url);
|
||||
request.addRequestHeader("cookie", cookies);
|
||||
request.addRequestHeader("User-Agent", userAgent);
|
||||
request.setDescription("Downloading file...");
|
||||
request.setTitle(file_name);
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, content_disposition, mime_type));
|
||||
DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
|
||||
dm.enqueue(request);
|
||||
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
Toast.makeText(getApplicationContext(), "Downloaded File", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
||||
request.setMimeType(mime_type);
|
||||
String cookies = CookieManager.getInstance().getCookie(url);
|
||||
request.addRequestHeader("cookie", cookies);
|
||||
request.addRequestHeader("User-Agent", userAgent);
|
||||
request.setDescription("Downloading file...");
|
||||
request.setTitle(file_name);
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, content_disposition, mime_type));
|
||||
DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
|
||||
dm.enqueue(request);
|
||||
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
web_view.setWebChromeClient(new WebChromeClient() {
|
||||
@Override
|
||||
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
|
||||
new AlertDialog.Builder(view.getContext())
|
||||
.setTitle("Tilde Friends")
|
||||
.setMessage(message)
|
||||
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
web_view.setWebChromeClient(new WebChromeClient() {
|
||||
@Override
|
||||
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
|
||||
new AlertDialog.Builder(view.getContext())
|
||||
.setTitle("Tilde Friends")
|
||||
.setMessage(message)
|
||||
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
||||
{
|
||||
result.confirm();
|
||||
}
|
||||
})
|
||||
.create()
|
||||
.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
|
||||
new AlertDialog.Builder(view.getContext())
|
||||
.setTitle("Tilde Friends")
|
||||
.setMessage(message)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.confirm();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.cancel();
|
||||
}
|
||||
})
|
||||
.create()
|
||||
.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
|
||||
EditText input = new EditText(view.getContext());
|
||||
input.setInputType(InputType.TYPE_CLASS_TEXT);
|
||||
input.setText(defaultValue);
|
||||
|
||||
new AlertDialog.Builder(view.getContext())
|
||||
.setTitle("Tilde Friends")
|
||||
.setMessage(message)
|
||||
.setView(input)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.confirm(input.getText().toString());
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.cancel();
|
||||
}
|
||||
})
|
||||
.create()
|
||||
.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
** https://stackoverflow.com/questions/5907369/file-upload-in-webview
|
||||
** 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("*/*");
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
web_view.setWebViewClient(new WebViewClient() {
|
||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
|
||||
{
|
||||
if (request.getUrl() != null && request.getUrl().toString().startsWith(base_url)) {
|
||||
return false;
|
||||
} else {
|
||||
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, request.getUrl()));
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.confirm();
|
||||
}
|
||||
})
|
||||
.create()
|
||||
.show();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
|
||||
new AlertDialog.Builder(view.getContext())
|
||||
.setTitle("Tilde Friends")
|
||||
.setMessage(message)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.confirm();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.cancel();
|
||||
}
|
||||
})
|
||||
.create()
|
||||
.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
|
||||
EditText input = new EditText(view.getContext());
|
||||
input.setInputType(InputType.TYPE_CLASS_TEXT);
|
||||
input.setText(defaultValue);
|
||||
|
||||
new AlertDialog.Builder(view.getContext())
|
||||
.setTitle("Tilde Friends")
|
||||
.setMessage(message)
|
||||
.setView(input)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.confirm(input.getText().toString());
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int which)
|
||||
{
|
||||
result.cancel();
|
||||
}
|
||||
})
|
||||
.create()
|
||||
.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
** https://stackoverflow.com/questions/5907369/file-upload-in-webview
|
||||
** 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("*/*");
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
web_view.setWebViewClient(new WebViewClient() {
|
||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
|
||||
{
|
||||
if (request.getUrl() != null && request.getUrl().toString().startsWith(base_url)) {
|
||||
return false;
|
||||
} else {
|
||||
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, request.getUrl()));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
s_activity.create_thread = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
s_activity = this;
|
||||
super.onCreate(savedInstanceState);
|
||||
StrictMode.setThreadPolicy(
|
||||
new StrictMode.ThreadPolicy.Builder()
|
||||
.detectAll()
|
||||
.penaltyDialog()
|
||||
.penaltyLog()
|
||||
.build());
|
||||
StrictMode.setVmPolicy(
|
||||
new StrictMode.VmPolicy.Builder()
|
||||
.detectLeakedClosableObjects()
|
||||
.detectAll()
|
||||
.penaltyLog()
|
||||
.build());
|
||||
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
setContentView(R.layout.activity_main);
|
||||
TextView refresh = (TextView)findViewById(R.id.refresh);
|
||||
refresh.setVisibility(View.GONE);
|
||||
refresh.setText("REFRESH");
|
||||
|
||||
create_thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
s_activity.createThread();
|
||||
}
|
||||
});
|
||||
create_thread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user