From ddd8bb7af6808df7f611d1f4847ef481ca1c8de3 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 7 May 2023 13:06:12 -0700 Subject: [PATCH] Add NativeX class --- .../java/gallery/memories/MainActivity.java | 33 +++------------- .../main/java/gallery/memories/NativeX.java | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/gallery/memories/NativeX.java diff --git a/app/src/main/java/gallery/memories/MainActivity.java b/app/src/main/java/gallery/memories/MainActivity.java index b8bc3010..7b8968f9 100644 --- a/app/src/main/java/gallery/memories/MainActivity.java +++ b/app/src/main/java/gallery/memories/MainActivity.java @@ -1,7 +1,7 @@ package gallery.memories; +import android.annotation.SuppressLint; import android.os.Bundle; -import android.webkit.JavascriptInterface; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; @@ -10,17 +10,11 @@ import android.webkit.WebViewClient; import androidx.appcompat.app.AppCompatActivity; import gallery.memories.databinding.ActivityMainBinding; -import gallery.memories.service.ImageService; -import gallery.memories.service.JsService; -import gallery.memories.service.TimelineQuery; public class MainActivity extends AppCompatActivity { public static final String TAG = "memories-native"; protected ActivityMainBinding binding; - - protected JsService mJsService; - protected ImageService mImageService; - protected TimelineQuery mQuery; + protected NativeX mNativeX; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,13 +23,11 @@ public class MainActivity extends AppCompatActivity { binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); - mJsService = new JsService(this, binding.webview); - mImageService = new ImageService(this); - mQuery = new TimelineQuery(this); - + mNativeX = new NativeX(this, binding.webview); initWebview(); } + @SuppressLint("SetJavaScriptEnabled") protected void initWebview() { binding.webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { @@ -52,22 +44,7 @@ public class MainActivity extends AppCompatActivity { webSettings.setDatabaseEnabled(true); webSettings.setUserAgentString("memories-native-android/0.0"); - binding.webview.addJavascriptInterface(this, "nativex"); + binding.webview.addJavascriptInterface(mNativeX, "nativex"); binding.webview.loadUrl("http://10.0.2.2:8035/index.php/apps/memories/"); } - - @JavascriptInterface - public boolean isNative() { - return true; - } - - @JavascriptInterface - public void getLocalByDayId(final String call, final long dayId) { - mJsService.runAsync(call, () -> mQuery.getByDayId(dayId).toString().getBytes()); - } - - @JavascriptInterface - public void getJpeg(final String call, final String uri) { - mJsService.runAsync(call, () -> mImageService.getFromURI(uri)); - } } \ No newline at end of file diff --git a/app/src/main/java/gallery/memories/NativeX.java b/app/src/main/java/gallery/memories/NativeX.java new file mode 100644 index 00000000..ba3b7b20 --- /dev/null +++ b/app/src/main/java/gallery/memories/NativeX.java @@ -0,0 +1,39 @@ +package gallery.memories; + +import android.app.Activity; +import android.webkit.JavascriptInterface; +import android.webkit.WebView; + +import gallery.memories.service.ImageService; +import gallery.memories.service.JsService; +import gallery.memories.service.TimelineQuery; + +public class NativeX { + Activity mActivity; + + protected JsService mJsService; + protected ImageService mImageService; + protected TimelineQuery mQuery; + + public NativeX(Activity activity, WebView webView) { + mActivity = activity; + mJsService = new JsService(activity, webView); + mImageService = new ImageService(activity); + mQuery = new TimelineQuery(activity); + } + + @JavascriptInterface + public boolean isNative() { + return true; + } + + @JavascriptInterface + public void getLocalByDayId(final String call, final long dayId) { + mJsService.runAsync(call, () -> mQuery.getByDayId(dayId).toString().getBytes()); + } + + @JavascriptInterface + public void getJpeg(final String call, final String uri) { + mJsService.runAsync(call, () -> mImageService.getFromURI(uri)); + } +}