blob: 54fc0447e4955ca88bc69847677143e76069a380 [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 Hackbornc4db95c2009-07-21 17:46:02 -070060 public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
61 public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
62 public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
63 public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
64 public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
65
66 public static final int SCREENLAYOUT_LONG_MASK = 0x30;
67 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
68 public static final int SCREENLAYOUT_LONG_NO = 0x10;
69 public static final int SCREENLAYOUT_LONG_YES = 0x20;
70
71 /**
72 * Special flag we generate to indicate that the screen layout requires
73 * us to use a compatibility mode for apps that are not modern layout
74 * aware.
75 * @hide
76 */
77 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
78
79 /**
80 * Bit mask of overall layout of the screen. Currently there are two
81 * fields:
82 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
83 * of the screen. They may be one of
84 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
85 * or {@link #SCREENLAYOUT_SIZE_LARGE}.
86 *
87 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
88 * is wider/taller than normal. They may be one of
89 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
90 */
91 public int screenLayout;
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 public static final int TOUCHSCREEN_UNDEFINED = 0;
94 public static final int TOUCHSCREEN_NOTOUCH = 1;
95 public static final int TOUCHSCREEN_STYLUS = 2;
96 public static final int TOUCHSCREEN_FINGER = 3;
97
98 /**
99 * The kind of touch screen attached to the device.
100 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
101 * {@link #TOUCHSCREEN_FINGER}.
102 */
103 public int touchscreen;
104
105 public static final int KEYBOARD_UNDEFINED = 0;
106 public static final int KEYBOARD_NOKEYS = 1;
107 public static final int KEYBOARD_QWERTY = 2;
108 public static final int KEYBOARD_12KEY = 3;
109
110 /**
111 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500112 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
113 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
115 public int keyboard;
116
117 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
118 public static final int KEYBOARDHIDDEN_NO = 1;
119 public static final int KEYBOARDHIDDEN_YES = 2;
120 /** Constant matching actual resource implementation. {@hide} */
121 public static final int KEYBOARDHIDDEN_SOFT = 3;
122
123 /**
124 * A flag indicating whether any keyboard is available. Unlike
125 * {@link #hardKeyboardHidden}, this also takes into account a soft
126 * keyboard, so if the hard keyboard is hidden but there is soft
127 * keyboard available, it will be set to NO. Value is one of:
128 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
129 */
130 public int keyboardHidden;
131
132 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
133 public static final int HARDKEYBOARDHIDDEN_NO = 1;
134 public static final int HARDKEYBOARDHIDDEN_YES = 2;
135
136 /**
137 * A flag indicating whether the hard keyboard has been hidden. This will
138 * be set on a device with a mechanism to hide the keyboard from the
139 * user, when that mechanism is closed. One of:
140 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
141 */
142 public int hardKeyboardHidden;
143
144 public static final int NAVIGATION_UNDEFINED = 0;
145 public static final int NAVIGATION_NONAV = 1;
146 public static final int NAVIGATION_DPAD = 2;
147 public static final int NAVIGATION_TRACKBALL = 3;
148 public static final int NAVIGATION_WHEEL = 4;
149
150 /**
151 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500152 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
153 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 */
155 public int navigation;
156
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700157 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
158 public static final int NAVIGATIONHIDDEN_NO = 1;
159 public static final int NAVIGATIONHIDDEN_YES = 2;
160
161 /**
162 * A flag indicating whether any 5-way or DPAD navigation available.
163 * This will be set on a device with a mechanism to hide the navigation
164 * controls from the user, when that mechanism is closed. One of:
165 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
166 */
167 public int navigationHidden;
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int ORIENTATION_UNDEFINED = 0;
170 public static final int ORIENTATION_PORTRAIT = 1;
171 public static final int ORIENTATION_LANDSCAPE = 2;
172 public static final int ORIENTATION_SQUARE = 3;
173
174 /**
175 * Overall orientation of the screen. May be one of
176 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
177 * or {@link #ORIENTATION_SQUARE}.
178 */
179 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100180
Tobias Haamel27b28b32010-02-09 23:09:17 +0100181 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800182 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
183 public static final int UI_MODE_TYPE_NORMAL = 0x01;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800184 public static final int UI_MODE_TYPE_DESK = 0x02;
185 public static final int UI_MODE_TYPE_CAR = 0x03;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100186
Tobias Haamel27b28b32010-02-09 23:09:17 +0100187 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100188 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100189 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100190 public static final int UI_MODE_NIGHT_YES = 0x20;
191
192 /**
193 * Bit mask of the ui mode. Currently there are two fields:
194 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
Dianne Hackborn7299c412010-03-04 18:41:49 -0800195 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
196 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
197 * or {@link #UI_MODE_TYPE_CAR}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100198 *
199 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
Dianne Hackborn7299c412010-03-04 18:41:49 -0800200 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
Tobias Haamel27b28b32010-02-09 23:09:17 +0100201 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100202 */
203 public int uiMode;
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800206 * @hide Internal book-keeping.
207 */
208 public int seq;
209
210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * Construct an invalid Configuration. You must call {@link #setToDefaults}
212 * for this object to be valid. {@more}
213 */
214 public Configuration() {
215 setToDefaults();
216 }
217
218 /**
219 * Makes a deep copy suitable for modification.
220 */
221 public Configuration(Configuration o) {
222 fontScale = o.fontScale;
223 mcc = o.mcc;
224 mnc = o.mnc;
225 if (o.locale != null) {
226 locale = (Locale) o.locale.clone();
227 }
228 userSetLocale = o.userSetLocale;
229 touchscreen = o.touchscreen;
230 keyboard = o.keyboard;
231 keyboardHidden = o.keyboardHidden;
232 hardKeyboardHidden = o.hardKeyboardHidden;
233 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700234 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700236 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100237 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800238 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
240
241 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700242 StringBuilder sb = new StringBuilder(128);
243 sb.append("{ scale=");
244 sb.append(fontScale);
245 sb.append(" imsi=");
246 sb.append(mcc);
247 sb.append("/");
248 sb.append(mnc);
249 sb.append(" loc=");
250 sb.append(locale);
251 sb.append(" touch=");
252 sb.append(touchscreen);
253 sb.append(" keys=");
254 sb.append(keyboard);
255 sb.append("/");
256 sb.append(keyboardHidden);
257 sb.append("/");
258 sb.append(hardKeyboardHidden);
259 sb.append(" nav=");
260 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700261 sb.append("/");
262 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700263 sb.append(" orien=");
264 sb.append(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700265 sb.append(" layout=");
266 sb.append(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100267 sb.append(" uiMode=");
268 sb.append(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800269 if (seq != 0) {
270 sb.append(" seq=");
271 sb.append(seq);
272 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700273 sb.append('}');
274 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
277 /**
278 * Set this object to the system defaults.
279 */
280 public void setToDefaults() {
281 fontScale = 1;
282 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800283 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 userSetLocale = false;
285 touchscreen = TOUCHSCREEN_UNDEFINED;
286 keyboard = KEYBOARD_UNDEFINED;
287 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
288 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
289 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700290 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700292 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800293 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800294 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
297 /** {@hide} */
298 @Deprecated public void makeDefault() {
299 setToDefaults();
300 }
301
302 /**
303 * Copy the fields from delta into this Configuration object, keeping
304 * track of which ones have changed. Any undefined fields in
305 * <var>delta</var> are ignored and not copied in to the current
306 * Configuration.
307 * @return Returns a bit mask of the changed fields, as per
308 * {@link #diff}.
309 */
310 public int updateFrom(Configuration delta) {
311 int changed = 0;
312 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
313 changed |= ActivityInfo.CONFIG_FONT_SCALE;
314 fontScale = delta.fontScale;
315 }
316 if (delta.mcc != 0 && mcc != delta.mcc) {
317 changed |= ActivityInfo.CONFIG_MCC;
318 mcc = delta.mcc;
319 }
320 if (delta.mnc != 0 && mnc != delta.mnc) {
321 changed |= ActivityInfo.CONFIG_MNC;
322 mnc = delta.mnc;
323 }
324 if (delta.locale != null
325 && (locale == null || !locale.equals(delta.locale))) {
326 changed |= ActivityInfo.CONFIG_LOCALE;
327 locale = delta.locale != null
328 ? (Locale) delta.locale.clone() : null;
329 }
330 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
331 {
332 userSetLocale = true;
333 changed |= ActivityInfo.CONFIG_LOCALE;
334 }
335 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
336 && touchscreen != delta.touchscreen) {
337 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
338 touchscreen = delta.touchscreen;
339 }
340 if (delta.keyboard != KEYBOARD_UNDEFINED
341 && keyboard != delta.keyboard) {
342 changed |= ActivityInfo.CONFIG_KEYBOARD;
343 keyboard = delta.keyboard;
344 }
345 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
346 && keyboardHidden != delta.keyboardHidden) {
347 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
348 keyboardHidden = delta.keyboardHidden;
349 }
350 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
351 && hardKeyboardHidden != delta.hardKeyboardHidden) {
352 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
353 hardKeyboardHidden = delta.hardKeyboardHidden;
354 }
355 if (delta.navigation != NAVIGATION_UNDEFINED
356 && navigation != delta.navigation) {
357 changed |= ActivityInfo.CONFIG_NAVIGATION;
358 navigation = delta.navigation;
359 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700360 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
361 && navigationHidden != delta.navigationHidden) {
362 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
363 navigationHidden = delta.navigationHidden;
364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 if (delta.orientation != ORIENTATION_UNDEFINED
366 && orientation != delta.orientation) {
367 changed |= ActivityInfo.CONFIG_ORIENTATION;
368 orientation = delta.orientation;
369 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700370 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700371 && screenLayout != delta.screenLayout) {
372 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
373 screenLayout = delta.screenLayout;
374 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800375 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100376 && uiMode != delta.uiMode) {
377 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800378 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
379 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
380 | (delta.uiMode&UI_MODE_TYPE_MASK);
381 }
382 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
383 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
384 | (delta.uiMode&UI_MODE_NIGHT_MASK);
385 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800388 if (delta.seq != 0) {
389 seq = delta.seq;
390 }
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 return changed;
393 }
394
395 /**
396 * Return a bit mask of the differences between this Configuration
397 * object and the given one. Does not change the values of either. Any
398 * undefined fields in <var>delta</var> are ignored.
399 * @return Returns a bit mask indicating which configuration
400 * values has changed, containing any combination of
401 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
402 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
403 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
404 * PackageManager.ActivityInfo.CONFIG_MCC},
405 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
406 * PackageManager.ActivityInfo.CONFIG_MNC},
407 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
408 * PackageManager.ActivityInfo.CONFIG_LOCALE},
409 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
410 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
411 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
412 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
413 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700414 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700416 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
417 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
418 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
420 public int diff(Configuration delta) {
421 int changed = 0;
422 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
423 changed |= ActivityInfo.CONFIG_FONT_SCALE;
424 }
425 if (delta.mcc != 0 && mcc != delta.mcc) {
426 changed |= ActivityInfo.CONFIG_MCC;
427 }
428 if (delta.mnc != 0 && mnc != delta.mnc) {
429 changed |= ActivityInfo.CONFIG_MNC;
430 }
431 if (delta.locale != null
432 && (locale == null || !locale.equals(delta.locale))) {
433 changed |= ActivityInfo.CONFIG_LOCALE;
434 }
435 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
436 && touchscreen != delta.touchscreen) {
437 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
438 }
439 if (delta.keyboard != KEYBOARD_UNDEFINED
440 && keyboard != delta.keyboard) {
441 changed |= ActivityInfo.CONFIG_KEYBOARD;
442 }
443 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
444 && keyboardHidden != delta.keyboardHidden) {
445 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
446 }
447 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
448 && hardKeyboardHidden != delta.hardKeyboardHidden) {
449 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
450 }
451 if (delta.navigation != NAVIGATION_UNDEFINED
452 && navigation != delta.navigation) {
453 changed |= ActivityInfo.CONFIG_NAVIGATION;
454 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700455 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
456 && navigationHidden != delta.navigationHidden) {
457 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 if (delta.orientation != ORIENTATION_UNDEFINED
460 && orientation != delta.orientation) {
461 changed |= ActivityInfo.CONFIG_ORIENTATION;
462 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700463 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700464 && screenLayout != delta.screenLayout) {
465 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
466 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800467 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100468 && uiMode != delta.uiMode) {
469 changed |= ActivityInfo.CONFIG_UI_MODE;
470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
472 return changed;
473 }
474
475 /**
476 * Determine if a new resource needs to be loaded from the bit set of
477 * configuration changes returned by {@link #updateFrom(Configuration)}.
478 *
479 * @param configChanges The mask of changes configurations as returned by
480 * {@link #updateFrom(Configuration)}.
481 * @param interestingChanges The configuration changes that the resource
482 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
483 *
484 * @return Return true if the resource needs to be loaded, else false.
485 */
486 public static boolean needNewResources(int configChanges, int interestingChanges) {
487 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
488 }
489
490 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800491 * @hide Return true if the sequence of 'other' is better than this. Assumes
492 * that 'this' is your current sequence and 'other' is a new one you have
493 * received some how and want to compare with what you have.
494 */
495 public boolean isOtherSeqNewer(Configuration other) {
496 if (other == null) {
497 // Sanity check.
498 return false;
499 }
500 if (other.seq == 0) {
501 // If the other sequence is not specified, then we must assume
502 // it is newer since we don't know any better.
503 return true;
504 }
505 if (seq == 0) {
506 // If this sequence is not specified, then we also consider the
507 // other is better. Yes we have a preference for other. Sue us.
508 return true;
509 }
510 int diff = other.seq - seq;
511 if (diff > 0x10000) {
512 // If there has been a sufficiently large jump, assume the
513 // sequence has wrapped around.
514 return false;
515 }
516 return diff > 0;
517 }
518
519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * Parcelable methods
521 */
522 public int describeContents() {
523 return 0;
524 }
525
526 public void writeToParcel(Parcel dest, int flags) {
527 dest.writeFloat(fontScale);
528 dest.writeInt(mcc);
529 dest.writeInt(mnc);
530 if (locale == null) {
531 dest.writeInt(0);
532 } else {
533 dest.writeInt(1);
534 dest.writeString(locale.getLanguage());
535 dest.writeString(locale.getCountry());
536 dest.writeString(locale.getVariant());
537 }
538 if(userSetLocale) {
539 dest.writeInt(1);
540 } else {
541 dest.writeInt(0);
542 }
543 dest.writeInt(touchscreen);
544 dest.writeInt(keyboard);
545 dest.writeInt(keyboardHidden);
546 dest.writeInt(hardKeyboardHidden);
547 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700548 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700550 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100551 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800552 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
555 public static final Parcelable.Creator<Configuration> CREATOR
556 = new Parcelable.Creator<Configuration>() {
557 public Configuration createFromParcel(Parcel source) {
558 return new Configuration(source);
559 }
560
561 public Configuration[] newArray(int size) {
562 return new Configuration[size];
563 }
564 };
565
566 /**
567 * Construct this Configuration object, reading from the Parcel.
568 */
569 private Configuration(Parcel source) {
570 fontScale = source.readFloat();
571 mcc = source.readInt();
572 mnc = source.readInt();
573 if (source.readInt() != 0) {
574 locale = new Locale(source.readString(), source.readString(),
575 source.readString());
576 }
577 userSetLocale = (source.readInt()==1);
578 touchscreen = source.readInt();
579 keyboard = source.readInt();
580 keyboardHidden = source.readInt();
581 hardKeyboardHidden = source.readInt();
582 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700583 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700585 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100586 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800587 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
589
590 public int compareTo(Configuration that) {
591 int n;
592 float a = this.fontScale;
593 float b = that.fontScale;
594 if (a < b) return -1;
595 if (a > b) return 1;
596 n = this.mcc - that.mcc;
597 if (n != 0) return n;
598 n = this.mnc - that.mnc;
599 if (n != 0) return n;
600 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
601 if (n != 0) return n;
602 n = this.locale.getCountry().compareTo(that.locale.getCountry());
603 if (n != 0) return n;
604 n = this.locale.getVariant().compareTo(that.locale.getVariant());
605 if (n != 0) return n;
606 n = this.touchscreen - that.touchscreen;
607 if (n != 0) return n;
608 n = this.keyboard - that.keyboard;
609 if (n != 0) return n;
610 n = this.keyboardHidden - that.keyboardHidden;
611 if (n != 0) return n;
612 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
613 if (n != 0) return n;
614 n = this.navigation - that.navigation;
615 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700616 n = this.navigationHidden - that.navigationHidden;
617 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700619 if (n != 0) return n;
620 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100621 if (n != 0) return n;
622 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 //if (n != 0) return n;
624 return n;
625 }
626
627 public boolean equals(Configuration that) {
628 if (that == null) return false;
629 if (that == this) return true;
630 return this.compareTo(that) == 0;
631 }
632
633 public boolean equals(Object that) {
634 try {
635 return equals((Configuration)that);
636 } catch (ClassCastException e) {
637 }
638 return false;
639 }
640
641 public int hashCode() {
642 return ((int)this.fontScale) + this.mcc + this.mnc
643 + this.locale.hashCode() + this.touchscreen
644 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700645 + this.navigation + this.navigationHidden
Tobias Haamel27b28b32010-02-09 23:09:17 +0100646 + this.orientation + this.screenLayout + this.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648}