Fixes for profile manager

This commit is contained in:
Marvin W 2022-01-18 13:50:45 +01:00
parent 68e116388b
commit 2dd6b6b173
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
3 changed files with 38 additions and 25 deletions

View File

@ -90,5 +90,8 @@ object Build {
@JvmField @JvmField
var SDK_INT: Int = 0 var SDK_INT: Int = 0
@JvmField
var SECURITY_PATCH: String? = null
} }
} }

View File

@ -6,12 +6,12 @@
package org.microg.gms.profile package org.microg.gms.profile
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import org.microg.gms.settings.SettingsContract import org.microg.gms.settings.SettingsContract
import org.microg.gms.settings.SettingsContract.Profile import org.microg.gms.settings.SettingsContract.Profile
import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParser
import java.util.*
import kotlin.random.Random import kotlin.random.Random
object ProfileManager { object ProfileManager {
@ -24,18 +24,18 @@ object ProfileManager {
private fun getProfileFromSettings(context: Context) = SettingsContract.getSettings(context, Profile.getContentUri(context), arrayOf(Profile.PROFILE)) { it.getString(0) } private fun getProfileFromSettings(context: Context) = SettingsContract.getSettings(context, Profile.getContentUri(context), arrayOf(Profile.PROFILE)) { it.getString(0) }
private fun getAutoProfile(context: Context): String { private fun getAutoProfile(context: Context): String {
val profile = "${android.os.Build.DEVICE}_${android.os.Build.VERSION.SDK_INT}" val profile = "${android.os.Build.PRODUCT}_${android.os.Build.VERSION.SDK_INT}"
if (hasProfile(context, profile)) return profile if (hasProfile(context, profile)) return profile
return PROFILE_NATIVE return PROFILE_NATIVE
} }
private fun getProfileResId(context: Context, profile: String) = context.resources.getIdentifier("${context.packageName}:xml/profile_$profile", null, null) private fun getProfileResId(context: Context, profile: String) = context.resources.getIdentifier("${context.packageName}:xml/profile_$profile".toLowerCase(Locale.US), null, null)
private fun hasProfile(context: Context, profile: String): Boolean = getProfileResId(context, profile) != 0 private fun hasProfile(context: Context, profile: String): Boolean = getProfileResId(context, profile) != 0
private fun getProfileData(context: Context, profile: String, realData: Map<String, String>): Map<String, String>? { private fun getProfileData(context: Context, profile: String, realData: Map<String, String>): Map<String, String> {
try { try {
if (profile in listOf(PROFILE_REAL, PROFILE_NATIVE)) return null if (profile in listOf(PROFILE_REAL, PROFILE_NATIVE)) return realData
val profileResId = getProfileResId(context, profile) val profileResId = getProfileResId(context, profile)
if (profileResId == 0) return null if (profileResId == 0) return realData
val resultData = mutableMapOf<String, String>() val resultData = mutableMapOf<String, String>()
resultData.putAll(realData) resultData.putAll(realData)
context.resources.getXml(profileResId).use { context.resources.getXml(profileResId).use {
@ -54,13 +54,10 @@ object ProfileManager {
next = it.next() next = it.next()
} }
} }
for (entry in resultData) {
Log.d(TAG, "<data key=\"${entry.key}\" value=\"${entry.value}\" />")
}
return resultData return resultData
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, e) Log.w(TAG, e)
return null return realData
} }
} }
@ -88,13 +85,16 @@ object ProfileManager {
private fun getProfileSerialTemplate(context: Context, profile: String): String { private fun getProfileSerialTemplate(context: Context, profile: String): String {
// Native // Native
if (profile in listOf(PROFILE_REAL, PROFILE_NATIVE)) { if (profile in listOf(PROFILE_REAL, PROFILE_NATIVE)) {
return kotlin.runCatching { var candidate = try {
if (android.os.Build.VERSION.SDK_INT >= 26) { if (android.os.Build.VERSION.SDK_INT >= 26) {
android.os.Build.getSerial() android.os.Build.getSerial()
} else { } else {
null android.os.Build.SERIAL
} }
}.getOrNull()?.takeIf { it != android.os.Build.UNKNOWN } ?: android.os.Build.SERIAL } catch (e: Exception) {
android.os.Build.SERIAL
}
if (candidate != android.os.Build.UNKNOWN) return candidate
} }
// From profile // From profile
@ -160,9 +160,12 @@ object ProfileManager {
"Build.VERSION.SDK" to android.os.Build.VERSION.SDK, "Build.VERSION.SDK" to android.os.Build.VERSION.SDK,
"Build.VERSION.SDK_INT" to android.os.Build.VERSION.SDK_INT.toString() "Build.VERSION.SDK_INT" to android.os.Build.VERSION.SDK_INT.toString()
).apply { ).apply {
if (android.os.Build.VERSION.SDK_INT > 21) { if (android.os.Build.VERSION.SDK_INT >= 21) {
put("Build.SUPPORTED_ABIS", android.os.Build.SUPPORTED_ABIS.joinToString(",")) put("Build.SUPPORTED_ABIS", android.os.Build.SUPPORTED_ABIS.joinToString(","))
} }
if (android.os.Build.VERSION.SDK_INT >= 23) {
put("Build.VERSION.SECURITY_PATCH", android.os.Build.VERSION.SECURITY_PATCH)
}
} }
private fun applyProfileData(profileData: Map<String, String>) { private fun applyProfileData(profileData: Map<String, String>) {
@ -195,8 +198,15 @@ object ProfileManager {
applyStringField("Build.VERSION.RELEASE") { Build.VERSION.RELEASE = it } applyStringField("Build.VERSION.RELEASE") { Build.VERSION.RELEASE = it }
applyStringField("Build.VERSION.SDK") { Build.VERSION.SDK = it } applyStringField("Build.VERSION.SDK") { Build.VERSION.SDK = it }
applyIntField("Build.VERSION.SDK_INT") { Build.VERSION.SDK_INT = it } applyIntField("Build.VERSION.SDK_INT") { Build.VERSION.SDK_INT = it }
if (android.os.Build.VERSION.SDK_INT > 21) { if (android.os.Build.VERSION.SDK_INT >= 21) {
Build.SUPPORTED_ABIS = profileData["Build.SUPPORTED_ABIS"]?.split(",")?.toTypedArray() ?: emptyArray() Build.SUPPORTED_ABIS = profileData["Build.SUPPORTED_ABIS"]?.split(",")?.toTypedArray() ?: emptyArray()
} else {
Build.SUPPORTED_ABIS = emptyArray()
}
if (android.os.Build.VERSION.SDK_INT >= 23) {
Build.VERSION.SECURITY_PATCH = profileData["Build.VERSION.SECURITY_PATCH"]
} else {
Build.VERSION.SECURITY_PATCH = null
} }
} }
@ -217,15 +227,15 @@ object ProfileManager {
@JvmStatic @JvmStatic
fun ensureInitialized(context: Context) { fun ensureInitialized(context: Context) {
if (initialized) return synchronized(this) {
try { if (initialized) return
val profile = getActiveProfile(context) try {
applyProfile(context, profile) val profile = getActiveProfile(context)
initialized = true applyProfile(context, profile)
} catch (e: Exception) { initialized = true
Log.w(TAG, e) } catch (e: Exception) {
Log.w(TAG, e)
}
} }
} }
} }

View File

@ -3,7 +3,7 @@
~ SPDX-FileCopyrightText: 2021, microG Project Team ~ SPDX-FileCopyrightText: 2021, microG Project Team
~ SPDX-License-Identifier: Apache-2.0 ~ SPDX-License-Identifier: Apache-2.0
--> -->
<profile name="Google Nexus 5X (Android 8.1.0)" device="bullhead" sdk="27" id="bullhead_27"> <profile name="Google Nexus 5X (Android 8.1.0)" product="bullhead" sdk="27" id="bullhead_27">
<!-- Data from OPM3.171019.016, Mar 2018 --> <!-- Data from OPM3.171019.016, Mar 2018 -->
<data key="Build.BOARD" value="bullhead" /> <data key="Build.BOARD" value="bullhead" />
<data key="Build.BOOTLOADER" value="BHZ31b" /> <data key="Build.BOOTLOADER" value="BHZ31b" />