launcher3: Improve SimpleBroadcastReceiver.

Unless explicitly annotated, parameters in java are by
  default nullable. There are a few cases where a null context
  may be passed to the unregisterReceiverSafely function
  of SimpleBroadcastReceiver.

  To mitigate misuses or incorrect contexts being passed for
  register vs unregister, keep the context as a strong reference
  in the constructor.

  Also added NonNull annotations for any public callsites to
  enforce behavior.

Bug: 395019017, 395966395
Flag: NONE - bug fixed
Test: manual - presubmit
Change-Id: Ie371fa45cadceaf51cf184b446df9123ef27c337
This commit is contained in:
Adnan Begovic
2025-02-11 16:03:45 -08:00
parent 4425ab3062
commit 1b26bb4a74
16 changed files with 118 additions and 113 deletions

View File

@@ -300,10 +300,10 @@ public class InvariantDeviceProfile {
lifeCycle.addCloseable(() -> prefs.removeListener(prefListener,
FIXED_LANDSCAPE_MODE, ENABLE_TWOLINE_ALLAPPS_TOGGLE));
SimpleBroadcastReceiver localeReceiver = new SimpleBroadcastReceiver(
SimpleBroadcastReceiver localeReceiver = new SimpleBroadcastReceiver(context,
MAIN_EXECUTOR, i -> onConfigChanged(context));
localeReceiver.register(context, Intent.ACTION_LOCALE_CHANGED);
lifeCycle.addCloseable(() -> localeReceiver.unregisterReceiverSafely(context));
localeReceiver.register(Intent.ACTION_LOCALE_CHANGED);
lifeCycle.addCloseable(() -> localeReceiver.unregisterReceiverSafely());
}
private String initGrid(Context context, String gridName) {