blob: 28ba4e7c119d30bec807c31e009a647583721491 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.content.res;
18
19import android.content.pm.ActivityInfo;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Locale;
24
25/**
26 * This class describes all device configuration information that can
27 * impact the resources the application retrieves. This includes both
28 * user-specified configuration options (locale and scaling) as well
29 * as dynamic device configuration (various types of input devices).
30 */
31public final class Configuration implements Parcelable, Comparable<Configuration> {
32 /**
33 * Current user preference for the scaling factor for fonts, relative
34 * to the base density scaling.
35 */
36 public float fontScale;
37
38 /**
39 * IMSI MCC (Mobile Country Code). 0 if undefined.
40 */
41 public int mcc;
42
43 /**
44 * IMSI MNC (Mobile Network Code). 0 if undefined.
45 */
46 public int mnc;
47
48 /**
49 * Current user preference for the locale.
50 */
51 public Locale locale;
52
53 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070054 * Locale should persist on setting. This is hidden because it is really
55 * questionable whether this is the right way to expose the functionality.
56 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 */
58 public boolean userSetLocale;
59
Dianne Hackborn2f98f262011-03-28 18:28:35 -070060 /** Constant for {@link #screenLayout}: bits that encode the size. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070061 public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070062 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
63 * value indicating that no size has been set. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070064 public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070065 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
66 * value indicating the screen is at least approximately 320x426 dp units.
67 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
68 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070069 public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070070 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
71 * value indicating the screen is at least approximately 320x470 dp units.
72 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
73 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070074 public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070075 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
76 * value indicating the screen is at least approximately 480x640 dp units.
77 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
78 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070079 public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070080 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
81 * value indicating the screen is at least approximately 720x960 dp units.
82 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
83 * Multiple Screens</a> for more information.*/
Dianne Hackborn14cee9f2010-04-23 17:51:26 -070084 public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070085
86 public static final int SCREENLAYOUT_LONG_MASK = 0x30;
87 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
88 public static final int SCREENLAYOUT_LONG_NO = 0x10;
89 public static final int SCREENLAYOUT_LONG_YES = 0x20;
90
91 /**
92 * Special flag we generate to indicate that the screen layout requires
93 * us to use a compatibility mode for apps that are not modern layout
94 * aware.
95 * @hide
96 */
97 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
98
99 /**
100 * Bit mask of overall layout of the screen. Currently there are two
101 * fields:
102 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
103 * of the screen. They may be one of
104 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
Dianne Hackborn14cee9f2010-04-23 17:51:26 -0700105 * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700106 *
107 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
108 * is wider/taller than normal. They may be one of
109 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
Dianne Hackborn2f98f262011-03-28 18:28:35 -0700110 *
111 * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
112 * Multiple Screens</a> for more information.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700113 */
114 public int screenLayout;
115
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800116 /**
117 * Check if the Configuration's current {@link #screenLayout} is at
118 * least the given size.
119 *
120 * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
121 * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
122 * {@link #SCREENLAYOUT_SIZE_XLARGE}.
123 * @return Returns true if the current screen layout size is at least
124 * the given size.
125 */
126 public boolean isLayoutSizeAtLeast(int size) {
127 int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
128 if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800129 return cur >= size;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800130 }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int TOUCHSCREEN_UNDEFINED = 0;
133 public static final int TOUCHSCREEN_NOTOUCH = 1;
134 public static final int TOUCHSCREEN_STYLUS = 2;
135 public static final int TOUCHSCREEN_FINGER = 3;
136
137 /**
138 * The kind of touch screen attached to the device.
139 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
140 * {@link #TOUCHSCREEN_FINGER}.
141 */
142 public int touchscreen;
143
144 public static final int KEYBOARD_UNDEFINED = 0;
145 public static final int KEYBOARD_NOKEYS = 1;
146 public static final int KEYBOARD_QWERTY = 2;
147 public static final int KEYBOARD_12KEY = 3;
148
149 /**
150 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500151 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
152 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
154 public int keyboard;
155
156 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
157 public static final int KEYBOARDHIDDEN_NO = 1;
158 public static final int KEYBOARDHIDDEN_YES = 2;
159 /** Constant matching actual resource implementation. {@hide} */
160 public static final int KEYBOARDHIDDEN_SOFT = 3;
161
162 /**
163 * A flag indicating whether any keyboard is available. Unlike
164 * {@link #hardKeyboardHidden}, this also takes into account a soft
165 * keyboard, so if the hard keyboard is hidden but there is soft
166 * keyboard available, it will be set to NO. Value is one of:
167 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
168 */
169 public int keyboardHidden;
170
171 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
172 public static final int HARDKEYBOARDHIDDEN_NO = 1;
173 public static final int HARDKEYBOARDHIDDEN_YES = 2;
174
175 /**
176 * A flag indicating whether the hard keyboard has been hidden. This will
177 * be set on a device with a mechanism to hide the keyboard from the
178 * user, when that mechanism is closed. One of:
179 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
180 */
181 public int hardKeyboardHidden;
182
183 public static final int NAVIGATION_UNDEFINED = 0;
184 public static final int NAVIGATION_NONAV = 1;
185 public static final int NAVIGATION_DPAD = 2;
186 public static final int NAVIGATION_TRACKBALL = 3;
187 public static final int NAVIGATION_WHEEL = 4;
188
189 /**
190 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500191 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
192 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 */
194 public int navigation;
195
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700196 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
197 public static final int NAVIGATIONHIDDEN_NO = 1;
198 public static final int NAVIGATIONHIDDEN_YES = 2;
199
200 /**
201 * A flag indicating whether any 5-way or DPAD navigation available.
202 * This will be set on a device with a mechanism to hide the navigation
203 * controls from the user, when that mechanism is closed. One of:
204 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
205 */
206 public int navigationHidden;
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public static final int ORIENTATION_UNDEFINED = 0;
209 public static final int ORIENTATION_PORTRAIT = 1;
210 public static final int ORIENTATION_LANDSCAPE = 2;
211 public static final int ORIENTATION_SQUARE = 3;
212
213 /**
214 * Overall orientation of the screen. May be one of
215 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
216 * or {@link #ORIENTATION_SQUARE}.
217 */
218 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100219
Tobias Haamel27b28b32010-02-09 23:09:17 +0100220 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800221 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
222 public static final int UI_MODE_TYPE_NORMAL = 0x01;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800223 public static final int UI_MODE_TYPE_DESK = 0x02;
224 public static final int UI_MODE_TYPE_CAR = 0x03;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100225
Tobias Haamel27b28b32010-02-09 23:09:17 +0100226 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100227 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100228 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100229 public static final int UI_MODE_NIGHT_YES = 0x20;
230
231 /**
232 * Bit mask of the ui mode. Currently there are two fields:
233 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
Dianne Hackborn7299c412010-03-04 18:41:49 -0800234 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
235 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
236 * or {@link #UI_MODE_TYPE_CAR}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100237 *
238 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
Dianne Hackborn7299c412010-03-04 18:41:49 -0800239 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
Tobias Haamel27b28b32010-02-09 23:09:17 +0100240 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100241 */
242 public int uiMode;
243
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700244 public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
245
246 /**
247 * The current width of the available screen space, in dp units.
248 */
249 public int screenWidthDp;
250
251 public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0;
252
253 /**
254 * The current height of the available screen space, in dp units.
255 */
256 public int screenHeightDp;
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800259 * @hide Internal book-keeping.
260 */
261 public int seq;
262
263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * Construct an invalid Configuration. You must call {@link #setToDefaults}
265 * for this object to be valid. {@more}
266 */
267 public Configuration() {
268 setToDefaults();
269 }
270
271 /**
272 * Makes a deep copy suitable for modification.
273 */
274 public Configuration(Configuration o) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700275 setTo(o);
276 }
277
278 public void setTo(Configuration o) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 fontScale = o.fontScale;
280 mcc = o.mcc;
281 mnc = o.mnc;
282 if (o.locale != null) {
283 locale = (Locale) o.locale.clone();
284 }
285 userSetLocale = o.userSetLocale;
286 touchscreen = o.touchscreen;
287 keyboard = o.keyboard;
288 keyboardHidden = o.keyboardHidden;
289 hardKeyboardHidden = o.hardKeyboardHidden;
290 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700291 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700293 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100294 uiMode = o.uiMode;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700295 screenWidthDp = o.screenWidthDp;
296 screenHeightDp = o.screenHeightDp;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800297 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700301 StringBuilder sb = new StringBuilder(128);
302 sb.append("{ scale=");
303 sb.append(fontScale);
304 sb.append(" imsi=");
305 sb.append(mcc);
306 sb.append("/");
307 sb.append(mnc);
308 sb.append(" loc=");
309 sb.append(locale);
310 sb.append(" touch=");
311 sb.append(touchscreen);
312 sb.append(" keys=");
313 sb.append(keyboard);
314 sb.append("/");
315 sb.append(keyboardHidden);
316 sb.append("/");
317 sb.append(hardKeyboardHidden);
318 sb.append(" nav=");
319 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700320 sb.append("/");
321 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700322 sb.append(" orien=");
Daniel Sandler7d6bddc2010-07-22 16:11:55 -0400323 switch(orientation) {
324 case ORIENTATION_LANDSCAPE:
325 sb.append("L"); break;
326 case ORIENTATION_PORTRAIT:
327 sb.append("P"); break;
328 default:
329 sb.append(orientation);
330 }
331 sb.append(" layout=0x");
332 sb.append(java.lang.Integer.toHexString(screenLayout));
333 sb.append(" uiMode=0x");
334 sb.append(java.lang.Integer.toHexString(uiMode));
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700335 sb.append(" wdp=");
336 sb.append(screenWidthDp);
337 sb.append(" hdp=");
338 sb.append(screenHeightDp);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800339 if (seq != 0) {
340 sb.append(" seq=");
341 sb.append(seq);
342 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700343 sb.append('}');
344 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
347 /**
348 * Set this object to the system defaults.
349 */
350 public void setToDefaults() {
351 fontScale = 1;
352 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800353 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 userSetLocale = false;
355 touchscreen = TOUCHSCREEN_UNDEFINED;
356 keyboard = KEYBOARD_UNDEFINED;
357 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
358 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
359 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700360 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700362 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800363 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700364 screenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
365 screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800366 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 }
368
369 /** {@hide} */
370 @Deprecated public void makeDefault() {
371 setToDefaults();
372 }
373
374 /**
375 * Copy the fields from delta into this Configuration object, keeping
376 * track of which ones have changed. Any undefined fields in
377 * <var>delta</var> are ignored and not copied in to the current
378 * Configuration.
379 * @return Returns a bit mask of the changed fields, as per
380 * {@link #diff}.
381 */
382 public int updateFrom(Configuration delta) {
383 int changed = 0;
384 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
385 changed |= ActivityInfo.CONFIG_FONT_SCALE;
386 fontScale = delta.fontScale;
387 }
388 if (delta.mcc != 0 && mcc != delta.mcc) {
389 changed |= ActivityInfo.CONFIG_MCC;
390 mcc = delta.mcc;
391 }
392 if (delta.mnc != 0 && mnc != delta.mnc) {
393 changed |= ActivityInfo.CONFIG_MNC;
394 mnc = delta.mnc;
395 }
396 if (delta.locale != null
397 && (locale == null || !locale.equals(delta.locale))) {
398 changed |= ActivityInfo.CONFIG_LOCALE;
399 locale = delta.locale != null
400 ? (Locale) delta.locale.clone() : null;
401 }
402 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
403 {
404 userSetLocale = true;
405 changed |= ActivityInfo.CONFIG_LOCALE;
406 }
407 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
408 && touchscreen != delta.touchscreen) {
409 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
410 touchscreen = delta.touchscreen;
411 }
412 if (delta.keyboard != KEYBOARD_UNDEFINED
413 && keyboard != delta.keyboard) {
414 changed |= ActivityInfo.CONFIG_KEYBOARD;
415 keyboard = delta.keyboard;
416 }
417 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
418 && keyboardHidden != delta.keyboardHidden) {
419 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
420 keyboardHidden = delta.keyboardHidden;
421 }
422 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
423 && hardKeyboardHidden != delta.hardKeyboardHidden) {
424 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
425 hardKeyboardHidden = delta.hardKeyboardHidden;
426 }
427 if (delta.navigation != NAVIGATION_UNDEFINED
428 && navigation != delta.navigation) {
429 changed |= ActivityInfo.CONFIG_NAVIGATION;
430 navigation = delta.navigation;
431 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700432 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
433 && navigationHidden != delta.navigationHidden) {
434 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
435 navigationHidden = delta.navigationHidden;
436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 if (delta.orientation != ORIENTATION_UNDEFINED
438 && orientation != delta.orientation) {
439 changed |= ActivityInfo.CONFIG_ORIENTATION;
440 orientation = delta.orientation;
441 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700442 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700443 && screenLayout != delta.screenLayout) {
444 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
445 screenLayout = delta.screenLayout;
446 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800447 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100448 && uiMode != delta.uiMode) {
449 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800450 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
451 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
452 | (delta.uiMode&UI_MODE_TYPE_MASK);
453 }
454 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
455 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
456 | (delta.uiMode&UI_MODE_NIGHT_MASK);
457 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100458 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700459 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
460 && screenWidthDp != delta.screenWidthDp) {
461 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
462 screenWidthDp = delta.screenWidthDp;
463 }
464 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
465 && screenHeightDp != delta.screenHeightDp) {
466 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
467 screenHeightDp = delta.screenHeightDp;
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800470 if (delta.seq != 0) {
471 seq = delta.seq;
472 }
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 return changed;
475 }
476
477 /**
478 * Return a bit mask of the differences between this Configuration
479 * object and the given one. Does not change the values of either. Any
480 * undefined fields in <var>delta</var> are ignored.
481 * @return Returns a bit mask indicating which configuration
482 * values has changed, containing any combination of
483 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
484 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
485 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
486 * PackageManager.ActivityInfo.CONFIG_MCC},
487 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
488 * PackageManager.ActivityInfo.CONFIG_MNC},
489 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
490 * PackageManager.ActivityInfo.CONFIG_LOCALE},
491 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
492 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
493 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
494 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
495 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700496 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700498 * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
Dianne Hackborn723738c2009-06-25 19:48:04 -0700499 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700500 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
501 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
502 * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 */
504 public int diff(Configuration delta) {
505 int changed = 0;
506 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
507 changed |= ActivityInfo.CONFIG_FONT_SCALE;
508 }
509 if (delta.mcc != 0 && mcc != delta.mcc) {
510 changed |= ActivityInfo.CONFIG_MCC;
511 }
512 if (delta.mnc != 0 && mnc != delta.mnc) {
513 changed |= ActivityInfo.CONFIG_MNC;
514 }
515 if (delta.locale != null
516 && (locale == null || !locale.equals(delta.locale))) {
517 changed |= ActivityInfo.CONFIG_LOCALE;
518 }
519 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
520 && touchscreen != delta.touchscreen) {
521 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
522 }
523 if (delta.keyboard != KEYBOARD_UNDEFINED
524 && keyboard != delta.keyboard) {
525 changed |= ActivityInfo.CONFIG_KEYBOARD;
526 }
527 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
528 && keyboardHidden != delta.keyboardHidden) {
529 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
530 }
531 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
532 && hardKeyboardHidden != delta.hardKeyboardHidden) {
533 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
534 }
535 if (delta.navigation != NAVIGATION_UNDEFINED
536 && navigation != delta.navigation) {
537 changed |= ActivityInfo.CONFIG_NAVIGATION;
538 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700539 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
540 && navigationHidden != delta.navigationHidden) {
541 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 if (delta.orientation != ORIENTATION_UNDEFINED
544 && orientation != delta.orientation) {
545 changed |= ActivityInfo.CONFIG_ORIENTATION;
546 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700547 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700548 && screenLayout != delta.screenLayout) {
549 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
550 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800551 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100552 && uiMode != delta.uiMode) {
553 changed |= ActivityInfo.CONFIG_UI_MODE;
554 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700555 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
556 && screenWidthDp != delta.screenWidthDp) {
557 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
558 }
559 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
560 && screenHeightDp != delta.screenHeightDp) {
561 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
564 return changed;
565 }
566
567 /**
568 * Determine if a new resource needs to be loaded from the bit set of
569 * configuration changes returned by {@link #updateFrom(Configuration)}.
570 *
571 * @param configChanges The mask of changes configurations as returned by
572 * {@link #updateFrom(Configuration)}.
573 * @param interestingChanges The configuration changes that the resource
574 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
575 *
576 * @return Return true if the resource needs to be loaded, else false.
577 */
578 public static boolean needNewResources(int configChanges, int interestingChanges) {
579 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
580 }
581
582 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800583 * @hide Return true if the sequence of 'other' is better than this. Assumes
584 * that 'this' is your current sequence and 'other' is a new one you have
585 * received some how and want to compare with what you have.
586 */
587 public boolean isOtherSeqNewer(Configuration other) {
588 if (other == null) {
589 // Sanity check.
590 return false;
591 }
592 if (other.seq == 0) {
593 // If the other sequence is not specified, then we must assume
594 // it is newer since we don't know any better.
595 return true;
596 }
597 if (seq == 0) {
598 // If this sequence is not specified, then we also consider the
599 // other is better. Yes we have a preference for other. Sue us.
600 return true;
601 }
602 int diff = other.seq - seq;
603 if (diff > 0x10000) {
604 // If there has been a sufficiently large jump, assume the
605 // sequence has wrapped around.
606 return false;
607 }
608 return diff > 0;
609 }
610
611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * Parcelable methods
613 */
614 public int describeContents() {
615 return 0;
616 }
617
618 public void writeToParcel(Parcel dest, int flags) {
619 dest.writeFloat(fontScale);
620 dest.writeInt(mcc);
621 dest.writeInt(mnc);
622 if (locale == null) {
623 dest.writeInt(0);
624 } else {
625 dest.writeInt(1);
626 dest.writeString(locale.getLanguage());
627 dest.writeString(locale.getCountry());
628 dest.writeString(locale.getVariant());
629 }
630 if(userSetLocale) {
631 dest.writeInt(1);
632 } else {
633 dest.writeInt(0);
634 }
635 dest.writeInt(touchscreen);
636 dest.writeInt(keyboard);
637 dest.writeInt(keyboardHidden);
638 dest.writeInt(hardKeyboardHidden);
639 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700640 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700642 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100643 dest.writeInt(uiMode);
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700644 dest.writeInt(screenWidthDp);
645 dest.writeInt(screenHeightDp);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800646 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700649 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 fontScale = source.readFloat();
651 mcc = source.readInt();
652 mnc = source.readInt();
653 if (source.readInt() != 0) {
654 locale = new Locale(source.readString(), source.readString(),
655 source.readString());
656 }
657 userSetLocale = (source.readInt()==1);
658 touchscreen = source.readInt();
659 keyboard = source.readInt();
660 keyboardHidden = source.readInt();
661 hardKeyboardHidden = source.readInt();
662 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700663 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700665 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100666 uiMode = source.readInt();
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700667 screenWidthDp = source.readInt();
668 screenHeightDp = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800669 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700671
672 public static final Parcelable.Creator<Configuration> CREATOR
673 = new Parcelable.Creator<Configuration>() {
674 public Configuration createFromParcel(Parcel source) {
675 return new Configuration(source);
676 }
677
678 public Configuration[] newArray(int size) {
679 return new Configuration[size];
680 }
681 };
682
683 /**
684 * Construct this Configuration object, reading from the Parcel.
685 */
686 private Configuration(Parcel source) {
687 readFromParcel(source);
688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689
690 public int compareTo(Configuration that) {
691 int n;
692 float a = this.fontScale;
693 float b = that.fontScale;
694 if (a < b) return -1;
695 if (a > b) return 1;
696 n = this.mcc - that.mcc;
697 if (n != 0) return n;
698 n = this.mnc - that.mnc;
699 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800700 if (this.locale == null) {
701 if (that.locale != null) return 1;
702 } else if (that.locale == null) {
703 return -1;
704 } else {
705 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
706 if (n != 0) return n;
707 n = this.locale.getCountry().compareTo(that.locale.getCountry());
708 if (n != 0) return n;
709 n = this.locale.getVariant().compareTo(that.locale.getVariant());
710 if (n != 0) return n;
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 n = this.touchscreen - that.touchscreen;
713 if (n != 0) return n;
714 n = this.keyboard - that.keyboard;
715 if (n != 0) return n;
716 n = this.keyboardHidden - that.keyboardHidden;
717 if (n != 0) return n;
718 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
719 if (n != 0) return n;
720 n = this.navigation - that.navigation;
721 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700722 n = this.navigationHidden - that.navigationHidden;
723 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700725 if (n != 0) return n;
726 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100727 if (n != 0) return n;
728 n = this.uiMode - that.uiMode;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700729 if (n != 0) return n;
730 n = this.screenWidthDp - that.screenWidthDp;
731 if (n != 0) return n;
732 n = this.screenHeightDp - that.screenHeightDp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 //if (n != 0) return n;
734 return n;
735 }
736
737 public boolean equals(Configuration that) {
738 if (that == null) return false;
739 if (that == this) return true;
740 return this.compareTo(that) == 0;
741 }
742
743 public boolean equals(Object that) {
744 try {
745 return equals((Configuration)that);
746 } catch (ClassCastException e) {
747 }
748 return false;
749 }
750
751 public int hashCode() {
752 return ((int)this.fontScale) + this.mcc + this.mnc
Dianne Hackborna8397032010-03-12 10:52:22 -0800753 + (this.locale != null ? this.locale.hashCode() : 0)
754 + this.touchscreen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700756 + this.navigation + this.navigationHidden
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700757 + this.orientation + this.screenLayout + this.uiMode
758 + this.screenWidthDp + this.screenHeightDp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760}