Merge "Don't set hard width/height for cling OK button." into jb-dev
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index 3a41255..45bb08a 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -547,13 +547,14 @@
         // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
         // by a broadcast receiver, and in order for it to work correctly, we need to know that
         // the AppWidgetService has already received and processed the same broadcast. Since there
-        // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
-        // we should have a more precise way of ensuring the AppWidgetService is up to date.
+        // is no guarantee about ordering of broadcast receipt, we just delay here. This is a
+        // workaround until we add a callback from AppWidgetService to AppWidgetHost when widget
+        // packages are added, updated or removed.
         postDelayed(new Runnable() {
            public void run() {
                updatePackages();
            }
-        }, 500);
+        }, 1500);
     }
 
     public void updatePackages() {
@@ -574,6 +575,9 @@
                 if (minSpanX <= LauncherModel.getCellCountX() &&
                         minSpanY <= LauncherModel.getCellCountY()) {
                     mWidgets.add(widget);
+                } else {
+                    Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
+                            widget.minWidth + ", " + widget.minHeight + ")");
                 }
             } else {
                 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index b2f27b9..9cae5e5 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -374,6 +374,13 @@
         IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
         registerReceiver(mCloseSystemDialogsReceiver, filter);
 
+        updateGlobalIcons();
+
+        // On large interfaces, we want the screen to auto-rotate based on the current orientation
+        unlockScreenOrientation(true);
+    }
+
+    private void updateGlobalIcons() {
         boolean searchVisible = false;
         boolean voiceVisible = false;
         // If we have a saved version of these external icons, we load them up immediately
@@ -396,9 +403,6 @@
             updateAppMarketIcon(sAppMarketIcon[coi]);
         }
         mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
-
-        // On large interfaces, we want the screen to auto-rotate based on the current orientation
-        unlockScreenOrientation(true);
     }
 
     private void checkForLocaleChange() {
@@ -682,6 +686,35 @@
             // Resets the previous all apps icon press state
             mAppsCustomizeContent.resetDrawableState();
         }
+        // It is possible that widgets can receive updates while launcher is not in the foreground.
+        // Consequently, the widgets will be inflated in the orientation of the foreground activity
+        // (framework issue). On resuming, we ensure that any widgets are inflated for the current
+        // orientation.
+        reinflateWidgetsIfNecessary();
+
+        // Again, as with the above scenario, it's possible that one or more of the global icons
+        // were updated in the wrong orientation.
+        updateGlobalIcons();
+    }
+
+    void reinflateWidgetsIfNecessary() {
+        for (LauncherAppWidgetInfo info: LauncherModel.getWidgets()) {
+            LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) info.hostView;
+            if (lahv != null && lahv.orientationChangedSincedInflation()) {
+                AppWidgetProviderInfo pInfo = lahv.getAppWidgetInfo();
+
+                // Remove the current widget which is inflated with the wrong orientation
+                getWorkspace().getParentCellLayoutForView(lahv).removeView(lahv);
+                // Re-inflate the widget using the correct orientation
+                AppWidgetHostView widget = mAppWidgetHost.createView(this, info.appWidgetId, pInfo);
+
+                // Add the new widget back
+                widget.setTag(info);
+                info.hostView = widget;
+                getWorkspace().addInScreen(widget, info.container, info.screen,
+                        info.cellX, info.cellY, info.spanX, info.spanY);
+            }
+        }
     }
 
     @Override
diff --git a/src/com/android/launcher2/LauncherAppWidgetHostView.java b/src/com/android/launcher2/LauncherAppWidgetHostView.java
index e34196e..9970c76 100644
--- a/src/com/android/launcher2/LauncherAppWidgetHostView.java
+++ b/src/com/android/launcher2/LauncherAppWidgetHostView.java
@@ -18,10 +18,14 @@
 
 import android.appwidget.AppWidgetHostView;
 import android.content.Context;
+import android.content.res.Configuration;
+import android.os.Bundle;
+import android.os.Parcel;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.RemoteViews;
 
 import com.android.launcher.R;
 
@@ -31,9 +35,12 @@
 public class LauncherAppWidgetHostView extends AppWidgetHostView {
     private CheckLongPressHelper mLongPressHelper;
     private LayoutInflater mInflater;
+    private Context mContext;
+    private int mPreviousOrientation;
 
     public LauncherAppWidgetHostView(Context context) {
         super(context);
+        mContext = context;
         mLongPressHelper = new CheckLongPressHelper(this);
         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }
@@ -43,6 +50,21 @@
         return mInflater.inflate(R.layout.appwidget_error, this, false);
     }
 
+    @Override
+    public void updateAppWidget(RemoteViews remoteViews) {
+        // Store the orientation in which the widget was inflated
+        mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
+        super.updateAppWidget(remoteViews);
+    }
+
+    public boolean orientationChangedSincedInflation() {
+        int orientation = mContext.getResources().getConfiguration().orientation;
+        if (mPreviousOrientation != orientation) {
+           return true;
+       }
+       return false;
+    }
+
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         // Consume any touch events for ourselves after longpress is triggered
         if (mLongPressHelper.hasPerformedLongPress()) {
diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java
index fc1a26d..1b17ef9 100644
--- a/src/com/android/launcher2/LauncherModel.java
+++ b/src/com/android/launcher2/LauncherModel.java
@@ -377,6 +377,10 @@
         return items;
     }
 
+    static ArrayList<LauncherAppWidgetInfo> getWidgets() {
+        return sAppWidgets;
+    }
+
     /**
      * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
      */