blob: 02956ba15dd651ee2f7abf4ad7ca65bac794adff [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public static final int TOUCHSCREEN_UNDEFINED = 0;
95 public static final int TOUCHSCREEN_NOTOUCH = 1;
96 public static final int TOUCHSCREEN_STYLUS = 2;
97 public static final int TOUCHSCREEN_FINGER = 3;
98
99 /**
100 * The kind of touch screen attached to the device.
101 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
102 * {@link #TOUCHSCREEN_FINGER}.
103 */
104 public int touchscreen;
105
106 public static final int KEYBOARD_UNDEFINED = 0;
107 public static final int KEYBOARD_NOKEYS = 1;
108 public static final int KEYBOARD_QWERTY = 2;
109 public static final int KEYBOARD_12KEY = 3;
110
111 /**
112 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500113 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
114 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
116 public int keyboard;
117
118 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
119 public static final int KEYBOARDHIDDEN_NO = 1;
120 public static final int KEYBOARDHIDDEN_YES = 2;
121 /** Constant matching actual resource implementation. {@hide} */
122 public static final int KEYBOARDHIDDEN_SOFT = 3;
123
124 /**
125 * A flag indicating whether any keyboard is available. Unlike
126 * {@link #hardKeyboardHidden}, this also takes into account a soft
127 * keyboard, so if the hard keyboard is hidden but there is soft
128 * keyboard available, it will be set to NO. Value is one of:
129 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
130 */
131 public int keyboardHidden;
132
133 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
134 public static final int HARDKEYBOARDHIDDEN_NO = 1;
135 public static final int HARDKEYBOARDHIDDEN_YES = 2;
136
137 /**
138 * A flag indicating whether the hard keyboard has been hidden. This will
139 * be set on a device with a mechanism to hide the keyboard from the
140 * user, when that mechanism is closed. One of:
141 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
142 */
143 public int hardKeyboardHidden;
144
145 public static final int NAVIGATION_UNDEFINED = 0;
146 public static final int NAVIGATION_NONAV = 1;
147 public static final int NAVIGATION_DPAD = 2;
148 public static final int NAVIGATION_TRACKBALL = 3;
149 public static final int NAVIGATION_WHEEL = 4;
150
151 /**
152 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500153 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
154 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 */
156 public int navigation;
157
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700158 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
159 public static final int NAVIGATIONHIDDEN_NO = 1;
160 public static final int NAVIGATIONHIDDEN_YES = 2;
161
162 /**
163 * A flag indicating whether any 5-way or DPAD navigation available.
164 * This will be set on a device with a mechanism to hide the navigation
165 * controls from the user, when that mechanism is closed. One of:
166 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
167 */
168 public int navigationHidden;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int ORIENTATION_UNDEFINED = 0;
171 public static final int ORIENTATION_PORTRAIT = 1;
172 public static final int ORIENTATION_LANDSCAPE = 2;
173 public static final int ORIENTATION_SQUARE = 3;
174
175 /**
176 * Overall orientation of the screen. May be one of
177 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
178 * or {@link #ORIENTATION_SQUARE}.
179 */
180 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100181
Tobias Haamel27b28b32010-02-09 23:09:17 +0100182 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800183 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
184 public static final int UI_MODE_TYPE_NORMAL = 0x01;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800185 public static final int UI_MODE_TYPE_DESK = 0x02;
186 public static final int UI_MODE_TYPE_CAR = 0x03;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100187
Tobias Haamel27b28b32010-02-09 23:09:17 +0100188 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100189 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100190 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100191 public static final int UI_MODE_NIGHT_YES = 0x20;
192
193 /**
194 * Bit mask of the ui mode. Currently there are two fields:
195 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
Dianne Hackborn7299c412010-03-04 18:41:49 -0800196 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
197 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
198 * or {@link #UI_MODE_TYPE_CAR}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100199 *
200 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
Dianne Hackborn7299c412010-03-04 18:41:49 -0800201 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
Tobias Haamel27b28b32010-02-09 23:09:17 +0100202 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100203 */
204 public int uiMode;
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800207 * @hide Internal book-keeping.
208 */
209 public int seq;
210
211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * Construct an invalid Configuration. You must call {@link #setToDefaults}
213 * for this object to be valid. {@more}
214 */
215 public Configuration() {
216 setToDefaults();
217 }
218
219 /**
220 * Makes a deep copy suitable for modification.
221 */
222 public Configuration(Configuration o) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700223 setTo(o);
224 }
225
226 public void setTo(Configuration o) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 fontScale = o.fontScale;
228 mcc = o.mcc;
229 mnc = o.mnc;
230 if (o.locale != null) {
231 locale = (Locale) o.locale.clone();
232 }
233 userSetLocale = o.userSetLocale;
234 touchscreen = o.touchscreen;
235 keyboard = o.keyboard;
236 keyboardHidden = o.keyboardHidden;
237 hardKeyboardHidden = o.hardKeyboardHidden;
238 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700239 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700241 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100242 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800243 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700247 StringBuilder sb = new StringBuilder(128);
248 sb.append("{ scale=");
249 sb.append(fontScale);
250 sb.append(" imsi=");
251 sb.append(mcc);
252 sb.append("/");
253 sb.append(mnc);
254 sb.append(" loc=");
255 sb.append(locale);
256 sb.append(" touch=");
257 sb.append(touchscreen);
258 sb.append(" keys=");
259 sb.append(keyboard);
260 sb.append("/");
261 sb.append(keyboardHidden);
262 sb.append("/");
263 sb.append(hardKeyboardHidden);
264 sb.append(" nav=");
265 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700266 sb.append("/");
267 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700268 sb.append(" orien=");
269 sb.append(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700270 sb.append(" layout=");
271 sb.append(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100272 sb.append(" uiMode=");
273 sb.append(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800274 if (seq != 0) {
275 sb.append(" seq=");
276 sb.append(seq);
277 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700278 sb.append('}');
279 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281
282 /**
283 * Set this object to the system defaults.
284 */
285 public void setToDefaults() {
286 fontScale = 1;
287 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800288 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 userSetLocale = false;
290 touchscreen = TOUCHSCREEN_UNDEFINED;
291 keyboard = KEYBOARD_UNDEFINED;
292 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
293 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
294 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700295 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700297 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800298 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800299 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 }
301
302 /** {@hide} */
303 @Deprecated public void makeDefault() {
304 setToDefaults();
305 }
306
307 /**
308 * Copy the fields from delta into this Configuration object, keeping
309 * track of which ones have changed. Any undefined fields in
310 * <var>delta</var> are ignored and not copied in to the current
311 * Configuration.
312 * @return Returns a bit mask of the changed fields, as per
313 * {@link #diff}.
314 */
315 public int updateFrom(Configuration delta) {
316 int changed = 0;
317 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
318 changed |= ActivityInfo.CONFIG_FONT_SCALE;
319 fontScale = delta.fontScale;
320 }
321 if (delta.mcc != 0 && mcc != delta.mcc) {
322 changed |= ActivityInfo.CONFIG_MCC;
323 mcc = delta.mcc;
324 }
325 if (delta.mnc != 0 && mnc != delta.mnc) {
326 changed |= ActivityInfo.CONFIG_MNC;
327 mnc = delta.mnc;
328 }
329 if (delta.locale != null
330 && (locale == null || !locale.equals(delta.locale))) {
331 changed |= ActivityInfo.CONFIG_LOCALE;
332 locale = delta.locale != null
333 ? (Locale) delta.locale.clone() : null;
334 }
335 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
336 {
337 userSetLocale = true;
338 changed |= ActivityInfo.CONFIG_LOCALE;
339 }
340 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
341 && touchscreen != delta.touchscreen) {
342 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
343 touchscreen = delta.touchscreen;
344 }
345 if (delta.keyboard != KEYBOARD_UNDEFINED
346 && keyboard != delta.keyboard) {
347 changed |= ActivityInfo.CONFIG_KEYBOARD;
348 keyboard = delta.keyboard;
349 }
350 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
351 && keyboardHidden != delta.keyboardHidden) {
352 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
353 keyboardHidden = delta.keyboardHidden;
354 }
355 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
356 && hardKeyboardHidden != delta.hardKeyboardHidden) {
357 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
358 hardKeyboardHidden = delta.hardKeyboardHidden;
359 }
360 if (delta.navigation != NAVIGATION_UNDEFINED
361 && navigation != delta.navigation) {
362 changed |= ActivityInfo.CONFIG_NAVIGATION;
363 navigation = delta.navigation;
364 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700365 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
366 && navigationHidden != delta.navigationHidden) {
367 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
368 navigationHidden = delta.navigationHidden;
369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 if (delta.orientation != ORIENTATION_UNDEFINED
371 && orientation != delta.orientation) {
372 changed |= ActivityInfo.CONFIG_ORIENTATION;
373 orientation = delta.orientation;
374 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700375 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700376 && screenLayout != delta.screenLayout) {
377 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
378 screenLayout = delta.screenLayout;
379 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800380 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100381 && uiMode != delta.uiMode) {
382 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800383 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
384 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
385 | (delta.uiMode&UI_MODE_TYPE_MASK);
386 }
387 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
388 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
389 | (delta.uiMode&UI_MODE_NIGHT_MASK);
390 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800393 if (delta.seq != 0) {
394 seq = delta.seq;
395 }
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return changed;
398 }
399
400 /**
401 * Return a bit mask of the differences between this Configuration
402 * object and the given one. Does not change the values of either. Any
403 * undefined fields in <var>delta</var> are ignored.
404 * @return Returns a bit mask indicating which configuration
405 * values has changed, containing any combination of
406 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
407 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
408 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
409 * PackageManager.ActivityInfo.CONFIG_MCC},
410 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
411 * PackageManager.ActivityInfo.CONFIG_MNC},
412 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
413 * PackageManager.ActivityInfo.CONFIG_LOCALE},
414 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
415 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
416 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
417 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
418 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700419 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700421 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
422 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
423 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 */
425 public int diff(Configuration delta) {
426 int changed = 0;
427 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
428 changed |= ActivityInfo.CONFIG_FONT_SCALE;
429 }
430 if (delta.mcc != 0 && mcc != delta.mcc) {
431 changed |= ActivityInfo.CONFIG_MCC;
432 }
433 if (delta.mnc != 0 && mnc != delta.mnc) {
434 changed |= ActivityInfo.CONFIG_MNC;
435 }
436 if (delta.locale != null
437 && (locale == null || !locale.equals(delta.locale))) {
438 changed |= ActivityInfo.CONFIG_LOCALE;
439 }
440 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
441 && touchscreen != delta.touchscreen) {
442 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
443 }
444 if (delta.keyboard != KEYBOARD_UNDEFINED
445 && keyboard != delta.keyboard) {
446 changed |= ActivityInfo.CONFIG_KEYBOARD;
447 }
448 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
449 && keyboardHidden != delta.keyboardHidden) {
450 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
451 }
452 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
453 && hardKeyboardHidden != delta.hardKeyboardHidden) {
454 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
455 }
456 if (delta.navigation != NAVIGATION_UNDEFINED
457 && navigation != delta.navigation) {
458 changed |= ActivityInfo.CONFIG_NAVIGATION;
459 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700460 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
461 && navigationHidden != delta.navigationHidden) {
462 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 if (delta.orientation != ORIENTATION_UNDEFINED
465 && orientation != delta.orientation) {
466 changed |= ActivityInfo.CONFIG_ORIENTATION;
467 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700468 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700469 && screenLayout != delta.screenLayout) {
470 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
471 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800472 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100473 && uiMode != delta.uiMode) {
474 changed |= ActivityInfo.CONFIG_UI_MODE;
475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
477 return changed;
478 }
479
480 /**
481 * Determine if a new resource needs to be loaded from the bit set of
482 * configuration changes returned by {@link #updateFrom(Configuration)}.
483 *
484 * @param configChanges The mask of changes configurations as returned by
485 * {@link #updateFrom(Configuration)}.
486 * @param interestingChanges The configuration changes that the resource
487 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
488 *
489 * @return Return true if the resource needs to be loaded, else false.
490 */
491 public static boolean needNewResources(int configChanges, int interestingChanges) {
492 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
493 }
494
495 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800496 * @hide Return true if the sequence of 'other' is better than this. Assumes
497 * that 'this' is your current sequence and 'other' is a new one you have
498 * received some how and want to compare with what you have.
499 */
500 public boolean isOtherSeqNewer(Configuration other) {
501 if (other == null) {
502 // Sanity check.
503 return false;
504 }
505 if (other.seq == 0) {
506 // If the other sequence is not specified, then we must assume
507 // it is newer since we don't know any better.
508 return true;
509 }
510 if (seq == 0) {
511 // If this sequence is not specified, then we also consider the
512 // other is better. Yes we have a preference for other. Sue us.
513 return true;
514 }
515 int diff = other.seq - seq;
516 if (diff > 0x10000) {
517 // If there has been a sufficiently large jump, assume the
518 // sequence has wrapped around.
519 return false;
520 }
521 return diff > 0;
522 }
523
524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * Parcelable methods
526 */
527 public int describeContents() {
528 return 0;
529 }
530
531 public void writeToParcel(Parcel dest, int flags) {
532 dest.writeFloat(fontScale);
533 dest.writeInt(mcc);
534 dest.writeInt(mnc);
535 if (locale == null) {
536 dest.writeInt(0);
537 } else {
538 dest.writeInt(1);
539 dest.writeString(locale.getLanguage());
540 dest.writeString(locale.getCountry());
541 dest.writeString(locale.getVariant());
542 }
543 if(userSetLocale) {
544 dest.writeInt(1);
545 } else {
546 dest.writeInt(0);
547 }
548 dest.writeInt(touchscreen);
549 dest.writeInt(keyboard);
550 dest.writeInt(keyboardHidden);
551 dest.writeInt(hardKeyboardHidden);
552 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700553 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700555 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100556 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800557 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700560 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 fontScale = source.readFloat();
562 mcc = source.readInt();
563 mnc = source.readInt();
564 if (source.readInt() != 0) {
565 locale = new Locale(source.readString(), source.readString(),
566 source.readString());
567 }
568 userSetLocale = (source.readInt()==1);
569 touchscreen = source.readInt();
570 keyboard = source.readInt();
571 keyboardHidden = source.readInt();
572 hardKeyboardHidden = source.readInt();
573 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700574 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700576 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100577 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800578 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700580
581 public static final Parcelable.Creator<Configuration> CREATOR
582 = new Parcelable.Creator<Configuration>() {
583 public Configuration createFromParcel(Parcel source) {
584 return new Configuration(source);
585 }
586
587 public Configuration[] newArray(int size) {
588 return new Configuration[size];
589 }
590 };
591
592 /**
593 * Construct this Configuration object, reading from the Parcel.
594 */
595 private Configuration(Parcel source) {
596 readFromParcel(source);
597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598
599 public int compareTo(Configuration that) {
600 int n;
601 float a = this.fontScale;
602 float b = that.fontScale;
603 if (a < b) return -1;
604 if (a > b) return 1;
605 n = this.mcc - that.mcc;
606 if (n != 0) return n;
607 n = this.mnc - that.mnc;
608 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800609 if (this.locale == null) {
610 if (that.locale != null) return 1;
611 } else if (that.locale == null) {
612 return -1;
613 } else {
614 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
615 if (n != 0) return n;
616 n = this.locale.getCountry().compareTo(that.locale.getCountry());
617 if (n != 0) return n;
618 n = this.locale.getVariant().compareTo(that.locale.getVariant());
619 if (n != 0) return n;
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 n = this.touchscreen - that.touchscreen;
622 if (n != 0) return n;
623 n = this.keyboard - that.keyboard;
624 if (n != 0) return n;
625 n = this.keyboardHidden - that.keyboardHidden;
626 if (n != 0) return n;
627 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
628 if (n != 0) return n;
629 n = this.navigation - that.navigation;
630 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700631 n = this.navigationHidden - that.navigationHidden;
632 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700634 if (n != 0) return n;
635 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100636 if (n != 0) return n;
637 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 //if (n != 0) return n;
639 return n;
640 }
641
642 public boolean equals(Configuration that) {
643 if (that == null) return false;
644 if (that == this) return true;
645 return this.compareTo(that) == 0;
646 }
647
648 public boolean equals(Object that) {
649 try {
650 return equals((Configuration)that);
651 } catch (ClassCastException e) {
652 }
653 return false;
654 }
655
656 public int hashCode() {
657 return ((int)this.fontScale) + this.mcc + this.mnc
Dianne Hackborna8397032010-03-12 10:52:22 -0800658 + (this.locale != null ? this.locale.hashCode() : 0)
659 + this.touchscreen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700661 + this.navigation + this.navigationHidden
Tobias Haamel27b28b32010-02-09 23:09:17 +0100662 + this.orientation + this.screenLayout + this.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664}