0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-19 19:42:18 +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"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<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(
prefs.sendScanBluetoothHost
) { con, send ->

View File

@ -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

View File

@ -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
}
}
}
private fun findByName(findableName: String): BluetoothDevice? {
val deviceList = blue.bondedDevices
for (device in deviceList) {
if (device.address == findableName) {
return device
}
}
return null
}
}
}

View File

@ -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)
}
}

View File

@ -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 {

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="no_bluetooth_hosts_paired">No Hosts Paired</string>
<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_send_fail">Could not send to bluetooth device.</string>
<string name="bluetooth_send_success">Successful bluetooth send.</string>