Make manual shorebird update actually work

Restarting just the activity doesn't seem to work.
To work around this, I finished the process and started the activity via alarm manager.
This is inspired from ProcessPhoenix
This commit is contained in:
Gergely Hegedus 2025-01-17 21:54:22 +02:00
parent 2519513638
commit 20c693004a
5 changed files with 123 additions and 28 deletions

View file

@ -17,6 +17,13 @@
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"

View file

@ -1,5 +1,55 @@
package org.fnives.flutter.experiment_shorebird
import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Intent
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity()
class MainActivity : FlutterActivity() {
private val channel = "MainChannel"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
channel
).setMethodCallHandler { call, result ->
when (call.method) {
Methods.RESTART.methodName -> {
val pendingIntent = PendingIntent.getActivity(
context,
4201,
if (isHomeApp()) Intent(intent.action).apply {
flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
intent.categories.forEach {
addCategory(it)
}
} else Intent(this, this::class.java).apply {
flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
},
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager
alarmManager[AlarmManager.RTC, System.currentTimeMillis() + 100] =
pendingIntent
if (context is Activity) {
(context as Activity).finishAndRemoveTask()
}
Runtime.getRuntime().exit(0)
result.success(Unit)
}
}
}
}
private fun isHomeApp(): Boolean {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME)
val res = packageManager.resolveActivity(intent, 0)
return res?.activityInfo != null && (packageName == res.activityInfo.packageName)
}
}

View file

@ -0,0 +1,7 @@
package org.fnives.flutter.experiment_shorebird
enum class Methods(val methodName: String) {
RESTART("restart");
}