blob: 31119d7ea3da15a5d7898e682db2ef066e323e76 [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;
Dianne Hackborn14cee9f2010-04-23 17:51:26 -070065 public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070066
67 public static final int SCREENLAYOUT_LONG_MASK = 0x30;
68 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
69 public static final int SCREENLAYOUT_LONG_NO = 0x10;
70 public static final int SCREENLAYOUT_LONG_YES = 0x20;
71
72 /**
73 * Special flag we generate to indicate that the screen layout requires
74 * us to use a compatibility mode for apps that are not modern layout
75 * aware.
76 * @hide
77 */
78 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
79
80 /**
81 * Bit mask of overall layout of the screen. Currently there are two
82 * fields:
83 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
84 * of the screen. They may be one of
85 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
Dianne Hackborn14cee9f2010-04-23 17:51:26 -070086 * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070087 *
88 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
89 * is wider/taller than normal. They may be one of
90 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
91 */
92 public int screenLayout;
93
Dianne Hackborn711e62a2010-11-29 16:38:22 -080094 /**
95 * Check if the Configuration's current {@link #screenLayout} is at
96 * least the given size.
97 *
98 * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
99 * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
100 * {@link #SCREENLAYOUT_SIZE_XLARGE}.
101 * @return Returns true if the current screen layout size is at least
102 * the given size.
103 */
104 public boolean isLayoutSizeAtLeast(int size) {
105 int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
106 if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800107 return cur >= size;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800108 }
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public static final int TOUCHSCREEN_UNDEFINED = 0;
111 public static final int TOUCHSCREEN_NOTOUCH = 1;
112 public static final int TOUCHSCREEN_STYLUS = 2;
113 public static final int TOUCHSCREEN_FINGER = 3;
114
115 /**
116 * The kind of touch screen attached to the device.
117 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
118 * {@link #TOUCHSCREEN_FINGER}.
119 */
120 public int touchscreen;
121
122 public static final int KEYBOARD_UNDEFINED = 0;
123 public static final int KEYBOARD_NOKEYS = 1;
124 public static final int KEYBOARD_QWERTY = 2;
125 public static final int KEYBOARD_12KEY = 3;
126
127 /**
128 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500129 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
130 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 */
132 public int keyboard;
133
134 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
135 public static final int KEYBOARDHIDDEN_NO = 1;
136 public static final int KEYBOARDHIDDEN_YES = 2;
137 /** Constant matching actual resource implementation. {@hide} */
138 public static final int KEYBOARDHIDDEN_SOFT = 3;
139
140 /**
141 * A flag indicating whether any keyboard is available. Unlike
142 * {@link #hardKeyboardHidden}, this also takes into account a soft
143 * keyboard, so if the hard keyboard is hidden but there is soft
144 * keyboard available, it will be set to NO. Value is one of:
145 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
146 */
147 public int keyboardHidden;
148
149 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
150 public static final int HARDKEYBOARDHIDDEN_NO = 1;
151 public static final int HARDKEYBOARDHIDDEN_YES = 2;
152
153 /**
154 * A flag indicating whether the hard keyboard has been hidden. This will
155 * be set on a device with a mechanism to hide the keyboard from the
156 * user, when that mechanism is closed. One of:
157 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
158 */
159 public int hardKeyboardHidden;
160
161 public static final int NAVIGATION_UNDEFINED = 0;
162 public static final int NAVIGATION_NONAV = 1;
163 public static final int NAVIGATION_DPAD = 2;
164 public static final int NAVIGATION_TRACKBALL = 3;
165 public static final int NAVIGATION_WHEEL = 4;
166
167 /**
168 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500169 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
170 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
172 public int navigation;
173
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700174 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
175 public static final int NAVIGATIONHIDDEN_NO = 1;
176 public static final int NAVIGATIONHIDDEN_YES = 2;
177
178 /**
179 * A flag indicating whether any 5-way or DPAD navigation available.
180 * This will be set on a device with a mechanism to hide the navigation
181 * controls from the user, when that mechanism is closed. One of:
182 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
183 */
184 public int navigationHidden;
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int ORIENTATION_UNDEFINED = 0;
187 public static final int ORIENTATION_PORTRAIT = 1;
188 public static final int ORIENTATION_LANDSCAPE = 2;
189 public static final int ORIENTATION_SQUARE = 3;
190
191 /**
192 * Overall orientation of the screen. May be one of
193 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
194 * or {@link #ORIENTATION_SQUARE}.
195 */
196 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100197
Tobias Haamel27b28b32010-02-09 23:09:17 +0100198 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800199 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
200 public static final int UI_MODE_TYPE_NORMAL = 0x01;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800201 public static final int UI_MODE_TYPE_DESK = 0x02;
202 public static final int UI_MODE_TYPE_CAR = 0x03;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100203
Tobias Haamel27b28b32010-02-09 23:09:17 +0100204 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100205 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100206 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100207 public static final int UI_MODE_NIGHT_YES = 0x20;
208
209 /**
210 * Bit mask of the ui mode. Currently there are two fields:
211 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
Dianne Hackborn7299c412010-03-04 18:41:49 -0800212 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
213 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
214 * or {@link #UI_MODE_TYPE_CAR}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100215 *
216 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
Dianne Hackborn7299c412010-03-04 18:41:49 -0800217 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
Tobias Haamel27b28b32010-02-09 23:09:17 +0100218 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100219 */
220 public int uiMode;
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800223 * @hide Internal book-keeping.
224 */
225 public int seq;
226
227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * Construct an invalid Configuration. You must call {@link #setToDefaults}
229 * for this object to be valid. {@more}
230 */
231 public Configuration() {
232 setToDefaults();
233 }
234
235 /**
236 * Makes a deep copy suitable for modification.
237 */
238 public Configuration(Configuration o) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700239 setTo(o);
240 }
241
242 public void setTo(Configuration o) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 fontScale = o.fontScale;
244 mcc = o.mcc;
245 mnc = o.mnc;
246 if (o.locale != null) {
247 locale = (Locale) o.locale.clone();
248 }
249 userSetLocale = o.userSetLocale;
250 touchscreen = o.touchscreen;
251 keyboard = o.keyboard;
252 keyboardHidden = o.keyboardHidden;
253 hardKeyboardHidden = o.hardKeyboardHidden;
254 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700255 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700257 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100258 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800259 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700263 StringBuilder sb = new StringBuilder(128);
264 sb.append("{ scale=");
265 sb.append(fontScale);
266 sb.append(" imsi=");
267 sb.append(mcc);
268 sb.append("/");
269 sb.append(mnc);
270 sb.append(" loc=");
271 sb.append(locale);
272 sb.append(" touch=");
273 sb.append(touchscreen);
274 sb.append(" keys=");
275 sb.append(keyboard);
276 sb.append("/");
277 sb.append(keyboardHidden);
278 sb.append("/");
279 sb.append(hardKeyboardHidden);
280 sb.append(" nav=");
281 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700282 sb.append("/");
283 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700284 sb.append(" orien=");
Daniel Sandler7d6bddc2010-07-22 16:11:55 -0400285 switch(orientation) {
286 case ORIENTATION_LANDSCAPE:
287 sb.append("L"); break;
288 case ORIENTATION_PORTRAIT:
289 sb.append("P"); break;
290 default:
291 sb.append(orientation);
292 }
293 sb.append(" layout=0x");
294 sb.append(java.lang.Integer.toHexString(screenLayout));
295 sb.append(" uiMode=0x");
296 sb.append(java.lang.Integer.toHexString(uiMode));
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800297 if (seq != 0) {
298 sb.append(" seq=");
299 sb.append(seq);
300 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700301 sb.append('}');
302 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304
305 /**
306 * Set this object to the system defaults.
307 */
308 public void setToDefaults() {
309 fontScale = 1;
310 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800311 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 userSetLocale = false;
313 touchscreen = TOUCHSCREEN_UNDEFINED;
314 keyboard = KEYBOARD_UNDEFINED;
315 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
316 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
317 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700318 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700320 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800321 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800322 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
324
325 /** {@hide} */
326 @Deprecated public void makeDefault() {
327 setToDefaults();
328 }
329
330 /**
331 * Copy the fields from delta into this Configuration object, keeping
332 * track of which ones have changed. Any undefined fields in
333 * <var>delta</var> are ignored and not copied in to the current
334 * Configuration.
335 * @return Returns a bit mask of the changed fields, as per
336 * {@link #diff}.
337 */
338 public int updateFrom(Configuration delta) {
339 int changed = 0;
340 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
341 changed |= ActivityInfo.CONFIG_FONT_SCALE;
342 fontScale = delta.fontScale;
343 }
344 if (delta.mcc != 0 && mcc != delta.mcc) {
345 changed |= ActivityInfo.CONFIG_MCC;
346 mcc = delta.mcc;
347 }
348 if (delta.mnc != 0 && mnc != delta.mnc) {
349 changed |= ActivityInfo.CONFIG_MNC;
350 mnc = delta.mnc;
351 }
352 if (delta.locale != null
353 && (locale == null || !locale.equals(delta.locale))) {
354 changed |= ActivityInfo.CONFIG_LOCALE;
355 locale = delta.locale != null
356 ? (Locale) delta.locale.clone() : null;
357 }
358 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
359 {
360 userSetLocale = true;
361 changed |= ActivityInfo.CONFIG_LOCALE;
362 }
363 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
364 && touchscreen != delta.touchscreen) {
365 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
366 touchscreen = delta.touchscreen;
367 }
368 if (delta.keyboard != KEYBOARD_UNDEFINED
369 && keyboard != delta.keyboard) {
370 changed |= ActivityInfo.CONFIG_KEYBOARD;
371 keyboard = delta.keyboard;
372 }
373 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
374 && keyboardHidden != delta.keyboardHidden) {
375 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
376 keyboardHidden = delta.keyboardHidden;
377 }
378 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
379 && hardKeyboardHidden != delta.hardKeyboardHidden) {
380 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
381 hardKeyboardHidden = delta.hardKeyboardHidden;
382 }
383 if (delta.navigation != NAVIGATION_UNDEFINED
384 && navigation != delta.navigation) {
385 changed |= ActivityInfo.CONFIG_NAVIGATION;
386 navigation = delta.navigation;
387 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700388 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
389 && navigationHidden != delta.navigationHidden) {
390 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
391 navigationHidden = delta.navigationHidden;
392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 if (delta.orientation != ORIENTATION_UNDEFINED
394 && orientation != delta.orientation) {
395 changed |= ActivityInfo.CONFIG_ORIENTATION;
396 orientation = delta.orientation;
397 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700398 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700399 && screenLayout != delta.screenLayout) {
400 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
401 screenLayout = delta.screenLayout;
402 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800403 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100404 && uiMode != delta.uiMode) {
405 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800406 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
407 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
408 | (delta.uiMode&UI_MODE_TYPE_MASK);
409 }
410 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
411 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
412 | (delta.uiMode&UI_MODE_NIGHT_MASK);
413 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800416 if (delta.seq != 0) {
417 seq = delta.seq;
418 }
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 return changed;
421 }
422
423 /**
424 * Return a bit mask of the differences between this Configuration
425 * object and the given one. Does not change the values of either. Any
426 * undefined fields in <var>delta</var> are ignored.
427 * @return Returns a bit mask indicating which configuration
428 * values has changed, containing any combination of
429 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
430 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
431 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
432 * PackageManager.ActivityInfo.CONFIG_MCC},
433 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
434 * PackageManager.ActivityInfo.CONFIG_MNC},
435 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
436 * PackageManager.ActivityInfo.CONFIG_LOCALE},
437 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
438 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
439 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
440 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
441 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700442 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700444 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
445 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
446 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
448 public int diff(Configuration delta) {
449 int changed = 0;
450 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
451 changed |= ActivityInfo.CONFIG_FONT_SCALE;
452 }
453 if (delta.mcc != 0 && mcc != delta.mcc) {
454 changed |= ActivityInfo.CONFIG_MCC;
455 }
456 if (delta.mnc != 0 && mnc != delta.mnc) {
457 changed |= ActivityInfo.CONFIG_MNC;
458 }
459 if (delta.locale != null
460 && (locale == null || !locale.equals(delta.locale))) {
461 changed |= ActivityInfo.CONFIG_LOCALE;
462 }
463 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
464 && touchscreen != delta.touchscreen) {
465 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
466 }
467 if (delta.keyboard != KEYBOARD_UNDEFINED
468 && keyboard != delta.keyboard) {
469 changed |= ActivityInfo.CONFIG_KEYBOARD;
470 }
471 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
472 && keyboardHidden != delta.keyboardHidden) {
473 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
474 }
475 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
476 && hardKeyboardHidden != delta.hardKeyboardHidden) {
477 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
478 }
479 if (delta.navigation != NAVIGATION_UNDEFINED
480 && navigation != delta.navigation) {
481 changed |= ActivityInfo.CONFIG_NAVIGATION;
482 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700483 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
484 && navigationHidden != delta.navigationHidden) {
485 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 if (delta.orientation != ORIENTATION_UNDEFINED
488 && orientation != delta.orientation) {
489 changed |= ActivityInfo.CONFIG_ORIENTATION;
490 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700491 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700492 && screenLayout != delta.screenLayout) {
493 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
494 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800495 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100496 && uiMode != delta.uiMode) {
497 changed |= ActivityInfo.CONFIG_UI_MODE;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
500 return changed;
501 }
502
503 /**
504 * Determine if a new resource needs to be loaded from the bit set of
505 * configuration changes returned by {@link #updateFrom(Configuration)}.
506 *
507 * @param configChanges The mask of changes configurations as returned by
508 * {@link #updateFrom(Configuration)}.
509 * @param interestingChanges The configuration changes that the resource
510 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
511 *
512 * @return Return true if the resource needs to be loaded, else false.
513 */
514 public static boolean needNewResources(int configChanges, int interestingChanges) {
515 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
516 }
517
518 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800519 * @hide Return true if the sequence of 'other' is better than this. Assumes
520 * that 'this' is your current sequence and 'other' is a new one you have
521 * received some how and want to compare with what you have.
522 */
523 public boolean isOtherSeqNewer(Configuration other) {
524 if (other == null) {
525 // Sanity check.
526 return false;
527 }
528 if (other.seq == 0) {
529 // If the other sequence is not specified, then we must assume
530 // it is newer since we don't know any better.
531 return true;
532 }
533 if (seq == 0) {
534 // If this sequence is not specified, then we also consider the
535 // other is better. Yes we have a preference for other. Sue us.
536 return true;
537 }
538 int diff = other.seq - seq;
539 if (diff > 0x10000) {
540 // If there has been a sufficiently large jump, assume the
541 // sequence has wrapped around.
542 return false;
543 }
544 return diff > 0;
545 }
546
547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 * Parcelable methods
549 */
550 public int describeContents() {
551 return 0;
552 }
553
554 public void writeToParcel(Parcel dest, int flags) {
555 dest.writeFloat(fontScale);
556 dest.writeInt(mcc);
557 dest.writeInt(mnc);
558 if (locale == null) {
559 dest.writeInt(0);
560 } else {
561 dest.writeInt(1);
562 dest.writeString(locale.getLanguage());
563 dest.writeString(locale.getCountry());
564 dest.writeString(locale.getVariant());
565 }
566 if(userSetLocale) {
567 dest.writeInt(1);
568 } else {
569 dest.writeInt(0);
570 }
571 dest.writeInt(touchscreen);
572 dest.writeInt(keyboard);
573 dest.writeInt(keyboardHidden);
574 dest.writeInt(hardKeyboardHidden);
575 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700576 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700578 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100579 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800580 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700583 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 fontScale = source.readFloat();
585 mcc = source.readInt();
586 mnc = source.readInt();
587 if (source.readInt() != 0) {
588 locale = new Locale(source.readString(), source.readString(),
589 source.readString());
590 }
591 userSetLocale = (source.readInt()==1);
592 touchscreen = source.readInt();
593 keyboard = source.readInt();
594 keyboardHidden = source.readInt();
595 hardKeyboardHidden = source.readInt();
596 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700597 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700599 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100600 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800601 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700603
604 public static final Parcelable.Creator<Configuration> CREATOR
605 = new Parcelable.Creator<Configuration>() {
606 public Configuration createFromParcel(Parcel source) {
607 return new Configuration(source);
608 }
609
610 public Configuration[] newArray(int size) {
611 return new Configuration[size];
612 }
613 };
614
615 /**
616 * Construct this Configuration object, reading from the Parcel.
617 */
618 private Configuration(Parcel source) {
619 readFromParcel(source);
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621
622 public int compareTo(Configuration that) {
623 int n;
624 float a = this.fontScale;
625 float b = that.fontScale;
626 if (a < b) return -1;
627 if (a > b) return 1;
628 n = this.mcc - that.mcc;
629 if (n != 0) return n;
630 n = this.mnc - that.mnc;
631 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800632 if (this.locale == null) {
633 if (that.locale != null) return 1;
634 } else if (that.locale == null) {
635 return -1;
636 } else {
637 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
638 if (n != 0) return n;
639 n = this.locale.getCountry().compareTo(that.locale.getCountry());
640 if (n != 0) return n;
641 n = this.locale.getVariant().compareTo(that.locale.getVariant());
642 if (n != 0) return n;
643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 n = this.touchscreen - that.touchscreen;
645 if (n != 0) return n;
646 n = this.keyboard - that.keyboard;
647 if (n != 0) return n;
648 n = this.keyboardHidden - that.keyboardHidden;
649 if (n != 0) return n;
650 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
651 if (n != 0) return n;
652 n = this.navigation - that.navigation;
653 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700654 n = this.navigationHidden - that.navigationHidden;
655 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700657 if (n != 0) return n;
658 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100659 if (n != 0) return n;
660 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 //if (n != 0) return n;
662 return n;
663 }
664
665 public boolean equals(Configuration that) {
666 if (that == null) return false;
667 if (that == this) return true;
668 return this.compareTo(that) == 0;
669 }
670
671 public boolean equals(Object that) {
672 try {
673 return equals((Configuration)that);
674 } catch (ClassCastException e) {
675 }
676 return false;
677 }
678
679 public int hashCode() {
680 return ((int)this.fontScale) + this.mcc + this.mnc
Dianne Hackborna8397032010-03-12 10:52:22 -0800681 + (this.locale != null ? this.locale.hashCode() : 0)
682 + this.touchscreen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700684 + this.navigation + this.navigationHidden
Tobias Haamel27b28b32010-02-09 23:09:17 +0100685 + this.orientation + this.screenLayout + this.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687}