blob: dbefa45181a52b774c03af123ec212a5590ef732 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 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
Sunny Goyal87dc48b2018-10-12 11:42:33 -070019import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
20
Sunny Goyalc6205602015-05-21 20:46:33 -070021import android.annotation.TargetApi;
Adam Cohen2e6da152015-05-06 11:42:25 -070022import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080023import android.content.res.Configuration;
Sunny Goyal819e1932016-07-07 16:43:58 -070024import android.content.res.TypedArray;
25import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070026import android.graphics.Point;
Sunny Goyal415f1732018-11-29 10:33:47 -080027import android.text.TextUtils;
28import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070030import android.util.Log;
Sunny Goyal819e1932016-07-07 16:43:58 -070031import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070032import android.view.Display;
33import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070034
Sunny Goyald0e360a2018-06-29 14:40:18 -070035import com.android.launcher3.util.ConfigMonitor;
36import com.android.launcher3.util.MainThreadInitializedObject;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070037
Sunny Goyal819e1932016-07-07 16:43:58 -070038import org.xmlpull.v1.XmlPullParser;
39import org.xmlpull.v1.XmlPullParserException;
40
41import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070042import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080043import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070044
Sunny Goyald2303072018-08-14 15:21:45 -070045import androidx.annotation.VisibleForTesting;
46
Adam Cohen2e6da152015-05-06 11:42:25 -070047public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070048
Sunny Goyald0e360a2018-06-29 14:40:18 -070049 // We do not need any synchronization for this variable as its only written on UI thread.
50 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070051 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070052
Sunny Goyal415f1732018-11-29 10:33:47 -080053 private static final String KEY_IDP_GRIP_NAME = "idp_grid_name";
54
Sunny Goyal53d7ee42015-05-22 12:25:45 -070055 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
56
Sunny Goyal87dc48b2018-10-12 11:42:33 -070057 public static final int CHANGE_FLAG_GRID = 1 << 0;
58 public static final int CHANGE_FLAG_ICON_SIZE = 1 << 1;
59
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070060 // Constants that affects the interpolation curve between statically defined device profile
61 // buckets.
62 private static float KNEARESTNEIGHBOR = 3;
63 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070064
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070065 // used to offset float not being able to express extremely small weights in extreme cases.
66 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070067
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070068 /**
69 * Number of icons per row and column in the workspace.
70 */
Adam Cohen2e6da152015-05-06 11:42:25 -070071 public int numRows;
72 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070073
74 /**
75 * Number of icons per row and column in the folder.
76 */
Adam Cohen2e6da152015-05-06 11:42:25 -070077 public int numFolderRows;
78 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070079 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070080 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070081 public int iconBitmapSize;
82 public int fillResIconDpi;
83 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070084
85 /**
86 * Number of icons inside the hotseat area.
87 */
Sunny Goyalf862a262015-12-14 14:27:38 -080088 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070089
Sunny Goyal415f1732018-11-29 10:33:47 -080090 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -070091 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -070092
cuijiaxingabda8d72017-03-20 09:51:36 -070093 public DeviceProfile landscapeProfile;
94 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070095
Sunny Goyal6f866092016-03-17 17:04:15 -070096 public Point defaultWallpaperSize;
97
Sunny Goyal87dc48b2018-10-12 11:42:33 -070098 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
99 private ConfigMonitor mConfigMonitor;
100
Sunny Goyalf633ef52018-03-13 09:57:05 -0700101 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700102 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700103
Sunny Goyalf633ef52018-03-13 09:57:05 -0700104 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800105 numRows = p.numRows;
106 numColumns = p.numColumns;
107 numFolderRows = p.numFolderRows;
108 numFolderColumns = p.numFolderColumns;
109 iconSize = p.iconSize;
110 landscapeIconSize = p.landscapeIconSize;
111 iconTextSize = p.iconTextSize;
112 numHotseatIcons = p.numHotseatIcons;
113 defaultLayoutId = p.defaultLayoutId;
114 demoModeLayoutId = p.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700115 }
116
Sunny Goyalbbf01842015-10-08 07:41:15 -0700117 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700118 private InvariantDeviceProfile(Context context) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800119 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRIP_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700120 mConfigMonitor = new ConfigMonitor(context,
121 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
122 }
123
Sunny Goyal415f1732018-11-29 10:33:47 -0800124 private void initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700125 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
126 Display display = wm.getDefaultDisplay();
127 DisplayMetrics dm = new DisplayMetrics();
128 display.getMetrics(dm);
129
130 Point smallestSize = new Point();
131 Point largestSize = new Point();
132 display.getCurrentSizeRange(smallestSize, largestSize);
133
Sunny Goyal415f1732018-11-29 10:33:47 -0800134 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700135 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800136 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
137 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
138 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800139 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800140 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
141 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
142 DisplayOption interpolatedDisplayOption =
143 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700144
Sunny Goyal415f1732018-11-29 10:33:47 -0800145 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 numRows = closestProfile.numRows;
147 numColumns = closestProfile.numColumns;
148 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700149 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700150 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700151 numFolderRows = closestProfile.numFolderRows;
152 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800153 if (!closestProfile.name.equals(gridName)) {
154 Utilities.getPrefs(context).edit()
155 .putString(KEY_IDP_GRIP_NAME, closestProfile.name).apply();
156 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700157
Sunny Goyal415f1732018-11-29 10:33:47 -0800158 iconSize = interpolatedDisplayOption.iconSize;
159 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700160 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800161 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700162 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700163
164 // If the partner customization apk contains any grid overrides, apply them
165 // Supported overrides: numRows, numColumns, iconSize
166 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700167
168 Point realSize = new Point();
169 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700170 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700171 // same in any orientation.
172 int smallSide = Math.min(realSize.x, realSize.y);
173 int largeSide = Math.max(realSize.x, realSize.y);
174
175 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800176 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700177 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800178 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700179
180 // We need to ensure that there is enough extra space in the wallpaper
181 // for the intended parallax effects
182 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
183 defaultWallpaperSize = new Point(
184 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
185 largeSide);
186 } else {
187 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
188 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700189 }
190
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700191 public void addOnChangeListener(OnIDPChangeListener listener) {
192 mChangeListeners.add(listener);
193 }
194
195 private void killProcess(Context context) {
196 Log.e("ConfigMonitor", "restarting launcher");
197 android.os.Process.killProcess(android.os.Process.myPid());
198 }
199
200 private void onConfigChanged(Context context) {
201 // Config changes, what shall we do?
202 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
203
204 // Re-init grid
Sunny Goyal415f1732018-11-29 10:33:47 -0800205 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRIP_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700206
207 int changeFlags = 0;
208 if (numRows != oldProfile.numRows ||
209 numColumns != oldProfile.numColumns ||
210 numFolderColumns != oldProfile.numFolderColumns ||
211 numFolderRows != oldProfile.numFolderRows ||
212 numHotseatIcons != oldProfile.numHotseatIcons) {
213 changeFlags |= CHANGE_FLAG_GRID;
214 }
215
216 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize) {
217 changeFlags |= CHANGE_FLAG_ICON_SIZE;
218 }
219
220 // Create a new config monitor
221 mConfigMonitor.unregister();
222 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
223
224 for (OnIDPChangeListener listener : mChangeListeners) {
225 listener.onIdpChanged(changeFlags, this);
226 }
227 }
228
Sunny Goyal415f1732018-11-29 10:33:47 -0800229 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
230 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700231 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
232 final int depth = parser.getDepth();
233 int type;
234
235 while (((type = parser.next()) != XmlPullParser.END_TAG ||
236 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800237 if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
238
239 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
240 final int displayDepth = parser.getDepth();
241 while (((type = parser.next()) != XmlPullParser.END_TAG ||
242 parser.getDepth() > displayDepth)
243 && type != XmlPullParser.END_DOCUMENT) {
244 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
245 parser.getName())) {
246 profiles.add(new DisplayOption(
247 gridOption, context, Xml.asAttributeSet(parser)));
248 }
249 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700250 }
251 }
252 } catch (IOException|XmlPullParserException e) {
253 throw new RuntimeException(e);
254 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800255
256 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
257 if (!TextUtils.isEmpty(gridName)) {
258 for (DisplayOption option : profiles) {
259 if (gridName.equals(option.grid.name)) {
260 filteredProfiles.add(option);
261 }
262 }
263 }
264 if (filteredProfiles.isEmpty()) {
265 // No grid found, use the default options
266 for (DisplayOption option : profiles) {
267 if (option.canBeDefault) {
268 filteredProfiles.add(option);
269 }
270 }
271 }
272 if (filteredProfiles.isEmpty()) {
273 throw new RuntimeException("No display option with canBeDefault=true");
274 }
275 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700276 }
277
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700278 private int getLauncherIconDensity(int requiredSize) {
279 // Densities typically defined by an app.
280 int[] densityBuckets = new int[] {
281 DisplayMetrics.DENSITY_LOW,
282 DisplayMetrics.DENSITY_MEDIUM,
283 DisplayMetrics.DENSITY_TV,
284 DisplayMetrics.DENSITY_HIGH,
285 DisplayMetrics.DENSITY_XHIGH,
286 DisplayMetrics.DENSITY_XXHIGH,
287 DisplayMetrics.DENSITY_XXXHIGH
288 };
289
290 int density = DisplayMetrics.DENSITY_XXXHIGH;
291 for (int i = densityBuckets.length - 1; i >= 0; i--) {
292 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
293 / DisplayMetrics.DENSITY_DEFAULT;
294 if (expectedSize >= requiredSize) {
295 density = densityBuckets[i];
296 }
297 }
298
299 return density;
300 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700301
Adam Cohen2e6da152015-05-06 11:42:25 -0700302 /**
303 * Apply any Partner customization grid overrides.
304 *
305 * Currently we support: all apps row / column count.
306 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700307 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
308 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700309 if (p != null) {
310 p.applyInvariantDeviceProfileOverrides(this, dm);
311 }
312 }
313
Sunny Goyal415f1732018-11-29 10:33:47 -0800314 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700315 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700316 }
317
Sunny Goyal415f1732018-11-29 10:33:47 -0800318 @VisibleForTesting
319 static DisplayOption invDistWeightedInterpolate(float width, float height,
320 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700321 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700322
Sunny Goyal415f1732018-11-29 10:33:47 -0800323 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700324 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
325 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700326 }
327
Sunny Goyal415f1732018-11-29 10:33:47 -0800328 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700329 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800330 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700331 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
332 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800333 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700334 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800335 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700336 }
337
Sunny Goyal27835952017-01-13 12:15:53 -0800338 public DeviceProfile getDeviceProfile(Context context) {
339 return context.getResources().getConfiguration().orientation
340 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
341 }
342
Sunny Goyal415f1732018-11-29 10:33:47 -0800343 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700344 float d = dist(x0, y0, x1, y1);
345 if (Float.compare(d, 0f) == 0) {
346 return Float.POSITIVE_INFINITY;
347 }
348 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
349 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700350
351 /**
352 * As a ratio of screen height, the total distance we want the parallax effect to span
353 * horizontally
354 */
355 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
356 float aspectRatio = width / (float) height;
357
358 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
359 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
360 // We will use these two data points to extrapolate how much the wallpaper parallax effect
361 // to span (ie travel) at any aspect ratio:
362
363 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
364 final float ASPECT_RATIO_PORTRAIT = 10/16f;
365 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
366 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
367
368 // To find out the desired width at different aspect ratios, we use the following two
369 // formulas, where the coefficient on x is the aspect ratio (width/height):
370 // (16/10)x + y = 1.5
371 // (10/16)x + y = 1.2
372 // We solve for x and y and end up with a final formula:
373 final float x =
374 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
375 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
376 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
377 return x * aspectRatio + y;
378 }
379
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700380 public interface OnIDPChangeListener {
381
382 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
383 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800384
385
386 private static final class GridOption {
387
388 private final String name;
389 private final int numRows;
390 private final int numColumns;
391
392 private final int numFolderRows;
393 private final int numFolderColumns;
394
395 private final int numHotseatIcons;
396
397 private final int defaultLayoutId;
398 private final int demoModeLayoutId;
399
400 GridOption(Context context, AttributeSet attrs) {
401 TypedArray a = context.obtainStyledAttributes(
402 attrs, R.styleable.GridDisplayOption);
403 name = a.getString(R.styleable.GridDisplayOption_name);
404 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
405 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
406
407 defaultLayoutId = a.getResourceId(
408 R.styleable.GridDisplayOption_defaultLayoutId, 0);
409 demoModeLayoutId = a.getResourceId(
410 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
411 numHotseatIcons = a.getInt(
412 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
413 numFolderRows = a.getInt(
414 R.styleable.GridDisplayOption_numFolderRows, numRows);
415 numFolderColumns = a.getInt(
416 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
417 a.recycle();
418 }
419 }
420
421 private static final class DisplayOption {
422 private final GridOption grid;
423
424 private final String name;
425 private final float minWidthDps;
426 private final float minHeightDps;
427 private final boolean canBeDefault;
428
429 private float iconSize;
430 private float landscapeIconSize;
431 private float iconTextSize;
432
433 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
434 this.grid = grid;
435
436 TypedArray a = context.obtainStyledAttributes(
437 attrs, R.styleable.ProfileDisplayOption);
438
439 name = a.getString(R.styleable.ProfileDisplayOption_name);
440 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
441 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
442 canBeDefault = a.getBoolean(
443 R.styleable.ProfileDisplayOption_canBeDefault, false);
444
445 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
446 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
447 iconSize);
448 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
449 a.recycle();
450 }
451
452 DisplayOption() {
453 grid = null;
454 name = null;
455 minWidthDps = 0;
456 minHeightDps = 0;
457 canBeDefault = false;
458 }
459
460 private DisplayOption multiply(float w) {
461 iconSize *= w;
462 landscapeIconSize *= w;
463 iconTextSize *= w;
464 return this;
465 }
466
467 private DisplayOption add(DisplayOption p) {
468 iconSize += p.iconSize;
469 landscapeIconSize += p.landscapeIconSize;
470 iconTextSize += p.iconTextSize;
471 return this;
472 }
473 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700474}