blob: a737283cf4ecf725c80d24296aaf55d69bf4a609 [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
Tobias Haamel27b28b32010-02-09 23:09:17 +0100165 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800166 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
167 public static final int UI_MODE_TYPE_NORMAL = 0x01;
168 public static final int UI_MODE_TYPE_CAR = 0x02;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100169
Tobias Haamel27b28b32010-02-09 23:09:17 +0100170 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100171 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100172 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100173 public static final int UI_MODE_NIGHT_YES = 0x20;
174
175 /**
176 * Bit mask of the ui mode. Currently there are two fields:
177 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
178 * device. They may be one of
179 * {@link #UI_MODE_TYPE_NORMAL} or {@link #UI_MODE_TYPE_CAR}.
180 *
181 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
182 * is in a special mode. They may be one of
183 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100184 */
185 public int uiMode;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800188 * @hide Internal book-keeping.
189 */
190 public int seq;
191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Construct an invalid Configuration. You must call {@link #setToDefaults}
194 * for this object to be valid. {@more}
195 */
196 public Configuration() {
197 setToDefaults();
198 }
199
200 /**
201 * Makes a deep copy suitable for modification.
202 */
203 public Configuration(Configuration o) {
204 fontScale = o.fontScale;
205 mcc = o.mcc;
206 mnc = o.mnc;
207 if (o.locale != null) {
208 locale = (Locale) o.locale.clone();
209 }
210 userSetLocale = o.userSetLocale;
211 touchscreen = o.touchscreen;
212 keyboard = o.keyboard;
213 keyboardHidden = o.keyboardHidden;
214 hardKeyboardHidden = o.hardKeyboardHidden;
215 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700216 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700218 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100219 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800220 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222
223 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700224 StringBuilder sb = new StringBuilder(128);
225 sb.append("{ scale=");
226 sb.append(fontScale);
227 sb.append(" imsi=");
228 sb.append(mcc);
229 sb.append("/");
230 sb.append(mnc);
231 sb.append(" loc=");
232 sb.append(locale);
233 sb.append(" touch=");
234 sb.append(touchscreen);
235 sb.append(" keys=");
236 sb.append(keyboard);
237 sb.append("/");
238 sb.append(keyboardHidden);
239 sb.append("/");
240 sb.append(hardKeyboardHidden);
241 sb.append(" nav=");
242 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700243 sb.append("/");
244 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700245 sb.append(" orien=");
246 sb.append(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700247 sb.append(" layout=");
248 sb.append(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100249 sb.append(" uiMode=");
250 sb.append(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800251 if (seq != 0) {
252 sb.append(" seq=");
253 sb.append(seq);
254 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700255 sb.append('}');
256 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258
259 /**
260 * Set this object to the system defaults.
261 */
262 public void setToDefaults() {
263 fontScale = 1;
264 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800265 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 userSetLocale = false;
267 touchscreen = TOUCHSCREEN_UNDEFINED;
268 keyboard = KEYBOARD_UNDEFINED;
269 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
270 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
271 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700272 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700274 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100275 uiMode = UI_MODE_TYPE_NORMAL;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800276 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
278
279 /** {@hide} */
280 @Deprecated public void makeDefault() {
281 setToDefaults();
282 }
283
284 /**
285 * Copy the fields from delta into this Configuration object, keeping
286 * track of which ones have changed. Any undefined fields in
287 * <var>delta</var> are ignored and not copied in to the current
288 * Configuration.
289 * @return Returns a bit mask of the changed fields, as per
290 * {@link #diff}.
291 */
292 public int updateFrom(Configuration delta) {
293 int changed = 0;
294 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
295 changed |= ActivityInfo.CONFIG_FONT_SCALE;
296 fontScale = delta.fontScale;
297 }
298 if (delta.mcc != 0 && mcc != delta.mcc) {
299 changed |= ActivityInfo.CONFIG_MCC;
300 mcc = delta.mcc;
301 }
302 if (delta.mnc != 0 && mnc != delta.mnc) {
303 changed |= ActivityInfo.CONFIG_MNC;
304 mnc = delta.mnc;
305 }
306 if (delta.locale != null
307 && (locale == null || !locale.equals(delta.locale))) {
308 changed |= ActivityInfo.CONFIG_LOCALE;
309 locale = delta.locale != null
310 ? (Locale) delta.locale.clone() : null;
311 }
312 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
313 {
314 userSetLocale = true;
315 changed |= ActivityInfo.CONFIG_LOCALE;
316 }
317 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
318 && touchscreen != delta.touchscreen) {
319 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
320 touchscreen = delta.touchscreen;
321 }
322 if (delta.keyboard != KEYBOARD_UNDEFINED
323 && keyboard != delta.keyboard) {
324 changed |= ActivityInfo.CONFIG_KEYBOARD;
325 keyboard = delta.keyboard;
326 }
327 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
328 && keyboardHidden != delta.keyboardHidden) {
329 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
330 keyboardHidden = delta.keyboardHidden;
331 }
332 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
333 && hardKeyboardHidden != delta.hardKeyboardHidden) {
334 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
335 hardKeyboardHidden = delta.hardKeyboardHidden;
336 }
337 if (delta.navigation != NAVIGATION_UNDEFINED
338 && navigation != delta.navigation) {
339 changed |= ActivityInfo.CONFIG_NAVIGATION;
340 navigation = delta.navigation;
341 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700342 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
343 && navigationHidden != delta.navigationHidden) {
344 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
345 navigationHidden = delta.navigationHidden;
346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 if (delta.orientation != ORIENTATION_UNDEFINED
348 && orientation != delta.orientation) {
349 changed |= ActivityInfo.CONFIG_ORIENTATION;
350 orientation = delta.orientation;
351 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700352 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700353 && screenLayout != delta.screenLayout) {
354 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
355 screenLayout = delta.screenLayout;
356 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100357 if (delta.uiMode != UI_MODE_TYPE_NORMAL
358 && uiMode != delta.uiMode) {
359 changed |= ActivityInfo.CONFIG_UI_MODE;
360 uiMode = delta.uiMode;
361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800363 if (delta.seq != 0) {
364 seq = delta.seq;
365 }
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 return changed;
368 }
369
370 /**
371 * Return a bit mask of the differences between this Configuration
372 * object and the given one. Does not change the values of either. Any
373 * undefined fields in <var>delta</var> are ignored.
374 * @return Returns a bit mask indicating which configuration
375 * values has changed, containing any combination of
376 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
377 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
378 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
379 * PackageManager.ActivityInfo.CONFIG_MCC},
380 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
381 * PackageManager.ActivityInfo.CONFIG_MNC},
382 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
383 * PackageManager.ActivityInfo.CONFIG_LOCALE},
384 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
385 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
386 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
387 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
388 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700389 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700391 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
392 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
393 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
395 public int diff(Configuration delta) {
396 int changed = 0;
397 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
398 changed |= ActivityInfo.CONFIG_FONT_SCALE;
399 }
400 if (delta.mcc != 0 && mcc != delta.mcc) {
401 changed |= ActivityInfo.CONFIG_MCC;
402 }
403 if (delta.mnc != 0 && mnc != delta.mnc) {
404 changed |= ActivityInfo.CONFIG_MNC;
405 }
406 if (delta.locale != null
407 && (locale == null || !locale.equals(delta.locale))) {
408 changed |= ActivityInfo.CONFIG_LOCALE;
409 }
410 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
411 && touchscreen != delta.touchscreen) {
412 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
413 }
414 if (delta.keyboard != KEYBOARD_UNDEFINED
415 && keyboard != delta.keyboard) {
416 changed |= ActivityInfo.CONFIG_KEYBOARD;
417 }
418 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
419 && keyboardHidden != delta.keyboardHidden) {
420 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
421 }
422 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
423 && hardKeyboardHidden != delta.hardKeyboardHidden) {
424 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
425 }
426 if (delta.navigation != NAVIGATION_UNDEFINED
427 && navigation != delta.navigation) {
428 changed |= ActivityInfo.CONFIG_NAVIGATION;
429 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700430 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
431 && navigationHidden != delta.navigationHidden) {
432 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 if (delta.orientation != ORIENTATION_UNDEFINED
435 && orientation != delta.orientation) {
436 changed |= ActivityInfo.CONFIG_ORIENTATION;
437 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700438 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700439 && screenLayout != delta.screenLayout) {
440 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
441 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100442 if (delta.uiMode != UI_MODE_TYPE_NORMAL
443 && uiMode != delta.uiMode) {
444 changed |= ActivityInfo.CONFIG_UI_MODE;
445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
447 return changed;
448 }
449
450 /**
451 * Determine if a new resource needs to be loaded from the bit set of
452 * configuration changes returned by {@link #updateFrom(Configuration)}.
453 *
454 * @param configChanges The mask of changes configurations as returned by
455 * {@link #updateFrom(Configuration)}.
456 * @param interestingChanges The configuration changes that the resource
457 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
458 *
459 * @return Return true if the resource needs to be loaded, else false.
460 */
461 public static boolean needNewResources(int configChanges, int interestingChanges) {
462 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
463 }
464
465 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800466 * @hide Return true if the sequence of 'other' is better than this. Assumes
467 * that 'this' is your current sequence and 'other' is a new one you have
468 * received some how and want to compare with what you have.
469 */
470 public boolean isOtherSeqNewer(Configuration other) {
471 if (other == null) {
472 // Sanity check.
473 return false;
474 }
475 if (other.seq == 0) {
476 // If the other sequence is not specified, then we must assume
477 // it is newer since we don't know any better.
478 return true;
479 }
480 if (seq == 0) {
481 // If this sequence is not specified, then we also consider the
482 // other is better. Yes we have a preference for other. Sue us.
483 return true;
484 }
485 int diff = other.seq - seq;
486 if (diff > 0x10000) {
487 // If there has been a sufficiently large jump, assume the
488 // sequence has wrapped around.
489 return false;
490 }
491 return diff > 0;
492 }
493
494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 * Parcelable methods
496 */
497 public int describeContents() {
498 return 0;
499 }
500
501 public void writeToParcel(Parcel dest, int flags) {
502 dest.writeFloat(fontScale);
503 dest.writeInt(mcc);
504 dest.writeInt(mnc);
505 if (locale == null) {
506 dest.writeInt(0);
507 } else {
508 dest.writeInt(1);
509 dest.writeString(locale.getLanguage());
510 dest.writeString(locale.getCountry());
511 dest.writeString(locale.getVariant());
512 }
513 if(userSetLocale) {
514 dest.writeInt(1);
515 } else {
516 dest.writeInt(0);
517 }
518 dest.writeInt(touchscreen);
519 dest.writeInt(keyboard);
520 dest.writeInt(keyboardHidden);
521 dest.writeInt(hardKeyboardHidden);
522 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700523 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700525 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100526 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800527 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529
530 public static final Parcelable.Creator<Configuration> CREATOR
531 = new Parcelable.Creator<Configuration>() {
532 public Configuration createFromParcel(Parcel source) {
533 return new Configuration(source);
534 }
535
536 public Configuration[] newArray(int size) {
537 return new Configuration[size];
538 }
539 };
540
541 /**
542 * Construct this Configuration object, reading from the Parcel.
543 */
544 private Configuration(Parcel source) {
545 fontScale = source.readFloat();
546 mcc = source.readInt();
547 mnc = source.readInt();
548 if (source.readInt() != 0) {
549 locale = new Locale(source.readString(), source.readString(),
550 source.readString());
551 }
552 userSetLocale = (source.readInt()==1);
553 touchscreen = source.readInt();
554 keyboard = source.readInt();
555 keyboardHidden = source.readInt();
556 hardKeyboardHidden = source.readInt();
557 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700558 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700560 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100561 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800562 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564
565 public int compareTo(Configuration that) {
566 int n;
567 float a = this.fontScale;
568 float b = that.fontScale;
569 if (a < b) return -1;
570 if (a > b) return 1;
571 n = this.mcc - that.mcc;
572 if (n != 0) return n;
573 n = this.mnc - that.mnc;
574 if (n != 0) return n;
575 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
576 if (n != 0) return n;
577 n = this.locale.getCountry().compareTo(that.locale.getCountry());
578 if (n != 0) return n;
579 n = this.locale.getVariant().compareTo(that.locale.getVariant());
580 if (n != 0) return n;
581 n = this.touchscreen - that.touchscreen;
582 if (n != 0) return n;
583 n = this.keyboard - that.keyboard;
584 if (n != 0) return n;
585 n = this.keyboardHidden - that.keyboardHidden;
586 if (n != 0) return n;
587 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
588 if (n != 0) return n;
589 n = this.navigation - that.navigation;
590 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700591 n = this.navigationHidden - that.navigationHidden;
592 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700594 if (n != 0) return n;
595 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100596 if (n != 0) return n;
597 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 //if (n != 0) return n;
599 return n;
600 }
601
602 public boolean equals(Configuration that) {
603 if (that == null) return false;
604 if (that == this) return true;
605 return this.compareTo(that) == 0;
606 }
607
608 public boolean equals(Object that) {
609 try {
610 return equals((Configuration)that);
611 } catch (ClassCastException e) {
612 }
613 return false;
614 }
615
616 public int hashCode() {
617 return ((int)this.fontScale) + this.mcc + this.mnc
618 + this.locale.hashCode() + this.touchscreen
619 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700620 + this.navigation + this.navigationHidden
Tobias Haamel27b28b32010-02-09 23:09:17 +0100621 + this.orientation + this.screenLayout + this.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623}