Skip to content

Commit

Permalink
save websettings, upgrade target SDK to 14
Browse files Browse the repository at this point in the history
  • Loading branch information
jvde-github committed Jul 9, 2024
1 parent cc03684 commit ca2cf89
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ plugins {

android {

compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.jvdegithub.aiscatcher"
minSdk 23
targetSdk 33
versionCode 96
versionName '0.96'
targetSdk 34
versionCode 97
versionName '0.97'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jvdegithub.aiscatcher.ui.main;

import android.app.UiModeManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
Expand All @@ -13,7 +12,6 @@
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -24,20 +22,19 @@
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.jvdegithub.aiscatcher.MainActivity;
import com.jvdegithub.aiscatcher.R;
import com.jvdegithub.aiscatcher.tools.LogBook;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class WebViewMapFragment extends Fragment {

private WebView webView;
private LogBook logbook;
private SharedPreferences sharedPreferences;

public static WebViewMapFragment newInstance() {
return new WebViewMapFragment();
Expand All @@ -49,9 +46,16 @@ private boolean isOnline() {
return networkCapabilities != null && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
}

// Method to get SharedPreferences instance
private SharedPreferences getSharedPreferences() {
if (sharedPreferences == null) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
}
return sharedPreferences;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

logbook = LogBook.getInstance();

View rootView = inflater.inflate(R.layout.fragment_map, container, false);
Expand All @@ -61,20 +65,19 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
webSettings.setDomStorageEnabled(true);

webView.setWebViewClient(new WebViewClient() {

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();

if (url.startsWith("https://cdn.jsdelivr.net/") || url.startsWith("https://unpkg.com/")) {
String prefix = url.startsWith("https://cdn.jsdelivr.net/") ? "https://cdn.jsdelivr.net/" : "https://unpkg.com/";

String remainingPath = "webassets/cdn/"+url.substring(prefix.length());
String remainingPath = "webassets/cdn/" + url.substring(prefix.length());

try {
Context context = getContext();

if(context == null) {
if (context == null) {
return null;
}

Expand All @@ -87,7 +90,7 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
contentType = "image/svg+xml";
} else if (remainingPath.endsWith(".png")) {
contentType = "image/png";
} else if (remainingPath.endsWith(".js")) {
} else if (remainingPath.endsWith(".js")) {
contentType = "text/plain";
} else return null;

Expand All @@ -101,9 +104,16 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque

return null;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
webView.setVisibility(View.INVISIBLE);

// Restore localStorage content as early as possible
String localStorageContent = getSharedPreferences().getString("localStorageContent", null);
if (localStorageContent != null) {
webView.evaluateJavascript("localStorage.setItem('settings', " + localStorageContent + ");", null);
}
}

@Override
Expand All @@ -116,7 +126,7 @@ public void onPageFinished(WebView view, String url) {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
logbook.addLog(String.format("W(%d): %s",
consoleMessage.lineNumber(), consoleMessage.message() ));
consoleMessage.lineNumber(), consoleMessage.message()));

return true;
}
Expand All @@ -135,9 +145,20 @@ public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return rootView;
}

@Override
public void onPause() {
super.onPause();
webView.evaluateJavascript("localStorage.getItem('settings');", value -> {
if (value != null && !value.equals("null")) {
getSharedPreferences().edit().putString("localStorageContent", value).apply();
}
});
}

@Override
public void onDestroyView() {
super.onDestroyView();
webView.stopLoading();
logbook.addLog("View is destroyed."); }
}
logbook.addLog("View is destroyed.");
}
}

0 comments on commit ca2cf89

Please sign in to comment.