make restart faster if the app is not the launcher

This commit is contained in:
Gergely Hegedus 2025-01-18 02:06:57 +02:00
parent 5de195714c
commit 924a0ebb51
3 changed files with 44 additions and 24 deletions

View file

@ -1,6 +1,5 @@
package org.fnives.flutter.experiment_shorebird package org.fnives.flutter.experiment_shorebird
import android.app.Activity
import android.app.AlarmManager import android.app.AlarmManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Intent import android.content.Intent
@ -20,36 +19,56 @@ class MainActivity : FlutterActivity() {
).setMethodCallHandler { call, result -> ).setMethodCallHandler { call, result ->
when (call.method) { when (call.method) {
Methods.RESTART.methodName -> { Methods.RESTART.methodName -> {
val pendingIntent = PendingIntent.getActivity( try {
context, if (this.intent.categories.contains(Intent.CATEGORY_HOME)) {
4201, if (isDefaultHomeApp()) {
if (isHomeApp()) Intent(intent.action).apply { runViaAlarmManager(getHomeIntent())
flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS } else {
intent.categories.forEach { runViaAlarmManager(Intent.makeRestartActivityTask(componentName))
addCategory(it)
} }
} else Intent(this, this::class.java).apply { } else {
flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS runDirectly(Intent.makeRestartActivityTask(componentName))
},
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()
} }
finishAndRemoveTask()
Runtime.getRuntime().exit(0) Runtime.getRuntime().exit(0)
result.success(Unit) result.success(null)
} catch (e: Throwable) {
result.error("couldn't start activity", e.message ?: "", null)
}
} }
} }
} }
} }
private fun isHomeApp(): Boolean { private fun getHomeIntent() =
Intent(intent.action).apply {
flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
intent.setComponent(component)
intent.categories.forEach {
addCategory(it)
}
}
private fun runDirectly(intent: Intent) {
context.startActivity(intent)
}
private fun runViaAlarmManager(intent: Intent) {
val pendingIntent = PendingIntent.getActivity(
context,
4201,
intent,
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager
alarmManager[AlarmManager.RTC, System.currentTimeMillis()+100] = pendingIntent
}
private fun isDefaultHomeApp(): Boolean {
val intent = Intent(Intent.ACTION_MAIN) val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME) intent.addCategory(Intent.CATEGORY_HOME)
val res = packageManager.resolveActivity(intent, 0) val res = packageManager.resolveActivity(intent, 0)
return res?.activityInfo != null && (packageName == res.activityInfo.packageName) val isHomeApp = res?.activityInfo != null && (packageName == res.activityInfo.packageName)
return isHomeApp
} }
} }

View file

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:shorebird_code_push/shorebird_code_push.dart'; import 'package:shorebird_code_push/shorebird_code_push.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
@ -151,7 +152,7 @@ class _MyHomePageState extends State<MyHomePage> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( const Text(
'This is a release 1.9.1', 'This is a release 1.9.9#6',
// 'You have times:', // 'You have times:',
), ),
const Text( const Text(

View file

@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.9.1+191 version: 1.9.9+199
environment: environment:
sdk: '>=3.4.3 <4.0.0' sdk: '>=3.4.3 <4.0.0'