Settings: Make provider package dependent and move to core

This commit is contained in:
Marvin W 2021-07-01 22:44:55 +02:00
parent a0558a5f38
commit d707288ec9
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
11 changed files with 69 additions and 55 deletions

View File

@ -4,6 +4,7 @@
*/
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'
apply plugin: 'signing'
@ -12,6 +13,7 @@ dependencies {
api "androidx.lifecycle:lifecycle-service:$lifecycleVersion"
implementation "androidx.annotation:annotation:$annotationVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
}
android {
@ -24,6 +26,10 @@ android {
targetSdkVersion androidTargetSdk
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8

View File

@ -3,7 +3,12 @@
~ SPDX-FileCopyrightText: 2020, microG Project Team
~ SPDX-License-Identifier: Apache-2.0
-->
<manifest package="org.microg.gms.base.core">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.microg.gms.base.core">
<application />
<application>
<provider
android:name="org.microg.gms.settings.SettingsProvider"
android:authorities="${applicationId}.microg.settings"
android:exported="false" />
</application>
</manifest>

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2021, microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.settings
import android.content.ContentValues
@ -7,13 +12,13 @@ import android.net.Uri
import android.os.Binder
object SettingsContract {
const val AUTHORITY = "org.microg.gms.settings"
val AUTHORITY_URI: Uri = Uri.parse("content://$AUTHORITY")
fun getAuthority(context: Context) = "${context.packageName}.microg.settings"
fun getAuthorityUri(context: Context): Uri = Uri.parse("content://${getAuthority(context)}")
object CheckIn {
private const val id = "check-in"
val CONTENT_URI: Uri = Uri.withAppendedPath(AUTHORITY_URI, id)
const val CONTENT_TYPE = "vnd.android.cursor.item/vnd.$AUTHORITY.$id"
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"
const val ENABLED = "checkin_enable_service"
const val ANDROID_ID = "androidId"
@ -38,8 +43,8 @@ object SettingsContract {
object Gcm {
private const val id = "gcm"
val CONTENT_URI: Uri = Uri.withAppendedPath(AUTHORITY_URI, id)
const val CONTENT_TYPE = "vnd.android.cursor.item/vnd.$AUTHORITY.$id"
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"
const val FULL_LOG = "gcm_full_log"
const val LAST_PERSISTENT_ID = "gcm_last_persistent_id"
@ -72,8 +77,8 @@ object SettingsContract {
object Auth {
private const val id = "auth"
val CONTENT_URI: Uri = Uri.withAppendedPath(AUTHORITY_URI, id)
const val CONTENT_TYPE = "vnd.android.cursor.item/vnd.$AUTHORITY.$id"
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"
const val TRUST_GOOGLE = "auth_manager_trust_google"
const val VISIBLE = "auth_manager_visible"
@ -86,8 +91,8 @@ object SettingsContract {
object Exposure {
private const val id = "exposureNotification"
val CONTENT_URI: Uri = Uri.withAppendedPath(AUTHORITY_URI, id)
const val CONTENT_TYPE = "vnd.android.cursor.item/vnd.$AUTHORITY.$id"
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"
const val SCANNER_ENABLED = "exposure_scanner_enabled"
const val LAST_CLEANUP = "exposure_last_cleanup"
@ -100,8 +105,8 @@ object SettingsContract {
object SafetyNet {
private const val id = "safety-net"
val CONTENT_URI: Uri = Uri.withAppendedPath(AUTHORITY_URI, id)
const val CONTENT_TYPE = "vnd.android.cursor.item/vnd.$AUTHORITY.$id"
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"
}
private fun <T> withoutCallingIdentity(f: () -> T): T {

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2021, microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.settings
import android.content.ContentProvider
@ -8,13 +13,13 @@ import android.content.SharedPreferences
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import androidx.preference.PreferenceManager
import android.preference.PreferenceManager
import org.microg.gms.common.PackageUtils.warnIfNotMainProcess
import org.microg.gms.settings.SettingsContract.AUTHORITY
import org.microg.gms.settings.SettingsContract.Auth
import org.microg.gms.settings.SettingsContract.CheckIn
import org.microg.gms.settings.SettingsContract.Exposure
import org.microg.gms.settings.SettingsContract.Gcm
import org.microg.gms.settings.SettingsContract.getAuthority
import java.io.File
/**
@ -52,10 +57,10 @@ class SettingsProvider : ContentProvider() {
selectionArgs: Array<out String>?,
sortOrder: String?
): Cursor? = when (uri) {
CheckIn.CONTENT_URI -> queryCheckIn(projection ?: CheckIn.PROJECTION)
Gcm.CONTENT_URI -> queryGcm(projection ?: Gcm.PROJECTION)
Auth.CONTENT_URI -> queryAuth(projection ?: Auth.PROJECTION)
Exposure.CONTENT_URI -> queryExposure(projection ?: Exposure.PROJECTION)
CheckIn.getContentUri(context!!) -> queryCheckIn(projection ?: CheckIn.PROJECTION)
Gcm.getContentUri(context!!) -> queryGcm(projection ?: Gcm.PROJECTION)
Auth.getContentUri(context!!) -> queryAuth(projection ?: Auth.PROJECTION)
Exposure.getContentUri(context!!) -> queryExposure(projection ?: Exposure.PROJECTION)
else -> null
}
@ -68,10 +73,10 @@ class SettingsProvider : ContentProvider() {
warnIfNotMainProcess(context, this.javaClass)
if (values == null) return 0
when (uri) {
CheckIn.CONTENT_URI -> updateCheckIn(values)
Gcm.CONTENT_URI -> updateGcm(values)
Auth.CONTENT_URI -> updateAuth(values)
Exposure.CONTENT_URI -> updateExposure(values)
CheckIn.getContentUri(context!!) -> updateCheckIn(values)
Gcm.getContentUri(context!!) -> updateGcm(values)
Auth.getContentUri(context!!) -> updateAuth(values)
Exposure.getContentUri(context!!) -> updateExposure(values)
else -> return 0
}
return 1
@ -221,7 +226,7 @@ class SettingsProvider : ContentProvider() {
}
override fun getType(uri: Uri): String {
return "vnd.android.cursor.item/vnd.$AUTHORITY.${uri.path}"
return "vnd.android.cursor.item/vnd.${getAuthority(context!!)}.${uri.path}"
}
override fun insert(uri: Uri, values: ContentValues?): Uri? {

View File

@ -128,13 +128,6 @@
android:exported="true"
android:permission="org.microg.gms.PROVISION" />
<!-- Internal Settings -->
<provider
android:name="org.microg.gms.settings.SettingsProvider"
android:authorities="org.microg.gms.settings"
android:exported="false" />
<!-- Location -->
<activity

View File

@ -8,14 +8,14 @@ object AuthPrefs {
@JvmStatic
fun isTrustGooglePermitted(context: Context): Boolean {
return SettingsContract.getSettings(context, Auth.CONTENT_URI, arrayOf(Auth.TRUST_GOOGLE)) { c ->
return SettingsContract.getSettings(context, Auth.getContentUri(context), arrayOf(Auth.TRUST_GOOGLE)) { c ->
c.getInt(0) != 0
}
}
@JvmStatic
fun isAuthVisible(context: Context): Boolean {
return SettingsContract.getSettings(context, Auth.CONTENT_URI, arrayOf(Auth.VISIBLE)) { c ->
return SettingsContract.getSettings(context, Auth.getContentUri(context), arrayOf(Auth.VISIBLE)) { c ->
c.getInt(0) != 0
}
}

View File

@ -13,7 +13,7 @@ object CheckinPrefs {
@JvmStatic
fun isEnabled(context: Context): Boolean {
val projection = arrayOf(CheckIn.ENABLED)
return SettingsContract.getSettings(context, CheckIn.CONTENT_URI, projection) { c ->
return SettingsContract.getSettings(context, CheckIn.getContentUri(context), projection) { c ->
c.getInt(0) != 0
}
}

View File

@ -48,7 +48,7 @@ data class LastCheckinInfo(
CheckIn.VERSION_INFO,
CheckIn.DEVICE_DATA_VERSION_INFO,
)
return SettingsContract.getSettings(context, CheckIn.CONTENT_URI, projection) { c ->
return SettingsContract.getSettings(context, CheckIn.getContentUri(context), projection) { c ->
LastCheckinInfo(
androidId = c.getLong(0),
digest = c.getString(1),
@ -61,7 +61,7 @@ data class LastCheckinInfo(
}
@JvmStatic
fun clear(context: Context) = SettingsContract.setSettings(context, CheckIn.CONTENT_URI) {
fun clear(context: Context) = SettingsContract.setSettings(context, CheckIn.getContentUri(context)) {
put(CheckIn.ANDROID_ID, 0L)
put(CheckIn.DIGEST, CheckIn.INITIAL_DIGEST)
put(CheckIn.LAST_CHECK_IN, 0L)
@ -71,7 +71,7 @@ data class LastCheckinInfo(
}
}
fun write(context: Context) = SettingsContract.setSettings(context, CheckIn.CONTENT_URI) {
fun write(context: Context) = SettingsContract.setSettings(context, CheckIn.getContentUri(context)) {
put(CheckIn.ANDROID_ID, androidId)
put(CheckIn.DIGEST, digest)
put(CheckIn.LAST_CHECK_IN, lastCheckin)

View File

@ -20,7 +20,7 @@ data class ServiceConfiguration(val enabled: Boolean) : Serializable
suspend fun getCheckinServiceInfo(context: Context): ServiceInfo = withContext(Dispatchers.IO) {
val projection = arrayOf(CheckIn.ENABLED, CheckIn.LAST_CHECK_IN, CheckIn.ANDROID_ID)
getSettings(context, CheckIn.CONTENT_URI, projection) { c ->
getSettings(context, CheckIn.getContentUri(context), projection) { c ->
ServiceInfo(
configuration = ServiceConfiguration(c.getInt(0) != 0),
lastCheckin = c.getLong(1),
@ -33,7 +33,7 @@ suspend fun setCheckinServiceConfiguration(context: Context, configuration: Serv
val serviceInfo = getCheckinServiceInfo(context)
if (serviceInfo.configuration == configuration) return@withContext
// enabled state is not already set, setting it now
setSettings(context, CheckIn.CONTENT_URI) {
setSettings(context, CheckIn.getContentUri(context)) {
put(CheckIn.ENABLED, configuration.enabled)
}
if (configuration.enabled) {

View File

@ -43,7 +43,7 @@ data class GcmPrefs(
@JvmStatic
fun get(context: Context): GcmPrefs {
return SettingsContract.getSettings(context, Gcm.CONTENT_URI, Gcm.PROJECTION) { c ->
return SettingsContract.getSettings(context, Gcm.getContentUri(context), Gcm.PROJECTION) { c ->
GcmPrefs(
isGcmLogEnabled = c.getInt(0) != 0,
lastPersistedId = c.getString(1),
@ -62,7 +62,7 @@ data class GcmPrefs(
fun write(context: Context, config: ServiceConfiguration) {
val gcmPrefs = get(context)
setSettings(context, Gcm.CONTENT_URI) {
setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.ENABLE_GCM, config.enabled)
put(Gcm.CONFIRM_NEW_APPS, config.confirmNewApps)
put(Gcm.NETWORK_MOBILE, config.mobile)
@ -75,7 +75,7 @@ data class GcmPrefs(
@JvmStatic
fun clearLastPersistedId(context: Context) {
setSettings(context, Gcm.CONTENT_URI) {
setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LAST_PERSISTENT_ID, "")
}
}
@ -124,13 +124,13 @@ data class GcmPrefs(
fun learnTimeout(context: Context, pref: String) {
Log.d("GmsGcmPrefs", "learnTimeout: $pref")
when (pref) {
PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> setSettings(context, Gcm.CONTENT_URI) {
PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LEARNT_MOBILE, (learntMobileInterval * 0.95).toInt())
}
PREF_NETWORK_WIFI -> setSettings(context, Gcm.CONTENT_URI) {
PREF_NETWORK_WIFI -> setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LEARNT_WIFI, (learntWifiInterval * 0.95).toInt())
}
else -> setSettings(context, Gcm.CONTENT_URI) {
else -> setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LEARNT_OTHER, (learntOtherInterval * 0.95).toInt())
}
}
@ -142,7 +142,7 @@ data class GcmPrefs(
PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> {
if (time > learntMobileInterval / 4 * 3) {
val newInterval = (learntMobileInterval * 1.02).toInt()
setSettings(context, Gcm.CONTENT_URI) {
setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LEARNT_MOBILE, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL)))
}
}
@ -150,7 +150,7 @@ data class GcmPrefs(
PREF_NETWORK_WIFI -> {
if (time > learntWifiInterval / 4 * 3) {
val newInterval = (learntWifiInterval * 1.02).toInt()
setSettings(context, Gcm.CONTENT_URI) {
setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LEARNT_WIFI, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL)))
}
}
@ -158,7 +158,7 @@ data class GcmPrefs(
else -> {
if (time > learntOtherInterval / 4 * 3) {
val newInterval = (learntOtherInterval * 1.02).toInt()
setSettings(context, Gcm.CONTENT_URI) {
setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LEARNT_OTHER, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL)))
}
}
@ -173,7 +173,7 @@ data class GcmPrefs(
fun extendLastPersistedId(context: Context, id: String) {
val newId = if (lastPersistedId.isNullOrEmpty()) id else "$lastPersistedId|$id"
setSettings(context, Gcm.CONTENT_URI) {
setSettings(context, Gcm.getContentUri(context)) {
put(Gcm.LAST_PERSISTENT_ID, newId)
}
}

View File

@ -7,7 +7,7 @@ package org.microg.gms.nearby.exposurenotification
import android.content.Context
import android.content.Intent
import org.microg.gms.settings.SettingsContract.Exposure.CONTENT_URI
import org.microg.gms.settings.SettingsContract.Exposure.getContentUri
import org.microg.gms.settings.SettingsContract.Exposure.LAST_CLEANUP
import org.microg.gms.settings.SettingsContract.Exposure.SCANNER_ENABLED
import org.microg.gms.settings.SettingsContract.getSettings
@ -16,12 +16,12 @@ import org.microg.gms.settings.SettingsContract.setSettings
class ExposurePreferences(private val context: Context) {
var enabled
get() = getSettings(context, CONTENT_URI, arrayOf(SCANNER_ENABLED)) { c ->
get() = getSettings(context, getContentUri(context), arrayOf(SCANNER_ENABLED)) { c ->
c.getInt(0) != 0
}
set(newStatus) {
val changed = enabled != newStatus
setSettings(context, CONTENT_URI) {
setSettings(context, getContentUri(context)) {
put(SCANNER_ENABLED, newStatus)
}
if (!changed) return
@ -34,10 +34,10 @@ class ExposurePreferences(private val context: Context) {
}
var lastCleanup
get() = getSettings(context, CONTENT_URI, arrayOf(LAST_CLEANUP)) { c ->
get() = getSettings(context, getContentUri(context), arrayOf(LAST_CLEANUP)) { c ->
c.getLong(0)
}
set(value) = setSettings(context, CONTENT_URI) {
set(value) = setSettings(context, getContentUri(context)) {
put(LAST_CLEANUP, value)
}