Merge "Use public API to get padding" into jb-dev
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 9ed824b..cd939db 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -171,4 +171,8 @@
         <attr name="title" format="reference" />
         <attr name="uri" format="string" />
     </declare-styleable>
+    <declare-styleable name="Extra">
+        <attr name="key" format="string" />
+        <attr name="value" format="string" />
+    </declare-styleable>
 </resources>
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index 586177b..fe9334b 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -758,7 +758,7 @@
             // drop in Workspace
             mLauncher.exitSpringLoadedDragMode();
         }
-        mLauncher.unlockScreenOrientation();
+        mLauncher.unlockScreenOrientation(false);
     }
 
     @Override
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 3c20a58..e5baf62 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -385,15 +385,8 @@
         }
         mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
 
-        final String forceEnableRotation =
-                SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
-
-        boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
-
         // On large interfaces, we want the screen to auto-rotate based on the current orientation
-        if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
-            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
-        }
+        unlockScreenOrientation(true);
     }
 
     private void checkForLocaleChange() {
@@ -1541,34 +1534,6 @@
             Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
             intent.setComponent(appWidget.configure);
             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
-            if (info != null) {
-                if (info.mimeType != null && !info.mimeType.isEmpty()) {
-                    intent.putExtra(InstallWidgetReceiver.
-                            EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
-
-                    final String mimeType = info.mimeType;
-                    final ClipData clipData = (ClipData) info.configurationData;
-                    final ClipDescription clipDesc = clipData.getDescription();
-                    for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
-                        if (clipDesc.getMimeType(i).equals(mimeType)) {
-                            final ClipData.Item item = clipData.getItemAt(i);
-                            final CharSequence stringData = item.getText();
-                            final Uri uriData = item.getUri();
-                            final Intent intentData = item.getIntent();
-                            final String key = InstallWidgetReceiver.
-                                    EXTRA_APPWIDGET_CONFIGURATION_DATA;
-                            if (uriData != null) {
-                                intent.putExtra(key, uriData);
-                            } else if (intentData != null) {
-                                intent.putExtra(key, intentData);
-                            } else if (stringData != null) {
-                                intent.putExtra(key, stringData);
-                            }
-                            break;
-                        }
-                    }
-                }
-            }
             startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
             mWidgetBeingBoundOrConfigured = info;
         } else {
@@ -3386,16 +3351,31 @@
         return oriMap[(d.getRotation() + indexOffset) % 4];
     }
 
-    public void lockScreenOrientation() {
-        setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
-                .getConfiguration().orientation));
+    public boolean isRotationEnabled() {
+        boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
+                FORCE_ENABLE_ROTATION_PROPERTY, "false"));
+        boolean enableRotation = forceEnableRotation ||
+                getResources().getBoolean(R.bool.allow_rotation);
+        return enableRotation;
     }
-    public void unlockScreenOrientation() {
-        mHandler.postDelayed(new Runnable() {
-            public void run() {
+    public void lockScreenOrientation() {
+        if (isRotationEnabled()) {
+            setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
+                    .getConfiguration().orientation));
+        }
+    }
+    public void unlockScreenOrientation(boolean immediate) {
+        if (isRotationEnabled()) {
+            if (immediate) {
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
+            } else {
+                mHandler.postDelayed(new Runnable() {
+                    public void run() {
+                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
+                    }
+                }, mRestoreScreenOrientationDelay);
             }
-        }, mRestoreScreenOrientationDelay);
+        }
     }
 
     /* Cling related */
diff --git a/src/com/android/launcher2/LauncherProvider.java b/src/com/android/launcher2/LauncherProvider.java
index 297c097..5e572a5 100644
--- a/src/com/android/launcher2/LauncherProvider.java
+++ b/src/com/android/launcher2/LauncherProvider.java
@@ -42,6 +42,7 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
+import android.os.Bundle;
 import android.provider.Settings;
 import android.text.TextUtils;
 import android.util.AttributeSet;
