Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
first vibration.
Browse files Browse the repository at this point in the history
  • Loading branch information
o-ksi-d committed Mar 27, 2023
1 parent 1fd0e43 commit 41fc31f
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>

</application>
<uses-permission android:name="android.permission.VIBRATE"/>

</manifest>
2 changes: 1 addition & 1 deletion app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/cpp/app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The authors disclaim copyright to this source code

#include <stdlib.h>
#include <string.h>


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;
}
29 changes: 22 additions & 7 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// The authors disclaim copyright to this source code

#include <jni.h>
#include <string>
#include <string.h>

#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());
}
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
67 changes: 58 additions & 9 deletions app/src/main/java/com/cod5/deafblind/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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")
}
Expand Down
13 changes: 4 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
20 changes: 20 additions & 0 deletions app/src/main/res/raw/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- The authors disclaim copyright to this source code -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Deaf - Blind</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>e-mail by Morse</h1>
<div id="txt" style="word-wrap: break-word;white-space: pre-wrap;width:100%; max-width:100%; height:240px; max-height:240px;"></div>
<div style="position:fixed;bottom:0px;width:100%;text-align:center;"
><button style="width:33%; height:200px;" id="short" onclick="cb(this.id, window.event.type, 0, 0)">.</button
><button style="width:33%; height:200px;" id="menu" onclick="cb(this.id, window.event.type, 0, 0)">[ ]</button
><button style="width:33%; height:200px;" id="long" onclick="cb(this.id, window.event.type, 0, 0)">_</button
></div>
<script src="script.js" ></script>
</body>
</html>
13 changes: 13 additions & 0 deletions app/src/main/res/raw/script.js
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions app/src/main/res/raw/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* The authors disclaim copyright to this source code */

body {margin: 0px; padding: 0px;}
button {display:inline-block;}

0 comments on commit 41fc31f

Please sign in to comment.