From 3a3e686e4c9f6f2c95b236e74a66896eba961c21 Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Wed, 21 Jul 2021 03:01:59 +0800 Subject: [PATCH] Add detailed settings screen Update dependencies. --- app/build.gradle | 11 +- app/src/main/AndroidManifest.xml | 1 + .../brokenithm/BrokenithmApplication.kt | 138 ++++++------ .../brokenithm/activity/MainActivity.kt | 209 ++++++++++-------- .../brokenithm/activity/SettingsActivity.kt | 14 ++ .../brokenithm/fragment/SettingsFragment.kt | 19 ++ app/src/main/res/layout/activity_main.xml | 38 ++-- app/src/main/res/layout/activity_settings.xml | 41 ++++ app/src/main/res/values/strings.xml | 10 + app/src/main/res/xml/preferences.xml | 55 +++++ build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 12 files changed, 351 insertions(+), 191 deletions(-) create mode 100644 app/src/main/java/com/github/brokenithm/activity/SettingsActivity.kt create mode 100644 app/src/main/java/com/github/brokenithm/fragment/SettingsFragment.kt create mode 100644 app/src/main/res/layout/activity_settings.xml create mode 100644 app/src/main/res/xml/preferences.xml diff --git a/app/build.gradle b/app/build.gradle index a17f6d6..fb5d676 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,14 +35,15 @@ android { dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.core:core-ktx:1.3.2' - implementation 'androidx.appcompat:appcompat:1.2.0' - implementation 'com.google.android.material:material:1.3.0' + implementation 'androidx.core:core-ktx:1.6.0' + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.2' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'net.cachapa.expandablelayout:expandablelayout:2.9.2' + implementation 'androidx.preference:preference-ktx:1.1.1' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 63baf77..b7dc07c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,6 +27,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/com/github/brokenithm/BrokenithmApplication.kt b/app/src/main/java/com/github/brokenithm/BrokenithmApplication.kt index a5c9925..111c3b5 100644 --- a/app/src/main/java/com/github/brokenithm/BrokenithmApplication.kt +++ b/app/src/main/java/com/github/brokenithm/BrokenithmApplication.kt @@ -1,77 +1,89 @@ package com.github.brokenithm import android.app.Application +import android.content.Context +import android.content.SharedPreferences class BrokenithmApplication : Application() { - var lastServer: String - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getString("server", "") ?: "" - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putString("server", value).apply() - } - var enableAir: Boolean - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getBoolean("enable_air", true) - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putBoolean("enable_air", value).apply() - } + abstract class BasePreference(context: Context, fileName: String) { + protected val config: SharedPreferences = context.getSharedPreferences(fileName, MODE_PRIVATE) + abstract fun value(): T + abstract fun update(value: T) + } - var airSource: Int - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getInt("air_source", 3) - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putInt("air_source", value).apply() - } + abstract class Settings(context: Context) : BasePreference(context, settings_preference) - var simpleAir: Boolean - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getBoolean("simple_air", false) - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putBoolean("simple_air", value).apply() - } + open class StringPreference( + context: Context, + private val key: String, + private val defValue: String + ) : Settings(context) { + override fun value() = config.getString(key, defValue) ?: defValue + override fun update(value: String) = config.edit().putString(key, value).apply() + } - var showDelay: Boolean - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getBoolean("show_delay", false) - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putBoolean("show_delay", value).apply() - } + open class BooleanPreference( + context: Context, + private val key: String, + private val defValue: Boolean + ) : Settings(context) { + override fun value() = config.getBoolean(key, defValue) + override fun update(value: Boolean) = config.edit().putBoolean(key, value).apply() + } - var enableVibrate: Boolean - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getBoolean("enable_vibrate", true) - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putBoolean("enable_vibrate", value).apply() - } + open class IntegerPreference( + context: Context, + private val key: String, + private val defValue: Int + ) : Settings(context) { + override fun value() = config.getInt(key, defValue) + override fun update(value: Int) = config.edit().putInt(key, value).apply() + } - var tcpMode: Boolean - get() { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - return config.getBoolean("tcp_mode", false) - } - set(value) { - val config = getSharedPreferences(settings_preference, MODE_PRIVATE) - config.edit().putBoolean("tcp_mode", value).apply() - } + open class FloatPreference( + context: Context, + private val key: String, + private val defValue: Float + ) : Settings(context) { + override fun value() = config.getString(key, defValue.toString())?.toFloat() ?: defValue + override fun update(value: Float) = config.edit().putString(key, value.toString()).apply() + } + + lateinit var lastServer : StringPreference + lateinit var enableAir : BooleanPreference + lateinit var airSource : IntegerPreference + lateinit var simpleAir : BooleanPreference + lateinit var showDelay : BooleanPreference + lateinit var enableVibrate : BooleanPreference + lateinit var tcpMode : BooleanPreference + lateinit var enableNFC : BooleanPreference + lateinit var wideTouchRange : BooleanPreference + lateinit var enableTouchSize : BooleanPreference + lateinit var fatTouchThreshold : FloatPreference + lateinit var extraFatTouchThreshold : FloatPreference + lateinit var gyroAirLowestBound : FloatPreference + lateinit var gyroAirHighestBound : FloatPreference + lateinit var accelAirThreshold : FloatPreference + + override fun onCreate() { + super.onCreate() + lastServer = StringPreference(this, "server", "") + enableAir = BooleanPreference(this, "enable_air", true) + airSource = IntegerPreference(this, "air_source", 3) + simpleAir = BooleanPreference(this, "simple_air", false) + showDelay = BooleanPreference(this, "show_delay", false) + enableVibrate = BooleanPreference(this, "enable_vibrate", true) + tcpMode = BooleanPreference(this, "tcp_mode", false) + enableNFC = BooleanPreference(this, "enable_nfc", true) + wideTouchRange = BooleanPreference(this, "wide_touch_range", false) + enableTouchSize = BooleanPreference(this, "enable_touch_size", false) + fatTouchThreshold = FloatPreference(this, "fat_touch_threshold", 0.027f) + extraFatTouchThreshold = FloatPreference(this, "extra_fat_touch_threshold", 0.035f) + gyroAirLowestBound = FloatPreference(this, "gyro_air_lowest", 0.8f) + gyroAirHighestBound = FloatPreference(this, "gyro_air_highest", 1.35f) + accelAirThreshold = FloatPreference(this, "accel_air_threshold", 2f) + } companion object { private const val settings_preference = "settings" diff --git a/app/src/main/java/com/github/brokenithm/activity/MainActivity.kt b/app/src/main/java/com/github/brokenithm/activity/MainActivity.kt index 45d2c61..8d47f0e 100644 --- a/app/src/main/java/com/github/brokenithm/activity/MainActivity.kt +++ b/app/src/main/java/com/github/brokenithm/activity/MainActivity.kt @@ -48,8 +48,9 @@ class MainActivity : AppCompatActivity() { private val numOfGaps = 16 private val buttonWidthToGap = 7.428571f private val numOfAirBlock = 6 - private val fatTouchSizeThreshold = 0.027f - private val extraFatTouchSizeThreshold = 0.035f + private var mEnableTouchSize = false + private var mFatTouchSizeThreshold = 0.027f + private var mExtraFatTouchSizeThreshold = 0.035f private var mCurrentDelay = 0f // Buttons @@ -68,6 +69,7 @@ class MainActivity : AppCompatActivity() { private lateinit var mButtonRenderer: View // vibrator + private var mEnableVibrate = true private lateinit var vibrator: Vibrator private lateinit var vibratorTask: AsyncTaskUtil.AsyncTask private lateinit var vibrateMethod: (Long) -> Unit @@ -80,7 +82,6 @@ class MainActivity : AppCompatActivity() { private var mSimpleAir = false private var mDebugInfo = false private var mShowDelay = false - private var mEnableVibrate = true private lateinit var mDelayText: TextView private var windowWidth = 0f private var windowHeight = 0f @@ -89,9 +90,10 @@ class MainActivity : AppCompatActivity() { // sensor private var mSensorManager: SensorManager? = null private var mSensorCallback: ((Float) -> Unit)? = null + private var mGyroLowestBound = 0.8f + private var mGyroHighestBound = 1.35f + private var mAccelThreshold = 2f private val listener = object : SensorEventListener { - - val threshold = 2f var current = 0 var lastAcceleration = 0f @@ -101,6 +103,7 @@ class MainActivity : AppCompatActivity() { override fun onSensorChanged(event: SensorEvent?) { event ?: return + val threshold = mAccelThreshold when (event.sensor.type) { Sensor.TYPE_LINEAR_ACCELERATION -> { if (mAirSource != 2) @@ -165,7 +168,7 @@ class MainActivity : AppCompatActivity() { } private var adapter: NfcAdapter? = null private val mAimeKey = byteArrayOf(0x57, 0x43, 0x43, 0x46, 0x76, 0x32) - private var enableNFC = true + private var mEnableNFC = true private var hasCard = false private var cardType = CardType.CARD_AIME private val cardId = ByteArray(10) @@ -183,7 +186,7 @@ class MainActivity : AppCompatActivity() { cardId[9] = 0 cardType = CardType.CARD_FELICA hasCard = true - Log.d(TAG, "found felica card: ${cardId.toHexString().removeRange(16..19)}") + Log.d(TAG, "Found FeliCa card: ${cardId.toHexString().removeRange(16..19)}") while (felica.isConnected) Thread.sleep(50) hasCard = false felica.close() @@ -203,11 +206,11 @@ class MainActivity : AppCompatActivity() { block.copyInto(cardId, 0, 6, 16) cardType = CardType.CARD_AIME hasCard = true - Log.d(TAG, "found aime card: ${cardId.toHexString()}") + Log.d(TAG, "Found Aime card: ${cardId.toHexString()}") while (mifare.isConnected) Thread.sleep(50) hasCard = false } else { - Log.d(TAG, "nfc auth failed") + Log.d(TAG, "NFC auth failed") } mifare.close() } catch (e: Exception) { @@ -236,18 +239,13 @@ class MainActivity : AppCompatActivity() { } } + val settings = findViewById