mirror of
https://git.quad4.io/RNS-Things/MeshChatX.git
synced 2026-06-04 01:01:22 +00:00
feat(android): initialize Android project with Gradle configuration, build scripts, and main application structure for MeshChatX
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.meshchatx;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Bundle;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.ProgressBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import com.chaquo.python.Python;
|
||||
import com.chaquo.python.android.AndroidPlatform;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private WebView webView;
|
||||
private ProgressBar progressBar;
|
||||
private static final String SERVER_URL = "http://127.0.0.1:8000";
|
||||
private static final int SERVER_PORT = 8000;
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
webView = findViewById(R.id.webView);
|
||||
progressBar = findViewById(R.id.progressBar);
|
||||
|
||||
if (!Python.isStarted()) {
|
||||
Python.start(new AndroidPlatform(this));
|
||||
}
|
||||
|
||||
WebSettings webSettings = webView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
webSettings.setDomStorageEnabled(true);
|
||||
webSettings.setDatabaseEnabled(true);
|
||||
webSettings.setAllowFileAccess(true);
|
||||
webSettings.setAllowContentAccess(true);
|
||||
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||||
|
||||
webView.setWebViewClient(new WebViewClient() {
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
progressBar.setVisibility(android.view.View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
|
||||
super.onPageStarted(view, url, favicon);
|
||||
progressBar.setVisibility(android.view.View.VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
startMeshChatServer();
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
runOnUiThread(() -> {
|
||||
webView.loadUrl(SERVER_URL);
|
||||
});
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void startMeshChatServer() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Python py = Python.getInstance();
|
||||
py.getModule("meshchat_wrapper").callAttr("start_server", SERVER_PORT);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (webView.canGoBack()) {
|
||||
webView.goBack();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (webView != null) {
|
||||
webView.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user