commit 80caff33e31146d184990924d19574c5364d76a3 Author: paumann Date: Thu May 4 11:36:17 2023 +0200 Initial commit for assignment 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..7174788 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,44 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' +} + +android { + compileSdk 33 + + defaultConfig { + applicationId "de.luh.hci.mi.atomikkeyboard" + minSdk 23 + targetSdk 33 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } + namespace 'de.luh.hci.mi.atomikkeyboard' +} + +dependencies { + + implementation 'androidx.core:core-ktx:1.7.0' + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'com.google.android.material:material:1.8.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/de/luh/hci/mi/atomikkeyboard/ExampleInstrumentedTest.kt b/app/src/androidTest/java/de/luh/hci/mi/atomikkeyboard/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..192513b --- /dev/null +++ b/app/src/androidTest/java/de/luh/hci/mi/atomikkeyboard/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package de.luh.hci.mi.atomikkeyboard + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("de.luh.hci.mi.atomikkeyboard", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..a8d139d --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/luh/hci/mi/atomikkeyboard/Key.kt b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/Key.kt new file mode 100644 index 0000000..eaf84fa --- /dev/null +++ b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/Key.kt @@ -0,0 +1,8 @@ +package de.luh.hci.mi.atomikkeyboard + +/** + * Represents a single key on the keyboard. + * letter is the symbol on the key. + * (x, y) are the coordinates of the center of the key. + */ +data class Key(val letter: Char, val x: Double, val y: Double) \ No newline at end of file diff --git a/app/src/main/java/de/luh/hci/mi/atomikkeyboard/Keyboard.kt b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/Keyboard.kt new file mode 100644 index 0000000..36e8ea8 --- /dev/null +++ b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/Keyboard.kt @@ -0,0 +1,51 @@ +package de.luh.hci.mi.atomikkeyboard + +import android.util.Log + +/** + * Represents a keyboard layout, i.e. 2D coordinates of keys. + * Allows accessing center positions and number of keys. + * The keys are arranged in 5 rows of 6 or 7 characters each. The keys are densely + * packed. The positions of the keys are their center coordinates. + * The coordinates are relative to a keyboard image of dimensions 635 (W) x 425 (H) pixels. + */ +class Keyboard { + companion object { + const val N = 4 + 6 + 7 + 6 + 4 // number of keys + const val L = 52 // center of the first key - left (x) + const val T = 58 // center of the first key - top (y) + const val W = 88 // width of a key (on the 635x425 pixel image) + const val H = 77 // height of a key (on the 635x425 pixel image) + } + + private val letters = charArrayOf( + 'b', 'k', 'd', 'g', // row 1 + 'c', 'a', 'n', 'i', 'm', 'q', // row 2 + 'f', 'l', 'e', ' ', 's', 'y', 'x', // row 3 + 'j', 'h', 't', 'o', 'p', 'v', // row 4 + 'r', 'u', 'w', 'z' // row 5 + ) + + val keys = hashMapOf() + + init { + var i = 0 + + // TODO: Implement the keyboard layout + + // row 1 + // ... + + // row 2 + // ... + + // row 3 + // ... + + // row 4 + // ... + + // row 5 + // ... + } +} \ No newline at end of file diff --git a/app/src/main/java/de/luh/hci/mi/atomikkeyboard/KeyboardView.kt b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/KeyboardView.kt new file mode 100644 index 0000000..18d6eca --- /dev/null +++ b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/KeyboardView.kt @@ -0,0 +1,49 @@ +package de.luh.hci.mi.atomikkeyboard + +import android.content.Context +import android.util.AttributeSet +import android.util.Log +import android.view.MotionEvent +import androidx.appcompat.widget.AppCompatImageView + +class KeyboardView(context: Context, attrs: AttributeSet?) : AppCompatImageView(context, attrs) { + private var scale = 0.0 + + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + scale = 635.0 / w // original atomik_keyboard.png has a width of 635 pixels + Log.d("AtomikKeyboard", "width = $w, height = $h, scale = $scale") + super.onSizeChanged(w, h, oldw, oldh) + } + + override fun onTouchEvent(event: MotionEvent): Boolean { + val t = System.currentTimeMillis() + val x = event.x * scale // convert to keyboard coordinate system + val y = event.y * scale // convert to keyboard coordinate system + Log.d("AtomikKeyboard", "touch: $x, $y") + val k = getKey(x, y) + if (k != null) { + Log.d("AtomikKeyboard", "key: " + k.letter) + val activity = context as MainActivity + activity.keyPressed(k, x, y, t); + } else { + Log.d("AtomikKeyboard", "key: ") + } + return super.onTouchEvent(event) + } + + private fun getKey(x: Double, y: Double): Key? { + val activity = context as MainActivity + var keyMin: Key? = null + var ddMin = Double.MAX_VALUE + for (key in activity.keyboard.keys.values) { + val dx = key.x - x + val dy = key.y - y + val dd = dx * dx + dy * dy // squared distance + if (dd < ddMin) { + keyMin = key + ddMin = dd + } + } + return keyMin + } +} \ No newline at end of file diff --git a/app/src/main/java/de/luh/hci/mi/atomikkeyboard/MainActivity.kt b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/MainActivity.kt new file mode 100644 index 0000000..afa2fd9 --- /dev/null +++ b/app/src/main/java/de/luh/hci/mi/atomikkeyboard/MainActivity.kt @@ -0,0 +1,193 @@ +package de.luh.hci.mi.atomikkeyboard + +import android.Manifest +import android.content.pm.PackageManager +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.os.Environment +import android.util.Log +import android.view.Menu +import android.view.View +import android.widget.Button +import android.widget.TextView +import java.io.File +import java.io.FileWriter +import java.io.IOException +import java.io.PrintWriter +import java.lang.Integer.min + +class MainActivity : AppCompatActivity() { + private var textInput: TextView? = null + private var textOutput: TextView? = null + private var nextButton: Button? = null + private var currentSentence = 0 + private var currentIndex = 0 + val keyboard = Keyboard() + private var logFile: File? = null + private var logOut: PrintWriter? = null + private val REQUEST_CODE_PERMISSION = 123 + + private val sentences = arrayOf( + "the quick brown fox jumps", + "my lazy dog sleeps well", + "east west north south", + "up down left right" + ) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + textInput = findViewById(R.id.textinput) as TextView + textOutput = findViewById(R.id.textoutput) as TextView + nextButton = findViewById(R.id.nextbutton) as Button + nextButton!!.setOnClickListener { _: View -> + logErrors() + textInput!!.text = "" + currentSentence++ + if (currentSentence >= sentences.size) currentSentence = 0 + textOutput!!.text = sentences[currentSentence] + currentIndex = 0 + } + textOutput!!.text = sentences[currentSentence] + requestPermissions( + arrayOf( + Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.READ_EXTERNAL_STORAGE + ), REQUEST_CODE_PERMISSION + ) + } + + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (requestCode == REQUEST_CODE_PERMISSION) { + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // permission was granted + if (isExternalStorageWritable()) { + logFile = createLogFile("log" + System.currentTimeMillis() + ".txt") + try { + logOut = PrintWriter(FileWriter(logFile)) + logOut?.let { + // header row, values separated by ; + it.println("sentence.letter;sentence.x;sentence.y;tap.x;tap.y;timestamp;key.letter;key.x;key.y;sentence;sentenceIndex;letterIndex") + it.flush() + } + } catch (ex: IOException) { + Log.e("AtomikKeyboard", ex.toString()) + } + } else { + Log.e("AtomikKeyboard", "External storage not writeable.") + } + } else { + // permission denied + Log.e("AtomikKeyboard", "You have to grant the permission!") + } + } + } + + fun keyPressed(key: Key, x: Double, y: Double, t: Long) { + val s = textInput!!.text.toString() + textInput!!.text = s + key.letter + saveInput(key, x, y, t) + currentIndex++ + val n = sentences[currentSentence].length + if (currentIndex >= n) currentIndex = n - 1 + } + + public fun array2dOfInt(sizeOuter: Int, sizeInner: Int): Array = + Array(sizeOuter) { IntArray(sizeInner) } + + private fun min(a: Int, b: Int, c: Int): Int { + return if (a < b) { + if (a < c) a else c + } else { + if (b < c) b else c + } + } + + private fun editDistance(src: String, dst: String): Int { + val s = src.length + var t = dst.length + var d = Array(s + 1) { IntArray(t + 1) } + for (i in 0..s) { + for (j in 0..t) { + if (i == 0) d[i][j] = j // special case row 0 + else if (j == 0) d[i][j] = i // special case column 0 + else { // assert: i > 0 && j > 0 + val del = d[i - 1][j] + 1 + val ins = d[i][j - 1] + 1 + val cop_rep = d[i - 1][j - 1] + (if (src[i - 1] == dst[j - 1]) 0 else 1) + d[i][j] = min(del, ins, cop_rep) + } + } + } + return d[s][t]; + } + + // Logs the error value for the current sentence. Called on each press of the next button. + private fun logErrors() { + val enteredSentence = textInput!!.text as String + val e = editDistance(sentences[currentSentence], enteredSentence) + logOut?.let { + it.printf( + "ERRORS;%s;%d;%s;%d\n", + sentences[currentSentence], + currentSentence, + enteredSentence, + e + ) + it.flush() + } + } + + // Saves the given input event as a line in the log. + private fun saveInput(key: Key, tapX: Double, tapY: Double, timestamp: Long) { + val sentenceLetter = sentences[currentSentence][currentIndex] + val sentenceKey = keyboard.keys[sentenceLetter] + if (logOut != null) { + // save key input event in this format: + // sentenceKey.letter;sentenceKey.x;sentenceKey.y;tap.x;tap.y;timestamp;key.letter;key.x;key.y + // attention: sentenceKey and/or key may be null + val sentenceX: Double = sentenceKey?.x ?: 0.0 + val sentenceY: Double = sentenceKey?.y ?: 0.0 + logOut!!.printf( + "%c;%f;%f;%f;%f;%d;%c;%f;%f;%s;%d;%d\n", + sentenceLetter, sentenceX, sentenceY, + tapX, tapY, timestamp, + key.letter, key.x, key.y, + sentences[currentSentence], currentSentence, currentIndex + ) + logOut!!.flush() + } + } + + // Checks if external storage is available for reading and writing. + private fun isExternalStorageWritable(): Boolean { + val state = Environment.getExternalStorageState() + if (Environment.MEDIA_MOUNTED == state) { + return true + } + Log.e("AtomikKeyboard", "External storage not mounted") + return false + } + + // Gets (create if necessary) the storage directory on external storage. + private fun createLogFile(fileName: String): File? { + val path = File( + Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_DOWNLOADS + ), "myatomikkeyboard" + ) + path.mkdirs() + Log.d("AtomikKeyboard", "path = " + path.absolutePath) + // path = /storage/emulated/0/Download/myatomikkeyboard + // View --> Tool Windows --> Device File Explorer --> (select device) --> sdcard --> Download --> myatomikkeyboard + if (!path.isDirectory) { + Log.e("MainActivity ", "Directory could not be created") + return null + } + return File(path, fileName) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/atomik_keyboard.png b/app/src/main/res/drawable-v24/atomik_keyboard.png new file mode 100644 index 0000000..6c74bde Binary files /dev/null and b/app/src/main/res/drawable-v24/atomik_keyboard.png differ diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..7dcb29f --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,54 @@ + + + + + + + + + +