mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2025-06-28 20:29:53 +00:00
android: restart the app on update (#5616)
This commit is contained in:
parent
38c5c19b17
commit
75685df2e8
3 changed files with 43 additions and 0 deletions
|
@ -179,5 +179,16 @@
|
|||
android:name=".SimplexService$AutoRestartReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
|
||||
<!-- SimplexService restart on app update -->
|
||||
<receiver
|
||||
android:name=".SimplexService$AppUpdateReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
|
|
|
@ -71,6 +71,7 @@ class SimplexApp: Application(), LifecycleEventObserver {
|
|||
context = this
|
||||
initHaskell(packageName)
|
||||
initMultiplatform()
|
||||
reconfigureBroadcastReceivers()
|
||||
runMigrations()
|
||||
tmpDir.deleteRecursively()
|
||||
tmpDir.mkdir()
|
||||
|
@ -216,6 +217,7 @@ class SimplexApp: Application(), LifecycleEventObserver {
|
|||
appPrefs.backgroundServiceNoticeShown.set(false)
|
||||
}
|
||||
SimplexService.StartReceiver.toggleReceiver(mode == NotificationsMode.SERVICE)
|
||||
SimplexService.AppUpdateReceiver.toggleReceiver(mode == NotificationsMode.SERVICE)
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
if (mode == NotificationsMode.SERVICE) {
|
||||
SimplexService.start()
|
||||
|
@ -371,4 +373,10 @@ class SimplexApp: Application(), LifecycleEventObserver {
|
|||
override val androidApiLevel: Int get() = Build.VERSION.SDK_INT
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure that receivers enabled state is in actual state (same as in prefs)
|
||||
private fun reconfigureBroadcastReceivers() {
|
||||
val mode = appPrefs.notificationsMode.get()
|
||||
SimplexService.StartReceiver.toggleReceiver(mode == NotificationsMode.SERVICE)
|
||||
SimplexService.AppUpdateReceiver.toggleReceiver(mode == NotificationsMode.SERVICE)}
|
||||
}
|
||||
|
|
|
@ -249,6 +249,29 @@ class SimplexService: Service() {
|
|||
}
|
||||
}
|
||||
|
||||
// restart on app update
|
||||
class AppUpdateReceiver: BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
// If notification service is enabled and battery optimization is disabled, restart the service on app update
|
||||
if (SimplexApp.context.allowToStartServiceAfterAppExit()) {
|
||||
Log.d(TAG, "AppUpdateReceiver: onReceive called")
|
||||
scheduleStart(context)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun toggleReceiver(enable: Boolean) {
|
||||
Log.d(TAG, "AppUpdateReceiver: toggleReceiver enabled: $enable")
|
||||
val component = ComponentName(BuildConfig.APPLICATION_ID, AppUpdateReceiver::class.java.name)
|
||||
SimplexApp.context.packageManager.setComponentEnabledSetting(
|
||||
component,
|
||||
if (enable) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ServiceStartWorker(private val context: Context, params: WorkerParameters): CoroutineWorker(context, params) {
|
||||
override suspend fun doWork(): Result {
|
||||
val id = this.id
|
||||
|
@ -694,6 +717,7 @@ class SimplexService: Service() {
|
|||
}
|
||||
ChatController.appPrefs.notificationsMode.set(NotificationsMode.OFF)
|
||||
StartReceiver.toggleReceiver(false)
|
||||
AppUpdateReceiver.toggleReceiver(false)
|
||||
androidAppContext.getWorkManagerInstance().cancelUniqueWork(SimplexService.SERVICE_START_WORKER_WORK_NAME_PERIODIC)
|
||||
MessagesFetcherWorker.cancelAll()
|
||||
safeStopService()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue