DynamicGrid.java revision b38002419dcb456b51f5d320b224737f16a07088
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.content.Context; 20import android.content.res.Resources; 21import android.util.DisplayMetrics; 22import android.util.TypedValue; 23 24import java.util.ArrayList; 25 26 27public class DynamicGrid { 28 @SuppressWarnings("unused") 29 private static final String TAG = "DynamicGrid"; 30 31 private DeviceProfile mProfile; 32 private float mMinWidth; 33 private float mMinHeight; 34 35 // This is a static that we use for the default icon size on a 4/5-inch phone 36 static float DEFAULT_ICON_SIZE_DP = 60; 37 static float DEFAULT_ICON_SIZE_PX = 0; 38 39 public static float dpiFromPx(int size, DisplayMetrics metrics){ 40 float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT; 41 return (size / densityRatio); 42 } 43 public static int pxFromDp(float size, DisplayMetrics metrics) { 44 return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45 size, metrics)); 46 } 47 public static int pxFromSp(float size, DisplayMetrics metrics) { 48 return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 49 size, metrics)); 50 } 51 52 public DynamicGrid(Context context, Resources resources, 53 int minWidthPx, int minHeightPx, 54 int widthPx, int heightPx, 55 int awPx, int ahPx) { 56 DisplayMetrics dm = resources.getDisplayMetrics(); 57 ArrayList<DeviceProfile> deviceProfiles = 58 new ArrayList<DeviceProfile>(); 59 boolean hasAA = !AppsCustomizePagedView.DISABLE_ALL_APPS; 60 DEFAULT_ICON_SIZE_PX = pxFromDp(DEFAULT_ICON_SIZE_DP, dm); 61 // Our phone profiles include the bar sizes in each orientation 62 deviceProfiles.add(new DeviceProfile("Super Short Stubby", 63 255, 300, 2, 3, 48, 13, (hasAA ? 5 : 4), 48)); 64 deviceProfiles.add(new DeviceProfile("Shorter Stubby", 65 255, 400, 3, 3, 48, 13, (hasAA ? 5 : 4), 48)); 66 deviceProfiles.add(new DeviceProfile("Short Stubby", 67 275, 420, 3, 4, 48, 13, (hasAA ? 5 : 4), 48)); 68 deviceProfiles.add(new DeviceProfile("Stubby", 69 255, 450, 3, 4, 48, 13, (hasAA ? 5 : 4), 48)); 70 deviceProfiles.add(new DeviceProfile("Nexus S", 71 296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 4), 48)); 72 deviceProfiles.add(new DeviceProfile("Nexus 4", 73 359, 518, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 4), 56)); 74 // The tablet profile is odd in that the landscape orientation 75 // also includes the nav bar on the side 76 deviceProfiles.add(new DeviceProfile("Nexus 7", 77 575, 904, 6, 6, 72, 14.4f, 7, 60)); 78 // Larger tablet profiles always have system bars on the top & bottom 79 deviceProfiles.add(new DeviceProfile("Nexus 10", 80 727, 1207, 5, 8, 80, 14.4f, 9, 64)); 81 /* 82 deviceProfiles.add(new DeviceProfile("Nexus 7", 83 600, 960, 5, 5, 72, 14.4f, 5, 60)); 84 deviceProfiles.add(new DeviceProfile("Nexus 10", 85 800, 1280, 5, 5, 80, 14.4f, (hasAA ? 7 : 6), 64)); 86 */ 87 deviceProfiles.add(new DeviceProfile("20-inch Tablet", 88 1527, 2527, 7, 7, 100, 20, 7, 72)); 89 mMinWidth = dpiFromPx(minWidthPx, dm); 90 mMinHeight = dpiFromPx(minHeightPx, dm); 91 mProfile = new DeviceProfile(context, deviceProfiles, 92 mMinWidth, mMinHeight, 93 widthPx, heightPx, 94 awPx, ahPx, 95 resources); 96 } 97 98 public DeviceProfile getDeviceProfile() { 99 return mProfile; 100 } 101 102 public String toString() { 103 return "-------- DYNAMIC GRID ------- \n" + 104 "Wd: " + mProfile.minWidthDps + ", Hd: " + mProfile.minHeightDps + 105 ", W: " + mProfile.widthPx + ", H: " + mProfile.heightPx + 106 " [r: " + mProfile.numRows + ", c: " + mProfile.numColumns + 107 ", is: " + mProfile.iconSizePx + ", its: " + mProfile.iconTextSizePx + 108 ", cw: " + mProfile.cellWidthPx + ", ch: " + mProfile.cellHeightPx + 109 ", hc: " + mProfile.numHotseatIcons + ", his: " + mProfile.hotseatIconSizePx + "]"; 110 } 111} 112