From 021d775835d6711ee914b796046a999c1b2c65e7 Mon Sep 17 00:00:00 2001 From: Markus Fisch Date: Wed, 15 Mar 2023 20:29:44 +0100 Subject: [PATCH] Clean up new additions Fix indent, apply auto format, remove commented out code, etc. --- app/src/main/AndroidManifest.xml | 4 +- .../binaryeye/activity/CameraActivity.kt | 5 +- .../android/binaryeye/app/Permissions.kt | 3 +- .../binaryeye/bluetooth/BluetoothSender.kt | 155 +++++++++--------- .../binaryeye/fragment/PreferencesFragment.kt | 12 +- .../binaryeye/preference/Preferences.kt | 10 +- app/src/main/res/values/bluetooth.xml | 8 + app/src/main/res/values/strings.xml | 6 - 8 files changed, 101 insertions(+), 102 deletions(-) create mode 100644 app/src/main/res/values/bluetooth.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3481445d..e953cfcb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,8 +6,8 @@ android:xlargeScreens="true"/> - - + + diff --git a/app/src/main/kotlin/de/markusfisch/android/binaryeye/app/Permissions.kt b/app/src/main/kotlin/de/markusfisch/android/binaryeye/app/Permissions.kt index c8c1147f..8a969803 100644 --- a/app/src/main/kotlin/de/markusfisch/android/binaryeye/app/Permissions.kt +++ b/app/src/main/kotlin/de/markusfisch/android/binaryeye/app/Permissions.kt @@ -33,14 +33,13 @@ fun Activity.hasLocationPermission(callback: () -> Any): Boolean { } const val PERMISSION_BLUETOOTH = 4 -fun Activity.hasBluetoothPermission() :Boolean { +fun Activity.hasBluetoothPermission(): Boolean { return hasPermission( Manifest.permission.BLUETOOTH_CONNECT, PERMISSION_BLUETOOTH ) } - private fun Activity.hasPermission( permission: String, requestCode: Int diff --git a/app/src/main/kotlin/de/markusfisch/android/binaryeye/bluetooth/BluetoothSender.kt b/app/src/main/kotlin/de/markusfisch/android/binaryeye/bluetooth/BluetoothSender.kt index f9e94981..c7e02428 100644 --- a/app/src/main/kotlin/de/markusfisch/android/binaryeye/bluetooth/BluetoothSender.kt +++ b/app/src/main/kotlin/de/markusfisch/android/binaryeye/bluetooth/BluetoothSender.kt @@ -13,102 +13,97 @@ import java.io.OutputStreamWriter import java.util.* fun Scan.sendBluetoothAsync( - host : String, - callback: (Boolean, Boolean) -> Unit + host: String, + callback: (Boolean, Boolean) -> Unit ) { - CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) - { - var connectResponse = true - var sendResponse = false + CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) { + var connectResponse = true + var sendResponse = false - if (!BluetoothSender.isConnected()) - connectResponse = BluetoothSender.connect(host) + if (!BluetoothSender.isConnected()) { + connectResponse = BluetoothSender.connect(host) + } - if (connectResponse) { - sendResponse = BluetoothSender.send(content) - } + if (connectResponse) { + sendResponse = BluetoothSender.send(content) + } - withContext(Dispatchers.Main) { - callback(connectResponse, sendResponse) - } - } + withContext(Dispatchers.Main) { + callback(connectResponse, sendResponse) + } + } } fun setBluetoothHosts(listPref: ListPreference) { - val devices = BluetoothAdapter.getDefaultAdapter().bondedDevices + val devices = BluetoothAdapter.getDefaultAdapter().bondedDevices + val (entries, entryValues) = Pair( + devices.map { it.name }, + devices.map { it.address } + ) - val (entries, entryValues) = Pair(devices.map { it.name }, devices.map {it.address}) + listPref.entries = entries.toTypedArray() + listPref.entryValues = entryValues.toTypedArray() - listPref.entries = entries.toTypedArray() - listPref.entryValues = entryValues.toTypedArray() - - listPref.callChangeListener(listPref.value) + listPref.callChangeListener(listPref.value) } +private class BluetoothSender { + companion object { + lateinit private var socket: BluetoothSocket + lateinit private var writer: OutputStreamWriter -private class BluetoothSender -{ - companion object { - //non-deprecated call requires api lvl 18 - private val blue: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter() + private val blue = BluetoothAdapter.getDefaultAdapter() + private val uuid = UUID.fromString( + "8a8478c9-2ca8-404b-a0de-101f34ab71ae" + ) - lateinit private var socket: BluetoothSocket - lateinit private var writer: OutputStreamWriter - private val uuid = UUID.fromString("8a8478c9-2ca8-404b-a0de-101f34ab71ae") - //using socket builtin function require api lvl 14 - private var isConnected = false + private var isConnected = false - fun connect(deviceName: String) : Boolean { - try { - val device = findByName(deviceName) - socket = device!!.createRfcommSocketToServiceRecord(uuid) - socket.connect() - isConnected = true - writer = socket.outputStream.writer() - } catch(e: Exception) { - close() - return false - } + fun connect(deviceName: String): Boolean { + try { + val device = findByName(deviceName) + socket = device!!.createRfcommSocketToServiceRecord(uuid) + socket.connect() + isConnected = true + writer = socket.outputStream.writer() + } catch (e: Exception) { + close() + return false + } - return true - } + return true + } - fun send(message: String) : Boolean { - return try { - writer.write(message) - writer.write("\n") - writer.flush() - true - } catch(e: Exception) { - close() - false - } - } + fun send(message: String): Boolean { + return try { + writer.write(message) + writer.write("\n") + writer.flush() + true + } catch (e: Exception) { + close() + false + } + } - fun close() { - writer.close() - socket.close() - isConnected = false - } + fun close() { + writer.close() + socket.close() + isConnected = false + } - fun isConnected() : Boolean { - return isConnected - //return socket.isConnected -// if(socket != null) -// return true -// else -// return false - } + fun isConnected(): Boolean { + return isConnected + } - private fun findByName(findableName : String) : BluetoothDevice? - { - val deviceList = blue.bondedDevices - for(device in deviceList) - { - if (device.address == findableName) - return device - } - return null - } - } -} \ No newline at end of file + private fun findByName(findableName: String): BluetoothDevice? { + val deviceList = blue.bondedDevices + for (device in deviceList) { + if (device.address == findableName) { + return device + } + } + return null + } + } +} diff --git a/app/src/main/kotlin/de/markusfisch/android/binaryeye/fragment/PreferencesFragment.kt b/app/src/main/kotlin/de/markusfisch/android/binaryeye/fragment/PreferencesFragment.kt index 4833b956..ef570762 100644 --- a/app/src/main/kotlin/de/markusfisch/android/binaryeye/fragment/PreferencesFragment.kt +++ b/app/src/main/kotlin/de/markusfisch/android/binaryeye/fragment/PreferencesFragment.kt @@ -45,8 +45,7 @@ class PreferencesFragment : PreferenceFragmentCompat() { val ac = activity ?: return if (ac.hasBluetoothPermission()) { setSummary(preference) - } - else { + } else { prefs.sendScanBluetooth = false setSummary(preference) } @@ -63,7 +62,9 @@ class PreferencesFragment : PreferenceFragmentCompat() { } private fun setBluetoothResources() { - setBluetoothHosts(findPreference("send_scan_bluetooth_host") as ListPreference) + setBluetoothHosts( + findPreference("send_scan_bluetooth_host") as ListPreference + ) } private fun wireClearNetworkPreferences() { @@ -141,12 +142,11 @@ class PreferencesFragment : PreferenceFragmentCompat() { } } else if (preference.key == "send_scan_bluetooth_host") { val ac = activity ?: return - if(ac.hasBluetoothPermission()) { + if (ac.hasBluetoothPermission()) { setBluetoothHosts(preference as ListPreference) } super.onDisplayPreferenceDialog(preference) - } - else { + } else { super.onDisplayPreferenceDialog(preference) } } diff --git a/app/src/main/kotlin/de/markusfisch/android/binaryeye/preference/Preferences.kt b/app/src/main/kotlin/de/markusfisch/android/binaryeye/preference/Preferences.kt index c5ad6ffc..21d1edc0 100644 --- a/app/src/main/kotlin/de/markusfisch/android/binaryeye/preference/Preferences.kt +++ b/app/src/main/kotlin/de/markusfisch/android/binaryeye/preference/Preferences.kt @@ -270,11 +270,11 @@ class Preferences { preferences.getString(SEND_SCAN_TYPE, sendScanType)?.also { sendScanType = it } - preferences.getBoolean( - SEND_SCAN_BLUETOOTH, - sendScanBluetooth - ) - preferences.getString(SEND_SCAN_BLUETOOTH_HOST, sendScanBluetoothHost)?.also { + preferences.getBoolean(SEND_SCAN_BLUETOOTH, sendScanBluetooth) + preferences.getString( + SEND_SCAN_BLUETOOTH_HOST, + sendScanBluetoothHost + )?.also { sendScanBluetoothHost = it } preferences.getString(CUSTOM_LOCALE, customLocale)?.also { diff --git a/app/src/main/res/values/bluetooth.xml b/app/src/main/res/values/bluetooth.xml new file mode 100644 index 00000000..9aeb59ed --- /dev/null +++ b/app/src/main/res/values/bluetooth.xml @@ -0,0 +1,8 @@ + + + @string/no_bluetooth_hosts_paired + + + @string/no_bluetooth_hosts_paired_value + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ec18508e..9b4727f7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -129,12 +129,6 @@ Host No Hosts Paired - - @string/no_bluetooth_hosts_paired - - - @string/no_bluetooth_hosts_paired_value - Could not connect to bluetooth device. Could not send to bluetooth device. Successful bluetooth send.