0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-20 03:52:16 +02:00

Clean up new additions

Fix indent, apply auto format, remove commented out code, etc.
This commit is contained in:
Markus Fisch 2023-03-15 20:29:44 +01:00
parent 06df0c4d64
commit 021d775835
8 changed files with 101 additions and 102 deletions

View File

@ -6,8 +6,8 @@
android:xlargeScreens="true"/> android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="28"/> android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

View File

@ -664,7 +664,10 @@ fun showResult(
} }
} }
} }
if (prefs.sendScanBluetooth && prefs.sendScanBluetoothHost.isNotEmpty() && activity.hasBluetoothPermission()) { if (prefs.sendScanBluetooth &&
prefs.sendScanBluetoothHost.isNotEmpty() &&
activity.hasBluetoothPermission()
) {
scan.sendBluetoothAsync( scan.sendBluetoothAsync(
prefs.sendScanBluetoothHost prefs.sendScanBluetoothHost
) { con, send -> ) { con, send ->

View File

@ -33,14 +33,13 @@ fun Activity.hasLocationPermission(callback: () -> Any): Boolean {
} }
const val PERMISSION_BLUETOOTH = 4 const val PERMISSION_BLUETOOTH = 4
fun Activity.hasBluetoothPermission() :Boolean { fun Activity.hasBluetoothPermission(): Boolean {
return hasPermission( return hasPermission(
Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_CONNECT,
PERMISSION_BLUETOOTH PERMISSION_BLUETOOTH
) )
} }
private fun Activity.hasPermission( private fun Activity.hasPermission(
permission: String, permission: String,
requestCode: Int requestCode: Int

View File

@ -13,102 +13,97 @@ import java.io.OutputStreamWriter
import java.util.* import java.util.*
fun Scan.sendBluetoothAsync( fun Scan.sendBluetoothAsync(
host : String, host: String,
callback: (Boolean, Boolean) -> Unit callback: (Boolean, Boolean) -> Unit
) { ) {
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
{ var connectResponse = true
var connectResponse = true var sendResponse = false
var sendResponse = false
if (!BluetoothSender.isConnected()) if (!BluetoothSender.isConnected()) {
connectResponse = BluetoothSender.connect(host) connectResponse = BluetoothSender.connect(host)
}
if (connectResponse) { if (connectResponse) {
sendResponse = BluetoothSender.send(content) sendResponse = BluetoothSender.send(content)
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
callback(connectResponse, sendResponse) callback(connectResponse, sendResponse)
} }
} }
} }
fun setBluetoothHosts(listPref: ListPreference) { 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.callChangeListener(listPref.value)
listPref.entryValues = entryValues.toTypedArray()
listPref.callChangeListener(listPref.value)
} }
private class BluetoothSender {
companion object {
lateinit private var socket: BluetoothSocket
lateinit private var writer: OutputStreamWriter
private class BluetoothSender private val blue = BluetoothAdapter.getDefaultAdapter()
{ private val uuid = UUID.fromString(
companion object { "8a8478c9-2ca8-404b-a0de-101f34ab71ae"
//non-deprecated call requires api lvl 18 )
private val blue: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
lateinit private var socket: BluetoothSocket private var isConnected = false
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
fun connect(deviceName: String) : Boolean { fun connect(deviceName: String): Boolean {
try { try {
val device = findByName(deviceName) val device = findByName(deviceName)
socket = device!!.createRfcommSocketToServiceRecord(uuid) socket = device!!.createRfcommSocketToServiceRecord(uuid)
socket.connect() socket.connect()
isConnected = true isConnected = true
writer = socket.outputStream.writer() writer = socket.outputStream.writer()
} catch(e: Exception) { } catch (e: Exception) {
close() close()
return false return false
} }
return true return true
} }
fun send(message: String) : Boolean { fun send(message: String): Boolean {
return try { return try {
writer.write(message) writer.write(message)
writer.write("\n") writer.write("\n")
writer.flush() writer.flush()
true true
} catch(e: Exception) { } catch (e: Exception) {
close() close()
false false
} }
} }
fun close() { fun close() {
writer.close() writer.close()
socket.close() socket.close()
isConnected = false isConnected = false
} }
fun isConnected() : Boolean { fun isConnected(): Boolean {
return isConnected return isConnected
//return socket.isConnected }
// if(socket != null)
// return true
// else
// return false
}
private fun findByName(findableName : String) : BluetoothDevice? private fun findByName(findableName: String): BluetoothDevice? {
{ val deviceList = blue.bondedDevices
val deviceList = blue.bondedDevices for (device in deviceList) {
for(device in deviceList) if (device.address == findableName) {
{ return device
if (device.address == findableName) }
return device }
} return null
return null }
} }
} }
}

View File

@ -45,8 +45,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
val ac = activity ?: return val ac = activity ?: return
if (ac.hasBluetoothPermission()) { if (ac.hasBluetoothPermission()) {
setSummary(preference) setSummary(preference)
} } else {
else {
prefs.sendScanBluetooth = false prefs.sendScanBluetooth = false
setSummary(preference) setSummary(preference)
} }
@ -63,7 +62,9 @@ class PreferencesFragment : PreferenceFragmentCompat() {
} }
private fun setBluetoothResources() { private fun setBluetoothResources() {
setBluetoothHosts(findPreference("send_scan_bluetooth_host") as ListPreference) setBluetoothHosts(
findPreference("send_scan_bluetooth_host") as ListPreference
)
} }
private fun wireClearNetworkPreferences() { private fun wireClearNetworkPreferences() {
@ -141,12 +142,11 @@ class PreferencesFragment : PreferenceFragmentCompat() {
} }
} else if (preference.key == "send_scan_bluetooth_host") { } else if (preference.key == "send_scan_bluetooth_host") {
val ac = activity ?: return val ac = activity ?: return
if(ac.hasBluetoothPermission()) { if (ac.hasBluetoothPermission()) {
setBluetoothHosts(preference as ListPreference) setBluetoothHosts(preference as ListPreference)
} }
super.onDisplayPreferenceDialog(preference) super.onDisplayPreferenceDialog(preference)
} } else {
else {
super.onDisplayPreferenceDialog(preference) super.onDisplayPreferenceDialog(preference)
} }
} }

View File

@ -270,11 +270,11 @@ class Preferences {
preferences.getString(SEND_SCAN_TYPE, sendScanType)?.also { preferences.getString(SEND_SCAN_TYPE, sendScanType)?.also {
sendScanType = it sendScanType = it
} }
preferences.getBoolean( preferences.getBoolean(SEND_SCAN_BLUETOOTH, sendScanBluetooth)
SEND_SCAN_BLUETOOTH, preferences.getString(
sendScanBluetooth SEND_SCAN_BLUETOOTH_HOST,
) sendScanBluetoothHost
preferences.getString(SEND_SCAN_BLUETOOTH_HOST, sendScanBluetoothHost)?.also { )?.also {
sendScanBluetoothHost = it sendScanBluetoothHost = it
} }
preferences.getString(CUSTOM_LOCALE, customLocale)?.also { preferences.getString(CUSTOM_LOCALE, customLocale)?.also {

View File

@ -0,0 +1,8 @@
<resources>
<string-array name="bluetooth_host_names">
<item>@string/no_bluetooth_hosts_paired</item>
</string-array>
<string-array name="bluetooth_host_values">
<item>@string/no_bluetooth_hosts_paired_value</item>
</string-array>
</resources>

View File

@ -129,12 +129,6 @@
<string name="send_scan_bluetooth_host">Host</string> <string name="send_scan_bluetooth_host">Host</string>
<string name="no_bluetooth_hosts_paired">No Hosts Paired</string> <string name="no_bluetooth_hosts_paired">No Hosts Paired</string>
<string name="no_bluetooth_hosts_paired_value" translatable="false" /> <string name="no_bluetooth_hosts_paired_value" translatable="false" />
<string-array name="bluetooth_host_names">
<item>@string/no_bluetooth_hosts_paired</item>
</string-array>
<string-array name="bluetooth_host_values">
<item>@string/no_bluetooth_hosts_paired_value</item>
</string-array>
<string name="bluetooth_connect_fail">Could not connect to bluetooth device.</string> <string name="bluetooth_connect_fail">Could not connect to bluetooth device.</string>
<string name="bluetooth_send_fail">Could not send to bluetooth device.</string> <string name="bluetooth_send_fail">Could not send to bluetooth device.</string>
<string name="bluetooth_send_success">Successful bluetooth send.</string> <string name="bluetooth_send_success">Successful bluetooth send.</string>