concurrency extension

This commit is contained in:
Milos Kozak 2020-04-02 21:46:30 +02:00
parent 295ae79d20
commit 58fe62c45a
3 changed files with 26 additions and 37 deletions

View file

@ -19,15 +19,13 @@ import info.nightscout.androidaps.plugins.pump.danaRS.activities.EnterPinActivit
import info.nightscout.androidaps.plugins.pump.danaRS.activities.PairingHelperActivity
import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRSMessageHashTable
import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet
import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet_Etc_Keep_Connection
import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet_General_Get_Pump_Check
import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption
import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPairingSuccess
import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.extensions.notify
import info.nightscout.androidaps.utils.extensions.waitMillis
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import okhttp3.internal.notify
import okhttp3.internal.waitMillis
import java.util.*
import java.util.concurrent.ScheduledFuture
import javax.inject.Inject
@ -230,7 +228,7 @@ class BLEComm @Inject internal constructor(
//aapsLogger.debug("writeCharacteristic:" + DanaRS_Packet.toHexString(data))
bluetoothGatt?.writeCharacteristic(characteristic)
}).start()
waitMillis(50)
SystemClock.sleep(50)
}
private val uartReadBTGattChar: BluetoothGattCharacteristic

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.utils
import info.nightscout.androidaps.utils.extensions.toHex
import org.spongycastle.util.encoders.Base64
import java.nio.ByteBuffer
import java.security.MessageDigest
@ -13,38 +14,6 @@ import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
private val HEX_CHARS = "0123456789abcdef"
private val HEX_CHARS_ARRAY = "0123456789abcdef".toCharArray()
fun String.hexStringToByteArray() : ByteArray {
val upperCased = this.toLowerCase()
val result = ByteArray(length / 2)
for (i in 0 until length step 2) {
val firstIndex = HEX_CHARS.indexOf(upperCased[i]);
val secondIndex = HEX_CHARS.indexOf(upperCased[i + 1]);
val octet = firstIndex.shl(4).or(secondIndex)
result.set(i.shr(1), octet.toByte())
}
return result
}
fun ByteArray.toHex() : String{
val result = StringBuffer()
forEach {
val octet = it.toInt()
val firstIndex = (octet and 0xF0).ushr(4)
val secondIndex = octet and 0x0F
result.append(HEX_CHARS_ARRAY[firstIndex])
result.append(HEX_CHARS_ARRAY[secondIndex])
}
return result.toString()
}
object CryptoUtil {
private const val IV_LENGTH_BYTE = 12

View file

@ -0,0 +1,22 @@
package info.nightscout.androidaps.utils.extensions
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
inline fun Any.wait() = (this as Object).wait()
/**
* Lock and wait a duration in milliseconds and nanos.
* Unlike [java.lang.Object.wait] this interprets 0 as "don't wait" instead of "wait forever".
*/
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
fun Any.waitMillis(timeout: Long, nanos: Int = 0) {
if (timeout > 0L || nanos > 0) {
(this as Object).wait(timeout, nanos)
}
}
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
inline fun Any.notify() = (this as Object).notify()
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
inline fun Any.notifyAll() = (this as Object).notifyAll()