diff --git a/app/build.gradle b/app/build.gradle index b8a9706..bb99247 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 29 + compileSdkVersion 30 buildToolsVersion "21.1.0" defaultConfig { applicationId "org.eu.octt.browserocto" minSdkVersion 1 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0.1" } diff --git a/app/src/main/java/org/eu/octt/browserocto/MainActivity.java b/app/src/main/java/org/eu/octt/browserocto/MainActivity.java index 4a25ffa..6eb6c34 100644 --- a/app/src/main/java/org/eu/octt/browserocto/MainActivity.java +++ b/app/src/main/java/org/eu/octt/browserocto/MainActivity.java @@ -20,7 +20,7 @@ public class MainActivity extends Activity { final EditText EditUrl = findViewById(R.id.EditUrl); final Button BtnOpen = findViewById(R.id.BtnOpen); final Button BtnShortcut = findViewById(R.id.BtnShortcut); - final Switch SwitchCache = findViewById(R.id.SwitchCache); + final CheckBox SwitchCache = findViewById(R.id.SwitchCache); BtnOpen.setOnClickListener(new OnClickListener() { @Override @@ -141,4 +141,4 @@ public class MainActivity extends Activity { Dial.show(); }; -}; \ No newline at end of file +}; diff --git a/app/src/main/java/org/eu/octt/browserocto/WebWindowActivity.java b/app/src/main/java/org/eu/octt/browserocto/WebWindowActivity.java index f9d13b9..1c91ec0 100644 --- a/app/src/main/java/org/eu/octt/browserocto/WebWindowActivity.java +++ b/app/src/main/java/org/eu/octt/browserocto/WebWindowActivity.java @@ -10,64 +10,60 @@ import java.io.*; import java.net.*; public class WebWindowActivity extends Activity { + WebView Web0; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webwindow); - Bundle Extra = getIntent().getExtras(); + final Bundle Extra = getIntent().getExtras(); LinearLayout LayMain = findViewById(R.id.LayMain); - WebView Web0 = findViewById(R.id.Web0); - //Web0.setWebViewClient(new WebViewClient()); - + /*final WebView */Web0 = findViewById(R.id.Web0); + final String WebCacheDir = "/data/data/" + getPackageName() + "/cache/WebCache/"; + Web0.setWebViewClient(new WebViewClient() { // https://gist.github.com/kmerrell42/b4ff31733c562a3262ee9a42f5704a89 @Override public WebResourceResponse shouldInterceptRequest(WebView v, String Url) { return HandleRequest(Url); - } + }; @Override public WebResourceResponse shouldInterceptRequest(WebView v, WebResourceRequest Req) { return HandleRequest(Req.getUrl().toString()); - } + }; + // TODO: Save all HTTP headers too and return them somehow private WebResourceResponse HandleRequest(String Url) { - //Thread thread = new Thread(new Runnable() { - // @Override - // public void run() { try { - //URL url; - //HttpURLConnection Connection = null; - URL Url_ = new URL(Url); - HttpURLConnection Connection = (HttpURLConnection)Url_.openConnection(); - String ContentType = Connection.getContentType(); - String ContentEncoding = null; + final String UrlDir = WebCacheDir + Url + "_"; + final File UrlFile = new File(UrlDir, "Content"); + if (!UrlFile.exists() || !Extra.getBoolean("Cache")) { + final URL Url_ = new URL(Url); + final HttpURLConnection Connection = (HttpURLConnection)Url_.openConnection(); + _Util.WriteStreamToFile(new BufferedInputStream(Connection.getInputStream()), UrlDir, "Content"); + _Util.WriteStreamToFile(new ByteArrayInputStream(Connection.getContentType().getBytes()), UrlDir, "Content-Type"); + }; + String ContentType = _Util.StreamToString(new FileInputStream(new File(UrlDir, "Content-Type"))); + final String ContentEncoding = null; if (ContentType != null) { - ContentType = ContentType.split("\\;")[0]; - ContentEncoding = ContentType.split("\\=")[1]; - } - InputStream In = new BufferedInputStream(Connection.getInputStream()); - return new WebResourceResponse(ContentType, ContentEncoding, In); + //ContentEncoding = ContentType.split("=")[1]; + ContentType = ContentType.split(";")[0]; + }; + return new WebResourceResponse(ContentType, ContentEncoding, new FileInputStream(UrlFile)); } catch (Exception Ex) { Log.e("browserocto/Log", "", Ex); }; - // };//}); return null; }; }); Web0.getSettings().setJavaScriptEnabled(true); Web0.getSettings().setDomStorageEnabled(true); - Web0.getSettings().setCacheMode(WhichCacheMode(Extra.getBoolean("Cache"))); - - // This is apparently not working like I want (force caching of all site resources in spite of bad Cache-Control HTTP headers) - //webView.getSettings().setAppCacheMaxSize(1024*1024*8); - //webView.getSettings().setAppCachePath("/data/data/" + getPackageName() + "/cache"); - //webView.getSettings().setAllowFileAccess(true); - //webView.getSettings().setAppCacheEnabled(true); + Web0.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//WhichCacheMode(Extra.getBoolean("Cache"))); Web0.loadUrl(Extra.getString("Url")); _Util.ToastMsg(Extra.getString("Url"), this); - + // Thread thread = new Thread(new Runnable() { // @Override // public void run() { @@ -97,10 +93,12 @@ public class WebWindowActivity extends Activity { // thread.start(); }; - public int WhichCacheMode(boolean Opt) { - if (Opt) { - return WebSettings.LOAD_CACHE_ELSE_NETWORK; + @Override + public void onBackPressed() { + if (Web0.canGoBack()) { + Web0.goBack(); + } else { + super.onBackPressed(); }; - return WebSettings.LOAD_NO_CACHE; }; -}; \ No newline at end of file +}; diff --git a/app/src/main/java/org/eu/octt/browserocto/_Util.java b/app/src/main/java/org/eu/octt/browserocto/_Util.java index 3a50a43..217c717 100644 --- a/app/src/main/java/org/eu/octt/browserocto/_Util.java +++ b/app/src/main/java/org/eu/octt/browserocto/_Util.java @@ -38,21 +38,43 @@ public class _Util extends Activity { }; // https://stackoverflow.com/a/5713929 - public static String StreamToString(InputStream is) throws IOException { - if (is != null) { + public static String StreamToString(InputStream In) throws IOException { + if (In != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { - Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); + Reader reader = new BufferedReader(new InputStreamReader(In, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); }; } finally { - is.close(); + In.close(); }; return writer.toString(); }; return ""; }; -}; \ No newline at end of file + + // https://stackoverflow.com/a/10857407 + public static void WriteStreamToFile(InputStream In, String Path, String Name) throws IOException { + try { + File dir = new File(Path); + dir.mkdirs(); + File file = new File(Path, Name); + file.createNewFile(); + try (OutputStream output = new FileOutputStream(file)) { + byte[] buffer = new byte[4 * 1024]; + int read; + + while ((read = In.read(buffer)) != -1) { + output.write(buffer, 0, read); + } + + output.flush(); + } + } finally { + In.close(); + } + }; +}; diff --git a/app/src/main/res/layout/main.xml b/app/src/main/res/layout/main.xml index a4b343f..cc72180 100644 --- a/app/src/main/res/layout/main.xml +++ b/app/src/main/res/layout/main.xml @@ -9,7 +9,8 @@ - - - \ No newline at end of file +