DeviceProfile.java revision 6178f13e2d1f0c1a89cea74538b51993ef3439e3
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Paint;
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Point;
26import android.graphics.Rect;
27import android.util.DisplayMetrics;
28import android.view.Gravity;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.ViewGroup.LayoutParams;
32import android.view.ViewGroup.MarginLayoutParams;
33import android.widget.FrameLayout;
34
35import com.android.launcher3.config.FeatureFlags;
36
37public class DeviceProfile {
38
39    public final InvariantDeviceProfile inv;
40
41    // Device properties
42    public final boolean isTablet;
43    public final boolean isLargeTablet;
44    public final boolean isPhone;
45    public final boolean transposeLayoutWithOrientation;
46
47    // Device properties in current orientation
48    public final boolean isLandscape;
49    public final int widthPx;
50    public final int heightPx;
51    public final int availableWidthPx;
52    public final int availableHeightPx;
53    /**
54     * The maximum amount of left/right workspace padding as a percentage of the screen width.
55     * To be clear, this means that up to 7% of the screen width can be used as left padding, and
56     * 7% of the screen width can be used as right padding.
57     */
58    private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
59
60    // Overview mode
61    private final int overviewModeMinIconZoneHeightPx;
62    private final int overviewModeMaxIconZoneHeightPx;
63    private final int overviewModeBarItemWidthPx;
64    private final int overviewModeBarSpacerWidthPx;
65    private final float overviewModeIconZoneRatio;
66
67    // Workspace
68    private int desiredWorkspaceLeftRightMarginPx;
69    public final int edgeMarginPx;
70    public final Rect defaultWidgetPadding;
71    private final int pageIndicatorHeightPx;
72    private final int defaultPageSpacingPx;
73    private final int topWorkspacePadding;
74    private float dragViewScale;
75    public float workspaceSpringLoadShrinkFactor;
76    public final int workspaceSpringLoadedBottomSpace;
77
78    // Workspace icons
79    public int iconSizePx;
80    public int iconTextSizePx;
81    public int iconDrawablePaddingPx;
82    public int iconDrawablePaddingOriginalPx;
83
84    public int cellWidthPx;
85    public int cellHeightPx;
86
87    // Folder
88    public int folderBackgroundOffset;
89    public int folderIconSizePx;
90    public int folderIconPreviewPadding;
91    public int folderCellWidthPx;
92    public int folderCellHeightPx;
93
94    // Hotseat
95    public int hotseatCellWidthPx;
96    public int hotseatCellHeightPx;
97    public int hotseatIconSizePx;
98    private int hotseatBarHeightPx;
99
100    // All apps
101    public int allAppsNumCols;
102    public int allAppsNumPredictiveCols;
103    public int allAppsButtonVisualSize;
104    public final int allAppsIconSizePx;
105    public final float allAppsIconTextSizeSp;
106
107    // Drop Target
108    public int dropTargetBarSizePx;
109
110    public DeviceProfile(Context context, InvariantDeviceProfile inv,
111            Point minSize, Point maxSize,
112            int width, int height, boolean isLandscape) {
113
114        this.inv = inv;
115        this.isLandscape = isLandscape;
116
117        Resources res = context.getResources();
118        DisplayMetrics dm = res.getDisplayMetrics();
119
120        // Constants from resources
121        isTablet = res.getBoolean(R.bool.is_tablet);
122        isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
123        isPhone = !isTablet && !isLargeTablet;
124
125        // Some more constants
126        transposeLayoutWithOrientation =
127                res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
128
129        ComponentName cn = new ComponentName(context.getPackageName(),
130                this.getClass().getName());
131        defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
132        edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
133        desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
134        pageIndicatorHeightPx =
135                res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
136        defaultPageSpacingPx =
137                res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
138        topWorkspacePadding =
139                res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
140        overviewModeMinIconZoneHeightPx =
141                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
142        overviewModeMaxIconZoneHeightPx =
143                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
144        overviewModeBarItemWidthPx =
145                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
146        overviewModeBarSpacerWidthPx =
147                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
148        overviewModeIconZoneRatio =
149                res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
150        iconDrawablePaddingOriginalPx =
151                res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
152        dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
153        workspaceSpringLoadedBottomSpace =
154                res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
155
156        // AllApps uses the original non-scaled icon text size
157        allAppsIconTextSizeSp = inv.iconTextSize;
158
159        // AllApps uses the original non-scaled icon size
160        allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
161
162        // Determine sizes.
163        widthPx = width;
164        heightPx = height;
165        if (isLandscape) {
166            availableWidthPx = maxSize.x;
167            availableHeightPx = minSize.y;
168        } else {
169            availableWidthPx = minSize.x;
170            availableHeightPx = maxSize.y;
171        }
172
173        // Calculate the remaining vars
174        updateAvailableDimensions(dm, res);
175        computeAllAppsButtonSize(context);
176    }
177
178    /**
179     * Determine the exact visual footprint of the all apps button, taking into account scaling
180     * and internal padding of the drawable.
181     */
182    private void computeAllAppsButtonSize(Context context) {
183        Resources res = context.getResources();
184        float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
185        allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
186                        .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
187    }
188
189    private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
190        // Check to see if the icons fit in the new available height.  If not, then we need to
191        // shrink the icon size.
192        float scale = 1f;
193        int drawablePadding = iconDrawablePaddingOriginalPx;
194        updateIconSize(1f, drawablePadding, res, dm);
195        float usedHeight = (cellHeightPx * inv.numRows);
196
197        int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
198        if (usedHeight > maxHeight) {
199            scale = maxHeight / usedHeight;
200            drawablePadding = 0;
201        }
202        updateIconSize(scale, drawablePadding, res, dm);
203    }
204
205    private void updateIconSize(float scale, int drawablePadding, Resources res,
206                                DisplayMetrics dm) {
207        iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
208        iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
209        iconDrawablePaddingPx = drawablePadding;
210        hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
211
212        // Calculate the actual text height
213        Paint textPaint = new Paint();
214        textPaint.setTextSize(iconTextSizePx);
215        FontMetrics fm = textPaint.getFontMetrics();
216        cellWidthPx = iconSizePx;
217        cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
218        final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
219                : res.getDimensionPixelSize(R.dimen.dragViewScale);
220        dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
221
222        // Hotseat
223        hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
224        hotseatCellWidthPx = iconSizePx;
225        hotseatCellHeightPx = iconSizePx;
226
227        if (!isVerticalBarLayout()) {
228            int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
229                    - pageIndicatorHeightPx - topWorkspacePadding;
230            float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
231            workspaceSpringLoadShrinkFactor = Math.min(
232                    res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
233                    1 - (minRequiredHeight / expectedWorkspaceHeight));
234        } else {
235            workspaceSpringLoadShrinkFactor =
236                    res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
237        }
238
239        // Folder
240        int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
241        // Don't let the folder get too close to the edges of the screen.
242        folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
243                (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
244        folderCellHeightPx = cellHeightPx + edgeMarginPx;
245        folderBackgroundOffset = -edgeMarginPx;
246        folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
247        folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
248    }
249
250    /**
251     * @param recyclerViewWidth the available width of the AllAppsRecyclerView
252     */
253    public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
254        int appsViewLeftMarginPx =
255                res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
256        int allAppsCellWidthGap =
257                res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
258        int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
259        int numAppsCols = (availableAppsWidthPx + allAppsCellWidthGap - appsViewLeftMarginPx) /
260                (allAppsIconSizePx + allAppsCellWidthGap);
261        int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
262        allAppsNumCols = numAppsCols;
263        allAppsNumPredictiveCols = numPredictiveAppCols;
264    }
265
266    /** Returns the width and height of the search bar, ignoring any padding. */
267    public Point getSearchBarDimensForWidgetOpts() {
268        if (isVerticalBarLayout()) {
269            return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
270        } else {
271            int gap;
272            if (isTablet) {
273                // Pad the left and right of the workspace to ensure consistent spacing
274                // between all icons
275                int width = getCurrentWidth();
276                // XXX: If the icon size changes across orientations, we will have to take
277                //      that into account here too.
278                gap = ((width - 2 * edgeMarginPx
279                        - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
280                        + edgeMarginPx;
281            } else {
282                gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
283            }
284            return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
285        }
286    }
287
288    public Point getCellSize() {
289        Point result = new Point();
290        // Since we are only concerned with the overall padding, layout direction does
291        // not matter.
292        Point padding = getTotalWorkspacePadding();
293        result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
294        result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
295        return result;
296    }
297
298    public Point getTotalWorkspacePadding() {
299        Rect padding = getWorkspacePadding(null);
300        return new Point(padding.left + padding.right, padding.top + padding.bottom);
301    }
302
303    /**
304     * Returns the workspace padding in the specified orientation.
305     * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
306     * this value is not reliable.
307     * Use {@link #getTotalWorkspacePadding()} instead.
308     */
309    public Rect getWorkspacePadding(Rect recycle) {
310        Rect padding = recycle == null ? new Rect() : recycle;
311        if (isVerticalBarLayout()) {
312            // in case of isVerticalBarLayout, the hotseat is always on the right and the drop
313            // target bar is on the left, independent of the layout direction.
314            padding.set(dropTargetBarSizePx, edgeMarginPx, hotseatBarHeightPx, edgeMarginPx);
315        } else {
316            int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
317            if (isTablet) {
318                // Pad the left and right of the workspace to ensure consistent spacing
319                // between all icons
320                float gapScale = 1f + (dragViewScale - 1f) / 2f;
321                int width = getCurrentWidth();
322                int height = getCurrentHeight();
323                // The amount of screen space available for left/right padding.
324                int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
325                        ((inv.numColumns - 1) * gapScale * cellWidthPx)));
326                availablePaddingX = (int) Math.min(availablePaddingX,
327                            width * MAX_HORIZONTAL_PADDING_PERCENT);
328                int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
329                        - (int) (2 * inv.numRows * cellHeightPx));
330                padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
331                        availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
332            } else {
333                // Pad the top and bottom of the workspace with search/hotseat bar sizes
334                padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
335                        topWorkspacePadding,
336                        desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
337                        paddingBottom);
338            }
339        }
340        return padding;
341    }
342
343    private int getWorkspacePageSpacing() {
344        if (isVerticalBarLayout() || isLargeTablet) {
345            // In landscape mode the page spacing is set to the default.
346            return defaultPageSpacingPx;
347        } else {
348            // In portrait, we want the pages spaced such that there is no
349            // overhang of the previous / next page into the current page viewport.
350            // We assume symmetrical padding in portrait mode.
351            return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(null).left);
352        }
353    }
354
355    int getOverviewModeButtonBarHeight() {
356        int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
357        zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
358                Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
359        return zoneHeight;
360    }
361
362    public static int calculateCellWidth(int width, int countX) {
363        return width / countX;
364    }
365    public static int calculateCellHeight(int height, int countY) {
366        return height / countY;
367    }
368
369    /**
370     * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
371     * When {@code false}, either device is in portrait mode or the device is in landscape mode and
372     * the hotseat is on the bottom row.
373     */
374    public boolean isVerticalBarLayout() {
375        return isLandscape && transposeLayoutWithOrientation;
376    }
377
378    boolean shouldFadeAdjacentWorkspaceScreens() {
379        return isVerticalBarLayout() || isLargeTablet;
380    }
381
382    private int getVisibleChildCount(ViewGroup parent) {
383        int visibleChildren = 0;
384        for (int i = 0; i < parent.getChildCount(); i++) {
385            if (parent.getChildAt(i).getVisibility() != View.GONE) {
386                visibleChildren++;
387            }
388        }
389        return visibleChildren;
390    }
391
392    public void layout(Launcher launcher) {
393        FrameLayout.LayoutParams lp;
394        boolean hasVerticalBarLayout = isVerticalBarLayout();
395        final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
396
397        // Layout the search bar space
398        Point searchBarBounds = getSearchBarDimensForWidgetOpts();
399        View searchBar = launcher.getDropTargetBar();
400        lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
401        lp.width = searchBarBounds.x;
402        lp.height = searchBarBounds.y;
403        lp.topMargin = edgeMarginPx;
404        searchBar.setLayoutParams(lp);
405
406        // Layout the workspace
407        PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
408        Rect padding = getWorkspacePadding(null);
409        workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
410        workspace.setPageSpacing(getWorkspacePageSpacing());
411
412        View qsbContainer = launcher.getQsbContainer();
413        lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
414        lp.topMargin = padding.top;
415        qsbContainer.setLayoutParams(lp);
416
417        // Layout the hotseat
418        View hotseat = launcher.findViewById(R.id.hotseat);
419        lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
420        // We want the edges of the hotseat to line up with the edges of the workspace, but the
421        // icons in the hotseat are a different size, and so don't line up perfectly. To account for
422        // this, we pad the left and right of the hotseat with half of the difference of a workspace
423        // cell vs a hotseat cell.
424        float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
425        float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
426        int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
427        if (hasVerticalBarLayout) {
428            // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
429            //                     screen regardless of RTL
430            lp.gravity = Gravity.RIGHT;
431            lp.width = hotseatBarHeightPx;
432            lp.height = LayoutParams.MATCH_PARENT;
433            hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
434        } else if (isTablet) {
435            // Pad the hotseat with the workspace padding calculated above
436            lp.gravity = Gravity.BOTTOM;
437            lp.width = LayoutParams.MATCH_PARENT;
438            lp.height = hotseatBarHeightPx;
439            hotseat.findViewById(R.id.layout).setPadding(
440                    hotseatAdjustment + padding.left, 0,
441                    hotseatAdjustment + padding.right, 2 * edgeMarginPx);
442        } else {
443            // For phones, layout the hotseat without any bottom margin
444            // to ensure that we have space for the folders
445            lp.gravity = Gravity.BOTTOM;
446            lp.width = LayoutParams.MATCH_PARENT;
447            lp.height = hotseatBarHeightPx;
448            hotseat.findViewById(R.id.layout).setPadding(
449                    hotseatAdjustment + padding.left, 0,
450                    hotseatAdjustment + padding.right, 0);
451        }
452        hotseat.setLayoutParams(lp);
453
454        // Layout the page indicators
455        View pageIndicator = launcher.findViewById(R.id.page_indicator);
456        if (pageIndicator != null) {
457            lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
458            if (!hasVerticalBarLayout) {
459                // Put the page indicators above the hotseat
460                lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
461                lp.width = LayoutParams.WRAP_CONTENT;
462                lp.bottomMargin = hotseatBarHeightPx;
463            }
464            pageIndicator.setLayoutParams(lp);
465        }
466
467        // Layout the Overview Mode
468        ViewGroup overviewMode = launcher.getOverviewPanel();
469        if (overviewMode != null) {
470            lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
471            lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
472
473            int visibleChildCount = getVisibleChildCount(overviewMode);
474            int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
475            int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
476
477            lp.width = Math.min(availableWidthPx, maxWidth);
478            lp.height = getOverviewModeButtonBarHeight();
479            overviewMode.setLayoutParams(lp);
480
481            if (lp.width > totalItemWidth && visibleChildCount > 1) {
482                // We have enough space. Lets add some margin too.
483                int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
484                View lastChild = null;
485
486                // Set margin of all visible children except the last visible child
487                for (int i = 0; i < visibleChildCount; i++) {
488                    if (lastChild != null) {
489                        MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
490                        if (isLayoutRtl) {
491                            clp.leftMargin = margin;
492                        } else {
493                            clp.rightMargin = margin;
494                        }
495                        lastChild.setLayoutParams(clp);
496                        lastChild = null;
497                    }
498                    View thisChild = overviewMode.getChildAt(i);
499                    if (thisChild.getVisibility() != View.GONE) {
500                        lastChild = thisChild;
501                    }
502                }
503            }
504        }
505    }
506
507    private int getCurrentWidth() {
508        return isLandscape
509                ? Math.max(widthPx, heightPx)
510                : Math.min(widthPx, heightPx);
511    }
512
513    private int getCurrentHeight() {
514        return isLandscape
515                ? Math.min(widthPx, heightPx)
516                : Math.max(widthPx, heightPx);
517    }
518
519
520    public static final int getContainerPadding(Context context, int availableWidth) {
521        Resources res = context.getResources();
522
523        int maxSize = res.getDimensionPixelSize(R.dimen.container_max_width);
524        int minMargin = res.getDimensionPixelSize(R.dimen.container_min_margin);
525
526        if (maxSize > 0) {
527            return Math.max(minMargin, (availableWidth - maxSize) / 2);
528        } else {
529            return Math.max(minMargin,
530                    (int) res.getFraction(R.fraction.container_margin, availableWidth, 1));
531        }
532    }
533}
534