From 2857f1c783e69461735a51159f9abdb85378e210 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 31 Mar 2016 11:27:13 -0700 Subject: Changing startListening to only fetch views which are bound Also associating a lastUpdateTime with every widget. This allows the host to query the widgets which were updated only after the last stopListening call. Change-Id: If9375cf2d8caa0ccca14b6649821d87ada1f3a84 --- .../server/appwidget/AppWidgetServiceImpl.java | 56 ++++++++++++++++------ 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'services/appwidget/java') diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java index 89b145e2f4c0..43551753774b 100644 --- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java +++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java @@ -742,8 +742,8 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku } @Override - public int[] startListening(IAppWidgetHost callbacks, String callingPackage, - int hostId, List updatedViews) { + public ParceledListSlice startListening(IAppWidgetHost callbacks, + String callingPackage, int hostId, int[] appWidgetIds, int[] updatedIds) { final int userId = UserHandle.getCallingUserId(); if (DEBUG) { @@ -760,21 +760,21 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku // sure the caller can only access hosts it owns. HostId id = new HostId(Binder.getCallingUid(), hostId, callingPackage); Host host = lookupOrAddHostLocked(id); - host.callbacks = callbacks; - updatedViews.clear(); - - ArrayList instances = host.widgets; - int N = instances.size(); - int[] updatedIds = new int[N]; + int N = appWidgetIds.length; + ArrayList outViews = new ArrayList<>(N); + RemoteViews rv; + int added = 0; for (int i = 0; i < N; i++) { - Widget widget = instances.get(i); - updatedIds[i] = widget.appWidgetId; - updatedViews.add(cloneIfLocalBinder(widget.getEffectiveViewsLocked())); + rv = host.getPendingViewsForId(appWidgetIds[i]); + if (rv != null) { + updatedIds[added] = appWidgetIds[i]; + outViews.add(rv); + added++; + } } - - return updatedIds; + return new ParceledListSlice<>(outViews); } } @@ -1882,6 +1882,10 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku } private void scheduleNotifyUpdateAppWidgetLocked(Widget widget, RemoteViews updateViews) { + long requestTime = SystemClock.uptimeMillis(); + if (widget != null) { + widget.lastUpdateTime = requestTime; + } if (widget == null || widget.provider == null || widget.provider.zombie || widget.host.callbacks == null || widget.host.zombie) { return; @@ -1891,6 +1895,7 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku args.arg1 = widget.host; args.arg2 = widget.host.callbacks; args.arg3 = updateViews; + args.arg4 = requestTime; args.argi1 = widget.appWidgetId; mCallbackHandler.obtainMessage( @@ -1899,9 +1904,10 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku } private void handleNotifyUpdateAppWidget(Host host, IAppWidgetHost callbacks, - int appWidgetId, RemoteViews views) { + int appWidgetId, RemoteViews views, long requestTime) { try { callbacks.updateAppWidget(appWidgetId, views); + host.lastWidgetUpdateTime = requestTime; } catch (RemoteException re) { synchronized (mLock) { Slog.e(TAG, "Widget host dead: " + host.id, re); @@ -3398,10 +3404,11 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku Host host = (Host) args.arg1; IAppWidgetHost callbacks = (IAppWidgetHost) args.arg2; RemoteViews views = (RemoteViews) args.arg3; + long requestTime = (Long) args.arg4; final int appWidgetId = args.argi1; args.recycle(); - handleNotifyUpdateAppWidget(host, callbacks, appWidgetId, views); + handleNotifyUpdateAppWidget(host, callbacks, appWidgetId, views, requestTime); } break; case MSG_NOTIFY_PROVIDER_CHANGED: { @@ -3769,6 +3776,7 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku boolean zombie; // if we're in safe mode, don't prune this just because nobody references it int tag = TAG_UNDEFINED; // for use while saving state (the index) + long lastWidgetUpdateTime; // last time we were successfully able to send an update. public int getUserId() { return UserHandle.getUserId(id.uid); @@ -3790,6 +3798,23 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku return false; } + /** + * Returns the RemoveViews for the provided widget id if an update is pending + * for that widget. + */ + public RemoteViews getPendingViewsForId(int appWidgetId) { + long updateTime = lastWidgetUpdateTime; + int N = widgets.size(); + for (int i = 0; i < N; i++) { + Widget widget = widgets.get(i); + if (widget.appWidgetId == appWidgetId + && widget.lastUpdateTime > updateTime) { + return cloneIfLocalBinder(widget.getEffectiveViewsLocked()); + } + } + return null; + } + @Override public String toString() { return "Host{" + id + (zombie ? " Z" : "") + '}'; @@ -3860,6 +3885,7 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku RemoteViews maskedViews; Bundle options; Host host; + long lastUpdateTime; @Override public String toString() { -- cgit v1.2.3-59-g8ed1b