@@ -74,6 +75,9 @@
     static final String DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED =
             "DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED";
 
+    private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
+            "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
+
     /**
      * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
      * {@link AppWidgetHost#deleteHost()} is called during database creation.
@@ -219,6 +223,7 @@
         private static final String TAG_APPWIDGET = "appwidget";
         private static final String TAG_SHORTCUT = "shortcut";
         private static final String TAG_FOLDER = "folder";
+        private static final String TAG_EXTRA = "extra";
 
         private final Context mContext;
         private final AppWidgetHost mAppWidgetHost;
@@ -820,7 +825,7 @@
                     } else if (TAG_CLOCK.equals(name)) {
                         added = addClockWidget(db, values);
                     } else if (TAG_APPWIDGET.equals(name)) {
-                        added = addAppWidget(db, values, a, packageManager);
+                        added = addAppWidget(parser, attrs, type, db, values, a, packageManager);
                     } else if (TAG_SHORTCUT.equals(name)) {
                         long id = addUriShortcut(db, values, a);
                         added = id >= 0;
@@ -972,17 +977,18 @@
 
         private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
             ComponentName cn = getSearchWidgetProvider();
-            return addAppWidget(db, values, cn, 4, 1);
+            return addAppWidget(db, values, cn, 4, 1, null);
         }
 
         private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
             ComponentName cn = new ComponentName("com.android.alarmclock",
                     "com.android.alarmclock.AnalogAppWidgetProvider");
-            return addAppWidget(db, values, cn, 2, 2);
+            return addAppWidget(db, values, cn, 2, 2, null);
         }
 
-        private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a,
-                PackageManager packageManager) {
+        private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
+                SQLiteDatabase db, ContentValues values, TypedArray a,
+                PackageManager packageManager) throws XmlPullParserException, IOException {
 
             String packageName = a.getString(R.styleable.Favorite_packageName);
             String className = a.getString(R.styleable.Favorite_className);
@@ -1009,14 +1015,39 @@
             if (hasPackage) {
                 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
                 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
-                return addAppWidget(db, values, cn, spanX, spanY);
+
+                // Read the extras
+                Bundle extras = new Bundle();
+                int widgetDepth = parser.getDepth();
+                while ((type = parser.next()) != XmlPullParser.END_TAG ||
+                        parser.getDepth() > widgetDepth) {
+                    if (type != XmlPullParser.START_TAG) {
+                        continue;
+                    }
+
+                    TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
+                    if (TAG_EXTRA.equals(parser.getName())) {
+                        String key = ar.getString(R.styleable.Extra_key);
+                        String value = ar.getString(R.styleable.Extra_value);
+                        if (key != null && value != null) {
+                            extras.putString(key, value);
+                        } else {
+                            throw new RuntimeException("Widget extras must have a key and value");
+                        }
+                    } else {
+                        throw new RuntimeException("Widgets can contain only extras");
+                    }
+                    ar.recycle();
+                }
+
+                return addAppWidget(db, values, cn, spanX, spanY, extras);
             }
 
             return false;
         }
 
         private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
-                int spanX, int spanY) {
+                int spanX, int spanY, Bundle extras) {
             boolean allocatedAppWidgets = false;
             final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
 
@@ -1034,6 +1065,15 @@
 
                 // TODO: need to check return value
                 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
+
+                // Send a broadcast to configure the widget
+                if (extras != null && !extras.isEmpty()) {
+                    Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
+                    intent.setComponent(cn);
+                    intent.putExtras(extras);
+                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+                    mContext.sendBroadcast(intent);
+                }
             } catch (RuntimeException ex) {
                 Log.e(TAG, "Problem allocating appWidgetId", ex);
             }
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 404efc2..ec18fb3 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -386,7 +386,7 @@
     public void onDragEnd() {
         mIsDragOccuring = false;
         updateChildrenLayersEnabled();
-        mLauncher.unlockScreenOrientation();
+        mLauncher.unlockScreenOrientation(false);
     }
 
     /**