Correctly rebind backends after upgrade

This commit is contained in:
Marvin W 2022-04-24 14:01:39 +02:00
parent 8c46f2c48a
commit 9c32fc7619
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
5 changed files with 43 additions and 12 deletions

View File

@ -40,16 +40,5 @@
<action android:name="org.microg.nlp.service.GEOCODE" /> <action android:name="org.microg.nlp.service.GEOCODE" />
</intent-filter> </intent-filter>
</service> </service>
<receiver android:name=".PackageChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>

View File

@ -6,8 +6,10 @@
package org.microg.nlp.service package org.microg.nlp.service
import android.app.ActivityManager import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager.PERMISSION_GRANTED import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.location.Address import android.location.Address
import android.os.Bundle import android.os.Bundle
@ -52,6 +54,19 @@ class GeocodeService : LifecycleService() {
} }
class GeocodeServiceImpl(private val context: Context, private val lifecycle: Lifecycle) : IGeocodeService.Stub(), LifecycleOwner { class GeocodeServiceImpl(private val context: Context, private val lifecycle: Lifecycle) : IGeocodeService.Stub(), LifecycleOwner {
private val packageFilter: IntentFilter = IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_CHANGED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addAction(Intent.ACTION_PACKAGE_REPLACED)
addAction(Intent.ACTION_PACKAGE_RESTARTED)
addDataScheme("package")
}
private val packageReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "Package updated, binding")
fuser.bind()
}
}
private val fuser = GeocodeFuser(context, lifecycle) private val fuser = GeocodeFuser(context, lifecycle)
init { init {
@ -60,6 +75,7 @@ class GeocodeServiceImpl(private val context: Context, private val lifecycle: Li
fuser.reset() fuser.reset()
fuser.bind() fuser.bind()
Log.d(TAG, "Finished preparing GeocodeFuser") Log.d(TAG, "Finished preparing GeocodeFuser")
context.registerReceiver(packageReceiver, packageFilter)
} }
} }
@ -188,6 +204,7 @@ class GeocodeServiceImpl(private val context: Context, private val lifecycle: Li
} }
fun destroy() { fun destroy() {
context.unregisterReceiver(packageReceiver)
fuser.destroy() fuser.destroy()
} }

View File

@ -6,8 +6,11 @@
package org.microg.nlp.service package org.microg.nlp.service
import android.app.ActivityManager import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.Intent.*
import android.content.IntentFilter
import android.content.pm.PackageManager.PERMISSION_GRANTED import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.location.Location import android.location.Location
import android.os.Bundle import android.os.Bundle
@ -104,6 +107,19 @@ class LocationRequestInternal(private var request: LocationRequest, private val
} }
class LocationServiceImpl(private val context: Context, private val lifecycle: Lifecycle) : ILocationService.Stub(), LifecycleOwner, LocationReceiver { class LocationServiceImpl(private val context: Context, private val lifecycle: Lifecycle) : ILocationService.Stub(), LifecycleOwner, LocationReceiver {
private val packageFilter: IntentFilter = IntentFilter().apply {
addAction(ACTION_PACKAGE_CHANGED)
addAction(ACTION_PACKAGE_REMOVED)
addAction(ACTION_PACKAGE_REPLACED)
addAction(ACTION_PACKAGE_RESTARTED)
addDataScheme("package")
}
private val packageReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "Package updated, binding")
fuser.bind()
}
}
private val requests = arrayListOf<LocationRequestInternal>() private val requests = arrayListOf<LocationRequestInternal>()
private val fuser = LocationFuser(context, lifecycle, this) private val fuser = LocationFuser(context, lifecycle, this)
private var lastLocation: Location? = null private var lastLocation: Location? = null
@ -119,6 +135,7 @@ class LocationServiceImpl(private val context: Context, private val lifecycle: L
fuser.bind() fuser.bind()
fuser.update() fuser.update()
Log.d(TAG, "Finished preparing LocationFuser") Log.d(TAG, "Finished preparing LocationFuser")
context.registerReceiver(packageReceiver, packageFilter)
} }
} }
@ -342,6 +359,7 @@ class LocationServiceImpl(private val context: Context, private val lifecycle: L
} }
fun destroy() { fun destroy() {
context.unregisterReceiver(packageReceiver)
fuser.destroy() fuser.destroy()
} }

View File

@ -11,6 +11,7 @@ import android.content.Intent
import android.content.Intent.* import android.content.Intent.*
import android.util.Log import android.util.Log
@Deprecated("Registered in LocationService or GeocodeService")
class PackageChangedReceiver : BroadcastReceiver() { class PackageChangedReceiver : BroadcastReceiver() {
private fun isProtectedAction(action: String) = when (action) { private fun isProtectedAction(action: String) = when (action) {
@ -28,6 +29,7 @@ class PackageChangedReceiver : BroadcastReceiver() {
if (backend.startsWith("$packageName/")) { if (backend.startsWith("$packageName/")) {
Log.d(TAG, "Reloading location service for $packageName") Log.d(TAG, "Reloading location service for $packageName")
UnifiedLocationServiceEntryPoint.reloadPreferences() UnifiedLocationServiceEntryPoint.reloadPreferences()
return return
} }
} }

View File

@ -117,8 +117,12 @@ class BackendDetailsFragment : Fragment(R.layout.backend_details), BackendDetail
entry.loadIntents(requireActivity() as AppCompatActivity) entry.loadIntents(requireActivity() as AppCompatActivity)
} }
if (entry.type == LOCATION && entry.enabled.get()) { if (entry.type == LOCATION && entry.enabled.get()) {
if (updateInProgress) return if (updateInProgress) {
Log.d(TAG, "Location update still in progress")
return
}
locationClient.connect() locationClient.connect()
Log.d(TAG, "Connected to location client")
updateInProgress = true updateInProgress = true
try { try {
val locationTemp = locationClient.getLastLocationForBackend( val locationTemp = locationClient.getLastLocationForBackend(
@ -153,6 +157,7 @@ class BackendDetailsFragment : Fragment(R.layout.backend_details), BackendDetail
var locationString = var locationString =
"${location.latitude.toStringWithDigits(6)}, ${location.longitude.toStringWithDigits(6)}" "${location.latitude.toStringWithDigits(6)}, ${location.longitude.toStringWithDigits(6)}"
Log.d(TAG, "Location reported is $locationString, trying to gather address")
val address = geocodeClient.requestReverseGeocode( val address = geocodeClient.requestReverseGeocode(
ReverseGeocodeRequest( ReverseGeocodeRequest(
LatLon( LatLon(