Skip to content

Commit

Permalink
more advanced options
Browse files Browse the repository at this point in the history
  • Loading branch information
mikespub committed Feb 6, 2020
1 parent ebc789b commit a467ebe
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
/captures
.externalNativeBuild
.cxx
/app/google-services.json

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "net.mikespub.mywebview"
minSdkVersion 19
targetSdkVersion 29
versionCode 13
versionName '1.12'
versionCode 14
versionName '1.13'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":13,"versionName":"1.12","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":14,"versionName":"1.13","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
2 changes: 1 addition & 1 deletion app/src/main/assets/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ <h2><a href="update.html">Update Settings</a></h2>
if(typeof androidAppProxy !== "undefined"){
androidAppProxy.showMessage("Android JavaScript Interface is available");
} else {
console.log("Android Javascript Interface is not enabled");
console.log("Console log messages are enabled. Javascript Interface is NOT enabled.");
}
</script>
</body>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/assets/web/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
["path", "endsWith", ".epub"],
["query", "contains", "type=epub"]
],
"remote_debug": true,
"console_log": false,
"js_interface": false,
"context_menu": false,
"not_matching": false,
"source": "assets",
"timestamp": "2020-02-02T14:01:00Z"
}
14 changes: 12 additions & 2 deletions app/src/main/assets/web/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,12 @@ <h2>Update Settings</h2>
</fieldset>

<fieldset id="options">
<legend>Advanced Options (requires restart)</legend>
<legend>Advanced Options (requires app restart)</legend>
<input id="remote_debug" type="checkbox" name="remote_debug" value="true"> <label for="remote_debug">Enable debugging of web contents</label><br>
<input id="console_log" type="checkbox" name="console_log" value="true"> <label for="console_log">Show console log messages</label><br>
<input id="js_interface" type="checkbox" name="js_interface" value="true"> <label for="js_interface">Javascript Interface for local assets</label><br>
<input id="context_menu" type="checkbox" name="context_menu" value="true"> <label for="context_menu">Context Menu for links &amp; images</label>
<input id="context_menu" type="checkbox" name="context_menu" value="true"> <label for="context_menu">Context Menu for links and images</label><br>
<input id="not_matching" type="checkbox" name="not_matching" value="true"> <label for="not_matching">Open non-matching links via browser</label>
</fieldset>

