From 41fc31f49f33ad9f24628ac8750fbcbbe6a0cc6a Mon Sep 17 00:00:00 2001 From: jml Date: Mon, 27 Mar 2023 17:38:22 +0200 Subject: [PATCH] first vibration. --- .idea/compiler.xml | 6 ++ .idea/gradle.xml | 6 ++ .idea/misc.xml | 2 +- app/src/main/AndroidManifest.xml | 3 +- app/src/main/cpp/CMakeLists.txt | 2 +- app/src/main/cpp/app.c | 18 +++++ app/src/main/cpp/native-lib.cpp | 29 ++++++-- .../java/com/cod5/deafblind/MainActivity.kt | 67 ++++++++++++++++--- app/src/main/res/layout/activity_main.xml | 13 ++-- app/src/main/res/raw/index.html | 20 ++++++ app/src/main/res/raw/script.js | 13 ++++ app/src/main/res/raw/style.css | 4 ++ 12 files changed, 155 insertions(+), 28 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 app/src/main/cpp/app.c create mode 100644 app/src/main/res/raw/index.html create mode 100644 app/src/main/res/raw/script.js create mode 100644 app/src/main/res/raw/style.css diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index f22a539..a2d7c21 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -7,6 +7,12 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 2052458..bdd9278 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fc92355..fae9375 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,11 +20,12 @@ - + + \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index d826c0e..2a8d34c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -21,7 +21,7 @@ add_library( # Sets the name of the library. SHARED # Provides a relative path to your source file(s). - native-lib.cpp) + native-lib.cpp app.c) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by diff --git a/app/src/main/cpp/app.c b/app/src/main/cpp/app.c new file mode 100644 index 0000000..8cf76a0 --- /dev/null +++ b/app/src/main/cpp/app.c @@ -0,0 +1,18 @@ +// The authors disclaim copyright to this source code + +#include +#include + + +char *cb(const char *id, const char *event, const char *err, const char *data) { + char *ret; + ret = ""; + if (!strcmp(id, "long")) { + ret = "vibrate:50;add:-;"; + } else if (!strcmp(id, "short")) { + ret = "vibrate:30;add:.;"; + } else if (!strcmp(id, "menu")) { + ret = "add: ;"; + } + return ret; +} \ No newline at end of file diff --git a/app/src/main/cpp/native-lib.cpp b/app/src/main/cpp/native-lib.cpp index 15fe52c..0db9874 100644 --- a/app/src/main/cpp/native-lib.cpp +++ b/app/src/main/cpp/native-lib.cpp @@ -1,10 +1,25 @@ +// The authors disclaim copyright to this source code + #include -#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +extern char *cb(const char *id, const char *event, const char *err, const char *data); -extern "C" JNIEXPORT jstring JNICALL -Java_com_cod5_deafblind_MainActivity_stringFromJNI( + JNIEXPORT jstring JNICALL +Java_com_cod5_deafblind_MainActivity_cbToJNI( JNIEnv* env, - jobject /* this */) { - std::string hello = "Hello from C++"; - return env->NewStringUTF(hello.c_str()); -} \ No newline at end of file +jobject self, jstring id, jstring event, jstring err, jstring data) { + const char *a, *b, *c, *d; + a = env->GetStringUTFChars(id, 0); + b = env->GetStringUTFChars(event, 0); + c = env->GetStringUTFChars(err, 0); + d = env->GetStringUTFChars(data, 0); + return env->NewStringUTF(cb(a, b, c, d)); +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/app/src/main/java/com/cod5/deafblind/MainActivity.kt b/app/src/main/java/com/cod5/deafblind/MainActivity.kt index ace1a81..358f5d5 100644 --- a/app/src/main/java/com/cod5/deafblind/MainActivity.kt +++ b/app/src/main/java/com/cod5/deafblind/MainActivity.kt @@ -1,8 +1,17 @@ + +// The authors disclaim copyright to this source code + package com.cod5.deafblind +import android.os.Build +import android.os.Vibrator; import androidx.appcompat.app.AppCompatActivity import android.os.Bundle -import android.widget.TextView +import android.os.VibrationEffect +import android.os.VibratorManager +import android.webkit.JavascriptInterface +import android.webkit.WebView +import androidx.core.content.getSystemService import com.cod5.deafblind.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { @@ -15,18 +24,58 @@ class MainActivity : AppCompatActivity() { binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - // Example of a call to a native method - binding.sampleText.text = stringFromJNI() + binding.web.settings.javaScriptEnabled = true + binding.web.loadUrl("file:///android_res/raw/index.html") + binding.web.addJavascriptInterface(MyJs(this), "Android") + } + + class MyJs(private val self:MainActivity) { + @JavascriptInterface + fun cb(id: String, event: String, err: String, data: String): String { + val r = self.cbToJNI(id, event, err, data) + if (r.startsWith("vibrate:")) { + val s = r.substring(8, r.indexOf(';')) + try { + val l = s.toLong() + if (l >= 10 && l <= 1000) { + self.vibrate(l) + } + return r.substring(r.indexOf(';') + 1) + } catch (e:Exception) { + + } + } + return r; + } } + private external fun cbToJNI(id: String, event: String, err: String, data:String): String + + + fun vibrate(tms: Long) { + try { + val v: Vibrator + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + val vibratorService = + this.getSystemService(VIBRATOR_MANAGER_SERVICE) as VibratorManager + v = vibratorService.defaultVibrator + } else { + @Suppress("DEPRECATION") + v = getSystemService(VIBRATOR_SERVICE) as Vibrator + } + v.vibrate(VibrationEffect.createOneShot(tms, VibrationEffect.DEFAULT_AMPLITUDE)) + } else { + @Suppress("DEPRECATION") + v = getSystemService(VIBRATOR_SERVICE) as Vibrator + @Suppress("DEPRECATION") + v.vibrate(tms) + } + } catch (e: Exception) { - /** - * A native method that is implemented by the 'deafblind' native library, - * which is packaged with this application. - */ - external fun stringFromJNI(): String + } + } companion object { - // Used to load the 'deafblind' library on application startup. init { System.loadLibrary("deafblind") } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0fdf985..f4341b8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,14 +6,9 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + \ No newline at end of file diff --git a/app/src/main/res/raw/index.html b/app/src/main/res/raw/index.html new file mode 100644 index 0000000..b2151bd --- /dev/null +++ b/app/src/main/res/raw/index.html @@ -0,0 +1,20 @@ + + + + + + + Deaf - Blind + + + +

e-mail by Morse

+
+
+ + + diff --git a/app/src/main/res/raw/script.js b/app/src/main/res/raw/script.js new file mode 100644 index 0000000..d48d2c1 --- /dev/null +++ b/app/src/main/res/raw/script.js @@ -0,0 +1,13 @@ +// The authors disclaim copyright to this source code + + +function cb(id, event, err, data) { + let d = document.getElementById("txt"); + + let r = window.Android.cb(id, event, err, data); + if (r.startsWith("add:")) { + d.innerText = d.innerText + r.substr(4, r.indexOf(';') - 4); + } + console.log(r); + return true; +} \ No newline at end of file diff --git a/app/src/main/res/raw/style.css b/app/src/main/res/raw/style.css new file mode 100644 index 0000000..793cc98 --- /dev/null +++ b/app/src/main/res/raw/style.css @@ -0,0 +1,4 @@ +/* The authors disclaim copyright to this source code */ + +body {margin: 0px; padding: 0px;} +button {display:inline-block;} \ No newline at end of file