Test robonames

This commit is contained in:
koalasat
2025-07-16 12:00:14 +02:00
parent 41f1b3a5f2
commit 05c7d02c4f
13 changed files with 148 additions and 34 deletions

View File

@ -11,17 +11,41 @@ interface AndroidAppRobosats {
}
class AndroidRobosats {
private promises: Record<string, (value: string | PromiseLike<string>) => void> = {};
private promises: Record<
string,
{
resolve: (value: string | PromiseLike<string>) => void;
reject: (reason?: string) => void;
}
> = {};
public storePromise: (
uuid: string,
promise: (value: string | PromiseLike<string>) => void,
) => void = (uuid, promise) => {
this.promises[uuid] = promise;
resolve: (value: string | PromiseLike<string>) => void,
reject?: (reason?: string) => void,
) => void = (uuid, resolve, reject) => {
this.promises[uuid] = {
resolve,
reject: reject || ((error) => console.error('Promise rejected:', error)),
};
};
public onResolvePromise: (uuid: string, response: string) => void = (uuid, respone) => {
this.promises[uuid](respone);
public onResolvePromise: (uuid: string, response: string) => void = (uuid, response) => {
if (this.promises[uuid]) {
this.promises[uuid].resolve(response);
delete this.promises[uuid]; // Clean up after resolving
} else {
console.warn(`No promise found for UUID: ${uuid}`);
}
};
public onRejectPromise: (uuid: string, error: string) => void = (uuid, error) => {
if (this.promises[uuid]) {
this.promises[uuid].reject(error);
delete this.promises[uuid]; // Clean up after rejecting
} else {
console.warn(`No promise found for UUID: ${uuid}`);
}
};
}

View File

@ -9,15 +9,19 @@ class RoboidentitiesAndroidClient implements RoboidentitiesClient {
if (this.robonames[initialString]) {
return this.robonames[initialString];
} else {
const result = await new Promise<string>((resolve) => {
const uuid: string = uuidv4();
window.AndroidAppRobosats?.generateRoboname(uuid, initialString);
window.AndroidRobosats?.storePromise(uuid, resolve);
});
try {
const result = await new Promise<string>((resolve, reject) => {
const uuid: string = uuidv4();
window.AndroidAppRobosats?.generateRoboname(uuid, initialString);
window.AndroidRobosats?.storePromise(uuid, resolve, reject);
});
this.robonames[initialString] = result;
return result;
this.robonames[initialString] = result;
return result;
} catch (error) {
console.error('Error generating roboname:', error);
return '';
}
}
};

View File

@ -60,7 +60,7 @@ android {
}
}
fun Packaging.() {
packaging {
jniLibs.useLegacyPackaging = true
}
}

View File

@ -6,7 +6,7 @@ import android.os.Bundle
import android.util.Log
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import com.koalasat.robosats.tor.WebAppInterface
import com.koalasat.robosats.WebAppInterface
import com.robosats.tor.TorKmp
import com.robosats.tor.TorKmpManager
import java.net.InetSocketAddress

View File

@ -0,0 +1,58 @@
package com.koalasat.robosats
import android.util.Log
class RoboIdentities {
companion object {
private const val TAG = "RoboIdentities"
private var librariesLoaded = false
init {
try {
System.loadLibrary("robonames")
System.loadLibrary("robohash")
librariesLoaded = true
Log.d(TAG, "Native libraries loaded successfully")
} catch (e: UnsatisfiedLinkError) {
Log.e(TAG, "Failed to load native libraries: ${e.message}", e)
librariesLoaded = false
} catch (e: Exception) {
Log.e(TAG, "Unexpected error loading native libraries: ${e.message}", e)
librariesLoaded = false
}
}
fun areLibrariesLoaded(): Boolean {
return librariesLoaded
}
}
fun generateRoboname(initial_string: String?): String? {
return try {
if (!areLibrariesLoaded()) {
Log.e(TAG, "Cannot generate roboname: Native libraries not loaded")
"" }
nativeGenerateRoboname(initial_string)
} catch (e: Exception) {
Log.e(TAG, "Error generating roboname: ${e.message}", e)
""
}
}
fun generateRobohash(initial_string: String?): String? {
return try {
if (!areLibrariesLoaded()) {
Log.e(TAG, "Cannot generate robohash: Native libraries not loaded")
return ""
}
nativeGenerateRobohash(initial_string)
} catch (e: Exception) {
Log.e(TAG, "Error generating robohash: ${e.message}", e)
""
}
}
// Native functions implemented in Rust.
private external fun nativeGenerateRoboname(initial_string: String?): String?
private external fun nativeGenerateRobohash(initial_string: String?): String?
}

View File

@ -0,0 +1,46 @@
package com.koalasat.robosats
import android.content.Context
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.widget.Toast
class WebAppInterface(private val context: Context, private val webView: WebView) {
private val TAG = "WebAppInterface"
private val roboIdentities = RoboIdentities()
init {
// Check if libraries are loaded and show a toast notification if there's an issue
if (!RoboIdentities.areLibrariesLoaded()) {
Log.w(TAG, "Native libraries for RoboIdentities are not loaded - fallback names will be used")
Toast.makeText(
context,
"Warning: Robot name generator is using fallback mode",
Toast.LENGTH_LONG
).show()
}
}
@JavascriptInterface
fun generateRoboname(uuid: String, message: String) {
try {
val roboname = roboIdentities.generateRoboname(message)
Log.d(TAG, "Generated roboname: $roboname for message: $message")
webView.post {
webView.evaluateJavascript("javascript:window.AndroidRobosats.onResolvePromise('${uuid}', '${roboname}')", null)
}
} catch (e: Exception) {
Log.e(TAG, "Error in generateRoboname: ${e.message}", e)
// Handle error gracefully by returning a fallback value
webView.post {
webView.evaluateJavascript(
"javascript:window.AndroidRobosats.onRejectPromise('${uuid}', 'Error generating robot name')",
null
)
}
}
}
}

View File

@ -1,18 +0,0 @@
package com.koalasat.robosats.tor
import android.content.Context
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.widget.Toast
class WebAppInterface(private val context: Context, private val webView: WebView) {
@JavascriptInterface
fun generateRoboname(uuid: String, message: String) {
// Handle the message received from JavaScript
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
webView.post {
webView.evaluateJavascript("javascript:window.AndroidRobosats.onResolvePromise('${uuid}', '${message}')", null)
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.