blob: 6490b65c947e83973315e79efdbe14b65ce7615c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001package android.content.res;
2
3import android.content.pm.ActivityInfo;
4import android.os.Parcel;
5import android.os.Parcelable;
6
7import java.util.Locale;
8
9/**
10 * This class describes all device configuration information that can
11 * impact the resources the application retrieves. This includes both
12 * user-specified configuration options (locale and scaling) as well
13 * as dynamic device configuration (various types of input devices).
14 */
15public final class Configuration implements Parcelable, Comparable<Configuration> {
16 /**
17 * Current user preference for the scaling factor for fonts, relative
18 * to the base density scaling.
19 */
20 public float fontScale;
21
22 /**
23 * IMSI MCC (Mobile Country Code). 0 if undefined.
24 */
25 public int mcc;
26
27 /**
28 * IMSI MNC (Mobile Network Code). 0 if undefined.
29 */
30 public int mnc;
31
32 /**
33 * Current user preference for the locale.
34 */
35 public Locale locale;
36
37 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070038 * Locale should persist on setting. This is hidden because it is really
39 * questionable whether this is the right way to expose the functionality.
40 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 */
42 public boolean userSetLocale;
43
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070044 public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
45 public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
46 public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
47 public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
48 public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
49
50 public static final int SCREENLAYOUT_LONG_MASK = 0x30;
51 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
52 public static final int SCREENLAYOUT_LONG_NO = 0x10;
53 public static final int SCREENLAYOUT_LONG_YES = 0x20;
54
55 /**
56 * Special flag we generate to indicate that the screen layout requires
57 * us to use a compatibility mode for apps that are not modern layout
58 * aware.
59 * @hide
60 */
61 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
62
63 /**
64 * Bit mask of overall layout of the screen. Currently there are two
65 * fields:
66 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
67 * of the screen. They may be one of
68 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
69 * or {@link #SCREENLAYOUT_SIZE_LARGE}.
70 *
71 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
72 * is wider/taller than normal. They may be one of
73 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
74 */
75 public int screenLayout;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static final int TOUCHSCREEN_UNDEFINED = 0;
78 public static final int TOUCHSCREEN_NOTOUCH = 1;
79 public static final int TOUCHSCREEN_STYLUS = 2;
80 public static final int TOUCHSCREEN_FINGER = 3;
81
82 /**
83 * The kind of touch screen attached to the device.
84 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
85 * {@link #TOUCHSCREEN_FINGER}.
86 */
87 public int touchscreen;
88
89 public static final int KEYBOARD_UNDEFINED = 0;
90 public static final int KEYBOARD_NOKEYS = 1;
91 public static final int KEYBOARD_QWERTY = 2;
92 public static final int KEYBOARD_12KEY = 3;
93
94 /**
95 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -050096 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
97 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 */
99 public int keyboard;
100
101 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
102 public static final int KEYBOARDHIDDEN_NO = 1;
103 public static final int KEYBOARDHIDDEN_YES = 2;
104 /** Constant matching actual resource implementation. {@hide} */
105 public static final int KEYBOARDHIDDEN_SOFT = 3;
106
107 /**
108 * A flag indicating whether any keyboard is available. Unlike
109 * {@link #hardKeyboardHidden}, this also takes into account a soft
110 * keyboard, so if the hard keyboard is hidden but there is soft
111 * keyboard available, it will be set to NO. Value is one of:
112 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
113 */
114 public int keyboardHidden;
115
116 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
117 public static final int HARDKEYBOARDHIDDEN_NO = 1;
118 public static final int HARDKEYBOARDHIDDEN_YES = 2;
119
120 /**
121 * A flag indicating whether the hard keyboard has been hidden. This will
122 * be set on a device with a mechanism to hide the keyboard from the
123 * user, when that mechanism is closed. One of:
124 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
125 */
126 public int hardKeyboardHidden;
127
128 public static final int NAVIGATION_UNDEFINED = 0;
129 public static final int NAVIGATION_NONAV = 1;
130 public static final int NAVIGATION_DPAD = 2;
131 public static final int NAVIGATION_TRACKBALL = 3;
132 public static final int NAVIGATION_WHEEL = 4;
133
134 /**
135 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500136 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
137 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 */
139 public int navigation;
140
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700141 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
142 public static final int NAVIGATIONHIDDEN_NO = 1;
143 public static final int NAVIGATIONHIDDEN_YES = 2;
144
145 /**
146 * A flag indicating whether any 5-way or DPAD navigation available.
147 * This will be set on a device with a mechanism to hide the navigation
148 * controls from the user, when that mechanism is closed. One of:
149 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
150 */
151 public int navigationHidden;
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public static final int ORIENTATION_UNDEFINED = 0;
154 public static final int ORIENTATION_PORTRAIT = 1;
155 public static final int ORIENTATION_LANDSCAPE = 2;
156 public static final int ORIENTATION_SQUARE = 3;
157
158 /**
159 * Overall orientation of the screen. May be one of
160 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
161 * or {@link #ORIENTATION_SQUARE}.
162 */
163 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100164
165 /** @hide (UIMODE) Pending API council approval */
166 public static final int UI_MODE_TYPE_MASK = 0x0f;
167 /** @hide (UIMODE) Pending API council approval */
168 public static final int UI_MODE_TYPE_NORMAL = 0x00;
169 /** @hide (UIMODE) Pending API council approval */
170 public static final int UI_MODE_TYPE_CAR = 0x01;
171
172 /** @hide (UIMODE) Pending API council approval */
173 public static final int UI_MODE_NIGHT_MASK = 0x30;
174 /** @hide (UIMODE) Pending API council approval */
175 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
176 /** @hide (UIMODE) Pending API council approval */
177 public static final int UI_MODE_NIGHT_NO = 0x10;
178 /** @hide (UIMODE) Pending API council approval */
179 public static final int UI_MODE_NIGHT_YES = 0x20;
180
181 /**
182 * Bit mask of the ui mode. Currently there are two fields:
183 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
184 * device. They may be one of
185 * {@link #UI_MODE_TYPE_NORMAL} or {@link #UI_MODE_TYPE_CAR}.
186 *
187 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
188 * is in a special mode. They may be one of
189 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
190 *
191 * @hide (UIMODE) Pending API council approval
192 */
193 public int uiMode;
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800196 * @hide Internal book-keeping.
197 */
198 public int seq;
199
200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * Construct an invalid Configuration. You must call {@link #setToDefaults}
202 * for this object to be valid. {@more}
203 */
204 public Configuration() {
205 setToDefaults();
206 }
207
208 /**
209 * Makes a deep copy suitable for modification.
210 */
211 public Configuration(Configuration o) {
212 fontScale = o.fontScale;
213 mcc = o.mcc;
214 mnc = o.mnc;
215 if (o.locale != null) {
216 locale = (Locale) o.locale.clone();
217 }
218 userSetLocale = o.userSetLocale;
219 touchscreen = o.touchscreen;
220 keyboard = o.keyboard;
221 keyboardHidden = o.keyboardHidden;
222 hardKeyboardHidden = o.hardKeyboardHidden;
223 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700224 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700226 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100227 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800228 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
230
231 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700232 StringBuilder sb = new StringBuilder(128);
233 sb.append("{ scale=");
234 sb.append(fontScale);
235 sb.append(" imsi=");
236 sb.append(mcc);
237 sb.append("/");
238 sb.append(mnc);
239 sb.append(" loc=");
240 sb.append(locale);
241 sb.append(" touch=");
242 sb.append(touchscreen);
243 sb.append(" keys=");
244 sb.append(keyboard);
245 sb.append("/");
246 sb.append(keyboardHidden);
247 sb.append("/");
248 sb.append(hardKeyboardHidden);
249 sb.append(" nav=");
250 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700251 sb.append("/");
252 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700253 sb.append(" orien=");
254 sb.append(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700255 sb.append(" layout=");
256 sb.append(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100257 sb.append(" uiMode=");
258 sb.append(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800259 if (seq != 0) {
260 sb.append(" seq=");
261 sb.append(seq);
262 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700263 sb.append('}');
264 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266
267 /**
268 * Set this object to the system defaults.
269 */
270 public void setToDefaults() {
271 fontScale = 1;
272 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800273 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 userSetLocale = false;
275 touchscreen = TOUCHSCREEN_UNDEFINED;
276 keyboard = KEYBOARD_UNDEFINED;
277 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
278 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
279 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700280 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700282 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100283 uiMode = UI_MODE_TYPE_NORMAL;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800284 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 }
286
287 /** {@hide} */
288 @Deprecated public void makeDefault() {
289 setToDefaults();
290 }
291
292 /**
293 * Copy the fields from delta into this Configuration object, keeping
294 * track of which ones have changed. Any undefined fields in
295 * <var>delta</var> are ignored and not copied in to the current
296 * Configuration.
297 * @return Returns a bit mask of the changed fields, as per
298 * {@link #diff}.
299 */
300 public int updateFrom(Configuration delta) {
301 int changed = 0;
302 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
303 changed |= ActivityInfo.CONFIG_FONT_SCALE;
304 fontScale = delta.fontScale;
305 }
306 if (delta.mcc != 0 && mcc != delta.mcc) {
307 changed |= ActivityInfo.CONFIG_MCC;
308 mcc = delta.mcc;
309 }
310 if (delta.mnc != 0 && mnc != delta.mnc) {
311 changed |= ActivityInfo.CONFIG_MNC;
312 mnc = delta.mnc;
313 }
314 if (delta.locale != null
315 && (locale == null || !locale.equals(delta.locale))) {
316 changed |= ActivityInfo.CONFIG_LOCALE;
317 locale = delta.locale != null
318 ? (Locale) delta.locale.clone() : null;
319 }
320 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
321 {
322 userSetLocale = true;
323 changed |= ActivityInfo.CONFIG_LOCALE;
324 }
325 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
326 && touchscreen != delta.touchscreen) {
327 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
328 touchscreen = delta.touchscreen;
329 }
330 if (delta.keyboard != KEYBOARD_UNDEFINED
331 && keyboard != delta.keyboard) {
332 changed |= ActivityInfo.CONFIG_KEYBOARD;
333 keyboard = delta.keyboard;
334 }
335 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
336 && keyboardHidden != delta.keyboardHidden) {
337 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
338 keyboardHidden = delta.keyboardHidden;
339 }
340 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
341 && hardKeyboardHidden != delta.hardKeyboardHidden) {
342 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
343 hardKeyboardHidden = delta.hardKeyboardHidden;
344 }
345 if (delta.navigation != NAVIGATION_UNDEFINED
346 && navigation != delta.navigation) {
347 changed |= ActivityInfo.CONFIG_NAVIGATION;
348 navigation = delta.navigation;
349 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700350 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
351 && navigationHidden != delta.navigationHidden) {
352 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
353 navigationHidden = delta.navigationHidden;
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 if (delta.orientation != ORIENTATION_UNDEFINED
356 && orientation != delta.orientation) {
357 changed |= ActivityInfo.CONFIG_ORIENTATION;
358 orientation = delta.orientation;
359 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700360 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700361 && screenLayout != delta.screenLayout) {
362 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
363 screenLayout = delta.screenLayout;
364 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100365 if (delta.uiMode != UI_MODE_TYPE_NORMAL
366 && uiMode != delta.uiMode) {
367 changed |= ActivityInfo.CONFIG_UI_MODE;
368 uiMode = delta.uiMode;
369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800371 if (delta.seq != 0) {
372 seq = delta.seq;
373 }
374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 return changed;
376 }
377
378 /**
379 * Return a bit mask of the differences between this Configuration
380 * object and the given one. Does not change the values of either. Any
381 * undefined fields in <var>delta</var> are ignored.
382 * @return Returns a bit mask indicating which configuration
383 * values has changed, containing any combination of
384 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
385 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
386 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
387 * PackageManager.ActivityInfo.CONFIG_MCC},
388 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
389 * PackageManager.ActivityInfo.CONFIG_MNC},
390 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
391 * PackageManager.ActivityInfo.CONFIG_LOCALE},
392 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
393 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
394 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
395 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
396 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700397 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700399 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
400 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
401 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
403 public int diff(Configuration delta) {
404 int changed = 0;
405 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
406 changed |= ActivityInfo.CONFIG_FONT_SCALE;
407 }
408 if (delta.mcc != 0 && mcc != delta.mcc) {
409 changed |= ActivityInfo.CONFIG_MCC;
410 }
411 if (delta.mnc != 0 && mnc != delta.mnc) {
412 changed |= ActivityInfo.CONFIG_MNC;
413 }
414 if (delta.locale != null
415 && (locale == null || !locale.equals(delta.locale))) {
416 changed |= ActivityInfo.CONFIG_LOCALE;
417 }
418 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
419 && touchscreen != delta.touchscreen) {
420 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
421 }
422 if (delta.keyboard != KEYBOARD_UNDEFINED
423 && keyboard != delta.keyboard) {
424 changed |= ActivityInfo.CONFIG_KEYBOARD;
425 }
426 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
427 && keyboardHidden != delta.keyboardHidden) {
428 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
429 }
430 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
431 && hardKeyboardHidden != delta.hardKeyboardHidden) {
432 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
433 }
434 if (delta.navigation != NAVIGATION_UNDEFINED
435 && navigation != delta.navigation) {
436 changed |= ActivityInfo.CONFIG_NAVIGATION;
437 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700438 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
439 && navigationHidden != delta.navigationHidden) {
440 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 if (delta.orientation != ORIENTATION_UNDEFINED
443 && orientation != delta.orientation) {
444 changed |= ActivityInfo.CONFIG_ORIENTATION;
445 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700446 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700447 && screenLayout != delta.screenLayout) {
448 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
449 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100450 if (delta.uiMode != UI_MODE_TYPE_NORMAL
451 && uiMode != delta.uiMode) {
452 changed |= ActivityInfo.CONFIG_UI_MODE;
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454
455 return changed;
456 }
457
458 /**
459 * Determine if a new resource needs to be loaded from the bit set of
460 * configuration changes returned by {@link #updateFrom(Configuration)}.
461 *
462 * @param configChanges The mask of changes configurations as returned by
463 * {@link #updateFrom(Configuration)}.
464 * @param interestingChanges The configuration changes that the resource
465 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
466 *
467 * @return Return true if the resource needs to be loaded, else false.
468 */
469 public static boolean needNewResources(int configChanges, int interestingChanges) {
470 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
471 }
472
473 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800474 * @hide Return true if the sequence of 'other' is better than this. Assumes
475 * that 'this' is your current sequence and 'other' is a new one you have
476 * received some how and want to compare with what you have.
477 */
478 public boolean isOtherSeqNewer(Configuration other) {
479 if (other == null) {
480 // Sanity check.
481 return false;
482 }
483 if (other.seq == 0) {
484 // If the other sequence is not specified, then we must assume
485 // it is newer since we don't know any better.
486 return true;
487 }
488 if (seq == 0) {
489 // If this sequence is not specified, then we also consider the
490 // other is better. Yes we have a preference for other. Sue us.
491 return true;
492 }
493 int diff = other.seq - seq;
494 if (diff > 0x10000) {
495 // If there has been a sufficiently large jump, assume the
496 // sequence has wrapped around.
497 return false;
498 }
499 return diff > 0;
500 }
501
502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 * Parcelable methods
504 */
505 public int describeContents() {
506 return 0;
507 }
508
509 public void writeToParcel(Parcel dest, int flags) {
510 dest.writeFloat(fontScale);
511 dest.writeInt(mcc);
512 dest.writeInt(mnc);
513 if (locale == null) {
514 dest.writeInt(0);
515 } else {
516 dest.writeInt(1);
517 dest.writeString(locale.getLanguage());
518 dest.writeString(locale.getCountry());
519 dest.writeString(locale.getVariant());
520 }
521 if(userSetLocale) {
522 dest.writeInt(1);
523 } else {
524 dest.writeInt(0);
525 }
526 dest.writeInt(touchscreen);
527 dest.writeInt(keyboard);
528 dest.writeInt(keyboardHidden);
529 dest.writeInt(hardKeyboardHidden);
530 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700531 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700533 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100534 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800535 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537
538 public static final Parcelable.Creator<Configuration> CREATOR
539 = new Parcelable.Creator<Configuration>() {
540 public Configuration createFromParcel(Parcel source) {
541 return new Configuration(source);
542 }
543
544 public Configuration[] newArray(int size) {
545 return new Configuration[size];
546 }
547 };
548
549 /**
550 * Construct this Configuration object, reading from the Parcel.
551 */
552 private Configuration(Parcel source) {
553 fontScale = source.readFloat();
554 mcc = source.readInt();
555 mnc = source.readInt();
556 if (source.readInt() != 0) {
557 locale = new Locale(source.readString(), source.readString(),
558 source.readString());
559 }
560 userSetLocale = (source.readInt()==1);
561 touchscreen = source.readInt();
562 keyboard = source.readInt();
563 keyboardHidden = source.readInt();
564 hardKeyboardHidden = source.readInt();
565 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700566 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700568 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100569 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800570 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
573 public int compareTo(Configuration that) {
574 int n;
575 float a = this.fontScale;
576 float b = that.fontScale;
577 if (a < b) return -1;
578 if (a > b) return 1;
579 n = this.mcc - that.mcc;
580 if (n != 0) return n;
581 n = this.mnc - that.mnc;
582 if (n != 0) return n;
583 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
584 if (n != 0) return n;
585 n = this.locale.getCountry().compareTo(that.locale.getCountry());
586 if (n != 0) return n;
587 n = this.locale.getVariant().compareTo(that.locale.getVariant());
588 if (n != 0) return n;
589 n = this.touchscreen - that.touchscreen;
590 if (n != 0) return n;
591 n = this.keyboard - that.keyboard;
592 if (n != 0) return n;
593 n = this.keyboardHidden - that.keyboardHidden;
594 if (n != 0) return n;
595 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
596 if (n != 0) return n;
597 n = this.navigation - that.navigation;
598 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700599 n = this.navigationHidden - that.navigationHidden;
600 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700602 if (n != 0) return n;
603 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100604 if (n != 0) return n;
605 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 //if (n != 0) return n;
607 return n;
608 }
609
610 public boolean equals(Configuration that) {
611 if (that == null) return false;
612 if (that == this) return true;
613 return this.compareTo(that) == 0;
614 }
615
616 public boolean equals(Object that) {
617 try {
618 return equals((Configuration)that);
619 } catch (ClassCastException e) {
620 }
621 return false;
622 }
623
624 public int hashCode() {
625 return ((int)this.fontScale) + this.mcc + this.mnc
626 + this.locale.hashCode() + this.touchscreen
627 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700628 + this.navigation + this.navigationHidden
Tobias Haamel27b28b32010-02-09 23:09:17 +0100629 + this.orientation + this.screenLayout + this.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631}