Skip to content

Commit

Permalink
add mimetypes + reflect get & stringCompare + localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
mikespub committed Feb 9, 2020
1 parent 2a6b656 commit a578001
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 77 deletions.
4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 15
versionName '1.14'
versionCode 16
versionName '1.15'
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":15,"versionName":"1.14","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":16,"versionName":"1.15","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
2 changes: 1 addition & 1 deletion app/src/main/java/net/mikespub/mywebview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public boolean onLongClick(View v) {
}

/**
* @return ViewModel with Saved State
* @return ViewModel with Saved State for Settings
*/
// https://stackoverflow.com/questions/57838759/how-android-jetpack-savedstateviewmodelfactory-works
public MySavedStateModel getSavedStateModel() {
Expand Down
85 changes: 35 additions & 50 deletions app/src/main/java/net/mikespub/mywebview/MyAppWebViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
*
* WebViewClient Methods + link to settings & downloads
*/
class MyAppWebViewClient extends WebViewClient {
static final String domainName;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void onReceive(Context context, Intent intent) {
}

/**
* Get setting
* Get setting for remote_debug
*
* @return setting
*/
Expand All @@ -120,7 +119,7 @@ Boolean hasDebuggingEnabled() {
}

/**
* Get setting
* Get setting for console_log
*
* @return setting
*/
Expand All @@ -129,7 +128,7 @@ Boolean hasConsoleLog() {
}

/**
* Get setting
* Get setting for js_interface
*
* @return setting
*/
Expand All @@ -138,7 +137,7 @@ Boolean hasJavascriptInterface() {
}

/**
* Get setting
* Get setting for context_menu
*
* @return setting
*/
Expand All @@ -147,7 +146,7 @@ Boolean hasContextMenu() {
}

/**
* Get setting
* Get setting for not_matching
*
* @return setting
*/
Expand All @@ -156,7 +155,7 @@ Boolean hasNotMatching() {
}

/**
* Get setting
* Get setting for update_zip
*
* @return setting
*/
Expand Down Expand Up @@ -262,39 +261,7 @@ private void loadStringConfig() {
}

/**
* @param var variable to compare
* @param oper operator for comparison
* @param value value to compare with
* @return comparison
*/
// https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string
// TODO: create hashmap of methods?
private boolean compare(String var, String oper, String value) {
if (var == null) {
return false;
}
// with single parameter, return boolean
try {
Method method;
if (oper.equals("equals")) {
method = var.getClass().getMethod(oper, Object.class);
} else if (oper.equals("contains")) {
method = var.getClass().getMethod(oper, CharSequence.class);
} else {
method = var.getClass().getMethod(oper, value.getClass());
}
Log.d("WebView Compare", "Method " + oper + ": " + method);
boolean result = (boolean) method.invoke(var, value); // pass arg
Log.d("WebView Compare", "Result " + value + ": " + result);
return result;
} catch (Exception e) {
Log.e("WebView Compare", e.toString());
return false;
}
}

/**
* Check if we need to override a particular url and/or create an Intent
* Check if we need to override a particular url and/or create an Intent for it
*
* @param view current WebView context
* @param url url to check for override
Expand All @@ -305,7 +272,7 @@ private boolean compare(String var, String oper, String value) {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("Web Override", url);
if(url.startsWith(domainUrl)) {
if(url.startsWith(domainUrl) || url.startsWith("http://localhost/")) {
return false;
}
final Uri uri = Uri.parse(url);
Expand All @@ -326,7 +293,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
for (String[] compare: this.myMatchCompare) {
String value = pieces.get(compare[0]);
Log.d("WebView Match", "Value " + compare[0] + ": " + value);
boolean result = this.compare(value, compare[1], compare[2]);
boolean result = MyReflectUtility.stringCompare(value, compare[1], compare[2]);
if (!result) {
continue;
}
Expand All @@ -336,8 +303,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
// String[] check = s.split("\\|");
for (String[] check: this.mySkipCompare) {
value = pieces.get(check[0]);
Log.i("WebView Skip", "Value " + check[0] + ": " + value);
result = this.compare(value, check[1], check[2]);
Log.d("WebView Skip", "Value " + check[0] + ": " + value);
result = MyReflectUtility.stringCompare(value, check[1], check[2]);
if (result) {
isSkip = true;
break;
Expand Down Expand Up @@ -399,7 +366,7 @@ public void onPageFinished(WebView view, String url) {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Log.d("Web Intercept", url);
if(!url.startsWith(domainUrl)) {
if(!url.startsWith(domainUrl) && !url.startsWith("http://localhost/")) {
return null;
}
final Uri uri = Uri.parse(url);
Expand All @@ -412,7 +379,7 @@ public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// Note: we could also have used the Javascript interface, but then this might be available for all sites
if (path.equals("/assets/web/fake_post.jsp")) {
// String query = uri.getQuery();
HashMap<String, Object> hashMap = this.mySavedStateModel.parseQuery(this.activity, uri);
HashMap<String, Object> hashMap = MySettingsRepository.parseQueryParameters(uri);
// add custom web settings here?
hashMap.put("web_settings", myCustomWebSettings);
String jsonString = this.mySavedStateModel.setSettings(this.activity, hashMap);
Expand Down Expand Up @@ -454,25 +421,43 @@ public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
}
if (path.startsWith("/assets/")) {
File extWebFile = new File(this.activity.getExternalFilesDir(null), path.substring("/assets/".length()));
if (extWebFile.exists()) {
if (path.endsWith("/") && extWebFile.exists() && extWebFile.isDirectory()) {
Log.d("WebResource External", extWebFile + " is directory - trying with index.html");
path += "index.html";
extWebFile = new File(this.activity.getExternalFilesDir(null), path.substring("/assets/".length()));
}
if (extWebFile.exists() && !extWebFile.isDirectory()) {
String type;
String extension = MimeTypeMap.getFileExtensionFromUrl(extWebFile.getName());
if (extension != null) {
extension = extension.toLowerCase();
}
MimeTypeMap mime = MimeTypeMap.getSingleton();
// https://www.iana.org/assignments/media-types/media-types.xhtml
if (extension != null && mime.hasExtension(extension)) {
type = mime.getMimeTypeFromExtension(extension);
} else if (extWebFile.getName().endsWith(".json")) {
type = "application/json";
} else if (extWebFile.getName().endsWith(".js")) {
type = "application/javascript";
} else if (extWebFile.getName().endsWith(".ttf")) {
//type = "application/x-font-ttf";
type = "font/ttf";
} else if (extWebFile.getName().endsWith(".woff")) {
//type = "application/font-woff";
type = "font/woff";
} else if (extWebFile.getName().endsWith(".woff2")) {
type = "font/woff2";
} else if (extWebFile.getName().endsWith(".svg")) {
type = "image/svg+xml";
} else {
type = "TODO";
}
if (!type.equals("TODO")) {
try {
InputStream targetStream = new FileInputStream(extWebFile);
Log.d("WebResource External", extWebFile + " mimetype: " + type);
if (type.startsWith("image/")) {
if (type.startsWith("image/") || type.startsWith("font/")) {
return new WebResourceResponse(type, null, targetStream);
} else {
return new WebResourceResponse(type, "UTF-8", targetStream);
Expand All @@ -481,7 +466,7 @@ public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Log.e("WebResource", e.toString());
}
}
Log.d("WebResource External", extWebFile + " mimetype: " + type);
Log.d("WebResource External", extWebFile.getName() + " extension: " + extension + " mimetype: " + type);
}
Log.d("WebResource Assets", extWebFile + " exists: " + extWebFile.exists());
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/net/mikespub/mywebview/MyContentUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class MyContentUtility {
private static final String TAG = "Content";

/**
* Show detail of a content uri via content resolver
*
* @param activity current Activity context
* @param uri content uri to show
*/
Expand Down Expand Up @@ -56,6 +58,8 @@ static void showContent(AppCompatActivity activity, Uri uri) {
}

/**
* Show columns for current cursor
*
* @param cursor current cursor
*/
static void showCursor(Cursor cursor) {
Expand Down Expand Up @@ -89,6 +93,8 @@ static void showCursor(Cursor cursor) {
}

/**
* Show app downloads via content provider
*
* @param activity current Activity context
*/
static void showMyDownloadFiles(AppCompatActivity activity) {
Expand All @@ -108,6 +114,8 @@ static void showMyDownloadFiles(AppCompatActivity activity) {
}

/**
* Get external downloads dir for this app
*
* @param activity current Activity context
* @return external downloads directory
*/
Expand All @@ -116,6 +124,8 @@ static File getMyExternalDownloadsDir(AppCompatActivity activity) {
}

/**
* Show external download files for this app
*
* @param activity current Activity context
*/
static void showMyExternalDownloadFiles(AppCompatActivity activity) {
Expand All @@ -127,6 +137,8 @@ static void showMyExternalDownloadFiles(AppCompatActivity activity) {
}

/**
* Get DownloadManager service
*
* @param activity current Activity context
* @return download manager
*/
Expand All @@ -135,6 +147,8 @@ static DownloadManager getDownloadManager(AppCompatActivity activity) {
}

/**
* Get download status for a download id
*
* @param activity current Activity context
* @param downloadId download id
* @return current status
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/net/mikespub/mywebview/MyJsonUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
class MyJsonUtility {

/**
* @param json object to convert to map
* Convert JSON string or object to map
*
* @param json string or object to convert to map
* @return converted map
* @throws JSONException trouble converting
*/
Expand Down Expand Up @@ -97,6 +99,8 @@ else if(value instanceof JSONObject) {
}

/**
* Convert object to JSON object or array
*
* @param object object to convert to JSON
* @return converted JSON object
* @throws JSONException trouble converting
Expand All @@ -114,6 +118,8 @@ static Object toJson(Object object) throws JSONException {
}

/**
* Convert map to JSON object
*
* @param map map to convert to JSON object
* @return converted JSON object
* @throws JSONException trouble converting
Expand All @@ -132,6 +138,8 @@ static JSONObject mapToJson(Map<String, Object> map) throws JSONException {
}

/**
* Convert list to JSON array
*
* @param list list to convert to JSON array
* @return converted JSON array
* @throws JSONException trouble converting
Expand All @@ -145,6 +153,8 @@ static JSONArray listToJson(Iterable list) throws JSONException {
}

/**
* Convert object to JSON string
*
* @param object object to convert to JSON string
* @return converted JSON string
* @throws JSONException trouble converting
Expand Down
Loading

0 comments on commit a578001

Please sign in to comment.