Return widget uid only when it is bound to screen

This fixes NPE that could happen when we call stopListening before the
widget id is deleted or bound to the screen.

Test: N/A
Fix: 212651639
Change-Id: Id4f1a7c112c29a8c058e70de51ac75d02908530c
This commit is contained in:
Sihua Ma
2023-01-03 20:00:58 +00:00
parent b638b557b8
commit 71e6090f5b

View File

@@ -817,7 +817,8 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
if (host != null) {
host.callbacks = null;
pruneHostLocked(host);
mAppOpsManagerInternal.updateAppWidgetVisibility(host.getWidgetUids(), false);
mAppOpsManagerInternal.updateAppWidgetVisibility(host.getWidgetUidsIfBound(),
false);
}
}
}
@@ -888,12 +889,8 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
Host host = lookupHostLocked(id);
if (host != null) {
try {
mAppOpsManagerInternal.updateAppWidgetVisibility(host.getWidgetUids(), false);
} catch (NullPointerException e) {
Slog.e(TAG, "setAppWidgetHidden(): Getting host uids: " + host.toString(), e);
throw e;
}
mAppOpsManagerInternal.updateAppWidgetVisibility(host.getWidgetUidsIfBound(),
false);
}
}
}
@@ -4345,14 +4342,15 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
PendingHostUpdate.appWidgetRemoved(appWidgetId));
}
public SparseArray<String> getWidgetUids() {
public SparseArray<String> getWidgetUidsIfBound() {
final SparseArray<String> uids = new SparseArray<>();
for (int i = widgets.size() - 1; i >= 0; i--) {
final Widget widget = widgets.get(i);
if (widget.provider == null) {
if (DEBUG) {
Slog.e(TAG, "Widget with no provider " + widget.toString());
Slog.d(TAG, "Widget with no provider " + widget.toString());
}
continue;
}
final ProviderId providerId = widget.provider.id;
uids.put(providerId.uid, providerId.componentName.getPackageName());