Skip to content

Commit a808476

Browse files
grotemar-v-in
authored andcommitted
Work around rare crashes that can happen when fragments are not attached to a context
while co-routines are still executing but requiring a context Change-Id: Ie6c7cee50014b59c25384d3bf9a122081b9917fc
1 parent ae8516a commit a808476

12 files changed

+56
-35
lines changed

play-services-core/src/main/kotlin/org/microg/gms/ui/DeviceRegistrationFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class DeviceRegistrationFragment : Fragment(R.layout.device_registration_fragmen
3131
}
3232

3333
fun setEnabled(newStatus: Boolean) {
34+
val appContext = requireContext().applicationContext
3435
lifecycleScope.launchWhenResumed {
35-
val info = getCheckinServiceInfo(requireContext())
36+
val info = getCheckinServiceInfo(appContext)
3637
val newConfiguration = info.configuration.copy(enabled = newStatus)
37-
setCheckinServiceConfiguration(requireContext(), newConfiguration)
38+
setCheckinServiceConfiguration(appContext, newConfiguration)
3839
displayServiceInfo(info.copy(configuration = newConfiguration))
3940
}
4041
}
@@ -45,8 +46,9 @@ class DeviceRegistrationFragment : Fragment(R.layout.device_registration_fragmen
4546

4647
override fun onResume() {
4748
super.onResume()
49+
val appContext = requireContext().applicationContext
4850
lifecycleScope.launchWhenResumed {
49-
displayServiceInfo(getCheckinServiceInfo(requireContext()))
51+
displayServiceInfo(getCheckinServiceInfo(appContext))
5052
}
5153
}
5254
}

play-services-core/src/main/kotlin/org/microg/gms/ui/DeviceRegistrationPreferencesFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class DeviceRegistrationPreferencesFragment : PreferenceFragmentCompat() {
4444

4545
private fun updateStatus() {
4646
handler.postDelayed(updateRunnable, UPDATE_INTERVAL)
47+
val appContext = requireContext().applicationContext
4748
lifecycleScope.launchWhenResumed {
48-
val serviceInfo = getCheckinServiceInfo(requireContext())
49+
val serviceInfo = getCheckinServiceInfo(appContext)
4950
statusCategory.isVisible = serviceInfo.configuration.enabled
5051
if (serviceInfo.lastCheckin > 0) {
5152
status.summary = getString(R.string.checkin_last_registration, DateUtils.getRelativeTimeSpanString(serviceInfo.lastCheckin, System.currentTimeMillis(), 0))

play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAdvancedFragment.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,50 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() {
3232
networkOther = preferenceScreen.findPreference(GcmPrefs.PREF_NETWORK_OTHER) ?: networkOther
3333

3434
confirmNewApps.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
35+
val appContext = requireContext().applicationContext
3536
lifecycleScope.launchWhenResumed {
3637
if (newValue is Boolean) {
37-
setGcmServiceConfiguration(requireContext(), getGcmServiceInfo(requireContext()).configuration.copy(confirmNewApps = newValue))
38+
setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(confirmNewApps = newValue))
3839
}
3940
updateContent()
4041
}
4142
true
4243
}
4344
networkMobile.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
45+
val appContext = requireContext().applicationContext
4446
lifecycleScope.launchWhenResumed {
4547
(newValue as? String)?.toIntOrNull()?.let {
46-
setGcmServiceConfiguration(requireContext(), getGcmServiceInfo(requireContext()).configuration.copy(mobile = it))
48+
setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(mobile = it))
4749
}
4850
updateContent()
4951
}
5052
true
5153
}
5254
networkWifi.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
55+
val appContext = requireContext().applicationContext
5356
lifecycleScope.launchWhenResumed {
5457
(newValue as? String)?.toIntOrNull()?.let {
55-
setGcmServiceConfiguration(requireContext(), getGcmServiceInfo(requireContext()).configuration.copy(wifi = it))
58+
setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(wifi = it))
5659
}
5760
updateContent()
5861
}
5962
true
6063
}
6164
networkRoaming.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
65+
val appContext = requireContext().applicationContext
6266
lifecycleScope.launchWhenResumed {
6367
(newValue as? String)?.toIntOrNull()?.let {
64-
setGcmServiceConfiguration(requireContext(), getGcmServiceInfo(requireContext()).configuration.copy(roaming = it))
68+
setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(roaming = it))
6569
}
6670
updateContent()
6771
}
6872
true
6973
}
7074
networkOther.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
75+
val appContext = requireContext().applicationContext
7176
lifecycleScope.launchWhenResumed {
7277
(newValue as? String)?.toIntOrNull()?.let {
73-
setGcmServiceConfiguration(requireContext(), getGcmServiceInfo(requireContext()).configuration.copy(other = it))
78+
setGcmServiceConfiguration(appContext, getGcmServiceInfo(appContext).configuration.copy(other = it))
7479
}
7580
updateContent()
7681
}
@@ -84,8 +89,9 @@ class PushNotificationAdvancedFragment : PreferenceFragmentCompat() {
8489
}
8590

