Improve device model detection

This commit is contained in:
Jonas Lochmann 2022-05-16 02:00:00 +02:00
parent b10a3ab6ba
commit 193b7c6f63
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
7 changed files with 149 additions and 6 deletions

View file

@ -119,8 +119,6 @@ dependencies {
implementation 'com.google.android:flexbox:1.0.0'
implementation 'com.jaredrummler:android-device-names:1.1.7'
implementation 'com.squareup.okio:okio:2.8.0'
implementation 'org.whispersystems:curve25519-java:0.5.0'

Binary file not shown.

View file

@ -0,0 +1,38 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.data.devicename
import android.content.Context
import android.os.Build
object DeviceName {
private val selfMarketingNameLock = Object()
private var selfMarketingName: String? = null
fun getDeviceNameSync(context: Context): String {
if (selfMarketingName == null) {
synchronized(selfMarketingNameLock) {
if (selfMarketingName == null) {
selfMarketingName = getDeviceNameSync(context, Build.DEVICE, Build.MODEL) ?: Build.MODEL
}
}
}
return selfMarketingName!!
}
private fun getDeviceNameSync(context: Context, device: String, model: String): String? = DeviceNameDatabase.fromAssets(context).getDeviceMarketingName(device, model)
}

View file

@ -0,0 +1,105 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.data.devicename
import android.content.Context
import android.util.Log
import io.timelimit.android.BuildConfig
import java.util.zip.InflaterInputStream
class DeviceNameDatabase (private val data: ByteArray) {
companion object {
private const val LOG_TAG = "DeviceNameDatabase"
fun fromAssets(context: Context): DeviceNameDatabase {
val data = context.assets.open("device-names.bin").use { stream ->
InflaterInputStream(stream).use { decompressed ->
decompressed.readBytes()
}
}
return DeviceNameDatabase(data)
}
}
private val stringBufferLength = read4(0)
private val deviceCounter = read4(4)
private val deviceDataStartIndex = stringBufferLength + 8
private fun read1(index: Int): Int = data[index].toInt() and 0xff
private fun read3(index: Int) = (read1(index) shl 16) or
(read1(index + 1) shl 8) or
read1(index + 2)
private fun read4(index: Int) = (read1(index) shl 24) or
(read1(index + 1) shl 16) or
(read1(index + 2) shl 8) or
read1(index + 3)
private fun readString(index: Int): String {
return String(data, read3(index) + 8, read1(index + 3), Charsets.UTF_8)
}
private fun getDeviceName(index: Int): String = readString(deviceDataStartIndex + index * 12 + 0)
private fun getDeviceModel(index: Int): String = readString(deviceDataStartIndex + index * 12 + 4)
private fun getDeviceMarketingName(index: Int): String = readString(deviceDataStartIndex + index * 12 + 8)
fun getDeviceMarketingName(device: String, model: String): String? {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "search ($device, $model)")
}
var low = 0
var high = deviceCounter - 1
while (low <= high) {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "low = $low; high = $high")
}
val index = low + (high - low) / 2
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "compare with (${getDeviceName(index)}, ${getDeviceModel(index)}, ${getDeviceMarketingName(index)})")
}
val cmp1 = getDeviceName(index).compareTo(device)
if (cmp1 < 0) low = index + 1
else if (cmp1 > 0) high = index - 1
else {
val cmp2 = getDeviceModel(index).compareTo(model)
if (cmp2 < 0) low = index + 1
else if (cmp2 > 0) high = index - 1
else {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "got match")
}
return getDeviceMarketingName(index)
}
}
}
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "nothing found")
}
return null
}
}

View file

@ -16,7 +16,6 @@
package io.timelimit.android.logic
import android.content.Context
import com.jaredrummler.android.device.DeviceName
import io.timelimit.android.R
import io.timelimit.android.async.Threads
import io.timelimit.android.coroutines.executeAndWait
@ -25,6 +24,7 @@ import io.timelimit.android.crypto.PasswordHashing
import io.timelimit.android.data.IdGenerator
import io.timelimit.android.data.backup.DatabaseBackup
import io.timelimit.android.data.customtypes.ImmutableBitmask
import io.timelimit.android.data.devicename.DeviceName
import io.timelimit.android.data.model.*
import io.timelimit.android.integration.platform.NewPermissionStatus
import io.timelimit.android.integration.platform.ProtectionLevel
@ -69,7 +69,7 @@ class AppSetupLogic(private val appLogic: AppLogic) {
run {
// add device
val deviceName = DeviceName.getDeviceName()
val deviceName = DeviceName.getDeviceNameSync(appLogic.context)
val device = Device(
id = ownDeviceId,

View file

@ -64,8 +64,6 @@
(<a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>)
\nAndroid X Paging Library
(<a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>)
\n<a href="https://github.com/jaredrummler/AndroidDeviceNames">Android Device Names</a>
(<a href="https://github.com/jaredrummler/AndroidDeviceNames/blob/master/LICENSE.txt">Apache License, Version 2.0</a>)
\n<a href="https://github.com/JakeWharton/ThreeTenABP">Three Ten Android Backport</a>
(<a href="https://github.com/JakeWharton/ThreeTenABP/blob/master/LICENSE.txt">Apache License, Version 2.0</a>)
\n<a href="https://github.com/jeremyh/jBCrypt">JBcrypt</a>

4
contrib/import-device-db.sh Executable file
View file

@ -0,0 +1,4 @@
#! /bin/sh
set -ex
cd "$(dirname $0)"
cp ../../timelimit-android/app/src/main/assets/device-names.bin ../app/src/main/assets/device-names.bin