android icon indicating copy to clipboard operation
android copied to clipboard

Allow background start activity for configure widget

Open TimoPtr opened this issue 9 months ago • 3 comments

Summary

Since Android 15, we need to opt-in to be able to start an activity in background. Even if we are not actually starting a new activity when pinning a widget the activity is put in background when dragging so we need to opt-in. This PR generalize the creation of the PendingIntent to avoid duplicating the logic on all our configure screens.

Checklist

  • [ ] New or updated tests have been added to cover the changes following the testing guidelines.
  • [x] The code follows the project's code style and best_practices.
  • [x] The changes have been thoroughly tested, and edge cases have been considered.
  • [x] Changes are backward compatible whenever feasible. Any breaking changes are documented in the changelog for users and/or in the code for developers depending on the relevance.

Any other notes

Fixes #5349

TimoPtr avatar May 23 '25 08:05 TimoPtr

Tested the debug APK here but still got the same error, here are some more logs just above in case its helpful

2025-05-23 10:38:51.350 19275-19275 ButtonWidget            io....stant.companion.android.debug  D  Broadcast received: 
                                                                                                    Broadcast action: android.appwidget.action.APPWIDGET_ENABLED
                                                                                                    AppWidgetId: -1
2025-05-23 10:38:51.356 19275-19275 ButtonWidget            io....stant.companion.android.debug  D  Broadcast received: 
                                                                                                    Broadcast action: android.appwidget.action.APPWIDGET_UPDATE
                                                                                                    AppWidgetId: -1
2025-05-23 10:38:51.627 19275-19275 VRI[Button...eActivity] io....stant.companion.android.debug  D  visibilityChanged oldVisibility=true newVisibility=false
2025-05-23 10:38:51.662 19275-19275 AutofillManager         io....stant.companion.android.debug  I  onInvisibleForAutofill(): expiringResponse
2025-05-23 10:38:53.067  1663-6673  ActivityTaskManager     system_server                        E  Background activity launch blocked! goo.gle/android-bal [callingPackage: io.homeassistant.companion.android.debug; callingPackageTargetSdk: 34; callingUid: 10455; callingPid: -1; appSwitchState: 2; callingUidHasVisibleActivity: false; callingUidHasNonAppVisibleWindow: false; callingUidProcState: LAST_ACTIVITY; isCallingUidPersistentSystemProcess: false; allowBalExemptionForSystemProcess: false; intent: Intent { flg=0x20000000 cmp=io.homeassistant.companion.android.debug/io.homeassistant.companion.android.widgets.button.ButtonWidgetConfigureActivity (has extras) }; callerApp: null; balAllowedByPiCreator: BSP.ALLOW_BAL; balAllowedByPiCreatorWithHardening: BSP.ALLOW_BAL; resultIfPiCreatorAllowsBal: BAL_BLOCK; callerStartMode: MODE_BACKGROUND_ACTIVITY_START_ALLOWED; hasRealCaller: true; isCallForResult: false; isPendingIntent: true; autoOptInReason: null; realCallingPackage: com.google.android.apps.nexuslauncher; realCallingPackageTargetSdk: 35; realCallingUid: 10256; realCallingPid: 24412; realCallingUidHasVisibleActivity: true; realCallingUidHasNonAppVisibleWindow: true; realCallingUidProcState: TOP; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: PendingIntentRecord{3e82d3c io.homeassistant.companion.android.debug startActivity}; realCallerApp: ProcessRecord{7c70108 24412:com.google.android.apps.nexuslauncher/u0a256}; realInVisibleTask: true; balAllowedByPiSender: BSP.NONE; resultIfPiSenderAllowsBal: BAL_ALLOW_VISIBLE_WINDOW; realCallerStartMode: MODE_BACKGROUND_ACTIVITY_START_DENIED; balRequireOptInByPendingIntentCreator: true; balDontBringExistingBackgroundTaskStackToFg: true]

dshokouhi avatar May 23 '25 17:05 dshokouhi

I didn't find any way to make it works on Android 15. The only solution seems to move away from getting the result in the activity to get the result in a BroadcastReceiver which works on all the version.

TimoPtr avatar May 26 '25 12:05 TimoPtr

The only solution seems to move away from getting the result in the activity to get the result in a BroadcastReceiver which works on all the version.

I agree. The callback intent currently used already forwards the data to the AppWidgetProvider (which is a BroadcastReceiver) so that should be possible without changes to the flow for the user.

jpelgrom avatar May 26 '25 17:05 jpelgrom