Custom logic for browser caching
This commit is contained in:
parent
7fb71c27a7
commit
1f0ab640ba
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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 "";
|
||||
};
|
||||
};
|
||||
|
||||
// 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
|
||||
android:layout_width="match_parent"
|
||||
android:ems="10"
|
||||
android:inputType="text"
|
||||
android:maxLines="1"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="URL..."
|
||||
android:text="https://"
|
||||
|
@ -38,8 +39,7 @@
|
|||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- TODO: Make this be CheckBox on Android < ICS because Switch doesn't exist -->
|
||||
<Switch
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="[Open Now] Force load from local cache"
|
||||
|
@ -47,4 +47,4 @@
|
|||
android:checked="true"
|
||||
android:id="@+id/SwitchCache"/>
|
||||
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
|
Loading…
Reference in New Issue