From a3027812f079221ddcced0021a108bc8939cbb40 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 31 May 2020 07:42:23 +0800 Subject: [PATCH] Misc refinements --- .../mygod/vpnhotspot/manage/BluetoothTethering.kt | 2 +- .../be/mygod/vpnhotspot/net/MacAddressCompat.kt | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt index 0f9c2e0e..e74dea6a 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -94,7 +94,7 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) : return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON && try { isTetheringOn.invoke(pan) as Boolean } catch (e: InvocationTargetException) { - activeFailureCause = e + activeFailureCause = e.cause ?: e if (e.cause is SecurityException && BuildCompat.isAtLeastR()) Timber.d(e) else Timber.w(e) return null } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/MacAddressCompat.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/MacAddressCompat.kt index 14a6ec1b..a8c076a2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/MacAddressCompat.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/MacAddressCompat.kt @@ -1,8 +1,13 @@ package be.mygod.vpnhotspot.net +import android.net.MacAddress +import androidx.annotation.RequiresApi import java.nio.ByteBuffer import java.nio.ByteOrder +/** + * Compat support class for [MacAddress]. + */ inline class MacAddressCompat(val addr: Long) { companion object { private const val ETHER_ADDR_LEN = 6 @@ -23,7 +28,6 @@ inline class MacAddressCompat(val addr: Long) { return addr.joinToString(":") { "%02x".format(it) } } - @Throws(IllegalArgumentException::class) fun fromString(addr: String) = MacAddressCompat(ByteBuffer.allocate(8).run { order(ByteOrder.LITTLE_ENDIAN) mark() @@ -37,9 +41,14 @@ inline class MacAddressCompat(val addr: Long) { }) } - override fun toString() = ByteBuffer.allocate(8).run { + fun toList() = ByteBuffer.allocate(8).run { order(ByteOrder.LITTLE_ENDIAN) putLong(addr) - bytesToString(array().take(6)) + array().take(6) } + + @RequiresApi(28) + fun toPlatform() = MacAddress.fromBytes(toList().toByteArray()) + + override fun toString() = bytesToString(toList()) }