1
0
Fork 0

For #11183: Redirect user to homescreen after widget added

Co-authored-by:  hakkikaancaliskan <caliskanhkaan@gmail.com>
master
Sawyer Blatz 2020-06-17 14:17:33 -07:00 committed by Emily Kager
parent 77ed670558
commit a8ed913654
1 changed files with 16 additions and 0 deletions

View File

@ -4,6 +4,7 @@
package org.mozilla.gecko.search
import android.app.ActivityManager
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH
@ -36,6 +37,21 @@ class SearchWidgetProvider : AppWidgetProvider() {
override fun onEnabled(context: Context) {
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) {