From 821980e93839c00bfb5718fcd92eb13d52f05653 Mon Sep 17 00:00:00 2001 From: azuo Date: Mon, 13 Sep 2021 23:34:54 +0800 Subject: [PATCH] Use getBroadcast instead of getActivity for ACTION_REFRESH intents. --- .../anotherwidget/helpers/IntentHelper.kt | 23 +++++++++++---- .../receivers/WidgetClickListenerReceiver.kt | 11 +++---- .../ui/fragments/tabs/GesturesFragment.kt | 29 +++++-------------- .../anotherwidget/ui/widgets/AlignedWidget.kt | 22 +++++++------- .../anotherwidget/ui/widgets/ClockWidget.kt | 2 +- .../ui/widgets/StandardWidget.kt | 22 +++++++------- 6 files changed, 54 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt index 712d533..a0f2b61 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/IntentHelper.kt @@ -1,5 +1,6 @@ package com.tommasoberlose.anotherwidget.helpers +import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.ContentUris @@ -27,6 +28,13 @@ object IntentHelper { const val DO_NOTHING_OPTION = "DO_NOTHING" const val REFRESH_WIDGET_OPTION = "REFRESH_WIDGET" + fun getPendingIntent(context: Context, requestCode: Int, intent: Intent, flags: Int): PendingIntent { + return if (intent.flags and Intent.FLAG_ACTIVITY_NEW_TASK == Intent.FLAG_ACTIVITY_NEW_TASK) + PendingIntent.getActivity(context, requestCode, intent, flags) + else + PendingIntent.getBroadcast(context, requestCode, intent, 0) + } + fun getWidgetUpdateIntent(context: Context): Intent { val widgetManager = AppWidgetManager.getInstance(context) val widgetComponent = ComponentName(context, MainWidget::class.java) @@ -40,7 +48,6 @@ object IntentHelper { private fun getWidgetRefreshIntent(context: Context): Intent { return Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_REFRESH - flags = Intent.FLAG_ACTIVITY_NEW_TASK } } @@ -50,11 +57,10 @@ object IntentHelper { //mapIntent.`package` = "com.google.android.apps.maps" return if (mapIntent.resolveActivity(context.packageManager) != null) { - mapIntent + mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } else { val map = "https://www.google.com/maps/search/?api=1&query=${Uri.encode(address)}" - val i = Intent(Intent.ACTION_VIEW, Uri.parse(map)) - i + Intent(Intent.ACTION_VIEW, Uri.parse(map)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } } @@ -62,7 +68,6 @@ object IntentHelper { return when (Preferences.weatherAppPackage) { DEFAULT_OPTION -> { Intent(Intent.ACTION_VIEW).apply { - addCategory(Intent.CATEGORY_DEFAULT) flags = Intent.FLAG_ACTIVITY_NEW_TASK data = Uri.parse("dynact://velour/weather/ProxyActivity") component = ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velour.DynamicActivityTrampoline") @@ -98,6 +103,7 @@ object IntentHelper { return when (Preferences.calendarAppPackage) { DEFAULT_OPTION -> { Intent(Intent.ACTION_VIEW).apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK data = calendarUri } } @@ -111,6 +117,8 @@ object IntentHelper { val pm: PackageManager = context.packageManager try { pm.getLaunchIntentForPackage(Preferences.calendarAppPackage)!!.apply { + addCategory(Intent.CATEGORY_LAUNCHER) + flags = Intent.FLAG_ACTIVITY_NEW_TASK action = Intent.ACTION_VIEW data = calendarUri } @@ -209,7 +217,7 @@ object IntentHelper { } fun getBatteryIntent(): Intent { - return Intent(Intent.ACTION_POWER_USAGE_SUMMARY) + return Intent(Intent.ACTION_POWER_USAGE_SUMMARY).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } fun getMusicIntent(context: Context): Intent { @@ -222,6 +230,7 @@ object IntentHelper { try { pm.getLaunchIntentForPackage(Preferences.mediaPlayerPackage)!!.apply { addCategory(Intent.CATEGORY_LAUNCHER) + flags = Intent.FLAG_ACTIVITY_NEW_TASK } } catch (e: Exception) { Intent() @@ -235,6 +244,7 @@ object IntentHelper { return try { pm.getLaunchIntentForPackage("com.google.android.apps.fitness")!!.apply { addCategory(Intent.CATEGORY_LAUNCHER) + flags = Intent.FLAG_ACTIVITY_NEW_TASK } } catch (e: Exception) { Intent() @@ -246,6 +256,7 @@ object IntentHelper { return try { pm.getLaunchIntentForPackage(Preferences.lastNotificationPackage)!!.apply { addCategory(Intent.CATEGORY_LAUNCHER) + flags = Intent.FLAG_ACTIVITY_NEW_TASK } } catch (e: Exception) { Intent() diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/WidgetClickListenerReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/WidgetClickListenerReceiver.kt index 9e022d8..2f1273c 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/WidgetClickListenerReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/WidgetClickListenerReceiver.kt @@ -16,14 +16,15 @@ class WidgetClickListenerReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action == Actions.ACTION_OPEN_WEATHER_INTENT) { try { - if (Preferences.weatherAppPackage == IntentHelper.REFRESH_WIDGET_OPTION) { - context.sendBroadcast(IntentHelper.getWeatherIntent(context)) - } else { - context.startActivity(IntentHelper.getWeatherIntent(context)) + IntentHelper.getWeatherIntent(context).run { + if (flags and Intent.FLAG_ACTIVITY_NEW_TASK == Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(this) + else + context.sendBroadcast(this) } } catch (e: Exception) { e.printStackTrace() - val uri = Uri.parse("http://www.google.com/search?q=weather") + val uri = Uri.parse("https://www.google.com/search?q=weather") val i = Intent(Intent.ACTION_VIEW, uri) i.flags = Intent.FLAG_ACTIVITY_NEW_TASK try { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/GesturesFragment.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/GesturesFragment.kt index d16aff1..c30caa0 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/GesturesFragment.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/GesturesFragment.kt @@ -102,15 +102,7 @@ class GesturesFragment : Fragment() { it == IntentHelper.DO_NOTHING_OPTION -> getString(R.string.gestures_do_nothing) it == IntentHelper.REFRESH_WIDGET_OPTION -> getString(R.string.gestures_refresh_widget) it != IntentHelper.DEFAULT_OPTION -> it - else -> { - if (IntentHelper.getCalendarIntent(requireContext()).isDefaultSet(requireContext())) { - getString( - R.string.default_calendar_app - ) - } else { - getString(R.string.gestures_do_nothing) - } - } + else -> getString(R.string.default_calendar_app) } } } @@ -127,15 +119,7 @@ class GesturesFragment : Fragment() { it == IntentHelper.DO_NOTHING_OPTION -> getString(R.string.gestures_do_nothing) it == IntentHelper.REFRESH_WIDGET_OPTION -> getString(R.string.gestures_refresh_widget) it != IntentHelper.DEFAULT_OPTION -> it - else -> { - if (IntentHelper.getClockIntent(requireContext()).isDefaultSet(requireContext())) { - getString( - R.string.default_clock_app - ) - } else { - getString(R.string.gestures_do_nothing) - } - } + else -> getString(R.string.default_clock_app) } } } @@ -173,9 +157,12 @@ class GesturesFragment : Fragment() { } binding.actionCalendarApp.setOnClickListener { - startActivityForResult(Intent(requireContext(), ChooseApplicationActivity::class.java).apply { - putExtra(Constants.RESULT_APP_PACKAGE, Preferences.calendarAppPackage) - }, RequestCode.CALENDAR_APP_REQUEST_CODE.code) + startActivityForResult( + Intent(requireContext(), ChooseApplicationActivity::class.java).apply { + putExtra(Constants.RESULT_APP_PACKAGE, Preferences.calendarAppPackage) + }, + RequestCode.CALENDAR_APP_REQUEST_CODE.code + ) } binding.actionClockApp.setOnClickListener { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt index d9ab2d9..2027e43 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt @@ -54,7 +54,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { "setImageAlpha", ColorHelper.getBackgroundAlpha(context.isDarkTheme()) ) - val refreshIntent = PendingIntent.getActivity( + val refreshIntent = IntentHelper.getPendingIntent( context, appWidgetId, IntentHelper.getWidgetUpdateIntent(context), @@ -126,7 +126,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { BitmapHelper.getBitmapFromView(bindingView.date, draw = false, width = bindingView.date.width, height = bindingView.date.height) ) - val calPIntent = PendingIntent.getActivity( + val calPIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getCalendarIntent(context), @@ -177,7 +177,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { } // Event intent - val eventIntent = PendingIntent.getActivity( + val eventIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getEventIntent(context, nextEvent), @@ -214,7 +214,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { // Event information if (nextEvent.address != "" && Preferences.secondRowInformation == 1) { - val mapIntent = PendingIntent.getActivity( + val mapIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getGoogleMapsIntentFromAddress(context, nextEvent.address), @@ -222,7 +222,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { ) views.setOnClickPendingIntent(R.id.sub_line_rect, mapIntent) } else { - val pIntentDetail = PendingIntent.getActivity( + val pIntentDetail = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getCalendarIntent(context, nextEvent.startDate), @@ -245,7 +245,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { when (provider) { Constants.GlanceProviderId.PLAYING_SONG -> { if (MediaPlayerHelper.isSomeonePlaying(context)) { - val musicIntent = PendingIntent.getActivity( + val musicIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getMusicIntent(context), @@ -260,7 +260,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { if (Preferences.showNextAlarm) { val nextAlarm = AlarmHelper.getNextAlarm(context) if (nextAlarm != "") { - val alarmIntent = PendingIntent.getActivity( + val alarmIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getClockIntent(context), @@ -276,7 +276,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { if (Preferences.showBatteryCharging) { BatteryHelper.updateBatteryInfo(context) if (Preferences.isCharging || Preferences.isBatteryLevelLow) { - val batteryIntent = PendingIntent.getActivity( + val batteryIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getBatteryIntent(), @@ -295,7 +295,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { } Constants.GlanceProviderId.GOOGLE_FIT_STEPS -> { if (Preferences.showDailySteps && Preferences.googleFitSteps > 0) { - val fitIntent = PendingIntent.getActivity( + val fitIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getFitIntent(context), @@ -316,7 +316,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { remotePackageContext, Preferences.lastNotificationIcon) } - val notificationIntent = PendingIntent.getActivity( + val notificationIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getNotificationIntent(context), @@ -340,7 +340,7 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { Constants.GlanceProviderId.EVENTS -> { if (Preferences.showEventsAsGlanceProvider&& Preferences.showEvents && context.checkGrantedPermission( Manifest.permission.READ_CALENDAR) && nextEvent != null) { - val pIntentDetail = PendingIntent.getActivity( + val pIntentDetail = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getEventIntent( diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/ClockWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/ClockWidget.kt index 44c750a..ca081a5 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/ClockWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/ClockWidget.kt @@ -39,7 +39,7 @@ class ClockWidget(val context: Context) { TypedValue.COMPLEX_UNIT_SP, Preferences.clockTextSize.toPixel(context) / 5 * 2 ) - val clockPIntent = PendingIntent.getActivity( + val clockPIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getClockIntent(context), diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt index 78f2d56..bdc6039 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt @@ -57,7 +57,7 @@ class StandardWidget(val context: Context) { "setImageAlpha", ColorHelper.getBackgroundAlpha(context.isDarkTheme()) ) - val refreshIntent = PendingIntent.getActivity( + val refreshIntent = IntentHelper.getPendingIntent( context, appWidgetId, IntentHelper.getWidgetUpdateIntent(context), @@ -129,7 +129,7 @@ class StandardWidget(val context: Context) { BitmapHelper.getBitmapFromView(bindingView.date, draw = false, width = bindingView.date.width, height = bindingView.date.height) ) - val calPIntent = PendingIntent.getActivity( + val calPIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getCalendarIntent(context), @@ -207,7 +207,7 @@ class StandardWidget(val context: Context) { } // Event intent - val eventIntent = PendingIntent.getActivity( + val eventIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getEventIntent(context, nextEvent), @@ -240,7 +240,7 @@ class StandardWidget(val context: Context) { // Event information if (nextEvent.address != "" && Preferences.secondRowInformation == 1) { - val mapIntent = PendingIntent.getActivity( + val mapIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getGoogleMapsIntentFromAddress(context, nextEvent.address), @@ -248,7 +248,7 @@ class StandardWidget(val context: Context) { ) views.setOnClickPendingIntent(R.id.sub_line_rect, mapIntent) } else { - val pIntentDetail = PendingIntent.getActivity( + val pIntentDetail = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getCalendarIntent(context, nextEvent.startDate), @@ -273,7 +273,7 @@ class StandardWidget(val context: Context) { when (provider) { Constants.GlanceProviderId.PLAYING_SONG -> { if (MediaPlayerHelper.isSomeonePlaying(context)) { - val musicIntent = PendingIntent.getActivity( + val musicIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getMusicIntent(context), @@ -288,7 +288,7 @@ class StandardWidget(val context: Context) { if (Preferences.showNextAlarm) { val nextAlarm = AlarmHelper.getNextAlarm(context) if (nextAlarm != "") { - val alarmIntent = PendingIntent.getActivity( + val alarmIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getClockIntent(context), @@ -304,7 +304,7 @@ class StandardWidget(val context: Context) { if (Preferences.showBatteryCharging) { BatteryHelper.updateBatteryInfo(context) if (Preferences.isCharging || Preferences.isBatteryLevelLow) { - val batteryIntent = PendingIntent.getActivity( + val batteryIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getBatteryIntent(), @@ -323,7 +323,7 @@ class StandardWidget(val context: Context) { } Constants.GlanceProviderId.GOOGLE_FIT_STEPS -> { if (Preferences.showDailySteps && Preferences.googleFitSteps > 0) { - val fitIntent = PendingIntent.getActivity( + val fitIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getFitIntent(context), @@ -344,7 +344,7 @@ class StandardWidget(val context: Context) { remotePackageContext, Preferences.lastNotificationIcon) } - val notificationIntent = PendingIntent.getActivity( + val notificationIntent = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getNotificationIntent(context), @@ -368,7 +368,7 @@ class StandardWidget(val context: Context) { Constants.GlanceProviderId.EVENTS -> { if (Preferences.showEventsAsGlanceProvider&& Preferences.showEvents && context.checkGrantedPermission( Manifest.permission.READ_CALENDAR) && nextEvent != null) { - val pIntentDetail = PendingIntent.getActivity( + val pIntentDetail = IntentHelper.getPendingIntent( context, widgetID, IntentHelper.getEventIntent(