From be87647e2ade2f442159e60da81d1c5a57eba8a9 Mon Sep 17 00:00:00 2001 From: Winson Chung Date: Wed, 14 May 2014 14:15:20 -0700 Subject: [PATCH] Adding support for dynamically resolved default layouts. Change-Id: Id05c60b93423ce37dd06a126ecf33c74a0ba1d62 --- ...orkspace.xml => default_workspace_4x4.xml} | 0 ... => default_workspace_4x4_no_all_apps.xml} | 0 .../default_workspace_5x5.xml} | 55 ++++++++++++ res/xml/default_workspace_5x5_no_all_apps.xml | 56 ++++++++++++ .../default_workspace_5x6.xml} | 0 res/xml/default_workspace_5x6_no_all_apps.xml | 56 ++++++++++++ src/com/android/launcher3/DeviceProfile.java | 86 ++++++++++++------- src/com/android/launcher3/DynamicGrid.java | 39 +++++---- .../android/launcher3/LauncherProvider.java | 6 +- 9 files changed, 250 insertions(+), 48 deletions(-) rename res/xml/{default_workspace.xml => default_workspace_4x4.xml} (100%) rename res/xml/{default_workspace_no_all_apps.xml => default_workspace_4x4_no_all_apps.xml} (100%) rename res/{xml-sw720dp/default_workspace.xml => xml/default_workspace_5x5.xml} (51%) create mode 100644 res/xml/default_workspace_5x5_no_all_apps.xml rename res/{xml-sw600dp/default_workspace.xml => xml/default_workspace_5x6.xml} (100%) create mode 100644 res/xml/default_workspace_5x6_no_all_apps.xml diff --git a/res/xml/default_workspace.xml b/res/xml/default_workspace_4x4.xml similarity index 100% rename from res/xml/default_workspace.xml rename to res/xml/default_workspace_4x4.xml diff --git a/res/xml/default_workspace_no_all_apps.xml b/res/xml/default_workspace_4x4_no_all_apps.xml similarity index 100% rename from res/xml/default_workspace_no_all_apps.xml rename to res/xml/default_workspace_4x4_no_all_apps.xml diff --git a/res/xml-sw720dp/default_workspace.xml b/res/xml/default_workspace_5x5.xml similarity index 51% rename from res/xml-sw720dp/default_workspace.xml rename to res/xml/default_workspace_5x5.xml index 1c1d70e92c..9bec86aa8f 100644 --- a/res/xml-sw720dp/default_workspace.xml +++ b/res/xml/default_workspace_5x5.xml @@ -58,4 +58,59 @@ launcher:y="3" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/xml/default_workspace_5x5_no_all_apps.xml b/res/xml/default_workspace_5x5_no_all_apps.xml new file mode 100644 index 0000000000..f54a20425b --- /dev/null +++ b/res/xml/default_workspace_5x5_no_all_apps.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + diff --git a/res/xml-sw600dp/default_workspace.xml b/res/xml/default_workspace_5x6.xml similarity index 100% rename from res/xml-sw600dp/default_workspace.xml rename to res/xml/default_workspace_5x6.xml diff --git a/res/xml/default_workspace_5x6_no_all_apps.xml b/res/xml/default_workspace_5x6_no_all_apps.xml new file mode 100644 index 0000000000..f54a20425b --- /dev/null +++ b/res/xml/default_workspace_5x6_no_all_apps.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + diff --git a/src/com/android/launcher3/DeviceProfile.java b/src/com/android/launcher3/DeviceProfile.java index e67ec197a2..8470b39a33 100644 --- a/src/com/android/launcher3/DeviceProfile.java +++ b/src/com/android/launcher3/DeviceProfile.java @@ -43,16 +43,18 @@ import java.util.Comparator; class DeviceProfileQuery { + DeviceProfile profile; float widthDps; float heightDps; float value; PointF dimens; - DeviceProfileQuery(float w, float h, float v) { - widthDps = w; - heightDps = h; + DeviceProfileQuery(DeviceProfile p, float v) { + widthDps = p.minWidthDps; + heightDps = p.minHeightDps; value = v; - dimens = new PointF(w, h); + dimens = new PointF(widthDps, heightDps); + profile = p; } } @@ -72,6 +74,9 @@ public class DeviceProfile { private int iconDrawablePaddingOriginalPx; private float hotseatIconSize; + int defaultLayoutId; + int defaultNoAllAppsLayoutId; + boolean isLandscape; boolean isTablet; boolean isLargeTablet; @@ -127,7 +132,7 @@ public class DeviceProfile { private ArrayList mCallbacks = new ArrayList(); DeviceProfile(String n, float w, float h, float r, float c, - float is, float its, float hs, float his) { + float is, float its, float hs, float his, int dlId, int dnalId) { // Ensure that we have an odd number of hotseat items (since we need to place all apps) if (!LauncherAppState.isDisableAllApps() && hs % 2 == 0) { throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces"); @@ -142,6 +147,8 @@ public class DeviceProfile { iconTextSize = its; numHotseatIcons = hs; hotseatIconSize = his; + defaultLayoutId = dlId; + defaultNoAllAppsLayoutId = dnalId; } DeviceProfile(Context context, @@ -182,29 +189,32 @@ public class DeviceProfile { overviewModeScaleFactor = res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f; - // Interpolate the rows + // Find the closes profile given the width/height for (DeviceProfile p : profiles) { - points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows)); + points.add(new DeviceProfileQuery(p, 0f)); } - numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points)); - // Interpolate the columns - points.clear(); - for (DeviceProfile p : profiles) { - points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns)); - } - numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points)); - // Interpolate the hotseat length - points.clear(); - for (DeviceProfile p : profiles) { - points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons)); - } - numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points)); + DeviceProfile closestProfile = findClosestDeviceProfile(minWidth, minHeight, points); + + // Snap to the closest row count + numRows = closestProfile.numRows; + + // Snap to the closest column count + numColumns = closestProfile.numColumns; + + // Snap to the closest hotseat size + numHotseatIcons = closestProfile.numHotseatIcons; hotseatAllAppsRank = (int) (numHotseatIcons / 2); + // Snap to the closest default layout id + defaultLayoutId = closestProfile.defaultLayoutId; + + // Snap to the closest default no all-apps layout id + defaultNoAllAppsLayoutId = closestProfile.defaultNoAllAppsLayoutId; + // Interpolate the icon size points.clear(); for (DeviceProfile p : profiles) { - points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize)); + points.add(new DeviceProfileQuery(p, p.iconSize)); } iconSize = invDistWeightedInterpolate(minWidth, minHeight, points); // AllApps uses the original non-scaled icon size @@ -213,7 +223,7 @@ public class DeviceProfile { // Interpolate the icon text size points.clear(); for (DeviceProfile p : profiles) { - points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize)); + points.add(new DeviceProfileQuery(p, p.iconTextSize)); } iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points); iconDrawablePaddingOriginalPx = @@ -224,7 +234,7 @@ public class DeviceProfile { // Interpolate the hotseat icon size points.clear(); for (DeviceProfile p : profiles) { - points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize)); + points.add(new DeviceProfileQuery(p, p.hotseatIconSize)); } // Hotseat hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points); @@ -398,6 +408,28 @@ public class DeviceProfile { return (float) (1f / Math.pow(d, pow)); } + /** Returns the closest device profile given the width and height and a list of profiles */ + private DeviceProfile findClosestDeviceProfile(float width, float height, + ArrayList points) { + return findClosestDeviceProfiles(width, height, points).get(0).profile; + } + + /** Returns the closest device profiles ordered by closeness to the specified width and height */ + private ArrayList findClosestDeviceProfiles(float width, float height, + ArrayList points) { + final PointF xy = new PointF(width, height); + + // Sort the profiles by their closeness to the dimensions + ArrayList pointsByNearness = points; + Collections.sort(pointsByNearness, new Comparator() { + public int compare(DeviceProfileQuery a, DeviceProfileQuery b) { + return (int) (dist(xy, a.dimens) - dist(xy, b.dimens)); + } + }); + + return pointsByNearness; + } + private float invDistWeightedInterpolate(float width, float height, ArrayList points) { float sum = 0; @@ -406,12 +438,8 @@ public class DeviceProfile { float kNearestNeighbors = 3; final PointF xy = new PointF(width, height); - ArrayList pointsByNearness = points; - Collections.sort(pointsByNearness, new Comparator() { - public int compare(DeviceProfileQuery a, DeviceProfileQuery b) { - return (int) (dist(xy, a.dimens) - dist(xy, b.dimens)); - } - }); + ArrayList pointsByNearness = findClosestDeviceProfiles(width, height, + points); for (int i = 0; i < pointsByNearness.size(); ++i) { DeviceProfileQuery p = pointsByNearness.get(i); diff --git a/src/com/android/launcher3/DynamicGrid.java b/src/com/android/launcher3/DynamicGrid.java index 447bb1cd87..94a07d7064 100644 --- a/src/com/android/launcher3/DynamicGrid.java +++ b/src/com/android/launcher3/DynamicGrid.java @@ -60,36 +60,41 @@ public class DynamicGrid { DEFAULT_ICON_SIZE_PX = pxFromDp(DEFAULT_ICON_SIZE_DP, dm); // Our phone profiles include the bar sizes in each orientation deviceProfiles.add(new DeviceProfile("Super Short Stubby", - 255, 300, 2, 3, 48, 13, (hasAA ? 5 : 5), 48)); + 255, 300, 2, 3, 48, 13, (hasAA ? 3 : 5), 48, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Shorter Stubby", - 255, 400, 3, 3, 48, 13, (hasAA ? 5 : 5), 48)); + 255, 400, 3, 3, 48, 13, (hasAA ? 3 : 5), 48, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Short Stubby", - 275, 420, 3, 4, 48, 13, (hasAA ? 5 : 5), 48)); + 275, 420, 3, 4, 48, 13, (hasAA ? 5 : 5), 48, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Stubby", - 255, 450, 3, 4, 48, 13, (hasAA ? 5 : 5), 48)); + 255, 450, 3, 4, 48, 13, (hasAA ? 5 : 5), 48, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Nexus S", - 296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 5), 48)); + 296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 5), 48, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Nexus 4", - 335, 567, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56)); + 335, 567, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Nexus 5", - 359, 567, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56)); + 359, 567, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 5), 56, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); deviceProfiles.add(new DeviceProfile("Large Phone", - 406, 694, 5, 5, 64, 14.4f, 5, 56)); + 406, 694, 5, 5, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5, + R.xml.default_workspace_5x5_no_all_apps)); // The tablet profile is odd in that the landscape orientation // also includes the nav bar on the side deviceProfiles.add(new DeviceProfile("Nexus 7", - 575, 904, 5, 6, 72, 14.4f, 7, 60)); + 575, 904, 5, 6, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6, + R.xml.default_workspace_5x6_no_all_apps)); // Larger tablet profiles always have system bars on the top & bottom deviceProfiles.add(new DeviceProfile("Nexus 10", - 727, 1207, 5, 6, 76, 14.4f, 7, 64)); - /* - deviceProfiles.add(new DeviceProfile("Nexus 7", - 600, 960, 5, 5, 72, 14.4f, 5, 60)); - deviceProfiles.add(new DeviceProfile("Nexus 10", - 800, 1280, 5, 5, 80, 14.4f, (hasAA ? 7 : 6), 64)); - */ + 727, 1207, 5, 6, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6, + R.xml.default_workspace_5x6_no_all_apps)); deviceProfiles.add(new DeviceProfile("20-inch Tablet", - 1527, 2527, 7, 7, 100, 20, 7, 72)); + 1527, 2527, 7, 7, 100, 20, 7, 72, R.xml.default_workspace_4x4, + R.xml.default_workspace_4x4_no_all_apps)); mMinWidth = dpiFromPx(minWidthPx, dm); mMinHeight = dpiFromPx(minHeightPx, dm); mProfile = new DeviceProfile(context, deviceProfiles, diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java index 437c2ad960..93708b5ed9 100644 --- a/src/com/android/launcher3/LauncherProvider.java +++ b/src/com/android/launcher3/LauncherProvider.java @@ -336,10 +336,12 @@ public class LauncherProvider extends ContentProvider { } private static int getDefaultWorkspaceResourceId() { + LauncherAppState app = LauncherAppState.getInstance(); + DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); if (LauncherAppState.isDisableAllApps()) { - return R.xml.default_workspace_no_all_apps; + return grid.defaultNoAllAppsLayoutId; } else { - return R.xml.default_workspace; + return grid.defaultLayoutId; } }