Bind converted widgets directly.

Before, Launcher2 sent an intent to
com.android.settings/com.android.settings.LauncherAppWidgetBinder
to bind any converted appwidgets. That doesn't seem to work anymore,
and is not how non-converted widgets are bound. Changing to
binding the widgets directly fixes a problem where the search
widget was not bound after an upgrade from Eclair to Master.

The search widget conversion problem was introduced in
Change I85d64defe155c0cad97fafef6a3db62c6cab504a
where I got rid of the built-in search widget.

This patch may break conversion of legacy built-in
clock and photo widgets, but I believe that was already broken
because of the non-functional widget binding mechanism.

Bug: http://b/2322160
diff --git a/src/com/android/launcher2/LauncherProvider.java b/src/com/android/launcher2/LauncherProvider.java
index 47d8991..d40e1ec 100644
--- a/src/com/android/launcher2/LauncherProvider.java
+++ b/src/com/android/launcher2/LauncherProvider.java
@@ -469,24 +469,16 @@
          * LauncherAppWidgetBinder to finish the actual binding.
          */
         private void convertWidgets(SQLiteDatabase db) {
+            final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
             final int[] bindSources = new int[] {
                     Favorites.ITEM_TYPE_WIDGET_CLOCK,
                     Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
                     Favorites.ITEM_TYPE_WIDGET_SEARCH,
             };
-            
-            final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
-            bindTargets.add(new ComponentName("com.android.alarmclock",
-                    "com.android.alarmclock.AnalogAppWidgetProvider"));
-            bindTargets.add(new ComponentName("com.android.camera",
-                    "com.android.camera.PhotoAppWidgetProvider"));
-            bindTargets.add(new ComponentName("com.android.quicksearchbox",
-                    "com.android.quicksearchbox.SearchWidgetProvider"));
 
             final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
             
             Cursor c = null;
-            boolean allocatedAppWidgets = false;
             
             db.beginTransaction();
             try {
@@ -524,8 +516,21 @@
 
                         String updateWhere = Favorites._ID + "=" + favoriteId;
                         db.update(TABLE_FAVORITES, values, updateWhere, null);
-                        
-                        allocatedAppWidgets = true;
+
+                        ComponentName cn = null;
+                        if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
+                            appWidgetManager.bindAppWidgetId(appWidgetId,
+                                    new ComponentName("com.android.alarmclock",
+                                    "com.android.alarmclock.AnalogAppWidgetProvider"));
+                        } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
+                            appWidgetManager.bindAppWidgetId(appWidgetId,
+                                    new ComponentName("com.android.camera",
+                                    "com.android.camera.PhotoAppWidgetProvider"));
+                        } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
+                            appWidgetManager.bindAppWidgetId(appWidgetId,
+                                    new ComponentName("com.android.quicksearchbox",
+                                    "com.android.quicksearchbox.SearchWidgetProvider"));
+                        }
                     } catch (RuntimeException ex) {
                         Log.e(TAG, "Problem allocating appWidgetId", ex);
                     }
@@ -540,34 +545,9 @@
                     c.close();
                 }
             }
-            
-            // If any appWidgetIds allocated, then launch over to binder
-            if (allocatedAppWidgets) {
-                launchAppWidgetBinder(bindSources, bindTargets);
-            }
         }
 
         /**
-         * Launch the widget binder that walks through the Launcher database,
-         * binding any matching widgets to the corresponding targets. We can't
-         * bind ourselves because our parent process can't obtain the
-         * BIND_APPWIDGET permission.
-         */
-        private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) {
-            final Intent intent = new Intent();
-            intent.setComponent(new ComponentName("com.android.settings",
-                    "com.android.settings.LauncherAppWidgetBinder"));
-            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-            
-            final Bundle extras = new Bundle();
-            extras.putIntArray(EXTRA_BIND_SOURCES, bindSources);
-            extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets);
-            intent.putExtras(extras);
-            
-            mContext.startActivity(intent);
-        }
-        
-        /**
          * Loads the default set of favorite packages from an xml file.
          *
          * @param db The database to write the values into
@@ -667,37 +647,9 @@
         }
 
         private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
-            final int[] bindSources = new int[] {
-                    Favorites.ITEM_TYPE_WIDGET_CLOCK,
-            };
-
-            final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
-            bindTargets.add(new ComponentName("com.android.alarmclock",
-                    "com.android.alarmclock.AnalogAppWidgetProvider"));
-
-            boolean allocatedAppWidgets = false;
-            
-            // Try binding to an analog clock widget
-            try {
-                int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
-
-                values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK);
-                values.put(Favorites.SPANX, 2);
-                values.put(Favorites.SPANY, 2);
-                values.put(Favorites.APPWIDGET_ID, appWidgetId);
-                db.insert(TABLE_FAVORITES, null, values);
-
-                allocatedAppWidgets = true;
-            } catch (RuntimeException ex) {
-                Log.e(TAG, "Problem allocating appWidgetId", ex);
-            }
-
-            // If any appWidgetIds allocated, then launch over to binder
-            if (allocatedAppWidgets) {
-                launchAppWidgetBinder(bindSources, bindTargets);
-            }
-
-            return allocatedAppWidgets;
+            ComponentName cn = new ComponentName("com.android.alarmclock",
+                    "com.android.alarmclock.AnalogAppWidgetProvider");
+            return addAppWidget(db, values, cn, 2, 2);
         }
         
         private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {