2013-08-12 16:19:28 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2013-10-10 18:57:15 -07:00
|
|
|
import android.appwidget.AppWidgetHostView;
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
2013-08-12 16:19:28 -07:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.Paint.FontMetrics;
|
2013-10-25 15:24:24 -07:00
|
|
|
import android.graphics.Point;
|
2013-10-10 18:57:15 -07:00
|
|
|
import android.graphics.PointF;
|
2013-08-12 16:19:28 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.util.DisplayMetrics;
|
|
|
|
|
import android.util.TypedValue;
|
2013-10-25 15:24:24 -07:00
|
|
|
import android.view.Display;
|
2013-08-12 16:19:28 -07:00
|
|
|
import android.view.Gravity;
|
2013-10-25 15:24:24 -07:00
|
|
|
import android.view.Surface;
|
2013-08-12 16:19:28 -07:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup.LayoutParams;
|
2013-10-25 15:24:24 -07:00
|
|
|
import android.view.WindowManager;
|
2013-08-12 16:19:28 -07:00
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceProfileQuery {
|
|
|
|
|
float widthDps;
|
|
|
|
|
float heightDps;
|
|
|
|
|
float value;
|
|
|
|
|
PointF dimens;
|
|
|
|
|
|
|
|
|
|
DeviceProfileQuery(float w, float h, float v) {
|
|
|
|
|
widthDps = w;
|
|
|
|
|
heightDps = h;
|
|
|
|
|
value = v;
|
|
|
|
|
dimens = new PointF(w, h);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DeviceProfile {
|
2013-10-25 15:24:24 -07:00
|
|
|
public static interface DeviceProfileCallbacks {
|
|
|
|
|
public void onAvailableSizeChanged(DeviceProfile grid);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
String name;
|
|
|
|
|
float minWidthDps;
|
|
|
|
|
float minHeightDps;
|
|
|
|
|
float numRows;
|
|
|
|
|
float numColumns;
|
|
|
|
|
float numHotseatIcons;
|
2013-10-25 15:24:24 -07:00
|
|
|
private float iconSize;
|
|
|
|
|
private float iconTextSize;
|
|
|
|
|
private int iconDrawablePaddingOriginalPx;
|
|
|
|
|
private float hotseatIconSize;
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
boolean isLandscape;
|
|
|
|
|
boolean isTablet;
|
|
|
|
|
boolean isLargeTablet;
|
|
|
|
|
boolean transposeLayoutWithOrientation;
|
|
|
|
|
|
2013-10-10 18:57:15 -07:00
|
|
|
int desiredWorkspaceLeftRightMarginPx;
|
2013-08-12 16:19:28 -07:00
|
|
|
int edgeMarginPx;
|
2013-10-10 18:57:15 -07:00
|
|
|
Rect defaultWidgetPadding;
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
int widthPx;
|
|
|
|
|
int heightPx;
|
2013-08-22 16:15:50 -07:00
|
|
|
int availableWidthPx;
|
|
|
|
|
int availableHeightPx;
|
2013-10-29 14:45:58 -07:00
|
|
|
int defaultPageSpacingPx;
|
2013-10-25 15:24:24 -07:00
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
int iconSizePx;
|
|
|
|
|
int iconTextSizePx;
|
2013-10-25 15:24:24 -07:00
|
|
|
int iconDrawablePaddingPx;
|
2013-08-12 16:19:28 -07:00
|
|
|
int cellWidthPx;
|
|
|
|
|
int cellHeightPx;
|
|
|
|
|
int folderBackgroundOffset;
|
|
|
|
|
int folderIconSizePx;
|
|
|
|
|
int folderCellWidthPx;
|
|
|
|
|
int folderCellHeightPx;
|
|
|
|
|
int hotseatCellWidthPx;
|
|
|
|
|
int hotseatCellHeightPx;
|
|
|
|
|
int hotseatIconSizePx;
|
|
|
|
|
int hotseatBarHeightPx;
|
2013-09-03 17:48:37 -07:00
|
|
|
int hotseatAllAppsRank;
|
|
|
|
|
int allAppsNumRows;
|
|
|
|
|
int allAppsNumCols;
|
2013-08-12 16:19:28 -07:00
|
|
|
int searchBarSpaceWidthPx;
|
|
|
|
|
int searchBarSpaceMaxWidthPx;
|
|
|
|
|
int searchBarSpaceHeightPx;
|
|
|
|
|
int searchBarHeightPx;
|
|
|
|
|
int pageIndicatorHeightPx;
|
|
|
|
|
|
2013-10-25 15:24:24 -07:00
|
|
|
private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
DeviceProfile(String n, float w, float h, float r, float c,
|
|
|
|
|
float is, float its, float hs, float his) {
|
2013-09-03 17:48:37 -07:00
|
|
|
// Ensure that we have an odd number of hotseat items (since we need to place all apps)
|
|
|
|
|
if (!AppsCustomizePagedView.DISABLE_ALL_APPS && hs % 2 == 0) {
|
|
|
|
|
throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
name = n;
|
|
|
|
|
minWidthDps = w;
|
|
|
|
|
minHeightDps = h;
|
|
|
|
|
numRows = r;
|
|
|
|
|
numColumns = c;
|
|
|
|
|
iconSize = is;
|
|
|
|
|
iconTextSize = its;
|
|
|
|
|
numHotseatIcons = hs;
|
|
|
|
|
hotseatIconSize = his;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 18:57:15 -07:00
|
|
|
DeviceProfile(Context context,
|
|
|
|
|
ArrayList<DeviceProfile> profiles,
|
2013-08-22 16:15:50 -07:00
|
|
|
float minWidth, float minHeight,
|
2013-08-12 16:19:28 -07:00
|
|
|
int wPx, int hPx,
|
2013-08-22 16:15:50 -07:00
|
|
|
int awPx, int ahPx,
|
2013-10-29 14:45:58 -07:00
|
|
|
Resources res) {
|
|
|
|
|
DisplayMetrics dm = res.getDisplayMetrics();
|
2013-08-12 16:19:28 -07:00
|
|
|
ArrayList<DeviceProfileQuery> points =
|
|
|
|
|
new ArrayList<DeviceProfileQuery>();
|
|
|
|
|
transposeLayoutWithOrientation =
|
2013-10-29 14:45:58 -07:00
|
|
|
res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
|
2013-08-12 16:19:28 -07:00
|
|
|
minWidthDps = minWidth;
|
|
|
|
|
minHeightDps = minHeight;
|
|
|
|
|
|
2013-10-10 18:57:15 -07:00
|
|
|
ComponentName cn = new ComponentName(context.getPackageName(),
|
|
|
|
|
this.getClass().getName());
|
|
|
|
|
defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
|
2013-10-29 14:45:58 -07:00
|
|
|
edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
|
2013-10-10 18:57:15 -07:00
|
|
|
desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
|
2013-10-29 14:45:58 -07:00
|
|
|
pageIndicatorHeightPx =
|
|
|
|
|
res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
|
|
|
|
|
defaultPageSpacingPx =
|
|
|
|
|
res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
// Interpolate the rows
|
|
|
|
|
for (DeviceProfile p : profiles) {
|
|
|
|
|
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows));
|
|
|
|
|
}
|
|
|
|
|
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));
|
2013-10-25 15:24:24 -07:00
|
|
|
// 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));
|
|
|
|
|
hotseatAllAppsRank = (int) (numHotseatIcons / 2);
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
// Interpolate the icon size
|
|
|
|
|
points.clear();
|
|
|
|
|
for (DeviceProfile p : profiles) {
|
|
|
|
|
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize));
|
|
|
|
|
}
|
|
|
|
|
iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
|
2013-08-22 16:15:50 -07:00
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
// Interpolate the icon text size
|
|
|
|
|
points.clear();
|
|
|
|
|
for (DeviceProfile p : profiles) {
|
|
|
|
|
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize));
|
|
|
|
|
}
|
|
|
|
|
iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
|
2013-10-29 14:45:58 -07:00
|
|
|
iconDrawablePaddingOriginalPx =
|
|
|
|
|
res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
|
2013-08-22 16:15:50 -07:00
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
// Interpolate the hotseat icon size
|
|
|
|
|
points.clear();
|
|
|
|
|
for (DeviceProfile p : profiles) {
|
|
|
|
|
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize));
|
|
|
|
|
}
|
|
|
|
|
// Hotseat
|
|
|
|
|
hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
|
2013-08-22 16:15:50 -07:00
|
|
|
|
2013-10-25 15:24:24 -07:00
|
|
|
// Calculate the remaining vars
|
2013-10-29 14:45:58 -07:00
|
|
|
updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
|
2013-10-25 15:24:24 -07:00
|
|
|
updateAvailableDimensions(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addCallback(DeviceProfileCallbacks cb) {
|
|
|
|
|
mCallbacks.add(cb);
|
|
|
|
|
cb.onAvailableSizeChanged(this);
|
|
|
|
|
}
|
|
|
|
|
void removeCallback(DeviceProfileCallbacks cb) {
|
|
|
|
|
mCallbacks.remove(cb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getDeviceOrientation(Context context) {
|
|
|
|
|
WindowManager windowManager = (WindowManager)
|
|
|
|
|
context.getSystemService(Context.WINDOW_SERVICE);
|
|
|
|
|
Resources resources = context.getResources();
|
|
|
|
|
DisplayMetrics dm = resources.getDisplayMetrics();
|
|
|
|
|
Configuration config = resources.getConfiguration();
|
|
|
|
|
int rotation = windowManager.getDefaultDisplay().getRotation();
|
|
|
|
|
|
|
|
|
|
boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
|
|
|
|
|
(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
|
|
|
|
|
boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
|
|
|
|
|
(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
|
|
|
|
|
if (isLandscape || isRotatedPortrait) {
|
|
|
|
|
return CellLayout.LANDSCAPE;
|
|
|
|
|
} else {
|
|
|
|
|
return CellLayout.PORTRAIT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateAvailableDimensions(Context context) {
|
|
|
|
|
WindowManager windowManager = (WindowManager)
|
|
|
|
|
context.getSystemService(Context.WINDOW_SERVICE);
|
|
|
|
|
Display display = windowManager.getDefaultDisplay();
|
|
|
|
|
Resources resources = context.getResources();
|
|
|
|
|
DisplayMetrics dm = resources.getDisplayMetrics();
|
|
|
|
|
Configuration config = resources.getConfiguration();
|
|
|
|
|
|
|
|
|
|
// There are three possible configurations that the dynamic grid accounts for, portrait,
|
|
|
|
|
// landscape with the nav bar at the bottom, and landscape with the nav bar at the side.
|
|
|
|
|
// To prevent waiting for fitSystemWindows(), we make the observation that in landscape,
|
|
|
|
|
// the height is the smallest height (either with the nav bar at the bottom or to the
|
|
|
|
|
// side) and otherwise, the height is simply the largest possible height for a portrait
|
|
|
|
|
// device.
|
|
|
|
|
Point size = new Point();
|
|
|
|
|
Point smallestSize = new Point();
|
|
|
|
|
Point largestSize = new Point();
|
|
|
|
|
display.getSize(size);
|
|
|
|
|
display.getCurrentSizeRange(smallestSize, largestSize);
|
|
|
|
|
availableWidthPx = size.x;
|
|
|
|
|
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
|
|
|
|
availableHeightPx = smallestSize.y;
|
|
|
|
|
} else {
|
|
|
|
|
availableHeightPx = largestSize.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check to see if the icons fit in the new available height. If not, then we need to
|
|
|
|
|
// shrink the icon size.
|
|
|
|
|
Rect workspacePadding = getWorkspacePadding();
|
|
|
|
|
float scale = 1f;
|
|
|
|
|
int drawablePadding = iconDrawablePaddingOriginalPx;
|
|
|
|
|
updateIconSize(1f, drawablePadding, resources, dm);
|
|
|
|
|
float usedHeight = (cellHeightPx * numRows);
|
|
|
|
|
int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
|
|
|
|
|
if (usedHeight > maxHeight) {
|
|
|
|
|
scale = maxHeight / usedHeight;
|
|
|
|
|
drawablePadding = 0;
|
|
|
|
|
}
|
|
|
|
|
updateIconSize(scale, drawablePadding, resources, dm);
|
|
|
|
|
|
|
|
|
|
// Make the callbacks
|
|
|
|
|
for (DeviceProfileCallbacks cb : mCallbacks) {
|
|
|
|
|
cb.onAvailableSizeChanged(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateIconSize(float scale, int drawablePadding, Resources resources,
|
|
|
|
|
DisplayMetrics dm) {
|
|
|
|
|
iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
|
|
|
|
|
iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
|
|
|
|
|
iconDrawablePaddingPx = drawablePadding;
|
|
|
|
|
hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
// Search Bar
|
|
|
|
|
searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
|
|
|
|
|
searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
|
|
|
|
|
searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
|
|
|
|
|
searchBarSpaceHeightPx = searchBarHeightPx + 2 * edgeMarginPx;
|
|
|
|
|
|
|
|
|
|
// Calculate the actual text height
|
|
|
|
|
Paint textPaint = new Paint();
|
|
|
|
|
textPaint.setTextSize(iconTextSizePx);
|
|
|
|
|
FontMetrics fm = textPaint.getFontMetrics();
|
|
|
|
|
cellWidthPx = iconSizePx;
|
2013-10-25 15:24:24 -07:00
|
|
|
cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
|
2013-08-22 16:15:50 -07:00
|
|
|
|
|
|
|
|
// Hotseat
|
|
|
|
|
hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
|
|
|
|
|
hotseatCellWidthPx = iconSizePx;
|
|
|
|
|
hotseatCellHeightPx = iconSizePx;
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
// Folder
|
|
|
|
|
folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
|
2013-10-25 15:24:24 -07:00
|
|
|
folderCellHeightPx = cellHeightPx + edgeMarginPx;
|
2013-08-12 16:19:28 -07:00
|
|
|
folderBackgroundOffset = -edgeMarginPx;
|
|
|
|
|
folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
|
2013-09-03 17:48:37 -07:00
|
|
|
|
2013-10-25 15:24:24 -07:00
|
|
|
// All Apps
|
2013-09-03 17:48:37 -07:00
|
|
|
Rect padding = getWorkspacePadding(isLandscape ?
|
|
|
|
|
CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
|
|
|
|
|
int pageIndicatorOffset =
|
2013-10-25 15:24:24 -07:00
|
|
|
resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
|
2013-09-13 11:14:45 -07:00
|
|
|
if (isLandscape) {
|
|
|
|
|
allAppsNumRows = (availableHeightPx - pageIndicatorOffset - 4 * edgeMarginPx) /
|
|
|
|
|
(iconSizePx + iconTextSizePx + 2 * edgeMarginPx);
|
|
|
|
|
} else {
|
|
|
|
|
allAppsNumRows = (int) numRows + 1;
|
|
|
|
|
}
|
2013-09-03 17:48:37 -07:00
|
|
|
allAppsNumCols = (availableWidthPx - padding.left - padding.right - 2 * edgeMarginPx) /
|
|
|
|
|
(iconSizePx + 2 * edgeMarginPx);
|
2013-08-12 16:19:28 -07:00
|
|
|
}
|
|
|
|
|
|
2013-10-25 15:24:24 -07:00
|
|
|
void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
|
|
|
|
|
int awPx, int ahPx) {
|
|
|
|
|
isLandscape = (resources.getConfiguration().orientation ==
|
|
|
|
|
Configuration.ORIENTATION_LANDSCAPE);
|
|
|
|
|
isTablet = resources.getBoolean(R.bool.is_tablet);
|
|
|
|
|
isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
|
|
|
|
|
widthPx = wPx;
|
|
|
|
|
heightPx = hPx;
|
|
|
|
|
availableWidthPx = awPx;
|
|
|
|
|
availableHeightPx = ahPx;
|
|
|
|
|
|
|
|
|
|
updateAvailableDimensions(context);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
private float dist(PointF p0, PointF p1) {
|
|
|
|
|
return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
|
|
|
|
|
(p1.y-p0.y)*(p1.y-p0.y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float weight(PointF a, PointF b,
|
|
|
|
|
float pow) {
|
|
|
|
|
float d = dist(a, b);
|
|
|
|
|
if (d == 0f) {
|
|
|
|
|
return Float.POSITIVE_INFINITY;
|
|
|
|
|
}
|
|
|
|
|
return (float) (1f / Math.pow(d, pow));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float invDistWeightedInterpolate(float width, float height,
|
|
|
|
|
ArrayList<DeviceProfileQuery> points) {
|
|
|
|
|
float sum = 0;
|
|
|
|
|
float weights = 0;
|
|
|
|
|
float pow = 5;
|
|
|
|
|
float kNearestNeighbors = 3;
|
|
|
|
|
final PointF xy = new PointF(width, height);
|
|
|
|
|
|
|
|
|
|
ArrayList<DeviceProfileQuery> pointsByNearness = points;
|
|
|
|
|
Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
|
|
|
|
|
public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
|
|
|
|
|
return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < pointsByNearness.size(); ++i) {
|
|
|
|
|
DeviceProfileQuery p = pointsByNearness.get(i);
|
|
|
|
|
if (i < kNearestNeighbors) {
|
|
|
|
|
float w = weight(xy, p.dimens, pow);
|
|
|
|
|
if (w == Float.POSITIVE_INFINITY) {
|
|
|
|
|
return p.value;
|
|
|
|
|
}
|
|
|
|
|
weights += w;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < pointsByNearness.size(); ++i) {
|
|
|
|
|
DeviceProfileQuery p = pointsByNearness.get(i);
|
|
|
|
|
if (i < kNearestNeighbors) {
|
|
|
|
|
float w = weight(xy, p.dimens, pow);
|
|
|
|
|
sum += w * p.value / weights;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-25 15:24:24 -07:00
|
|
|
Rect getWorkspacePadding() {
|
|
|
|
|
return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
|
|
|
|
|
}
|
2013-10-29 14:45:58 -07:00
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
Rect getWorkspacePadding(int orientation) {
|
|
|
|
|
Rect padding = new Rect();
|
2013-09-26 16:07:17 -07:00
|
|
|
if (orientation == CellLayout.LANDSCAPE &&
|
|
|
|
|
transposeLayoutWithOrientation) {
|
2013-08-12 16:19:28 -07:00
|
|
|
// Pad the left and right of the workspace with search/hotseat bar sizes
|
|
|
|
|
padding.set(searchBarSpaceHeightPx, edgeMarginPx,
|
|
|
|
|
hotseatBarHeightPx, edgeMarginPx);
|
|
|
|
|
} else {
|
|
|
|
|
if (isTablet()) {
|
|
|
|
|
// Pad the left and right of the workspace to ensure consistent spacing
|
|
|
|
|
// between all icons
|
|
|
|
|
int width = (orientation == CellLayout.LANDSCAPE)
|
|
|
|
|
? Math.max(widthPx, heightPx)
|
|
|
|
|
: Math.min(widthPx, heightPx);
|
|
|
|
|
// XXX: If the icon size changes across orientations, we will have to take
|
|
|
|
|
// that into account here too.
|
|
|
|
|
int gap = (int) ((width - 2 * edgeMarginPx -
|
|
|
|
|
(numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
|
|
|
|
|
padding.set(edgeMarginPx + gap,
|
|
|
|
|
searchBarSpaceHeightPx,
|
|
|
|
|
edgeMarginPx + gap,
|
|
|
|
|
hotseatBarHeightPx + pageIndicatorHeightPx);
|
|
|
|
|
} else {
|
|
|
|
|
// Pad the top and bottom of the workspace with search/hotseat bar sizes
|
2013-10-10 18:57:15 -07:00
|
|
|
padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
|
2013-08-12 16:19:28 -07:00
|
|
|
searchBarSpaceHeightPx,
|
2013-10-10 18:57:15 -07:00
|
|
|
desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
|
2013-08-12 16:19:28 -07:00
|
|
|
hotseatBarHeightPx + pageIndicatorHeightPx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return padding;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-29 14:45:58 -07:00
|
|
|
int getWorkspacePageSpacing(int orientation) {
|
|
|
|
|
if (orientation == CellLayout.LANDSCAPE &&
|
|
|
|
|
transposeLayoutWithOrientation) {
|
|
|
|
|
// In landscape mode the page spacing is set to the default.
|
|
|
|
|
return defaultPageSpacingPx;
|
|
|
|
|
} else {
|
|
|
|
|
// In portrait, we want the pages spaced such that there is no
|
|
|
|
|
// overhang of the previous / next page into the current page viewport.
|
|
|
|
|
// We assume symmetrical padding in portrait mode.
|
|
|
|
|
return getWorkspacePadding().left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 15:41:09 -07:00
|
|
|
// The rect returned will be extended to below the system ui that covers the workspace
|
|
|
|
|
Rect getHotseatRect() {
|
|
|
|
|
if (isVerticalBarLayout()) {
|
|
|
|
|
return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
|
|
|
|
|
Integer.MAX_VALUE, availableHeightPx);
|
|
|
|
|
} else {
|
|
|
|
|
return new Rect(0, availableHeightPx - hotseatBarHeightPx,
|
|
|
|
|
availableWidthPx, Integer.MAX_VALUE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
int calculateCellWidth(int width, int countX) {
|
|
|
|
|
return width / countX;
|
|
|
|
|
}
|
|
|
|
|
int calculateCellHeight(int height, int countY) {
|
|
|
|
|
return height / countY;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 18:26:31 -07:00
|
|
|
boolean isPhone() {
|
|
|
|
|
return !isTablet && !isLargeTablet;
|
|
|
|
|
}
|
2013-08-12 16:19:28 -07:00
|
|
|
boolean isTablet() {
|
|
|
|
|
return isTablet;
|
|
|
|
|
}
|
|
|
|
|
boolean isLargeTablet() {
|
|
|
|
|
return isLargeTablet;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 15:41:09 -07:00
|
|
|
boolean isVerticalBarLayout() {
|
|
|
|
|
return isLandscape && transposeLayoutWithOrientation;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-29 14:45:58 -07:00
|
|
|
boolean shouldFadeAdjacentWorkspaceScreens() {
|
|
|
|
|
return isVerticalBarLayout() || isLargeTablet();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 16:19:28 -07:00
|
|
|
public void layout(Launcher launcher) {
|
|
|
|
|
FrameLayout.LayoutParams lp;
|
|
|
|
|
Resources res = launcher.getResources();
|
2013-09-24 15:41:09 -07:00
|
|
|
boolean hasVerticalBarLayout = isVerticalBarLayout();
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
// Layout the search bar space
|
2013-09-27 11:44:58 -07:00
|
|
|
View searchBar = launcher.getSearchBar();
|
|
|
|
|
lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
|
2013-08-12 16:19:28 -07:00
|
|
|
if (hasVerticalBarLayout) {
|
|
|
|
|
// Vertical search bar
|
|
|
|
|
lp.gravity = Gravity.TOP | Gravity.LEFT;
|
|
|
|
|
lp.width = searchBarSpaceHeightPx;
|
|
|
|
|
lp.height = LayoutParams.MATCH_PARENT;
|
2013-09-27 11:44:58 -07:00
|
|
|
searchBar.setPadding(
|
2013-08-12 16:19:28 -07:00
|
|
|
0, 2 * edgeMarginPx, 0,
|
|
|
|
|
2 * edgeMarginPx);
|
|
|
|
|
} else {
|
|
|
|
|
// Horizontal search bar
|
|
|
|
|
lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
|
|
|
|
|
lp.width = searchBarSpaceWidthPx;
|
|
|
|
|
lp.height = searchBarSpaceHeightPx;
|
2013-09-27 11:44:58 -07:00
|
|
|
searchBar.setPadding(
|
2013-08-12 16:19:28 -07:00
|
|
|
2 * edgeMarginPx,
|
|
|
|
|
2 * edgeMarginPx,
|
|
|
|
|
2 * edgeMarginPx, 0);
|
|
|
|
|
}
|
2013-09-27 11:44:58 -07:00
|
|
|
searchBar.setLayoutParams(lp);
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
// Layout the search bar
|
2013-09-27 11:44:58 -07:00
|
|
|
View qsbBar = launcher.getQsbBar();
|
|
|
|
|
LayoutParams vglp = qsbBar.getLayoutParams();
|
|
|
|
|
vglp.width = LayoutParams.MATCH_PARENT;
|
|
|
|
|
vglp.height = LayoutParams.MATCH_PARENT;
|
|
|
|
|
qsbBar.setLayoutParams(vglp);
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
// Layout the voice proxy
|
|
|
|
|
View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
|
|
|
|
|
if (voiceButtonProxy != null) {
|
|
|
|
|
if (hasVerticalBarLayout) {
|
|
|
|
|
// TODO: MOVE THIS INTO SEARCH BAR MEASURE
|
|
|
|
|
} else {
|
|
|
|
|
lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
|
|
|
|
|
lp.gravity = Gravity.TOP | Gravity.END;
|
|
|
|
|
lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
|
|
|
|
|
2 * iconSizePx;
|
|
|
|
|
lp.height = searchBarSpaceHeightPx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Layout the workspace
|
2013-10-29 14:45:58 -07:00
|
|
|
PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
|
2013-08-12 16:19:28 -07:00
|
|
|
lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
|
|
|
|
|
lp.gravity = Gravity.CENTER;
|
2013-10-29 14:45:58 -07:00
|
|
|
int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
|
|
|
|
|
Rect padding = getWorkspacePadding(orientation);
|
2013-08-12 16:19:28 -07:00
|
|
|
workspace.setLayoutParams(lp);
|
2013-10-29 14:45:58 -07:00
|
|
|
workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
|
|
|
|
|
workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
|
2013-08-12 16:19:28 -07:00
|
|
|
|
|
|
|
|
// Layout the hotseat
|
|
|
|
|
View hotseat = launcher.findViewById(R.id.hotseat);
|
|
|
|
|
lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
|
|
|
|
|
if (hasVerticalBarLayout) {
|
|
|
|
|
// Vertical hotseat
|
|
|
|
|
lp.gravity = Gravity.RIGHT;
|
|
|
|
|
lp.width = hotseatBarHeightPx;
|
|
|
|
|
lp.height = LayoutParams.MATCH_PARENT;
|
2013-10-25 15:24:24 -07:00
|
|
|
hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
|
2013-08-12 16:19:28 -07:00
|
|
|
} else if (isTablet()) {
|
|
|
|
|
// Pad the hotseat with the grid gap calculated above
|
|
|
|
|
int gridGap = (int) ((widthPx - 2 * edgeMarginPx -
|
|
|
|
|
(numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
|
|
|
|
|
int gridWidth = (int) ((numColumns * cellWidthPx) +
|
|
|
|
|
((numColumns - 1) * gridGap));
|
|
|
|
|
int hotseatGap = (int) Math.max(0,
|
|
|
|
|
(gridWidth - (numHotseatIcons * hotseatCellWidthPx))
|
|
|
|
|
/ (numHotseatIcons - 1));
|
|
|
|
|
lp.gravity = Gravity.BOTTOM;
|
|
|
|
|
lp.width = LayoutParams.MATCH_PARENT;
|
|
|
|
|
lp.height = hotseatBarHeightPx;
|
|
|
|
|
hotseat.setPadding(2 * edgeMarginPx + gridGap + hotseatGap, 0,
|
|
|
|
|
2 * edgeMarginPx + gridGap + hotseatGap,
|
|
|
|
|
2 * edgeMarginPx);
|
|
|
|
|
} else {
|
|
|
|
|
// For phones, layout the hotseat without any bottom margin
|
|
|
|
|
// to ensure that we have space for the folders
|
|
|
|
|
lp.gravity = Gravity.BOTTOM;
|
|
|
|
|
lp.width = LayoutParams.MATCH_PARENT;
|
|
|
|
|
lp.height = hotseatBarHeightPx;
|
2013-09-23 16:53:31 -07:00
|
|
|
hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
|
2013-08-12 16:19:28 -07:00
|
|
|
2 * edgeMarginPx, 0);
|
|
|
|
|
}
|
|
|
|
|
hotseat.setLayoutParams(lp);
|
|
|
|
|
|
|
|
|
|
// Layout the page indicators
|
|
|
|
|
View pageIndicator = launcher.findViewById(R.id.page_indicator);
|
|
|
|
|
if (pageIndicator != null) {
|
|
|
|
|
if (hasVerticalBarLayout) {
|
|
|
|
|
// Hide the page indicators when we have vertical search/hotseat
|
|
|
|
|
pageIndicator.setVisibility(View.GONE);
|
|
|
|
|
} else {
|
|
|
|
|
// Put the page indicators above the hotseat
|
|
|
|
|
lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
|
|
|
|
|
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
|
|
|
|
|
lp.width = LayoutParams.WRAP_CONTENT;
|
2013-10-07 17:11:27 -07:00
|
|
|
lp.height = LayoutParams.WRAP_CONTENT;
|
2013-08-12 16:19:28 -07:00
|
|
|
lp.bottomMargin = hotseatBarHeightPx;
|
|
|
|
|
pageIndicator.setLayoutParams(lp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DynamicGrid {
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
private static final String TAG = "DynamicGrid";
|
|
|
|
|
|
|
|
|
|
private DeviceProfile mProfile;
|
|
|
|
|
private float mMinWidth;
|
|
|
|
|
private float mMinHeight;
|
|
|
|
|
|
2013-08-22 16:15:50 -07:00
|
|
|
public static float dpiFromPx(int size, DisplayMetrics metrics){
|
2013-08-12 16:19:28 -07:00
|
|
|
float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
|
2013-08-22 16:15:50 -07:00
|
|
|
return (size / densityRatio);
|
|
|
|
|
}
|
|
|
|
|
public static int pxFromDp(float size, DisplayMetrics metrics) {
|
|
|
|
|
return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
|
|
|
|
|
size, metrics));
|
|
|
|
|
}
|
|
|
|
|
public static int pxFromSp(float size, DisplayMetrics metrics) {
|
|
|
|
|
return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
|
|
|
|
|
size, metrics));
|
2013-08-12 16:19:28 -07:00
|
|
|
}
|
|
|
|
|
|
2013-10-10 18:57:15 -07:00
|
|
|
public DynamicGrid(Context context, Resources resources,
|
|
|
|
|
int minWidthPx, int minHeightPx,
|
2013-08-22 16:15:50 -07:00
|
|
|
int widthPx, int heightPx,
|
|
|
|
|
int awPx, int ahPx) {
|
2013-08-12 16:19:28 -07:00
|
|
|
DisplayMetrics dm = resources.getDisplayMetrics();
|
|
|
|
|
ArrayList<DeviceProfile> deviceProfiles =
|
|
|
|
|
new ArrayList<DeviceProfile>();
|
2013-09-03 17:48:37 -07:00
|
|
|
boolean hasAA = !AppsCustomizePagedView.DISABLE_ALL_APPS;
|
2013-08-12 16:19:28 -07:00
|
|
|
// Our phone profiles include the bar sizes in each orientation
|
|
|
|
|
deviceProfiles.add(new DeviceProfile("Super Short Stubby",
|
2013-10-01 16:28:15 -07:00
|
|
|
255, 300, 2, 3, 48, 13, (hasAA ? 5 : 4), 48));
|
2013-08-12 16:19:28 -07:00
|
|
|
deviceProfiles.add(new DeviceProfile("Shorter Stubby",
|
2013-10-01 16:28:15 -07:00
|
|
|
255, 400, 3, 3, 48, 13, (hasAA ? 5 : 4), 48));
|
2013-08-12 16:19:28 -07:00
|
|
|
deviceProfiles.add(new DeviceProfile("Short Stubby",
|
2013-10-01 16:28:15 -07:00
|
|
|
275, 420, 3, 4, 48, 13, (hasAA ? 5 : 4), 48));
|
2013-08-12 16:19:28 -07:00
|
|
|
deviceProfiles.add(new DeviceProfile("Stubby",
|
2013-10-01 16:28:15 -07:00
|
|
|
255, 450, 3, 4, 48, 13, (hasAA ? 5 : 4), 48));
|
2013-08-12 16:19:28 -07:00
|
|
|
deviceProfiles.add(new DeviceProfile("Nexus S",
|
2013-10-01 16:28:15 -07:00
|
|
|
296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 4), 48));
|
2013-08-12 16:19:28 -07:00
|
|
|
deviceProfiles.add(new DeviceProfile("Nexus 4",
|
2013-10-01 16:28:15 -07:00
|
|
|
359, 518, 4, 4, 60, 13, (hasAA ? 5 : 4), 56));
|
2013-08-12 16:19:28 -07:00
|
|
|
// 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, 6, 6, 72, 14.4f, 7, 60));
|
|
|
|
|
// Larger tablet profiles always have system bars on the top & bottom
|
|
|
|
|
deviceProfiles.add(new DeviceProfile("Nexus 10",
|
|
|
|
|
727, 1207, 5, 8, 80, 14.4f, 9, 64));
|
|
|
|
|
/*
|
|
|
|
|
deviceProfiles.add(new DeviceProfile("Nexus 7",
|
|
|
|
|
600, 960, 5, 5, 72, 14.4f, 5, 60));
|
|
|
|
|
deviceProfiles.add(new DeviceProfile("Nexus 10",
|
2013-09-03 17:48:37 -07:00
|
|
|
800, 1280, 5, 5, 80, 14.4f, (hasAA ? 7 : 6), 64));
|
2013-08-12 16:19:28 -07:00
|
|
|
*/
|
|
|
|
|
deviceProfiles.add(new DeviceProfile("20-inch Tablet",
|
|
|
|
|
1527, 2527, 7, 7, 100, 20, 7, 72));
|
|
|
|
|
mMinWidth = dpiFromPx(minWidthPx, dm);
|
|
|
|
|
mMinHeight = dpiFromPx(minHeightPx, dm);
|
2013-10-10 18:57:15 -07:00
|
|
|
mProfile = new DeviceProfile(context, deviceProfiles,
|
2013-08-22 16:15:50 -07:00
|
|
|
mMinWidth, mMinHeight,
|
2013-08-12 16:19:28 -07:00
|
|
|
widthPx, heightPx,
|
2013-08-22 16:15:50 -07:00
|
|
|
awPx, ahPx,
|
2013-08-12 16:19:28 -07:00
|
|
|
resources);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceProfile getDeviceProfile() {
|
|
|
|
|
return mProfile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "-------- DYNAMIC GRID ------- \n" +
|
|
|
|
|
"Wd: " + mProfile.minWidthDps + ", Hd: " + mProfile.minHeightDps +
|
|
|
|
|
", W: " + mProfile.widthPx + ", H: " + mProfile.heightPx +
|
|
|
|
|
" [r: " + mProfile.numRows + ", c: " + mProfile.numColumns +
|
2013-10-25 15:24:24 -07:00
|
|
|
", is: " + mProfile.iconSizePx + ", its: " + mProfile.iconTextSizePx +
|
2013-08-12 16:19:28 -07:00
|
|
|
", cw: " + mProfile.cellWidthPx + ", ch: " + mProfile.cellHeightPx +
|
|
|
|
|
", hc: " + mProfile.numHotseatIcons + ", his: " + mProfile.hotseatIconSizePx + "]";
|
|
|
|
|
}
|
|
|
|
|
}
|