From 15d161f61bd97e937e95fe8a8e520a947113c7b1 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 1 Sep 2011 21:30:56 -0700 Subject: [PATCH] Ensure appwidgets are loaded before usage. Before accessing appwidget details, always ensure that configuration is loaded from disk. Change-Id: Ic71dd62fcdb1d1e5f7bae397efa8dce787434ba5 --- .../com/android/server/AppWidgetService.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/services/java/com/android/server/AppWidgetService.java b/services/java/com/android/server/AppWidgetService.java index f5fd6bd866a5f..9f4936df40b23 100644 --- a/services/java/com/android/server/AppWidgetService.java +++ b/services/java/com/android/server/AppWidgetService.java @@ -170,6 +170,7 @@ class AppWidgetService extends IAppWidgetService.Stub final ArrayList mAppWidgetIds = new ArrayList(); ArrayList mHosts = new ArrayList(); boolean mSafeMode; + boolean mStateLoaded; AppWidgetService(Context context) { mContext = context; @@ -180,8 +181,9 @@ class AppWidgetService extends IAppWidgetService.Stub public void systemReady(boolean safeMode) { mSafeMode = safeMode; - loadAppWidgetList(); - loadStateLocked(); + synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); + } // Register for the boot completed broadcast, so we can send the // ENABLE broacasts. If we try to send them now, they time out, @@ -209,6 +211,14 @@ class AppWidgetService extends IAppWidgetService.Stub mContext.registerReceiver(mBroadcastReceiver, sdFilter); } + private void ensureStateLoadedLocked() { + if (!mStateLoaded) { + loadAppWidgetList(); + loadStateLocked(); + mStateLoaded = true; + } + } + @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) @@ -285,6 +295,7 @@ class AppWidgetService extends IAppWidgetService.Stub public int allocateAppWidgetId(String packageName, int hostId) { int callingUid = enforceCallingUid(packageName); synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); int appWidgetId = mNextAppWidgetId++; Host host = lookupOrAddHostLocked(callingUid, packageName, hostId); @@ -304,6 +315,7 @@ class AppWidgetService extends IAppWidgetService.Stub public void deleteAppWidgetId(int appWidgetId) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); if (id != null) { deleteAppWidgetLocked(id); @@ -314,6 +326,7 @@ class AppWidgetService extends IAppWidgetService.Stub public void deleteHost(int hostId) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); int callingUid = getCallingUid(); Host host = lookupHostLocked(callingUid, hostId); if (host != null) { @@ -325,6 +338,7 @@ class AppWidgetService extends IAppWidgetService.Stub public void deleteAllHosts() { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); int callingUid = getCallingUid(); final int N = mHosts.size(); boolean changed = false; @@ -405,6 +419,7 @@ class AppWidgetService extends IAppWidgetService.Stub final long ident = Binder.clearCallingIdentity(); try { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); if (id == null) { throw new IllegalArgumentException("bad appWidgetId"); @@ -448,6 +463,7 @@ class AppWidgetService extends IAppWidgetService.Stub // Binds to a specific RemoteViewsService public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); if (id == null) { throw new IllegalArgumentException("bad appWidgetId"); @@ -499,6 +515,7 @@ class AppWidgetService extends IAppWidgetService.Stub // Unbinds from a specific RemoteViewsService public void unbindRemoteViewsService(int appWidgetId, Intent intent) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); // Unbind from the RemoteViewsService (which will trigger a callback to the bound // RemoteViewsAdapter) Pair key = Pair.create(appWidgetId, @@ -610,6 +627,7 @@ class AppWidgetService extends IAppWidgetService.Stub public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); if (id != null && id.provider != null && !id.provider.zombie) { return id.provider.info; @@ -620,6 +638,7 @@ class AppWidgetService extends IAppWidgetService.Stub public RemoteViews getAppWidgetViews(int appWidgetId) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); if (id != null) { return id.views; @@ -630,6 +649,7 @@ class AppWidgetService extends IAppWidgetService.Stub public List getInstalledProviders() { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); final int N = mInstalledProviders.size(); ArrayList result = new ArrayList(N); for (int i=0; i updatedViews) { int callingUid = enforceCallingUid(packageName); synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); Host host = lookupOrAddHostLocked(callingUid, packageName, hostId); host.callbacks = callbacks; @@ -778,6 +803,7 @@ class AppWidgetService extends IAppWidgetService.Stub public void stopListening(int hostId) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); Host host = lookupHostLocked(getCallingUid(), hostId); if (host != null) { host.callbacks = null; @@ -965,6 +991,7 @@ class AppWidgetService extends IAppWidgetService.Stub public int[] getAppWidgetIds(ComponentName provider) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); Provider p = lookupProviderLocked(provider); if (p != null && getCallingUid() == p.uid) { return getAppWidgetIds(p); @@ -1087,6 +1114,7 @@ class AppWidgetService extends IAppWidgetService.Stub void sendInitialBroadcasts() { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); final int N = mInstalledProviders.size(); for (int i=0; i=0; i--) { Provider p = mInstalledProviders.get(i); @@ -1428,6 +1457,7 @@ class AppWidgetService extends IAppWidgetService.Stub } if (added || changed) { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); Bundle extras = intent.getExtras(); if (changed || (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false))) { @@ -1449,6 +1479,7 @@ class AppWidgetService extends IAppWidgetService.Stub // The package is being updated. We'll receive a PACKAGE_ADDED shortly. } else { synchronized (mAppWidgetIds) { + ensureStateLoadedLocked(); for (String pkgName : pkgList) { removeProvidersForPackageLocked(pkgName); saveStateLocked();