blob: 257e46c2f4a206f6600299c44e76a54baa54a9ef [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;
Adam Cohen2e6da152015-05-06 11:42:25 -070043
Sunny Goyald2303072018-08-14 15:21:45 -070044import androidx.annotation.VisibleForTesting;
45
Adam Cohen2e6da152015-05-06 11:42:25 -070046public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070047
Sunny Goyald0e360a2018-06-29 14:40:18 -070048 // We do not need any synchronization for this variable as its only written on UI thread.
49 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070050 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070051
Sunny Goyal415f1732018-11-29 10:33:47 -080052 private static final String KEY_IDP_GRIP_NAME = "idp_grid_name";
53
Sunny Goyal53d7ee42015-05-22 12:25:45 -070054 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
55
Sunny Goyal87dc48b2018-10-12 11:42:33 -070056 public static final int CHANGE_FLAG_GRID = 1 << 0;
57 public static final int CHANGE_FLAG_ICON_SIZE = 1 << 1;
58
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070059 // Constants that affects the interpolation curve between statically defined device profile
60 // buckets.
61 private static float KNEARESTNEIGHBOR = 3;
62 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070063
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070064 // used to offset float not being able to express extremely small weights in extreme cases.
65 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070066
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070067 /**
68 * Number of icons per row and column in the workspace.
69 */
Adam Cohen2e6da152015-05-06 11:42:25 -070070 public int numRows;
71 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070072
73 /**
74 * Number of icons per row and column in the folder.
75 */
Adam Cohen2e6da152015-05-06 11:42:25 -070076 public int numFolderRows;
77 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070078 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070079 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070080 public int iconBitmapSize;
81 public int fillResIconDpi;
82 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070083
84 /**
85 * Number of icons inside the hotseat area.
86 */
Sunny Goyalf862a262015-12-14 14:27:38 -080087 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070088
Sunny Goyal415f1732018-11-29 10:33:47 -080089 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -070090 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -070091
cuijiaxingabda8d72017-03-20 09:51:36 -070092 public DeviceProfile landscapeProfile;
93 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070094
Sunny Goyal6f866092016-03-17 17:04:15 -070095 public Point defaultWallpaperSize;
96
Sunny Goyal87dc48b2018-10-12 11:42:33 -070097 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
98 private ConfigMonitor mConfigMonitor;
99
Sunny Goyalf633ef52018-03-13 09:57:05 -0700100 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700101 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700102
Sunny Goyalf633ef52018-03-13 09:57:05 -0700103 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800104 numRows = p.numRows;
105 numColumns = p.numColumns;
106 numFolderRows = p.numFolderRows;
107 numFolderColumns = p.numFolderColumns;
108 iconSize = p.iconSize;
109 landscapeIconSize = p.landscapeIconSize;
110 iconTextSize = p.iconTextSize;
111 numHotseatIcons = p.numHotseatIcons;
112 defaultLayoutId = p.defaultLayoutId;
113 demoModeLayoutId = p.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 }
115
Sunny Goyalbbf01842015-10-08 07:41:15 -0700116 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700117 private InvariantDeviceProfile(Context context) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800118 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRIP_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700119 mConfigMonitor = new ConfigMonitor(context,
120 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
121 }
122
Sunny Goyal415f1732018-11-29 10:33:47 -0800123 private void initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
125 Display display = wm.getDefaultDisplay();
126 DisplayMetrics dm = new DisplayMetrics();
127 display.getMetrics(dm);
128
129 Point smallestSize = new Point();
130 Point largestSize = new Point();
131 display.getCurrentSizeRange(smallestSize, largestSize);
132
Sunny Goyal415f1732018-11-29 10:33:47 -0800133 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700134 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800135 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
136 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
137 // Sort the profiles based on the closeness to the device size
138 allOptions.sort((a, b) ->
139 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
140 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
141 DisplayOption interpolatedDisplayOption =
142 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700143
Sunny Goyal415f1732018-11-29 10:33:47 -0800144 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700145 numRows = closestProfile.numRows;
146 numColumns = closestProfile.numColumns;
147 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700148 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700149 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700150 numFolderRows = closestProfile.numFolderRows;
151 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800152 if (!closestProfile.name.equals(gridName)) {
153 Utilities.getPrefs(context).edit()
154 .putString(KEY_IDP_GRIP_NAME, closestProfile.name).apply();
155 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700156
Sunny Goyal415f1732018-11-29 10:33:47 -0800157 iconSize = interpolatedDisplayOption.iconSize;
158 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700159 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800160 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700161 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700162
163 // If the partner customization apk contains any grid overrides, apply them
164 // Supported overrides: numRows, numColumns, iconSize
165 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700166
167 Point realSize = new Point();
168 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700169 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700170 // same in any orientation.
171 int smallSide = Math.min(realSize.x, realSize.y);
172 int largeSide = Math.max(realSize.x, realSize.y);
173
174 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800175 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700176 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800177 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700178
179 // We need to ensure that there is enough extra space in the wallpaper
180 // for the intended parallax effects
181 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
182 defaultWallpaperSize = new Point(
183 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
184 largeSide);
185 } else {
186 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
187 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700188 }
189
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700190 public void addOnChangeListener(OnIDPChangeListener listener) {
191 mChangeListeners.add(listener);
192 }
193
194 private void killProcess(Context context) {
195 Log.e("ConfigMonitor", "restarting launcher");
196 android.os.Process.killProcess(android.os.Process.myPid());
197 }
198
199 private void onConfigChanged(Context context) {
200 // Config changes, what shall we do?
201 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
202
203 // Re-init grid
Sunny Goyal415f1732018-11-29 10:33:47 -0800204 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRIP_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700205
206 int changeFlags = 0;
207 if (numRows != oldProfile.numRows ||
208 numColumns != oldProfile.numColumns ||
209 numFolderColumns != oldProfile.numFolderColumns ||
210 numFolderRows != oldProfile.numFolderRows ||
211 numHotseatIcons != oldProfile.numHotseatIcons) {
212 changeFlags |= CHANGE_FLAG_GRID;
213 }
214
215 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize) {
216 changeFlags |= CHANGE_FLAG_ICON_SIZE;
217 }
218
219 // Create a new config monitor
220 mConfigMonitor.unregister();
221 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
222
223 for (OnIDPChangeListener listener : mChangeListeners) {
224 listener.onIdpChanged(changeFlags, this);
225 }
226 }
227
Sunny Goyal415f1732018-11-29 10:33:47 -0800228 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
229 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700230 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
231 final int depth = parser.getDepth();
232 int type;
233
234 while (((type = parser.next()) != XmlPullParser.END_TAG ||
235 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800236 if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
237
238 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
239 final int displayDepth = parser.getDepth();
240 while (((type = parser.next()) != XmlPullParser.END_TAG ||
241 parser.getDepth() > displayDepth)
242 && type != XmlPullParser.END_DOCUMENT) {
243 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
244 parser.getName())) {
245 profiles.add(new DisplayOption(
246 gridOption, context, Xml.asAttributeSet(parser)));
247 }
248 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700249 }
250 }
251 } catch (IOException|XmlPullParserException e) {
252 throw new RuntimeException(e);
253 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800254
255 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
256 if (!TextUtils.isEmpty(gridName)) {
257 for (DisplayOption option : profiles) {
258 if (gridName.equals(option.grid.name)) {
259 filteredProfiles.add(option);
260 }
261 }
262 }
263 if (filteredProfiles.isEmpty()) {
264 // No grid found, use the default options
265 for (DisplayOption option : profiles) {
266 if (option.canBeDefault) {
267 filteredProfiles.add(option);
268 }
269 }
270 }
271 if (filteredProfiles.isEmpty()) {
272 throw new RuntimeException("No display option with canBeDefault=true");
273 }
274 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700275 }
276
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700277 private int getLauncherIconDensity(int requiredSize) {
278 // Densities typically defined by an app.
279 int[] densityBuckets = new int[] {
280 DisplayMetrics.DENSITY_LOW,
281 DisplayMetrics.DENSITY_MEDIUM,
282 DisplayMetrics.DENSITY_TV,
283 DisplayMetrics.DENSITY_HIGH,
284 DisplayMetrics.DENSITY_XHIGH,
285 DisplayMetrics.DENSITY_XXHIGH,
286 DisplayMetrics.DENSITY_XXXHIGH
287 };
288
289 int density = DisplayMetrics.DENSITY_XXXHIGH;
290 for (int i = densityBuckets.length - 1; i >= 0; i--) {
291 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
292 / DisplayMetrics.DENSITY_DEFAULT;
293 if (expectedSize >= requiredSize) {
294 density = densityBuckets[i];
295 }
296 }
297
298 return density;
299 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700300
Adam Cohen2e6da152015-05-06 11:42:25 -0700301 /**
302 * Apply any Partner customization grid overrides.
303 *
304 * Currently we support: all apps row / column count.
305 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700306 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
307 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700308 if (p != null) {
309 p.applyInvariantDeviceProfileOverrides(this, dm);
310 }
311 }
312
Sunny Goyal415f1732018-11-29 10:33:47 -0800313 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700314 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700315 }
316
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700317 /**
318 * Returns the closest device profiles ordered by closeness to the specified width and height
319 */
Sunny Goyal415f1732018-11-29 10:33:47 -0800320 @VisibleForTesting
321 static ArrayList<DisplayOption> sortByClosenessToSize(
322 float width, float height, ArrayList<DisplayOption> points) {
323 points.sort((a, b) ->
324 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
325 dist(width, height, b.minWidthDps, b.minHeightDps)));
326 return points;
Adam Cohen2e6da152015-05-06 11:42:25 -0700327 }
328
Sunny Goyal415f1732018-11-29 10:33:47 -0800329 @VisibleForTesting
330 static DisplayOption invDistWeightedInterpolate(float width, float height,
331 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700332 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700333
Sunny Goyal415f1732018-11-29 10:33:47 -0800334 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700335 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
336 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700337 }
338
Sunny Goyal415f1732018-11-29 10:33:47 -0800339 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700340 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800341 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700342 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
343 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800344 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700345 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800346 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700347 }
348
Sunny Goyal27835952017-01-13 12:15:53 -0800349 public DeviceProfile getDeviceProfile(Context context) {
350 return context.getResources().getConfiguration().orientation
351 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
352 }
353
Sunny Goyal415f1732018-11-29 10:33:47 -0800354 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700355 float d = dist(x0, y0, x1, y1);
356 if (Float.compare(d, 0f) == 0) {
357 return Float.POSITIVE_INFINITY;
358 }
359 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
360 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700361
362 /**
363 * As a ratio of screen height, the total distance we want the parallax effect to span
364 * horizontally
365 */
366 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
367 float aspectRatio = width / (float) height;
368
369 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
370 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
371 // We will use these two data points to extrapolate how much the wallpaper parallax effect
372 // to span (ie travel) at any aspect ratio:
373
374 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
375 final float ASPECT_RATIO_PORTRAIT = 10/16f;
376 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
377 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
378
379 // To find out the desired width at different aspect ratios, we use the following two
380 // formulas, where the coefficient on x is the aspect ratio (width/height):
381 // (16/10)x + y = 1.5
382 // (10/16)x + y = 1.2
383 // We solve for x and y and end up with a final formula:
384 final float x =
385 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
386 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
387 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
388 return x * aspectRatio + y;
389 }
390
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700391 public interface OnIDPChangeListener {
392
393 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
394 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800395
396
397 private static final class GridOption {
398
399 private final String name;
400 private final int numRows;
401 private final int numColumns;
402
403 private final int numFolderRows;
404 private final int numFolderColumns;
405
406 private final int numHotseatIcons;
407
408 private final int defaultLayoutId;
409 private final int demoModeLayoutId;
410
411 GridOption(Context context, AttributeSet attrs) {
412 TypedArray a = context.obtainStyledAttributes(
413 attrs, R.styleable.GridDisplayOption);
414 name = a.getString(R.styleable.GridDisplayOption_name);
415 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
416 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
417
418 defaultLayoutId = a.getResourceId(
419 R.styleable.GridDisplayOption_defaultLayoutId, 0);
420 demoModeLayoutId = a.getResourceId(
421 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
422 numHotseatIcons = a.getInt(
423 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
424 numFolderRows = a.getInt(
425 R.styleable.GridDisplayOption_numFolderRows, numRows);
426 numFolderColumns = a.getInt(
427 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
428 a.recycle();
429 }
430 }
431
432 private static final class DisplayOption {
433 private final GridOption grid;
434
435 private final String name;
436 private final float minWidthDps;
437 private final float minHeightDps;
438 private final boolean canBeDefault;
439
440 private float iconSize;
441 private float landscapeIconSize;
442 private float iconTextSize;
443
444 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
445 this.grid = grid;
446
447 TypedArray a = context.obtainStyledAttributes(
448 attrs, R.styleable.ProfileDisplayOption);
449
450 name = a.getString(R.styleable.ProfileDisplayOption_name);
451 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
452 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
453 canBeDefault = a.getBoolean(
454 R.styleable.ProfileDisplayOption_canBeDefault, false);
455
456 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
457 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
458 iconSize);
459 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
460 a.recycle();
461 }
462
463 DisplayOption() {
464 grid = null;
465 name = null;
466 minWidthDps = 0;
467 minHeightDps = 0;
468 canBeDefault = false;
469 }
470
471 private DisplayOption multiply(float w) {
472 iconSize *= w;
473 landscapeIconSize *= w;
474 iconTextSize *= w;
475 return this;
476 }
477
478 private DisplayOption add(DisplayOption p) {
479 iconSize += p.iconSize;
480 landscapeIconSize += p.landscapeIconSize;
481 iconTextSize += p.iconTextSize;
482 return this;
483 }
484 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700485}