<!--
Expand Down Expand Up @@ -260,6 +262,10 @@ <h2>Update Settings</h2>
if (source) {
source.value = settings["source"];
}
var remote_debug = document.getElementById("remote_debug");
if (remote_debug && settings["remote_debug"]) {
remote_debug.checked = true;
}
var console_log = document.getElementById("console_log");
if (console_log && settings["console_log"]) {
console_log.checked = true;
Expand All @@ -272,6 +278,10 @@ <h2>Update Settings</h2>
if (context_menu && settings["context_menu"]) {
context_menu.checked = true;
}
var not_matching = document.getElementById("not_matching");
if (not_matching && settings["not_matching"]) {
not_matching.checked = true;
}
var timestamp = document.getElementById("timestamp");
if (timestamp) {
timestamp.value = settings["timestamp"];
Expand Down
60 changes: 48 additions & 12 deletions app/src/main/java/net/mikespub/mywebview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.mikespub.mywebview;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -41,9 +43,11 @@ protected void onCreate(Bundle savedInstanceState) {
webSettings.setJavaScriptEnabled(true);
// See https://ukacademe.com/MobileApplication/AndroidGUI/Android_WebView
// webSettings.setBuiltInZoomControls(true);
WebView.setWebContentsDebuggingEnabled(true);
// Stop local links and redirects from opening in browser instead of WebView
MyAppWebViewClient myWebViewClient = new MyAppWebViewClient(this);
if (myWebViewClient.hasDebuggingEnabled()) {
WebView.setWebContentsDebuggingEnabled(true);
}
myWebView.setWebViewClient(myWebViewClient);
// Add Javascript interface
if (myWebViewClient.hasJavascriptInterface()) {
Expand All @@ -70,6 +74,7 @@ public boolean onConsoleMessage(ConsoleMessage cm) {
}
// Support context menu for links and images in WebView
// WebViewClient myWebViewClient = myWebView.getWebViewClient();
//registerForContextMenu(myWebView);
if (myWebViewClient.hasContextMenu()) {
//Log.d("WebView", Boolean.toString(myWebView.isLongClickable()));
myWebView.setLongClickable(true);
Expand All @@ -78,23 +83,54 @@ public boolean onConsoleMessage(ConsoleMessage cm) {

@Override
public boolean onLongClick(View v) {
Log.d("WebView", v.toString());
WebView.HitTestResult hitTestResult = myWebView.getHitTestResult();
if (hitTestResult == null) {
return false;
}
if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE
|| hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
Log.d("WebView", "image");
//showDownDialog(hitTestResult.getExtra());
//} else if (!mIsShowTitle) {
// //showActionSheet();
} else if (hitTestResult.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
Log.d("WebView", "link");
} else {
Log.d("WebView", "not interesting");
int getType = hitTestResult.getType();
String extra = hitTestResult.getExtra();
if (extra == null || extra.equals("")) {
return false;
}
Uri uri = Uri.parse(extra);
Intent intent;
Intent chooser;
switch (getType) {
case WebView.HitTestResult.IMAGE_TYPE:
Log.d("WebView", "image");
//showDownDialog(hitTestResult.getExtra());
intent = new Intent(Intent.ACTION_VIEW, uri);
//myWebView.getContext().startActivity(intent);
chooser = Intent.createChooser(intent, null);
myWebView.getContext().startActivity(chooser);
return true;
case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Log.d("WebView", "image anchor");
intent = new Intent(Intent.ACTION_VIEW, uri);
//myWebView.getContext().startActivity(intent);
chooser = Intent.createChooser(intent, null);
myWebView.getContext().startActivity(chooser);
return true;
case WebView.HitTestResult.SRC_ANCHOR_TYPE:
Log.d("WebView", "anchor");
//intent = new Intent(Intent.ACTION_SEND, uri);
// See also https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents
//intent = new Intent(Intent.ACTION_SEND);
//intent.putExtra(Intent.EXTRA_TEXT, uri.toString());
//intent.putExtra(Intent.EXTRA_TITLE, "Send Me");
//intent.setType("text/plain");
intent = new Intent(Intent.ACTION_VIEW, uri);
// Create intent to show chooser
String title = uri.toString() + "\n\nOpen with";
chooser = Intent.createChooser(intent, title);
//chooser = Intent.createChooser(intent, null);
//chooser.putExtra(Intent.EXTRA_TITLE, uri.toString() + "\n\nOpen with");
myWebView.getContext().startActivity(chooser);
return true;
default:
Log.d("WebView", "other " + getType);
break;
}

Log.d("WebView", "Type:" + hitTestResult.getType() + ";Extra:" + hitTestResult.getExtra());
//return true;
Expand Down
84 changes: 39 additions & 45 deletions app/src/main/java/net/mikespub/mywebview/MyAppWebViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.webkit.WebViewAssetLoader;

Expand Down Expand Up @@ -36,6 +37,10 @@ class MyAppWebViewClient extends WebViewClient {
loadSettings();
}

Boolean hasDebuggingEnabled() {
return (Boolean) mySavedStateModel.getValue("remote_debug");
}

Boolean hasConsoleLog() {
return (Boolean) mySavedStateModel.getValue("console_log");
}
Expand All @@ -48,6 +53,10 @@ Boolean hasContextMenu() {
return (Boolean) mySavedStateModel.getValue("context_menu");
}

Boolean hasNotMatching() {
return (Boolean) mySavedStateModel.getValue("not_matching");
}

private void loadSettings() {
HashMap<String, Object> hashMap = mySavedStateModel.getSettings(this.activity);
Log.d("State Get", hashMap.toString());
Expand Down Expand Up @@ -142,6 +151,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
pieces.put("path", uri.getPath());
pieces.put("query", uri.getQuery());
pieces.put("url", uri.toString());
boolean isMatch = false;
boolean isSkip = false;
// https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string
//for (String m: this.myMatchArr) {
// String[] compare = m.split("\\|");
Expand All @@ -152,7 +163,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!result) {
continue;
}
boolean isSkip = false;
isMatch = true;
isSkip = false;
//for (String s: this.mySkipArr) {
// String[] check = s.split("\\|");
for (String[] check: this.mySkipCompare) {
Expand All @@ -164,54 +176,36 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
break;
}
}
if (isSkip) {
continue;
}
return false;
}
/*
if (host != null) {
String[] myHostEndsArr = this.activity.getResources().getStringArray(R.array.urlhost_endswith);
for (String s : myHostEndsArr) {
if (!host.endsWith(s)) {
continue;
}
// skip epub files - TODO: save in media directory https://developer.android.com/training/data-storage/app-specific#media ?
// if (!path.endsWith(".epub") && (query == null || !query.contains("type=epub"))) {
// return false;
// }
boolean isMatch = false;
if (path != null) {
String[] myPathNotEndsArr = this.activity.getResources().getStringArray(R.array.urlpath_not_endswith);
for (String t : myPathNotEndsArr) {
if (path.endsWith(t)) {
isMatch = true;
break;
}
}
if (isMatch) {
continue;
}
}
if (query != null) {
String[] myQueryNotContainsArr = this.activity.getResources().getStringArray(R.array.urlquery_not_contains);
for (String q : myQueryNotContainsArr) {
if (query.contains(q)) {
isMatch = true;
break;
}
}
if (isMatch) {
continue;
}
}
if (!isSkip) {
return false;
}
}
*/
// skip epub files - TODO: save in media directory https://developer.android.com/training/data-storage/app-specific#media ?
if (isMatch && isSkip) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// Create intent to show chooser
String title = uri.toString() + "\n\nOpen with";
Intent chooser = Intent.createChooser(intent, title);
view.getContext().startActivity(chooser);
return true;
}

// TODO: make this configurable for non-matching urls
if (hasNotMatching()) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// Create intent to show chooser
String title = uri.toString() + "\n\nOpen with";
Intent chooser = Intent.createChooser(intent, title);
view.getContext().startActivity(chooser);
} else {
String message = uri.toString() + "\n\nLink not matching. You can allow opening via regular browser in Advanced Options.";
Toast toast = Toast.makeText(
view.getContext().getApplicationContext(),
message,
Toast.LENGTH_LONG);
toast.show();

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
view.getContext().startActivity(intent);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ void setValue(String key, Object value) {
hashMap.put("match", match);
List<List<String>> skip = (ArrayList) getValue("skip");
hashMap.put("skip", skip);
Boolean remote_debug = (Boolean) getValue("remote_debug");
hashMap.put("remote_debug", remote_debug);
Boolean console_log = (Boolean) getValue("console_log");
hashMap.put("console_log", console_log);
Boolean js_interface = (Boolean) getValue("js_interface");
hashMap.put("js_interface", js_interface);
Boolean context_menu = (Boolean) getValue("context_menu");
hashMap.put("context_menu", context_menu);
Boolean not_matching = (Boolean) getValue("not_matching");
hashMap.put("not_matching", not_matching);
return hashMap;
}

Expand All @@ -66,9 +70,11 @@ private void setValuesFromMap(HashMap<String, Object> hashMap) {
setValue("match", hashMap.get("match"));
setValue("skip", hashMap.get("skip"));
setValue("source", hashMap.get("source"));
setValue("remote_debug", hashMap.get("remote_debug"));
setValue("console_log", hashMap.get("console_log"));
setValue("js_interface", hashMap.get("js_interface"));
setValue("context_menu", hashMap.get("context_menu"));
setValue("not_matching", hashMap.get("not_matching"));
setValue("timestamp", hashMap.get("timestamp"));
}

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/net/mikespub/mywebview/MySettingsRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ HashMap<String, Object> parseQueryParameters(Uri uri) {
List<String> skip1 = uri.getQueryParameters("skip1[]");
List<String> skip2 = uri.getQueryParameters("skip2[]");
String other = uri.getQueryParameter("other");
String remote_debug = uri.getQueryParameter("remote_debug");
String console_log = uri.getQueryParameter("console_log");
String js_interface = uri.getQueryParameter("js_interface");
String context_menu = uri.getQueryParameter("context_menu");
String not_matching = uri.getQueryParameter("not_matching");
HashMap<String, Object> hashMap = new HashMap<>();
List<HashMap<String, String>> sites = new ArrayList<>();
for (int i = 0; i < titles.size(); i++) {
Expand Down Expand Up @@ -252,6 +254,11 @@ HashMap<String, Object> parseQueryParameters(Uri uri) {
skips.add(skip);
}
hashMap.put("skip", skips);
if (remote_debug != null && remote_debug.equals("true")) {
hashMap.put("remote_debug", true);
} else {
hashMap.put("remote_debug", false);
}
if (console_log != null && console_log.equals("true")) {
hashMap.put("console_log", true);
} else {
Expand All @@ -267,6 +274,11 @@ HashMap<String, Object> parseQueryParameters(Uri uri) {
} else {
hashMap.put("context_menu", false);
}
if (not_matching != null && not_matching.equals("true")) {
hashMap.put("not_matching", true);
} else {
hashMap.put("not_matching", false);
}
hashMap.put("source", "updated via webview");
hashMap.put("timestamp", getTimestamp(0));
return hashMap;
Expand Down

0 comments on commit a467ebe

Please sign in to comment.