From 08f46a1a49ac7d337cbee2da16b1763227edca94 Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sun, 24 Dec 2023 01:47:40 +0900 Subject: [PATCH] Set max auto heartbeat interval to 15min https://github.com/microg/GmsCore/commit/1e96dd137eae86bbf3be3ca72a6345fa837ad8ff --- .../microg/mgms/settings/SettingsProvider.kt | 9 ++-- .../java/org/microg/gms/gcm/McsService.java | 4 +- .../kotlin/org/microg/gms/gcm/GcmPrefs.kt | 49 +++++++++++++++++-- .../ui/PushNotificationAdvancedFragment.kt | 12 ++--- .../main/res/xml/preferences_gcm_advanced.xml | 4 ++ 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt b/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt index c24c3cfa..eea6c2aa 100644 --- a/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt +++ b/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt @@ -30,9 +30,6 @@ import java.io.File */ @SuppressLint("DiscouragedPrivateApi") class SettingsProvider : ContentProvider() { - companion object { - const val INTERVAL = 1 * 60 * 1000 // 1 minute - } private val preferences: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) @@ -167,9 +164,15 @@ class SettingsProvider : ContentProvider() { Gcm.NETWORK_ROAMING -> Integer.parseInt(preferences.getString(key, "0") ?: "0") Gcm.NETWORK_OTHER -> Integer.parseInt(preferences.getString(key, "0") ?: "0") +<<<<<<< HEAD Gcm.LEARNT_MOBILE -> preferences.getInt(key, INTERVAL) Gcm.LEARNT_WIFI -> preferences.getInt(key, INTERVAL) Gcm.LEARNT_OTHER -> preferences.getInt(key, INTERVAL) +======= + Gcm.LEARNT_MOBILE -> preferences.getInt(key, 300000) + Gcm.LEARNT_WIFI -> preferences.getInt(key, 300000) + Gcm.LEARNT_OTHER -> preferences.getInt(key, 300000) +>>>>>>> b0b92c8c (Set max auto heartbeat interval to 15min https://github.com/microg/GmsCore/commit/1e96dd137eae86bbf3be3ca72a6345fa837ad8ff) else -> throw IllegalArgumentException("Unknown key: $key") } diff --git a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java index 7073b5fa..1d24c819 100644 --- a/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java +++ b/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java @@ -286,8 +286,8 @@ public class McsService extends Service implements Handler.Callback { public synchronized static long getCurrentDelay() { long delay = currentDelay == 0 ? 5000 : currentDelay; - if (currentDelay < SettingsProvider.INTERVAL) currentDelay += 10000; - if (currentDelay >= SettingsProvider.INTERVAL && currentDelay < 600000) currentDelay += SettingsProvider.INTERVAL; + if (currentDelay < 60000) currentDelay += 10000; + if (currentDelay >= 60000 && currentDelay < 600000) currentDelay += 60000; return delay; } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt b/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt index 77cf34b0..23e551be 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt @@ -11,7 +11,8 @@ import org.microg.gms.gcm.TriggerReceiver.FORCE_TRY_RECONNECT import org.microg.mgms.settings.SettingsContract import org.microg.mgms.settings.SettingsContract.Gcm import org.microg.mgms.settings.SettingsContract.setSettings -import org.microg.mgms.settings.SettingsProvider +import kotlin.math.max +import kotlin.math.min data class GcmPrefs( val isGcmLogEnabled: Boolean, @@ -37,6 +38,9 @@ data class GcmPrefs( const val PREF_NETWORK_ROAMING = Gcm.NETWORK_ROAMING const val PREF_NETWORK_OTHER = Gcm.NETWORK_OTHER + private const val MIN_INTERVAL = 5 * 60 * 1000 // 5 minutes + private const val MAX_INTERVAL = 15 * 60 * 1000 // 15 minutes + @JvmStatic fun get(context: Context): GcmPrefs? { return Gcm.getContentUri(context)?.let { @@ -93,6 +97,7 @@ data class GcmPrefs( } } + @Suppress("DEPRECATION") fun getNetworkPrefForInfo(info: NetworkInfo?): String { if (info == null) return PREF_NETWORK_OTHER return if (info.isRoaming) PREF_NETWORK_ROAMING else when (info.type) { @@ -102,25 +107,27 @@ data class GcmPrefs( } } + @Suppress("DEPRECATION") fun getHeartbeatMsFor(info: NetworkInfo?): Int { return getHeartbeatMsFor(getNetworkPrefForInfo(info)) } fun getHeartbeatMsFor(pref: String): Int { return if (PREF_NETWORK_ROAMING == pref) { - if (networkRoaming != 0) networkRoaming * SettingsProvider.INTERVAL else learntMobileInterval + if (networkRoaming != 0) networkRoaming * 60000 else learntMobileInterval } else if (PREF_NETWORK_MOBILE == pref) { - if (networkMobile != 0) networkMobile * SettingsProvider.INTERVAL else learntMobileInterval + if (networkMobile != 0) networkMobile * 60000 else learntMobileInterval } else if (PREF_NETWORK_WIFI == pref) { - if (networkWifi != 0) networkWifi * SettingsProvider.INTERVAL else learntWifiInterval + if (networkWifi != 0) networkWifi * 60000 else learntWifiInterval } else { - if (networkOther != 0) networkOther * SettingsProvider.INTERVAL else learntOtherInterval + if (networkOther != 0) networkOther * 60000 else learntOtherInterval } } fun learnTimeout(context: Context, pref: String) { Log.d("GmsGcmPrefs", "learnTimeout: $pref") when (pref) { +<<<<<<< HEAD PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> Gcm.getContentUri(context)?.let { setSettings(context, it) { val newInterval = (learntMobileInterval * 0.95).toInt() @@ -138,6 +145,19 @@ data class GcmPrefs( val newInterval = (learntMobileInterval * 0.95).toInt() put(Gcm.LEARNT_OTHER, newInterval) } +======= + PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> setSettings(context, Gcm.getContentUri(context)) { + val newInterval = (learntMobileInterval * 0.95).toInt() + put(Gcm.LEARNT_MOBILE, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL))) + } + PREF_NETWORK_WIFI -> setSettings(context, Gcm.getContentUri(context)) { + val newInterval = (learntWifiInterval * 0.95).toInt() + put(Gcm.LEARNT_WIFI, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL))) + } + else -> setSettings(context, Gcm.getContentUri(context)) { + val newInterval = (learntOtherInterval * 0.95).toInt() + put(Gcm.LEARNT_OTHER, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL))) +>>>>>>> b0b92c8c (Set max auto heartbeat interval to 15min https://github.com/microg/GmsCore/commit/1e96dd137eae86bbf3be3ca72a6345fa837ad8ff) } } } @@ -147,34 +167,53 @@ data class GcmPrefs( when (pref) { PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> { if (time > learntMobileInterval / 4 * 3) { +<<<<<<< HEAD Gcm.getContentUri(context)?.let { setSettings(context, it) { put(Gcm.LEARNT_MOBILE, SettingsProvider.INTERVAL) } +======= + val newInterval = (learntMobileInterval * 1.02).toInt() + setSettings(context, Gcm.getContentUri(context)) { + put(Gcm.LEARNT_MOBILE, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL))) +>>>>>>> b0b92c8c (Set max auto heartbeat interval to 15min https://github.com/microg/GmsCore/commit/1e96dd137eae86bbf3be3ca72a6345fa837ad8ff) } } } PREF_NETWORK_WIFI -> { if (time > learntWifiInterval / 4 * 3) { +<<<<<<< HEAD Gcm.getContentUri(context)?.let { setSettings(context, it) { put(Gcm.LEARNT_WIFI, SettingsProvider.INTERVAL) } +======= + val newInterval = (learntWifiInterval * 1.02).toInt() + setSettings(context, Gcm.getContentUri(context)) { + put(Gcm.LEARNT_WIFI, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL))) +>>>>>>> b0b92c8c (Set max auto heartbeat interval to 15min https://github.com/microg/GmsCore/commit/1e96dd137eae86bbf3be3ca72a6345fa837ad8ff) } } } else -> { if (time > learntOtherInterval / 4 * 3) { +<<<<<<< HEAD Gcm.getContentUri(context)?.let { setSettings(context, it) { put(Gcm.LEARNT_OTHER, SettingsProvider.INTERVAL) } +======= + val newInterval = (learntOtherInterval * 1.02).toInt() + setSettings(context, Gcm.getContentUri(context)) { + put(Gcm.LEARNT_OTHER, max(MIN_INTERVAL, min(newInterval, MAX_INTERVAL))) +>>>>>>> b0b92c8c (Set max auto heartbeat interval to 15min https://github.com/microg/GmsCore/commit/1e96dd137eae86bbf3be3ca72a6345fa837ad8ff) } } } } } + @Suppress("DEPRECATION") fun isEnabledFor(info: NetworkInfo?): Boolean { return isEnabled && info != null && getHeartbeatMsFor(info) >= 0 } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAdvancedFragment.kt b/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAdvancedFragment.kt index d3cef96f..37bda799 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAdvancedFragment.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAdvancedFragment.kt @@ -37,7 +37,7 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() { val appContext = requireContext().applicationContext lifecycleScope.launchWhenResumed { (newValue as? String)?.toIntOrNull()?.let { - setGcmServiceConfiguration(requireContext(), getGcmServiceInfo(appContext).configuration.copy(mobile = it)) + setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(mobile = it)) } updateContent() } @@ -47,7 +47,7 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() { val appContext = requireContext().applicationContext lifecycleScope.launchWhenResumed { (newValue as? String)?.toIntOrNull()?.let { - setGcmServiceConfiguration(appContext, getGcmServiceInfo(requireContext()).configuration.copy(wifi = it)) + setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(wifi = it)) } updateContent() } @@ -57,7 +57,7 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() { val appContext = requireContext().applicationContext lifecycleScope.launchWhenResumed { (newValue as? String)?.toIntOrNull()?.let { - setGcmServiceConfiguration(appContext, getGcmServiceInfo(requireContext()).configuration.copy(roaming = it)) + setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(roaming = it)) } updateContent() } @@ -67,7 +67,7 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() { val appContext = requireContext().applicationContext lifecycleScope.launchWhenResumed { (newValue as? String)?.toIntOrNull()?.let { - setGcmServiceConfiguration(appContext, getGcmServiceInfo(requireContext()).configuration.copy(other = it)) + setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(other = it)) } updateContent() } @@ -98,13 +98,13 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() { private fun getSummaryString(value: Int, learnt: Int): String = when (value) { -1 -> getString(R.string.service_status_disabled_short) 0 -> getString(R.string.service_status_enabled_short) + " / " + getString(R.string.gcm_status_pref_default) + ": " + getHeartbeatString(learnt) - else -> getString(R.string.service_status_enabled_short) + " / " + getString(R.string.gcm_status_pref_manual) + ": " + getHeartbeatString(value * SettingsProvider.INTERVAL) + else -> getString(R.string.service_status_enabled_short) + " / " + getString(R.string.gcm_status_pref_manual) + ": " + getHeartbeatString(value * 60000) } private fun getHeartbeatString(heartbeatMs: Int): String { return if (heartbeatMs < 120000) { (heartbeatMs / 1000).toString() + " " + getString(R.string.gcm_status_pref_sec) - } else (heartbeatMs / SettingsProvider.INTERVAL).toString() + " " + getString(R.string.gcm_status_pref_min) + } else (heartbeatMs / 60000).toString() + " " + getString(R.string.gcm_status_pref_min) } companion object { diff --git a/play-services-core/src/main/res/xml/preferences_gcm_advanced.xml b/play-services-core/src/main/res/xml/preferences_gcm_advanced.xml index 5c104f8a..efac059b 100755 --- a/play-services-core/src/main/res/xml/preferences_gcm_advanced.xml +++ b/play-services-core/src/main/res/xml/preferences_gcm_advanced.xml @@ -23,6 +23,7 @@