blob: d22427f44317c6a81735322223d5c83addeb3b1f [file] [log] [blame]
Winson Chung5f8afe62013-08-12 16:19:28 -07001/*
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
Winson Chungf7d45852013-10-10 18:57:15 -070019import android.content.Context;
Winson Chung5f8afe62013-08-12 16:19:28 -070020import android.content.res.Resources;
Winson Chung5f8afe62013-08-12 16:19:28 -070021import android.util.DisplayMetrics;
22import android.util.TypedValue;
Winson Chung5f8afe62013-08-12 16:19:28 -070023
24import java.util.ArrayList;
Winson Chung5f8afe62013-08-12 16:19:28 -070025
26
Winson Chung5f8afe62013-08-12 16:19:28 -070027public 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
Winson Chung67ca7e42013-10-31 16:53:19 -070035 // 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
Winson Chung892c74d2013-08-22 16:15:50 -070039 public static float dpiFromPx(int size, DisplayMetrics metrics){
Winson Chung5f8afe62013-08-12 16:19:28 -070040 float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
Winson Chung892c74d2013-08-22 16:15:50 -070041 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));
Winson Chung5f8afe62013-08-12 16:19:28 -070050 }
51
Winson Chungf7d45852013-10-10 18:57:15 -070052 public DynamicGrid(Context context, Resources resources,
53 int minWidthPx, int minHeightPx,
Winson Chung892c74d2013-08-22 16:15:50 -070054 int widthPx, int heightPx,
55 int awPx, int ahPx) {
Winson Chung5f8afe62013-08-12 16:19:28 -070056 DisplayMetrics dm = resources.getDisplayMetrics();
57 ArrayList<DeviceProfile> deviceProfiles =
58 new ArrayList<DeviceProfile>();
Winson Chung67ca7e42013-10-31 16:53:19 -070059 DEFAULT_ICON_SIZE_PX = pxFromDp(DEFAULT_ICON_SIZE_DP, dm);
Winson Chung5f8afe62013-08-12 16:19:28 -070060 // Our phone profiles include the bar sizes in each orientation
61 deviceProfiles.add(new DeviceProfile("Super Short Stubby",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070062 255, 300, 2, 3, 2, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
Winson Chung5f8afe62013-08-12 16:19:28 -070063 deviceProfiles.add(new DeviceProfile("Shorter Stubby",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070064 255, 400, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
Winson Chung5f8afe62013-08-12 16:19:28 -070065 deviceProfiles.add(new DeviceProfile("Short Stubby",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070066 275, 420, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Winson Chung5f8afe62013-08-12 16:19:28 -070067 deviceProfiles.add(new DeviceProfile("Stubby",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070068 255, 450, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Winson Chung5f8afe62013-08-12 16:19:28 -070069 deviceProfiles.add(new DeviceProfile("Nexus S",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070070 296, 491.33f, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Winson Chung5f8afe62013-08-12 16:19:28 -070071 deviceProfiles.add(new DeviceProfile("Nexus 4",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070072 335, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
Winson Chung3abfd162014-01-17 14:43:02 -080073 deviceProfiles.add(new DeviceProfile("Nexus 5",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070074 359, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
Winson Chung6d110112014-01-16 14:03:55 -080075 deviceProfiles.add(new DeviceProfile("Large Phone",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070076 406, 694, 5, 5, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
Winson Chung5f8afe62013-08-12 16:19:28 -070077 // The tablet profile is odd in that the landscape orientation
78 // also includes the nav bar on the side
79 deviceProfiles.add(new DeviceProfile("Nexus 7",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070080 575, 904, 5, 6, 4, 5, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
Winson Chung5f8afe62013-08-12 16:19:28 -070081 // Larger tablet profiles always have system bars on the top & bottom
82 deviceProfiles.add(new DeviceProfile("Nexus 10",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070083 727, 1207, 5, 6, 4, 5, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
Winson Chung5f8afe62013-08-12 16:19:28 -070084 deviceProfiles.add(new DeviceProfile("20-inch Tablet",
Sunny Goyalfee35bb2015-05-11 11:38:19 -070085 1527, 2527, 7, 7, 6, 6, 100, 20, 7, 72, R.xml.default_workspace_4x4));
Winson Chung5f8afe62013-08-12 16:19:28 -070086 mMinWidth = dpiFromPx(minWidthPx, dm);
87 mMinHeight = dpiFromPx(minHeightPx, dm);
Winson Chungf7d45852013-10-10 18:57:15 -070088 mProfile = new DeviceProfile(context, deviceProfiles,
Winson Chung892c74d2013-08-22 16:15:50 -070089 mMinWidth, mMinHeight,
Winson Chung5f8afe62013-08-12 16:19:28 -070090 widthPx, heightPx,
Winson Chung892c74d2013-08-22 16:15:50 -070091 awPx, ahPx,
Winson Chung5f8afe62013-08-12 16:19:28 -070092 resources);
93 }
94
Winson Chungb3800242013-10-24 11:01:54 -070095 public DeviceProfile getDeviceProfile() {
Winson Chung5f8afe62013-08-12 16:19:28 -070096 return mProfile;
97 }
98
99 public String toString() {
100 return "-------- DYNAMIC GRID ------- \n" +
101 "Wd: " + mProfile.minWidthDps + ", Hd: " + mProfile.minHeightDps +
102 ", W: " + mProfile.widthPx + ", H: " + mProfile.heightPx +
103 " [r: " + mProfile.numRows + ", c: " + mProfile.numColumns +
Winson Chung6e1c0d32013-10-25 15:24:24 -0700104 ", is: " + mProfile.iconSizePx + ", its: " + mProfile.iconTextSizePx +
Winson Chung5f8afe62013-08-12 16:19:28 -0700105 ", cw: " + mProfile.cellWidthPx + ", ch: " + mProfile.cellHeightPx +
106 ", hc: " + mProfile.numHotseatIcons + ", his: " + mProfile.hotseatIconSizePx + "]";
107 }
108}