Adding support for negative x and y in layout xml.
Fixing default configurations for some screen sizes.

> Negative values for x and y are parsed as distance from end
column and row respectively. e.g.  (-1, -2) => (3, 2) in a
4x4 grid

Bug: 26110981
Change-Id: I4ca30e225ed6e2a31610ab23235d2cd10e8d317c
diff --git a/src/com/android/launcher3/AutoInstallsLayout.java b/src/com/android/launcher3/AutoInstallsLayout.java
index 440e4e7..151048c 100644
--- a/src/com/android/launcher3/AutoInstallsLayout.java
+++ b/src/com/android/launcher3/AutoInstallsLayout.java
@@ -125,8 +125,12 @@
     private static final String ATTR_CLASS_NAME = "className";
     private static final String ATTR_TITLE = "title";
     private static final String ATTR_SCREEN = "screen";
+
+    // x and y can be specified as negative integers, in which case -1 represents the
+    // last row / column, -2 represents the second last, and so on.
     private static final String ATTR_X = "x";
     private static final String ATTR_Y = "y";
+
     private static final String ATTR_SPAN_X = "spanX";
     private static final String ATTR_SPAN_Y = "spanY";
     private static final String ATTR_ICON = "icon";
@@ -154,6 +158,8 @@
     protected final int mLayoutId;
 
     private final int mHotseatAllAppsRank;
+    private final int mRowCount;
+    private final int mColumnCount;
 
     private final long[] mTemp = new long[2];
     @Thunk final ContentValues mValues;
@@ -164,13 +170,6 @@
     public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
             LayoutParserCallback callback, Resources res,
             int layoutId, String rootTag) {
-        this(context, appWidgetHost, callback, res, layoutId, rootTag,
-                LauncherAppState.getInstance().getInvariantDeviceProfile().hotseatAllAppsRank);
-    }
-
-    public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
-            LayoutParserCallback callback, Resources res,
-            int layoutId, String rootTag, int hotseatAllAppsRank) {
         mContext = context;
         mAppWidgetHost = appWidgetHost;
         mCallback = callback;
@@ -181,7 +180,11 @@
 
         mSourceRes = res;
         mLayoutId = layoutId;
-        mHotseatAllAppsRank = hotseatAllAppsRank;
+
+        InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
+        mHotseatAllAppsRank = idp.hotseatAllAppsRank;
+        mRowCount = idp.numRows;
+        mColumnCount = idp.numColumns;
     }
 
     /**
@@ -261,8 +264,11 @@
 
         mValues.put(Favorites.CONTAINER, container);
         mValues.put(Favorites.SCREEN, screenId);
-        mValues.put(Favorites.CELLX, getAttributeValue(parser, ATTR_X));
-        mValues.put(Favorites.CELLY, getAttributeValue(parser, ATTR_Y));
+
+        mValues.put(Favorites.CELLX,
+                convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount);
+        mValues.put(Favorites.CELLY,
+                convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount);
 
         TagParser tagParser = tagParserMap.get(parser.getName());
         if (tagParser == null) {
@@ -648,6 +654,16 @@
         }
     }
 
+    private static String convertToDistanceFromEnd(String value, int endValue) {
+        if (!TextUtils.isEmpty(value)) {
+            int x = Integer.parseInt(value);
+            if (x < 0) {
+                return Integer.toString(endValue + x);
+            }
+        }
+        return value;
+    }
+
     /**
      * Return attribute value, attempting launcher-specific namespace first
      * before falling back to anonymous attribute.
diff --git a/src/com/android/launcher3/CommonAppTypeParser.java b/src/com/android/launcher3/CommonAppTypeParser.java
index 5314ecf..5ea4726 100644
--- a/src/com/android/launcher3/CommonAppTypeParser.java
+++ b/src/com/android/launcher3/CommonAppTypeParser.java
@@ -93,7 +93,7 @@
 
         public MyLayoutParser() {
             super(CommonAppTypeParser.this.mContext, null, CommonAppTypeParser.this,
-                    CommonAppTypeParser.this.mContext.getResources(), mResId, TAG_RESOLVE, 0);
+                    CommonAppTypeParser.this.mContext.getResources(), mResId, TAG_RESOLVE);
         }
 
         @Override
diff --git a/src/com/android/launcher3/DefaultLayoutParser.java b/src/com/android/launcher3/DefaultLayoutParser.java
index 7b91c67..2bba380 100644
--- a/src/com/android/launcher3/DefaultLayoutParser.java
+++ b/src/com/android/launcher3/DefaultLayoutParser.java
@@ -48,9 +48,8 @@
     }
 
     public DefaultLayoutParser(Context context, AppWidgetHost appWidgetHost,
-            LayoutParserCallback callback, Resources sourceRes, int layoutId, String rootTag,
-            int hotseatAllAppsRank) {
-        super(context, appWidgetHost, callback, sourceRes, layoutId, rootTag, hotseatAllAppsRank);
+            LayoutParserCallback callback, Resources sourceRes, int layoutId, String rootTag) {
+        super(context, appWidgetHost, callback, sourceRes, layoutId, rootTag);
     }
 
     @Override
diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java
index a91181d..b3a8bbc 100644
--- a/src/com/android/launcher3/InvariantDeviceProfile.java
+++ b/src/com/android/launcher3/InvariantDeviceProfile.java
@@ -173,9 +173,9 @@
         // width, height, #rows, #columns, #folder rows, #folder columns,
         // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
         predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
-                255, 300,     2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
+                255, 300,     2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_3x3));
         predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
-                255, 400,     3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
+                255, 400,     3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_3x3));
         predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
                 275, 420,     3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
         predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
@@ -196,7 +196,7 @@
         predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
                 727, 1207,    5, 6, 4, 5, 4, 76, 14.4f,  7, 76, R.xml.default_workspace_5x6));
         predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
-                1527, 2527,   7, 7, 6, 6, 4, 100, 20,  7, 72, R.xml.default_workspace_4x4));
+                1527, 2527,   7, 7, 6, 6, 4, 100, 20,  7, 72, R.xml.default_workspace_5x6));
         return predefinedDeviceProfiles;
     }