VPN Hotspot 2.0: Client+ (#39)
Fix #13, #38. I don't have a lot of confidence that this would work very well for every device. Also here's an SQL command that hopefully somebody could make into the app for me: `SELECT TrafficRecord.mac, SUM(TrafficRecord.sentPackets), SUM(TrafficRecord.sentBytes), SUM(TrafficRecord.receivedPackets), SUM(TrafficRecord.receivedBytes) FROM TrafficRecord LEFT JOIN TrafficRecord AS Next ON TrafficRecord.id = Next.previousId WHERE Next.id IS NULL GROUP BY TrafficRecord.mac;`
This commit is contained in:
53
mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt
Normal file
53
mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package be.mygod.vpnhotspot.room
|
||||
|
||||
import android.os.Parcel
|
||||
import android.text.TextUtils
|
||||
import androidx.room.TypeConverter
|
||||
import java.net.InetAddress
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
class Converters {
|
||||
@TypeConverter
|
||||
fun persistCharSequence(cs: CharSequence): ByteArray {
|
||||
val p = Parcel.obtain()
|
||||
try {
|
||||
TextUtils.writeToParcel(cs, p, 0)
|
||||
return p.marshall()
|
||||
} finally {
|
||||
p.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun unpersistCharSequence(data: ByteArray): CharSequence {
|
||||
val p = Parcel.obtain()
|
||||
try {
|
||||
p.unmarshall(data, 0, data.size)
|
||||
p.setDataPosition(0)
|
||||
return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p)
|
||||
} finally {
|
||||
p.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun persistInetAddress(address: InetAddress): ByteArray = address.address
|
||||
|
||||
@TypeConverter
|
||||
fun unpersistInetAddress(data: ByteArray): InetAddress = InetAddress.getByAddress(data)
|
||||
}
|
||||
|
||||
fun String.macToLong(): Long = ByteBuffer.allocate(8).run {
|
||||
order(ByteOrder.LITTLE_ENDIAN)
|
||||
mark()
|
||||
put(split(':').map { Integer.parseInt(it, 16).toByte() }.toByteArray())
|
||||
reset()
|
||||
long
|
||||
}
|
||||
|
||||
fun Long.macToString(): String = ByteBuffer.allocate(8).run {
|
||||
order(ByteOrder.LITTLE_ENDIAN)
|
||||
putLong(this@macToString)
|
||||
array().take(6).joinToString(":") { "%02x".format(it) }
|
||||
}
|
||||
Reference in New Issue
Block a user