mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-10-21 14:38:19 +00:00
Compare commits
4 Commits
master
...
albertvaka
Author | SHA1 | Date | |
---|---|---|---|
|
6bae0d7503 | ||
|
f27fdbf52e | ||
|
0c14b3fb1a | ||
|
f4a0c53ca0 |
@@ -25,15 +25,14 @@ import java.util.UUID
|
||||
import kotlin.text.Charsets.UTF_8
|
||||
|
||||
class BluetoothLink(
|
||||
context: Context?,
|
||||
connection: ConnectionMultiplexer,
|
||||
context: Context,
|
||||
private val connection: ConnectionMultiplexer,
|
||||
val input: InputStream,
|
||||
val output: OutputStream,
|
||||
val remoteAddress: BluetoothDevice,
|
||||
val theDeviceInfo: DeviceInfo,
|
||||
val linkProvider: BluetoothLinkProvider
|
||||
) : BaseLink(context!!, linkProvider) {
|
||||
private val connection: ConnectionMultiplexer? = connection
|
||||
) : BaseLink(context, linkProvider) {
|
||||
private var continueAccepting = true
|
||||
private val receivingThread = Thread(object : Runnable {
|
||||
override fun run() {
|
||||
@@ -99,13 +98,10 @@ class BluetoothLink(
|
||||
}
|
||||
|
||||
override fun disconnect() {
|
||||
if (connection == null) {
|
||||
return
|
||||
}
|
||||
continueAccepting = false
|
||||
try {
|
||||
connection.close()
|
||||
} catch (ignored: IOException) {
|
||||
} catch (_: IOException) {
|
||||
}
|
||||
linkProvider.disconnectedLink(this, remoteAddress)
|
||||
}
|
||||
@@ -124,7 +120,7 @@ class BluetoothLink(
|
||||
return try {
|
||||
var transferUuid: UUID? = null
|
||||
if (np.hasPayload()) {
|
||||
transferUuid = connection!!.newChannel()
|
||||
transferUuid = connection.newChannel()
|
||||
val payloadTransferInfo = JSONObject()
|
||||
payloadTransferInfo.put("uuid", transferUuid.toString())
|
||||
np.payloadTransferInfo = payloadTransferInfo
|
||||
@@ -132,7 +128,7 @@ class BluetoothLink(
|
||||
sendMessage(np)
|
||||
if (transferUuid != null) {
|
||||
try {
|
||||
connection!!.getChannelOutputStream(transferUuid).use { payloadStream ->
|
||||
connection.getChannelOutputStream(transferUuid).use { payloadStream ->
|
||||
val BUFFER_LENGTH = 1024
|
||||
val buffer = ByteArray(BUFFER_LENGTH)
|
||||
var bytesRead: Int
|
||||
|
@@ -133,6 +133,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
override fun close() {
|
||||
flush()
|
||||
lock.withLock {
|
||||
if (!open) return
|
||||
open = false
|
||||
readBuffer.clear()
|
||||
lockCondition.signalAll()
|
||||
@@ -158,7 +159,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
if (freeWriteAmount == 0) {
|
||||
try {
|
||||
lockCondition.await()
|
||||
} catch (ignored: Exception) {
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
} else {
|
||||
break
|
||||
@@ -177,9 +178,9 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
private var socket: BluetoothSocket?
|
||||
private val socket: BluetoothSocket
|
||||
private val channels: MutableMap<UUID, Channel> = HashMap()
|
||||
private val lock = ReentrantLock()
|
||||
private val channelsLock = ReentrantLock()
|
||||
private var open = true
|
||||
private var receivedProtocolVersion = false
|
||||
|
||||
@@ -199,27 +200,27 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
message.position(19)
|
||||
message.putShort(1.toShort())
|
||||
message.putShort(1.toShort())
|
||||
socket!!.outputStream.write(data)
|
||||
socket.outputStream.write(data)
|
||||
}
|
||||
|
||||
private fun handleException(@Suppress("UNUSED_PARAMETER") ignored: Exception) {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
open = false
|
||||
for (channel in channels.values) {
|
||||
channel.doClose()
|
||||
}
|
||||
channels.clear()
|
||||
if (socket != null && socket!!.isConnected) {
|
||||
if (socket.isConnected) {
|
||||
try {
|
||||
socket!!.close()
|
||||
} catch (ignored: IOException) {
|
||||
socket.close()
|
||||
} catch (_: IOException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun closeChannel(id: UUID) {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
if (channels.containsKey(id)) {
|
||||
channels.remove(id)
|
||||
val data = ByteArray(19)
|
||||
@@ -230,7 +231,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
message.putLong(id.mostSignificantBits)
|
||||
message.putLong(id.leastSignificantBits)
|
||||
try {
|
||||
socket!!.outputStream.write(data)
|
||||
socket.outputStream.write(data)
|
||||
} catch (e: IOException) {
|
||||
handleException(e)
|
||||
}
|
||||
@@ -239,7 +240,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
}
|
||||
|
||||
private fun readRequest(id: UUID) {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[id] ?: return
|
||||
val data = ByteArray(21)
|
||||
channel.lock.withLock {
|
||||
@@ -254,7 +255,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
message.putShort(amount.toShort())
|
||||
channel.requestedReadAmount += amount
|
||||
try {
|
||||
socket!!.outputStream.write(data)
|
||||
socket.outputStream.write(data)
|
||||
} catch (e: IOException) {
|
||||
handleException(e)
|
||||
} catch (e: NullPointerException) {
|
||||
@@ -267,7 +268,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun writeRequest(id: UUID, writeData: ByteArray, off: Int, writeLen: Int): Int {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[id] ?: return 0
|
||||
val data = ByteArray(19 + BUFFER_SIZE)
|
||||
var length: Int
|
||||
@@ -296,7 +297,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
channel.lockCondition.signalAll()
|
||||
}
|
||||
try {
|
||||
socket!!.outputStream.write(data, 0, 19 + length)
|
||||
socket.outputStream.write(data, 0, 19 + length)
|
||||
} catch (e: IOException) {
|
||||
handleException(e)
|
||||
}
|
||||
@@ -306,29 +307,27 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun flush() {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
if (!open) return
|
||||
socket!!.outputStream.flush()
|
||||
socket.outputStream.flush()
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun close() {
|
||||
if (socket == null) {
|
||||
return
|
||||
channelsLock.withLock {
|
||||
socket.close()
|
||||
for (channel in channels.values) {
|
||||
channel.doClose()
|
||||
}
|
||||
channels.clear()
|
||||
}
|
||||
socket!!.close()
|
||||
socket = null
|
||||
for (channel in channels.values) {
|
||||
channel.doClose()
|
||||
}
|
||||
channels.clear()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun newChannel(): UUID {
|
||||
val id = UUID.randomUUID()
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val data = ByteArray(19)
|
||||
val message = ByteBuffer.wrap(data)
|
||||
message.order(ByteOrder.BIG_ENDIAN)
|
||||
@@ -337,7 +336,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
message.putLong(id.mostSignificantBits)
|
||||
message.putLong(id.leastSignificantBits)
|
||||
try {
|
||||
socket!!.outputStream.write(data)
|
||||
socket.outputStream.write(data)
|
||||
} catch (e: IOException) {
|
||||
handleException(e)
|
||||
throw e
|
||||
@@ -357,7 +356,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getChannelInputStream(id: UUID): InputStream {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[id] ?: throw IOException("Invalid channel!")
|
||||
return ChannelInputStream(channel)
|
||||
}
|
||||
@@ -365,7 +364,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getChannelOutputStream(id: UUID): OutputStream {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[id] ?: throw IOException("Invalid channel!")
|
||||
return ChannelOutputStream(channel)
|
||||
}
|
||||
@@ -416,12 +415,12 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
}
|
||||
when (type) {
|
||||
MESSAGE_OPEN_CHANNEL -> {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
channels.put(channelId, Channel(this@ConnectionMultiplexer, channelId))
|
||||
}
|
||||
}
|
||||
MESSAGE_CLOSE_CHANNEL -> {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[channelId] ?: return
|
||||
channels.remove(channelId)
|
||||
channel.doClose()
|
||||
@@ -435,7 +434,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
var amount = ByteBuffer.wrap(data, 0, 2).order(ByteOrder.BIG_ENDIAN).short.toInt()
|
||||
//signed short -> unsigned short (as int) conversion
|
||||
if (amount < 0) amount += 0x10000
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[channelId] ?: return
|
||||
channel.lock.withLock {
|
||||
channel.freeWriteAmount += amount
|
||||
@@ -448,7 +447,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
throw IOException("Message length is bigger than read size!")
|
||||
}
|
||||
readBuffer(data, length)
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
val channel = channels[channelId] ?: return
|
||||
channel.lock.withLock {
|
||||
if (channel.requestedReadAmount < length) {
|
||||
@@ -495,7 +494,7 @@ class ConnectionMultiplexer(socket: BluetoothSocket) : Closeable {
|
||||
|
||||
override fun run() {
|
||||
while (true) {
|
||||
lock.withLock {
|
||||
channelsLock.withLock {
|
||||
if (!open) {
|
||||
Log.w("ConnectionMultiplexer", "connection not open, returning")
|
||||
return
|
||||
|
Reference in New Issue
Block a user