For #11183: Redirect user to homescreen after widget added
Co-authored-by: hakkikaancaliskan <caliskanhkaan@gmail.com>master
parent
77ed670558
commit
a8ed913654
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
package org.mozilla.gecko.search
|
package org.mozilla.gecko.search
|
||||||
|
|
||||||
|
import android.app.ActivityManager
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.appwidget.AppWidgetManager
|
import android.appwidget.AppWidgetManager
|
||||||
import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH
|
import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH
|
||||||
|
@ -36,6 +37,21 @@ class SearchWidgetProvider : AppWidgetProvider() {
|
||||||
|
|
||||||
override fun onEnabled(context: Context) {
|
override fun onEnabled(context: Context) {
|
||||||
context.settings().addSearchWidgetInstalled(1)
|
context.settings().addSearchWidgetInstalled(1)
|
||||||
|
if (isAppInForeground(context)) {
|
||||||
|
val goHomeOnWidgetAdded = Intent(Intent.ACTION_MAIN)
|
||||||
|
goHomeOnWidgetAdded.addCategory(Intent.CATEGORY_HOME)
|
||||||
|
goHomeOnWidgetAdded.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
|
context.startActivity(goHomeOnWidgetAdded)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need this because user can not add widget via launcher app without this
|
||||||
|
private fun isAppInForeground(context: Context): Boolean {
|
||||||
|
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||||
|
val runningAppProcesses =
|
||||||
|
activityManager.runningAppProcesses ?: return false
|
||||||
|
return runningAppProcesses.any { it.processName == context.packageName &&
|
||||||
|
it.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||||
|
|
Loading…
Reference in New Issue