VancedMicroG/play-services-core/src/main/kotlin/org/microg/gms/checkin/ServiceInfo.kt

43 lines
1.6 KiB
Kotlin
Raw Normal View History

2020-09-11 10:11:10 +02:00
/*
* SPDX-FileCopyrightText: 2020, microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.checkin
import android.content.Context
import android.content.Intent
2021-06-26 09:45:33 +02:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
2021-07-05 20:36:56 +02:00
import org.microg.mgms.settings.SettingsContract.CheckIn
import org.microg.mgms.settings.SettingsContract.getSettings
import org.microg.mgms.settings.SettingsContract.setSettings
2020-09-11 10:11:10 +02:00
import java.io.Serializable
data class ServiceInfo(val configuration: ServiceConfiguration, val lastCheckin: Long, val androidId: Long) : Serializable
2021-06-26 09:45:33 +02:00
data class ServiceConfiguration(val enabled: Boolean) : Serializable
2020-09-11 10:11:10 +02:00
2021-06-26 09:45:33 +02:00
suspend fun getCheckinServiceInfo(context: Context): ServiceInfo = withContext(Dispatchers.IO) {
val projection = arrayOf(CheckIn.ENABLED, CheckIn.LAST_CHECK_IN, CheckIn.ANDROID_ID)
2021-09-11 09:36:50 +02:00
getSettings(context, CheckIn.getContentUri(context), projection) { c ->
2021-06-26 09:45:33 +02:00
ServiceInfo(
configuration = ServiceConfiguration(c.getInt(0) != 0),
lastCheckin = c.getLong(1),
androidId = c.getLong(2),
)
2020-09-11 10:11:10 +02:00
}
}
2021-06-26 09:45:33 +02:00
suspend fun setCheckinServiceConfiguration(context: Context, configuration: ServiceConfiguration) = withContext(Dispatchers.IO) {
val serviceInfo = getCheckinServiceInfo(context)
if (serviceInfo.configuration == configuration) return@withContext
// enabled state is not already set, setting it now
2021-09-11 09:36:50 +02:00
setSettings(context, CheckIn.getContentUri(context)) {
2021-06-26 09:45:33 +02:00
put(CheckIn.ENABLED, configuration.enabled)
}
if (configuration.enabled) {
context.sendOrderedBroadcast(Intent(context, TriggerReceiver::class.java), null)
2020-09-11 10:11:10 +02:00
}
}