mirror of
https://gitlab.com/octtspacc/browserocto
synced 2025-06-05 21:49:19 +02:00
Custom logic for browser caching
This commit is contained in:
@ -1,13 +1,13 @@
|
|||||||
apply plugin: 'com.android.application'
|
apply plugin: 'com.android.application'
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 29
|
compileSdkVersion 30
|
||||||
buildToolsVersion "21.1.0"
|
buildToolsVersion "21.1.0"
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "org.eu.octt.browserocto"
|
applicationId "org.eu.octt.browserocto"
|
||||||
minSdkVersion 1
|
minSdkVersion 1
|
||||||
targetSdkVersion 29
|
targetSdkVersion 30
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0.1"
|
versionName "1.0.1"
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ public class MainActivity extends Activity {
|
|||||||
final EditText EditUrl = findViewById(R.id.EditUrl);
|
final EditText EditUrl = findViewById(R.id.EditUrl);
|
||||||
final Button BtnOpen = findViewById(R.id.BtnOpen);
|
final Button BtnOpen = findViewById(R.id.BtnOpen);
|
||||||
final Button BtnShortcut = findViewById(R.id.BtnShortcut);
|
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() {
|
BtnOpen.setOnClickListener(new OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -141,4 +141,4 @@ public class MainActivity extends Activity {
|
|||||||
|
|
||||||
Dial.show();
|
Dial.show();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -10,64 +10,60 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
public class WebWindowActivity extends Activity {
|
public class WebWindowActivity extends Activity {
|
||||||
|
WebView Web0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.webwindow);
|
setContentView(R.layout.webwindow);
|
||||||
Bundle Extra = getIntent().getExtras();
|
final Bundle Extra = getIntent().getExtras();
|
||||||
LinearLayout LayMain = findViewById(R.id.LayMain);
|
LinearLayout LayMain = findViewById(R.id.LayMain);
|
||||||
|
|
||||||
WebView Web0 = findViewById(R.id.Web0);
|
/*final WebView */Web0 = findViewById(R.id.Web0);
|
||||||
//Web0.setWebViewClient(new WebViewClient());
|
final String WebCacheDir = "/data/data/" + getPackageName() + "/cache/WebCache/";
|
||||||
|
|
||||||
Web0.setWebViewClient(new WebViewClient() {
|
Web0.setWebViewClient(new WebViewClient() {
|
||||||
// https://gist.github.com/kmerrell42/b4ff31733c562a3262ee9a42f5704a89
|
// https://gist.github.com/kmerrell42/b4ff31733c562a3262ee9a42f5704a89
|
||||||
@Override
|
@Override
|
||||||
public WebResourceResponse shouldInterceptRequest(WebView v, String Url) {
|
public WebResourceResponse shouldInterceptRequest(WebView v, String Url) {
|
||||||
return HandleRequest(Url);
|
return HandleRequest(Url);
|
||||||
}
|
};
|
||||||
@Override
|
@Override
|
||||||
public WebResourceResponse shouldInterceptRequest(WebView v, WebResourceRequest Req) {
|
public WebResourceResponse shouldInterceptRequest(WebView v, WebResourceRequest Req) {
|
||||||
return HandleRequest(Req.getUrl().toString());
|
return HandleRequest(Req.getUrl().toString());
|
||||||
}
|
};
|
||||||
|
// TODO: Save all HTTP headers too and return them somehow
|
||||||
private WebResourceResponse HandleRequest(String Url) {
|
private WebResourceResponse HandleRequest(String Url) {
|
||||||
//Thread thread = new Thread(new Runnable() {
|
|
||||||
// @Override
|
|
||||||
// public void run() {
|
|
||||||
try {
|
try {
|
||||||
//URL url;
|
final String UrlDir = WebCacheDir + Url + "_";
|
||||||
//HttpURLConnection Connection = null;
|
final File UrlFile = new File(UrlDir, "Content");
|
||||||
URL Url_ = new URL(Url);
|
if (!UrlFile.exists() || !Extra.getBoolean("Cache")) {
|
||||||
HttpURLConnection Connection = (HttpURLConnection)Url_.openConnection();
|
final URL Url_ = new URL(Url);
|
||||||
String ContentType = Connection.getContentType();
|
final HttpURLConnection Connection = (HttpURLConnection)Url_.openConnection();
|
||||||
String ContentEncoding = null;
|
_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) {
|
if (ContentType != null) {
|
||||||
ContentType = ContentType.split("\\;")[0];
|
//ContentEncoding = ContentType.split("=")[1];
|
||||||
ContentEncoding = ContentType.split("\\=")[1];
|
ContentType = ContentType.split(";")[0];
|
||||||
}
|
};
|
||||||
InputStream In = new BufferedInputStream(Connection.getInputStream());
|
return new WebResourceResponse(ContentType, ContentEncoding, new FileInputStream(UrlFile));
|
||||||
return new WebResourceResponse(ContentType, ContentEncoding, In);
|
|
||||||
} catch (Exception Ex) {
|
} catch (Exception Ex) {
|
||||||
Log.e("browserocto/Log", "", Ex);
|
Log.e("browserocto/Log", "", Ex);
|
||||||
};
|
};
|
||||||
// };//});
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
Web0.getSettings().setJavaScriptEnabled(true);
|
Web0.getSettings().setJavaScriptEnabled(true);
|
||||||
Web0.getSettings().setDomStorageEnabled(true);
|
Web0.getSettings().setDomStorageEnabled(true);
|
||||||
Web0.getSettings().setCacheMode(WhichCacheMode(Extra.getBoolean("Cache")));
|
Web0.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//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.loadUrl(Extra.getString("Url"));
|
Web0.loadUrl(Extra.getString("Url"));
|
||||||
_Util.ToastMsg(Extra.getString("Url"), this);
|
_Util.ToastMsg(Extra.getString("Url"), this);
|
||||||
|
|
||||||
// Thread thread = new Thread(new Runnable() {
|
// Thread thread = new Thread(new Runnable() {
|
||||||
// @Override
|
// @Override
|
||||||
// public void run() {
|
// public void run() {
|
||||||
@ -97,10 +93,12 @@ public class WebWindowActivity extends Activity {
|
|||||||
// thread.start();
|
// thread.start();
|
||||||
};
|
};
|
||||||
|
|
||||||
public int WhichCacheMode(boolean Opt) {
|
@Override
|
||||||
if (Opt) {
|
public void onBackPressed() {
|
||||||
return WebSettings.LOAD_CACHE_ELSE_NETWORK;
|
if (Web0.canGoBack()) {
|
||||||
|
Web0.goBack();
|
||||||
|
} else {
|
||||||
|
super.onBackPressed();
|
||||||
};
|
};
|
||||||
return WebSettings.LOAD_NO_CACHE;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -38,21 +38,43 @@ public class _Util extends Activity {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// https://stackoverflow.com/a/5713929
|
// https://stackoverflow.com/a/5713929
|
||||||
public static String StreamToString(InputStream is) throws IOException {
|
public static String StreamToString(InputStream In) throws IOException {
|
||||||
if (is != null) {
|
if (In != null) {
|
||||||
Writer writer = new StringWriter();
|
Writer writer = new StringWriter();
|
||||||
char[] buffer = new char[1024];
|
char[] buffer = new char[1024];
|
||||||
try {
|
try {
|
||||||
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
Reader reader = new BufferedReader(new InputStreamReader(In, "UTF-8"));
|
||||||
int n;
|
int n;
|
||||||
while ((n = reader.read(buffer)) != -1) {
|
while ((n = reader.read(buffer)) != -1) {
|
||||||
writer.write(buffer, 0, n);
|
writer.write(buffer, 0, n);
|
||||||
};
|
};
|
||||||
} finally {
|
} finally {
|
||||||
is.close();
|
In.close();
|
||||||
};
|
};
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
};
|
};
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:ems="10"
|
android:inputType="text"
|
||||||
|
android:maxLines="1"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:hint="URL..."
|
android:hint="URL..."
|
||||||
android:text="https://"
|
android:text="https://"
|
||||||
@ -38,8 +39,7 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- TODO: Make this be CheckBox on Android < ICS because Switch doesn't exist -->
|
<CheckBox
|
||||||
<Switch
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="[Open Now] Force load from local cache"
|
android:text="[Open Now] Force load from local cache"
|
||||||
@ -47,4 +47,4 @@
|
|||||||
android:checked="true"
|
android:checked="true"
|
||||||
android:id="@+id/SwitchCache"/>
|
android:id="@+id/SwitchCache"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
Reference in New Issue
Block a user