8691
private fun updateContent() {
92+
val appContext = requireContext().applicationContext
8793
lifecycleScope.launchWhenResumed {
88-
val serviceInfo = getGcmServiceInfo(requireContext())
94+
val serviceInfo = getGcmServiceInfo(appContext)
8995
confirmNewApps.isChecked = serviceInfo.configuration.confirmNewApps
9096
networkMobile.value = serviceInfo.configuration.mobile.toString()
9197
networkMobile.summary = getSummaryString(serviceInfo.configuration.mobile, serviceInfo.learntMobileInterval)

play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAllAppsFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class PushNotificationAllAppsFragment : PreferenceFragmentCompat() {
5252
}
5353

5454
private fun updateContent() {
55+
val context = requireContext().applicationContext
5556
lifecycleScope.launchWhenResumed {
56-
val context = requireContext()
5757
val apps = withContext(Dispatchers.IO) {
5858
val res = database.appList.map { app ->
5959
app to context.packageManager.getApplicationInfoIfExists(app.packageName)

play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationAppFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ class PushNotificationAppFragment : Fragment(R.layout.push_notification_fragment
4646

4747
override fun onResume() {
4848
super.onResume()
49+
val appContext = requireContext().applicationContext
4950
lifecycleScope.launchWhenResumed {
50-
val pm = requireContext().packageManager
51+
val pm = appContext.packageManager
5152
val applicationInfo = pm.getApplicationInfoIfExists(packageName)
5253
binding.appName = applicationInfo?.loadLabel(pm)?.toString() ?: packageName
5354
binding.appIcon = applicationInfo?.loadIcon(pm)
54-
?: AppCompatResources.getDrawable(requireContext(), android.R.mipmap.sym_def_app_icon)
55+
?: AppCompatResources.getDrawable(appContext, android.R.mipmap.sym_def_app_icon)
5556
}
5657
}
5758
}

play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationFragment.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ class PushNotificationFragment : Fragment(R.layout.push_notification_fragment) {
3030
}
3131

3232
fun setEnabled(newStatus: Boolean) {
33+
val appContext = requireContext().applicationContext
3334
lifecycleScope.launchWhenResumed {
34-
val info = getGcmServiceInfo(requireContext())
35+
val info = getGcmServiceInfo(appContext)
3536
val newConfiguration = info.configuration.copy(enabled = newStatus)
36-
setGcmServiceConfiguration(requireContext(), newConfiguration)
37+
setGcmServiceConfiguration(appContext, newConfiguration)
3738
displayServiceInfo(info.copy(configuration = newConfiguration))
3839
}
3940
}
@@ -44,9 +45,10 @@ class PushNotificationFragment : Fragment(R.layout.push_notification_fragment) {
4445

4546
override fun onResume() {
4647
super.onResume()
48+
val appContext = requireContext().applicationContext
4749
lifecycleScope.launchWhenResumed {
48-
displayServiceInfo(getGcmServiceInfo(requireContext()))
49-
binding.checkinEnabled = getCheckinServiceInfo(requireContext()).configuration.enabled
50+
displayServiceInfo(getGcmServiceInfo(appContext))
51+
binding.checkinEnabled = getCheckinServiceInfo(appContext).configuration.enabled
5052
}
5153
}
5254

play-services-core/src/main/kotlin/org/microg/gms/ui/SafetyNetFragment.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class SafetyNetFragment : Fragment(R.layout.safety_net_fragment) {
3232
}
3333

3434
fun setEnabled(newStatus: Boolean) {
35+
val appContext = requireContext().applicationContext
3536
lifecycleScope.launchWhenResumed {
36-
val info = getSafetyNetServiceInfo(requireContext())
37+
val info = getSafetyNetServiceInfo(appContext)
3738
val newConfiguration = info.configuration.copy(enabled = newStatus)
38-
displayServiceInfo(setSafetyNetServiceConfiguration(requireContext(), newConfiguration))
39+
displayServiceInfo(setSafetyNetServiceConfiguration(appContext, newConfiguration))
3940
}
4041
}
4142

@@ -45,9 +46,10 @@ class SafetyNetFragment : Fragment(R.layout.safety_net_fragment) {
4546

4647
override fun onResume() {
4748
super.onResume()
49+
val appContext = requireContext().applicationContext
4850
lifecycleScope.launchWhenResumed {
49-
binding.checkinEnabled = getCheckinServiceInfo(requireContext()).configuration.enabled
50-
displayServiceInfo(getSafetyNetServiceInfo(requireContext()))
51+
binding.checkinEnabled = getCheckinServiceInfo(appContext).configuration.enabled
52+
displayServiceInfo(getSafetyNetServiceInfo(appContext))
5153
}
5254
}
5355

play-services-core/src/main/kotlin/org/microg/gms/ui/SettingsFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.microg.gms.ui
77

8+
import android.content.Context
89
import android.os.Bundle
910
import androidx.lifecycle.lifecycleScope
1011
import androidx.navigation.fragment.findNavController
@@ -50,12 +51,13 @@ class SettingsFragment : ResourceSettingsFragment() {
5051

5152
override fun onResume() {
5253
super.onResume()
54+
val appContext = requireContext().applicationContext
5355
lifecycleScope.launchWhenResumed {
54-
updateDetails()
56+
updateDetails(appContext)
5557
}
5658
}
5759

58-
private suspend fun updateDetails() {
60+
private suspend fun updateDetails(context: Context) {
5961
if (getGcmServiceInfo(requireContext()).configuration.enabled) {
6062
val database = GcmDatabase(context)
6163
val regCount = database.registrationList.size
@@ -68,7 +70,7 @@ class SettingsFragment : ResourceSettingsFragment() {
6870
findPreference<Preference>(PREF_CHECKIN)!!.setSummary(if (getCheckinServiceInfo(requireContext()).configuration.enabled) R.string.service_status_enabled_short else R.string.service_status_disabled_short)
6971
findPreference<Preference>(PREF_SNET)!!.setSummary(if (getSafetyNetServiceInfo(requireContext()).configuration.enabled) R.string.service_status_enabled_short else R.string.service_status_disabled_short)
7072

71-
val backendCount = UnifiedLocationClient[requireContext()].getLocationBackends().size + UnifiedLocationClient[requireContext()].getGeocoderBackends().size
73+
val backendCount = UnifiedLocationClient[context].getLocationBackends().size + UnifiedLocationClient[requireContext()].getGeocoderBackends().size
7274
findPreference<Preference>(PREF_UNIFIEDNLP)!!.summary = resources.getQuantityString(R.plurals.pref_unifiednlp_summary, backendCount, backendCount);
7375

7476
findPreference<Preference>(PREF_EXPOSURE)?.isVisible = NearbyPreferencesIntegration.isAvailable

play-services-nearby-core-ui/src/main/kotlin/org/microg/gms/nearby/core/ui/ExposureNotificationsAppFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ class ExposureNotificationsAppFragment : Fragment(R.layout.exposure_notification
4040

4141
override fun onResume() {
4242
super.onResume()
43+
val appContext = requireContext().applicationContext
4344
lifecycleScope.launchWhenResumed {
44-
val pm = requireContext().packageManager
45+
val pm = appContext.packageManager
4546
val applicationInfo = pm.getApplicationInfoIfExists(packageName)
4647
binding.appName = applicationInfo?.loadLabel(pm)?.toString() ?: packageName
4748
binding.appIcon = applicationInfo?.loadIcon(pm)
48-
?: AppCompatResources.getDrawable(requireContext(), android.R.mipmap.sym_def_app_icon)
49+
?: AppCompatResources.getDrawable(appContext, android.R.mipmap.sym_def_app_icon)
4950
}
5051
}
5152
}

play-services-nearby-core-ui/src/main/kotlin/org/microg/gms/nearby/core/ui/ExposureNotificationsFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class ExposureNotificationsFragment : Fragment(R.layout.exposure_notifications_f
3232
}
3333

3434
fun setEnabled(newStatus: Boolean) {
35+
val appContext = requireContext().applicationContext
3536
lifecycleScope.launchWhenResumed {
36-
val info = getExposureNotificationsServiceInfo(requireContext())
37+
val info = getExposureNotificationsServiceInfo(appContext)
3738
val newConfiguration = info.configuration.copy(enabled = newStatus)
38-
setExposureNotificationsServiceConfiguration(requireContext(), newConfiguration)
39+
setExposureNotificationsServiceConfiguration(appContext, newConfiguration)
3940
displayServiceInfo(info.copy(configuration = newConfiguration))
4041
}
4142
}
@@ -46,8 +47,9 @@ class ExposureNotificationsFragment : Fragment(R.layout.exposure_notifications_f
4647

4748
override fun onResume() {
4849
super.onResume()
50+
val appContext = requireContext().applicationContext
4951
lifecycleScope.launchWhenResumed {
50-
displayServiceInfo(getExposureNotificationsServiceInfo(requireContext()))
52+
displayServiceInfo(getExposureNotificationsServiceInfo(appContext))
5153
}
5254
}
5355
}

0 commit comments

Comments
 (0)