Add NativeX class

pull/653/merge
Varun Patil 2023-05-07 13:06:12 -07:00
parent 37415f7dd5
commit ddd8bb7af6
2 changed files with 44 additions and 28 deletions

View File

@ -1,7 +1,7 @@
package gallery.memories; package gallery.memories;
import android.annotation.SuppressLint;
import android.os.Bundle; import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest; import android.webkit.WebResourceRequest;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
@ -10,17 +10,11 @@ import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import gallery.memories.databinding.ActivityMainBinding; 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 class MainActivity extends AppCompatActivity {
public static final String TAG = "memories-native"; public static final String TAG = "memories-native";
protected ActivityMainBinding binding; protected ActivityMainBinding binding;
protected NativeX mNativeX;
protected JsService mJsService;
protected ImageService mImageService;
protected TimelineQuery mQuery;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -29,13 +23,11 @@ public class MainActivity extends AppCompatActivity {
binding = ActivityMainBinding.inflate(getLayoutInflater()); binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot()); setContentView(binding.getRoot());
mJsService = new JsService(this, binding.webview); mNativeX = new NativeX(this, binding.webview);
mImageService = new ImageService(this);
mQuery = new TimelineQuery(this);
initWebview(); initWebview();
} }
@SuppressLint("SetJavaScriptEnabled")
protected void initWebview() { protected void initWebview() {
binding.webview.setWebViewClient(new WebViewClient() { binding.webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
@ -52,22 +44,7 @@ public class MainActivity extends AppCompatActivity {
webSettings.setDatabaseEnabled(true); webSettings.setDatabaseEnabled(true);
webSettings.setUserAgentString("memories-native-android/0.0"); 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/"); 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));
}
} }

View File

@ -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));
}
}