make restart faster if the app is not the launcher
This commit is contained in:
parent
5de195714c
commit
924a0ebb51
3 changed files with 44 additions and 24 deletions
|
|
@ -1,6 +1,5 @@
|
|||
package org.fnives.flutter.experiment_shorebird
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
|
|
@ -20,36 +19,56 @@ class MainActivity : FlutterActivity() {
|
|||
).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)
|
||||
try {
|
||||
if (this.intent.categories.contains(Intent.CATEGORY_HOME)) {
|
||||
if (isDefaultHomeApp()) {
|
||||
runViaAlarmManager(getHomeIntent())
|
||||
} else {
|
||||
runViaAlarmManager(Intent.makeRestartActivityTask(componentName))
|
||||
}
|
||||
} 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()
|
||||
} else {
|
||||
runDirectly(Intent.makeRestartActivityTask(componentName))
|
||||
}
|
||||
finishAndRemoveTask()
|
||||
Runtime.getRuntime().exit(0)
|
||||
result.success(null)
|
||||
} catch (e: Throwable) {
|
||||
result.error("couldn't start activity", e.message ?: "", null)
|
||||
}
|
||||
Runtime.getRuntime().exit(0)
|
||||
result.success(Unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
intent.addCategory(Intent.CATEGORY_HOME)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue