Fix 2223769 - Uninstalling a widget (via adb uninstall) crashed launcher2 and com.cooliris.media

If we haven't even initialized yet, don't respond to app list changes.

I think this will fix that bug.  It's possible that there's some other race in there somehow.
diff --git a/src/com/android/launcher2/LauncherApplication.java b/src/com/android/launcher2/LauncherApplication.java
index ee6006c..9b63524 100644
--- a/src/com/android/launcher2/LauncherApplication.java
+++ b/src/com/android/launcher2/LauncherApplication.java
@@ -17,7 +17,6 @@
 package com.android.launcher2;
 
 import android.app.Application;
-import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
@@ -27,7 +26,11 @@
 import dalvik.system.VMRuntime;
 
 public class LauncherApplication extends Application {
-    public static final LauncherModel sModel = new LauncherModel();
+    public final LauncherModel mModel;
+
+    public LauncherApplication() {
+        mModel = new LauncherModel(this);
+    }
 
     @Override
     public void onCreate() {
@@ -40,7 +43,7 @@
         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
         filter.addDataScheme("package");
-        registerReceiver(mApplicationsReceiver, filter);
+        registerReceiver(mModel, filter);
 
         // Register for changes to the favorites
         ContentResolver resolver = getContentResolver();
@@ -55,36 +58,26 @@
     public void onTerminate() {
         super.onTerminate();
 
-        unregisterReceiver(mApplicationsReceiver);
+        unregisterReceiver(mModel);
 
         ContentResolver resolver = getContentResolver();
         resolver.unregisterContentObserver(mFavoritesObserver);
     }
 
     /**
-     * Receives notifications when applications are added/removed.
-     */
-    private final BroadcastReceiver mApplicationsReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            sModel.onReceiveIntent(LauncherApplication.this, intent);
-        }
-    };
-
-    /**
      * Receives notifications whenever the user favorites have changed.
      */
     private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
         @Override
         public void onChange(boolean selfChange) {
             // TODO: lockAllApps();
-            sModel.setWorkspaceDirty();
-            sModel.startLoader(LauncherApplication.this, false);
+            mModel.setWorkspaceDirty();
+            mModel.startLoader(LauncherApplication.this, false);
         }
     };
 
     LauncherModel setLauncher(Launcher launcher) {
-        sModel.initialize(launcher);
-        return sModel;
+        mModel.initialize(launcher);
+        return mModel;
     }
 }
diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java
index 12dd016..ac8d3f3 100644
--- a/src/com/android/launcher2/LauncherModel.java
+++ b/src/com/android/launcher2/LauncherModel.java
@@ -16,6 +16,7 @@
 
 package com.android.launcher2;
 
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.ContentValues;
@@ -49,14 +50,16 @@
  * LauncherModel object held in a static. Also provide APIs for updating the database state
  * for the Launcher.
  */
-public class LauncherModel {
+public class LauncherModel extends BroadcastReceiver {
     static final boolean DEBUG_LOADERS = true;
     static final String TAG = "Launcher.Model";
 
+    private final LauncherApplication mApp;
     private final Object mLock = new Object();
     private DeferredHandler mHandler = new DeferredHandler();
     private Loader mLoader = new Loader();
 
+    private boolean mBeforeFirstLoad = true;
     private WeakReference<Callbacks> mCallbacks;
 
     private AllAppsList mAllAppsList = new AllAppsList();
@@ -73,6 +76,9 @@
         public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps);
     }
 
+    LauncherModel(LauncherApplication app) {
+        mApp = app;
+    }
 
     /**
      * Adds an item to the DB if it was not created previously, or move it to a new
@@ -264,7 +270,10 @@
      * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
      * ACTION_PACKAGE_CHANGED.
      */
-    public void onReceiveIntent(Context context, Intent intent) {
+    public void onReceive(Context context, Intent intent) {
+        // Use the app as the context.
+        context = mApp;
+
         final String packageName = intent.getData().getSchemeSpecificPart();
 
         ArrayList<ApplicationInfo> added = null;
@@ -274,6 +283,12 @@
         boolean remove = false;
 
         synchronized (mLock) {
+            if (mBeforeFirstLoad) {
+                // If we haven't even loaded yet, don't bother, since we'll just pick
+                // up the changes.
+                return;
+            }
+
             final String action = intent.getAction();
             final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
 
@@ -515,6 +530,8 @@
                 synchronized (mLock) {
                     allAppsSeq = mAllAppsSeq;
                     allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
+                    //Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
+                    //          + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
                 }
                 if (allAppsDirty) {
                     loadAllApps();
@@ -901,6 +918,8 @@
                 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
 
                 synchronized (mLock) {
+                    mBeforeFirstLoad = false;
+
                     mAllAppsList.clear();
                     if (apps != null) {
                         long t = SystemClock.uptimeMillis();