blob: b9dfd72a1878772806417ff728d15aea0befefca [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);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700302 sb.append("{ fnt=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700303 sb.append(fontScale);
304 sb.append(" imsi=");
305 sb.append(mcc);
306 sb.append("/");
307 sb.append(mnc);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700308 if (locale != null) {
309 sb.append(" ");
310 sb.append(locale);
311 } else {
312 sb.append(" (no locale)");
Daniel Sandler7d6bddc2010-07-22 16:11:55 -0400313 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700314 switch (touchscreen) {
315 case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
316 case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
317 case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
318 case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
319 default: sb.append(" touch="); sb.append(touchscreen); break;
320 }
321 switch (keyboard) {
322 case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
323 case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
324 case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
325 case KEYBOARD_12KEY: sb.append(" 12key"); break;
326 default: sb.append(" keys="); sb.append(keyboard); break;
327 }
328 switch (keyboardHidden) {
329 case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
330 case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
331 case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
332 case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
333 default: sb.append("/"); sb.append(keyboardHidden); break;
334 }
335 switch (hardKeyboardHidden) {
336 case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
337 case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
338 case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
339 default: sb.append("/"); sb.append(hardKeyboardHidden); break;
340 }
341 switch (navigation) {
342 case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
343 case NAVIGATION_NONAV: sb.append(" -nav"); break;
344 case NAVIGATION_DPAD: sb.append(" dpad"); break;
345 case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
346 case NAVIGATION_WHEEL: sb.append(" wheel"); break;
347 default: sb.append(" nav="); sb.append(navigation); break;
348 }
349 switch (navigationHidden) {
350 case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
351 case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
352 case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
353 default: sb.append("/"); sb.append(navigationHidden); break;
354 }
355 switch (orientation) {
356 case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
357 case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
358 case ORIENTATION_PORTRAIT: sb.append(" port"); break;
359 default: sb.append(" orien="); sb.append(orientation); break;
360 }
361 switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
362 case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
363 case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
364 case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
365 case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
366 case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
367 default: sb.append(" layoutSize=");
368 sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
369 }
370 switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
371 case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
372 case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
373 case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
374 default: sb.append(" layoutLong=");
375 sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
376 }
377 switch ((uiMode&UI_MODE_TYPE_MASK)) {
378 case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
379 case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
380 case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
381 case UI_MODE_TYPE_CAR: sb.append(" car"); break;
382 default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
383 }
384 switch ((uiMode&UI_MODE_NIGHT_MASK)) {
385 case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
386 case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
387 case UI_MODE_NIGHT_YES: sb.append(" night"); break;
388 default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
389 }
390 if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
391 sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
392 } else {
393 sb.append("?wdp");
394 }
395 if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
396 sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
397 } else {
398 sb.append("?hdp");
399 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800400 if (seq != 0) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700401 sb.append(" s.");
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800402 sb.append(seq);
403 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700404 sb.append('}');
405 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407
408 /**
409 * Set this object to the system defaults.
410 */
411 public void setToDefaults() {
412 fontScale = 1;
413 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800414 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 userSetLocale = false;
416 touchscreen = TOUCHSCREEN_UNDEFINED;
417 keyboard = KEYBOARD_UNDEFINED;
418 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
419 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
420 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700421 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700423 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800424 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700425 screenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
426 screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800427 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 /** {@hide} */
431 @Deprecated public void makeDefault() {
432 setToDefaults();
433 }
434
435 /**
436 * Copy the fields from delta into this Configuration object, keeping
437 * track of which ones have changed. Any undefined fields in
438 * <var>delta</var> are ignored and not copied in to the current
439 * Configuration.
440 * @return Returns a bit mask of the changed fields, as per
441 * {@link #diff}.
442 */
443 public int updateFrom(Configuration delta) {
444 int changed = 0;
445 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
446 changed |= ActivityInfo.CONFIG_FONT_SCALE;
447 fontScale = delta.fontScale;
448 }
449 if (delta.mcc != 0 && mcc != delta.mcc) {
450 changed |= ActivityInfo.CONFIG_MCC;
451 mcc = delta.mcc;
452 }
453 if (delta.mnc != 0 && mnc != delta.mnc) {
454 changed |= ActivityInfo.CONFIG_MNC;
455 mnc = delta.mnc;
456 }
457 if (delta.locale != null
458 && (locale == null || !locale.equals(delta.locale))) {
459 changed |= ActivityInfo.CONFIG_LOCALE;
460 locale = delta.locale != null
461 ? (Locale) delta.locale.clone() : null;
462 }
463 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
464 {
465 userSetLocale = true;
466 changed |= ActivityInfo.CONFIG_LOCALE;
467 }
468 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
469 && touchscreen != delta.touchscreen) {
470 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
471 touchscreen = delta.touchscreen;
472 }
473 if (delta.keyboard != KEYBOARD_UNDEFINED
474 && keyboard != delta.keyboard) {
475 changed |= ActivityInfo.CONFIG_KEYBOARD;
476 keyboard = delta.keyboard;
477 }
478 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
479 && keyboardHidden != delta.keyboardHidden) {
480 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
481 keyboardHidden = delta.keyboardHidden;
482 }
483 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
484 && hardKeyboardHidden != delta.hardKeyboardHidden) {
485 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
486 hardKeyboardHidden = delta.hardKeyboardHidden;
487 }
488 if (delta.navigation != NAVIGATION_UNDEFINED
489 && navigation != delta.navigation) {
490 changed |= ActivityInfo.CONFIG_NAVIGATION;
491 navigation = delta.navigation;
492 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700493 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
494 && navigationHidden != delta.navigationHidden) {
495 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
496 navigationHidden = delta.navigationHidden;
497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 if (delta.orientation != ORIENTATION_UNDEFINED
499 && orientation != delta.orientation) {
500 changed |= ActivityInfo.CONFIG_ORIENTATION;
501 orientation = delta.orientation;
502 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700503 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700504 && screenLayout != delta.screenLayout) {
505 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
506 screenLayout = delta.screenLayout;
507 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800508 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100509 && uiMode != delta.uiMode) {
510 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800511 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
512 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
513 | (delta.uiMode&UI_MODE_TYPE_MASK);
514 }
515 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
516 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
517 | (delta.uiMode&UI_MODE_NIGHT_MASK);
518 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100519 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700520 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
521 && screenWidthDp != delta.screenWidthDp) {
522 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
523 screenWidthDp = delta.screenWidthDp;
524 }
525 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
526 && screenHeightDp != delta.screenHeightDp) {
527 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
528 screenHeightDp = delta.screenHeightDp;
529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800531 if (delta.seq != 0) {
532 seq = delta.seq;
533 }
534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 return changed;
536 }
537
538 /**
539 * Return a bit mask of the differences between this Configuration
540 * object and the given one. Does not change the values of either. Any
541 * undefined fields in <var>delta</var> are ignored.
542 * @return Returns a bit mask indicating which configuration
543 * values has changed, containing any combination of
544 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
545 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
546 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
547 * PackageManager.ActivityInfo.CONFIG_MCC},
548 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
549 * PackageManager.ActivityInfo.CONFIG_MNC},
550 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
551 * PackageManager.ActivityInfo.CONFIG_LOCALE},
552 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
553 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
554 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
555 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
556 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700557 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700559 * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
Dianne Hackborn723738c2009-06-25 19:48:04 -0700560 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700561 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
562 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
563 * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 */
565 public int diff(Configuration delta) {
566 int changed = 0;
567 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
568 changed |= ActivityInfo.CONFIG_FONT_SCALE;
569 }
570 if (delta.mcc != 0 && mcc != delta.mcc) {
571 changed |= ActivityInfo.CONFIG_MCC;
572 }
573 if (delta.mnc != 0 && mnc != delta.mnc) {
574 changed |= ActivityInfo.CONFIG_MNC;
575 }
576 if (delta.locale != null
577 && (locale == null || !locale.equals(delta.locale))) {
578 changed |= ActivityInfo.CONFIG_LOCALE;
579 }
580 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
581 && touchscreen != delta.touchscreen) {
582 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
583 }
584 if (delta.keyboard != KEYBOARD_UNDEFINED
585 && keyboard != delta.keyboard) {
586 changed |= ActivityInfo.CONFIG_KEYBOARD;
587 }
588 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
589 && keyboardHidden != delta.keyboardHidden) {
590 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
591 }
592 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
593 && hardKeyboardHidden != delta.hardKeyboardHidden) {
594 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
595 }
596 if (delta.navigation != NAVIGATION_UNDEFINED
597 && navigation != delta.navigation) {
598 changed |= ActivityInfo.CONFIG_NAVIGATION;
599 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700600 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
601 && navigationHidden != delta.navigationHidden) {
602 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 if (delta.orientation != ORIENTATION_UNDEFINED
605 && orientation != delta.orientation) {
606 changed |= ActivityInfo.CONFIG_ORIENTATION;
607 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700608 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700609 && screenLayout != delta.screenLayout) {
610 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
611 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800612 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100613 && uiMode != delta.uiMode) {
614 changed |= ActivityInfo.CONFIG_UI_MODE;
615 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700616 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
617 && screenWidthDp != delta.screenWidthDp) {
618 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
619 }
620 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
621 && screenHeightDp != delta.screenHeightDp) {
622 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
625 return changed;
626 }
627
628 /**
629 * Determine if a new resource needs to be loaded from the bit set of
630 * configuration changes returned by {@link #updateFrom(Configuration)}.
631 *
632 * @param configChanges The mask of changes configurations as returned by
633 * {@link #updateFrom(Configuration)}.
634 * @param interestingChanges The configuration changes that the resource
635 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
636 *
637 * @return Return true if the resource needs to be loaded, else false.
638 */
639 public static boolean needNewResources(int configChanges, int interestingChanges) {
640 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
641 }
642
643 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800644 * @hide Return true if the sequence of 'other' is better than this. Assumes
645 * that 'this' is your current sequence and 'other' is a new one you have
646 * received some how and want to compare with what you have.
647 */
648 public boolean isOtherSeqNewer(Configuration other) {
649 if (other == null) {
650 // Sanity check.
651 return false;
652 }
653 if (other.seq == 0) {
654 // If the other sequence is not specified, then we must assume
655 // it is newer since we don't know any better.
656 return true;
657 }
658 if (seq == 0) {
659 // If this sequence is not specified, then we also consider the
660 // other is better. Yes we have a preference for other. Sue us.
661 return true;
662 }
663 int diff = other.seq - seq;
664 if (diff > 0x10000) {
665 // If there has been a sufficiently large jump, assume the
666 // sequence has wrapped around.
667 return false;
668 }
669 return diff > 0;
670 }
671
672 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 * Parcelable methods
674 */
675 public int describeContents() {
676 return 0;
677 }
678
679 public void writeToParcel(Parcel dest, int flags) {
680 dest.writeFloat(fontScale);
681 dest.writeInt(mcc);
682 dest.writeInt(mnc);
683 if (locale == null) {
684 dest.writeInt(0);
685 } else {
686 dest.writeInt(1);
687 dest.writeString(locale.getLanguage());
688 dest.writeString(locale.getCountry());
689 dest.writeString(locale.getVariant());
690 }
691 if(userSetLocale) {
692 dest.writeInt(1);
693 } else {
694 dest.writeInt(0);
695 }
696 dest.writeInt(touchscreen);
697 dest.writeInt(keyboard);
698 dest.writeInt(keyboardHidden);
699 dest.writeInt(hardKeyboardHidden);
700 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700701 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700703 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100704 dest.writeInt(uiMode);
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700705 dest.writeInt(screenWidthDp);
706 dest.writeInt(screenHeightDp);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800707 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700710 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 fontScale = source.readFloat();
712 mcc = source.readInt();
713 mnc = source.readInt();
714 if (source.readInt() != 0) {
715 locale = new Locale(source.readString(), source.readString(),
716 source.readString());
717 }
718 userSetLocale = (source.readInt()==1);
719 touchscreen = source.readInt();
720 keyboard = source.readInt();
721 keyboardHidden = source.readInt();
722 hardKeyboardHidden = source.readInt();
723 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700724 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700726 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100727 uiMode = source.readInt();
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700728 screenWidthDp = source.readInt();
729 screenHeightDp = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800730 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700732
733 public static final Parcelable.Creator<Configuration> CREATOR
734 = new Parcelable.Creator<Configuration>() {
735 public Configuration createFromParcel(Parcel source) {
736 return new Configuration(source);
737 }
738
739 public Configuration[] newArray(int size) {
740 return new Configuration[size];
741 }
742 };
743
744 /**
745 * Construct this Configuration object, reading from the Parcel.
746 */
747 private Configuration(Parcel source) {
748 readFromParcel(source);
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750
751 public int compareTo(Configuration that) {
752 int n;
753 float a = this.fontScale;
754 float b = that.fontScale;
755 if (a < b) return -1;
756 if (a > b) return 1;
757 n = this.mcc - that.mcc;
758 if (n != 0) return n;
759 n = this.mnc - that.mnc;
760 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800761 if (this.locale == null) {
762 if (that.locale != null) return 1;
763 } else if (that.locale == null) {
764 return -1;
765 } else {
766 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
767 if (n != 0) return n;
768 n = this.locale.getCountry().compareTo(that.locale.getCountry());
769 if (n != 0) return n;
770 n = this.locale.getVariant().compareTo(that.locale.getVariant());
771 if (n != 0) return n;
772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 n = this.touchscreen - that.touchscreen;
774 if (n != 0) return n;
775 n = this.keyboard - that.keyboard;
776 if (n != 0) return n;
777 n = this.keyboardHidden - that.keyboardHidden;
778 if (n != 0) return n;
779 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
780 if (n != 0) return n;
781 n = this.navigation - that.navigation;
782 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700783 n = this.navigationHidden - that.navigationHidden;
784 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700786 if (n != 0) return n;
787 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100788 if (n != 0) return n;
789 n = this.uiMode - that.uiMode;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700790 if (n != 0) return n;
791 n = this.screenWidthDp - that.screenWidthDp;
792 if (n != 0) return n;
793 n = this.screenHeightDp - that.screenHeightDp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 //if (n != 0) return n;
795 return n;
796 }
797
798 public boolean equals(Configuration that) {
799 if (that == null) return false;
800 if (that == this) return true;
801 return this.compareTo(that) == 0;
802 }
803
804 public boolean equals(Object that) {
805 try {
806 return equals((Configuration)that);
807 } catch (ClassCastException e) {
808 }
809 return false;
810 }
811
812 public int hashCode() {
813 return ((int)this.fontScale) + this.mcc + this.mnc
Dianne Hackborna8397032010-03-12 10:52:22 -0800814 + (this.locale != null ? this.locale.hashCode() : 0)
815 + this.touchscreen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700817 + this.navigation + this.navigationHidden
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700818 + this.orientation + this.screenLayout + this.uiMode
819 + this.screenWidthDp + this.screenHeightDp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821}