blob: 72fa07c40d9d26a1b659022f214a582224419638 [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 Hackborn2f98f262011-03-28 18:28:35 -070060 /** Constant for {@link #screenLayout}: bits that encode the size. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070061 public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070062 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
63 * value indicating that no size has been set. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070064 public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070065 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
66 * value indicating the screen is at least approximately 320x426 dp units.
67 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
68 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070069 public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070070 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
71 * value indicating the screen is at least approximately 320x470 dp units.
72 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
73 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070074 public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070075 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
76 * value indicating the screen is at least approximately 480x640 dp units.
77 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
78 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070079 public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070080 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
81 * value indicating the screen is at least approximately 720x960 dp units.
82 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
83 * Multiple Screens</a> for more information.*/
Dianne Hackborn14cee9f2010-04-23 17:51:26 -070084 public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070085
86 public static final int SCREENLAYOUT_LONG_MASK = 0x30;
87 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
88 public static final int SCREENLAYOUT_LONG_NO = 0x10;
89 public static final int SCREENLAYOUT_LONG_YES = 0x20;
90
91 /**
92 * Special flag we generate to indicate that the screen layout requires
93 * us to use a compatibility mode for apps that are not modern layout
94 * aware.
95 * @hide
96 */
97 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
98
99 /**
100 * Bit mask of overall layout of the screen. Currently there are two
101 * fields:
102 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
103 * of the screen. They may be one of
104 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
Dianne Hackborn14cee9f2010-04-23 17:51:26 -0700105 * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700106 *
107 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
108 * is wider/taller than normal. They may be one of
109 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
Dianne Hackborn2f98f262011-03-28 18:28:35 -0700110 *
111 * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
112 * Multiple Screens</a> for more information.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700113 */
114 public int screenLayout;
115
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800116 /**
117 * Check if the Configuration's current {@link #screenLayout} is at
118 * least the given size.
119 *
120 * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
121 * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
122 * {@link #SCREENLAYOUT_SIZE_XLARGE}.
123 * @return Returns true if the current screen layout size is at least
124 * the given size.
125 */
126 public boolean isLayoutSizeAtLeast(int size) {
127 int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
128 if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800129 return cur >= size;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800130 }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int TOUCHSCREEN_UNDEFINED = 0;
133 public static final int TOUCHSCREEN_NOTOUCH = 1;
134 public static final int TOUCHSCREEN_STYLUS = 2;
135 public static final int TOUCHSCREEN_FINGER = 3;
136
137 /**
138 * The kind of touch screen attached to the device.
139 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
140 * {@link #TOUCHSCREEN_FINGER}.
141 */
142 public int touchscreen;
143
144 public static final int KEYBOARD_UNDEFINED = 0;
145 public static final int KEYBOARD_NOKEYS = 1;
146 public static final int KEYBOARD_QWERTY = 2;
147 public static final int KEYBOARD_12KEY = 3;
148
149 /**
150 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500151 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
152 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
154 public int keyboard;
155
156 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
157 public static final int KEYBOARDHIDDEN_NO = 1;
158 public static final int KEYBOARDHIDDEN_YES = 2;
159 /** Constant matching actual resource implementation. {@hide} */
160 public static final int KEYBOARDHIDDEN_SOFT = 3;
161
162 /**
163 * A flag indicating whether any keyboard is available. Unlike
164 * {@link #hardKeyboardHidden}, this also takes into account a soft
165 * keyboard, so if the hard keyboard is hidden but there is soft
166 * keyboard available, it will be set to NO. Value is one of:
167 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
168 */
169 public int keyboardHidden;
170
171 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
172 public static final int HARDKEYBOARDHIDDEN_NO = 1;
173 public static final int HARDKEYBOARDHIDDEN_YES = 2;
174
175 /**
176 * A flag indicating whether the hard keyboard has been hidden. This will
177 * be set on a device with a mechanism to hide the keyboard from the
178 * user, when that mechanism is closed. One of:
179 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
180 */
181 public int hardKeyboardHidden;
182
183 public static final int NAVIGATION_UNDEFINED = 0;
184 public static final int NAVIGATION_NONAV = 1;
185 public static final int NAVIGATION_DPAD = 2;
186 public static final int NAVIGATION_TRACKBALL = 3;
187 public static final int NAVIGATION_WHEEL = 4;
188
189 /**
190 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500191 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
192 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 */
194 public int navigation;
195
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700196 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
197 public static final int NAVIGATIONHIDDEN_NO = 1;
198 public static final int NAVIGATIONHIDDEN_YES = 2;
199
200 /**
201 * A flag indicating whether any 5-way or DPAD navigation available.
202 * This will be set on a device with a mechanism to hide the navigation
203 * controls from the user, when that mechanism is closed. One of:
204 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
205 */
206 public int navigationHidden;
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public static final int ORIENTATION_UNDEFINED = 0;
209 public static final int ORIENTATION_PORTRAIT = 1;
210 public static final int ORIENTATION_LANDSCAPE = 2;
211 public static final int ORIENTATION_SQUARE = 3;
212
213 /**
214 * Overall orientation of the screen. May be one of
215 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
216 * or {@link #ORIENTATION_SQUARE}.
217 */
218 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100219
Tobias Haamel27b28b32010-02-09 23:09:17 +0100220 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800221 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
222 public static final int UI_MODE_TYPE_NORMAL = 0x01;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800223 public static final int UI_MODE_TYPE_DESK = 0x02;
224 public static final int UI_MODE_TYPE_CAR = 0x03;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100225
Tobias Haamel27b28b32010-02-09 23:09:17 +0100226 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100227 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100228 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100229 public static final int UI_MODE_NIGHT_YES = 0x20;
230
231 /**
232 * Bit mask of the ui mode. Currently there are two fields:
233 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
Dianne Hackborn7299c412010-03-04 18:41:49 -0800234 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
235 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
236 * or {@link #UI_MODE_TYPE_CAR}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100237 *
238 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
Dianne Hackborn7299c412010-03-04 18:41:49 -0800239 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
Tobias Haamel27b28b32010-02-09 23:09:17 +0100240 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100241 */
242 public int uiMode;
243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800245 * @hide Internal book-keeping.
246 */
247 public int seq;
248
249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * Construct an invalid Configuration. You must call {@link #setToDefaults}
251 * for this object to be valid. {@more}
252 */
253 public Configuration() {
254 setToDefaults();
255 }
256
257 /**
258 * Makes a deep copy suitable for modification.
259 */
260 public Configuration(Configuration o) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700261 setTo(o);
262 }
263
264 public void setTo(Configuration o) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 fontScale = o.fontScale;
266 mcc = o.mcc;
267 mnc = o.mnc;
268 if (o.locale != null) {
269 locale = (Locale) o.locale.clone();
270 }
271 userSetLocale = o.userSetLocale;
272 touchscreen = o.touchscreen;
273 keyboard = o.keyboard;
274 keyboardHidden = o.keyboardHidden;
275 hardKeyboardHidden = o.hardKeyboardHidden;
276 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700277 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700279 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100280 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800281 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700285 StringBuilder sb = new StringBuilder(128);
286 sb.append("{ scale=");
287 sb.append(fontScale);
288 sb.append(" imsi=");
289 sb.append(mcc);
290 sb.append("/");
291 sb.append(mnc);
292 sb.append(" loc=");
293 sb.append(locale);
294 sb.append(" touch=");
295 sb.append(touchscreen);
296 sb.append(" keys=");
297 sb.append(keyboard);
298 sb.append("/");
299 sb.append(keyboardHidden);
300 sb.append("/");
301 sb.append(hardKeyboardHidden);
302 sb.append(" nav=");
303 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700304 sb.append("/");
305 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700306 sb.append(" orien=");
Daniel Sandler7d6bddc2010-07-22 16:11:55 -0400307 switch(orientation) {
308 case ORIENTATION_LANDSCAPE:
309 sb.append("L"); break;
310 case ORIENTATION_PORTRAIT:
311 sb.append("P"); break;
312 default:
313 sb.append(orientation);
314 }
315 sb.append(" layout=0x");
316 sb.append(java.lang.Integer.toHexString(screenLayout));
317 sb.append(" uiMode=0x");
318 sb.append(java.lang.Integer.toHexString(uiMode));
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800319 if (seq != 0) {
320 sb.append(" seq=");
321 sb.append(seq);
322 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700323 sb.append('}');
324 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326
327 /**
328 * Set this object to the system defaults.
329 */
330 public void setToDefaults() {
331 fontScale = 1;
332 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800333 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 userSetLocale = false;
335 touchscreen = TOUCHSCREEN_UNDEFINED;
336 keyboard = KEYBOARD_UNDEFINED;
337 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
338 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
339 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700340 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700342 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800343 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800344 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
347 /** {@hide} */
348 @Deprecated public void makeDefault() {
349 setToDefaults();
350 }
351
352 /**
353 * Copy the fields from delta into this Configuration object, keeping
354 * track of which ones have changed. Any undefined fields in
355 * <var>delta</var> are ignored and not copied in to the current
356 * Configuration.
357 * @return Returns a bit mask of the changed fields, as per
358 * {@link #diff}.
359 */
360 public int updateFrom(Configuration delta) {
361 int changed = 0;
362 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
363 changed |= ActivityInfo.CONFIG_FONT_SCALE;
364 fontScale = delta.fontScale;
365 }
366 if (delta.mcc != 0 && mcc != delta.mcc) {
367 changed |= ActivityInfo.CONFIG_MCC;
368 mcc = delta.mcc;
369 }
370 if (delta.mnc != 0 && mnc != delta.mnc) {
371 changed |= ActivityInfo.CONFIG_MNC;
372 mnc = delta.mnc;
373 }
374 if (delta.locale != null
375 && (locale == null || !locale.equals(delta.locale))) {
376 changed |= ActivityInfo.CONFIG_LOCALE;
377 locale = delta.locale != null
378 ? (Locale) delta.locale.clone() : null;
379 }
380 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
381 {
382 userSetLocale = true;
383 changed |= ActivityInfo.CONFIG_LOCALE;
384 }
385 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
386 && touchscreen != delta.touchscreen) {
387 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
388 touchscreen = delta.touchscreen;
389 }
390 if (delta.keyboard != KEYBOARD_UNDEFINED
391 && keyboard != delta.keyboard) {
392 changed |= ActivityInfo.CONFIG_KEYBOARD;
393 keyboard = delta.keyboard;
394 }
395 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
396 && keyboardHidden != delta.keyboardHidden) {
397 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
398 keyboardHidden = delta.keyboardHidden;
399 }
400 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
401 && hardKeyboardHidden != delta.hardKeyboardHidden) {
402 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
403 hardKeyboardHidden = delta.hardKeyboardHidden;
404 }
405 if (delta.navigation != NAVIGATION_UNDEFINED
406 && navigation != delta.navigation) {
407 changed |= ActivityInfo.CONFIG_NAVIGATION;
408 navigation = delta.navigation;
409 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700410 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
411 && navigationHidden != delta.navigationHidden) {
412 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
413 navigationHidden = delta.navigationHidden;
414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 if (delta.orientation != ORIENTATION_UNDEFINED
416 && orientation != delta.orientation) {
417 changed |= ActivityInfo.CONFIG_ORIENTATION;
418 orientation = delta.orientation;
419 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700420 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700421 && screenLayout != delta.screenLayout) {
422 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
423 screenLayout = delta.screenLayout;
424 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800425 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100426 && uiMode != delta.uiMode) {
427 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800428 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
429 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
430 | (delta.uiMode&UI_MODE_TYPE_MASK);
431 }
432 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
433 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
434 | (delta.uiMode&UI_MODE_NIGHT_MASK);
435 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800438 if (delta.seq != 0) {
439 seq = delta.seq;
440 }
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 return changed;
443 }
444
445 /**
446 * Return a bit mask of the differences between this Configuration
447 * object and the given one. Does not change the values of either. Any
448 * undefined fields in <var>delta</var> are ignored.
449 * @return Returns a bit mask indicating which configuration
450 * values has changed, containing any combination of
451 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
452 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
453 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
454 * PackageManager.ActivityInfo.CONFIG_MCC},
455 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
456 * PackageManager.ActivityInfo.CONFIG_MNC},
457 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
458 * PackageManager.ActivityInfo.CONFIG_LOCALE},
459 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
460 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
461 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
462 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
463 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700464 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700466 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
467 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
468 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
470 public int diff(Configuration delta) {
471 int changed = 0;
472 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
473 changed |= ActivityInfo.CONFIG_FONT_SCALE;
474 }
475 if (delta.mcc != 0 && mcc != delta.mcc) {
476 changed |= ActivityInfo.CONFIG_MCC;
477 }
478 if (delta.mnc != 0 && mnc != delta.mnc) {
479 changed |= ActivityInfo.CONFIG_MNC;
480 }
481 if (delta.locale != null
482 && (locale == null || !locale.equals(delta.locale))) {
483 changed |= ActivityInfo.CONFIG_LOCALE;
484 }
485 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
486 && touchscreen != delta.touchscreen) {
487 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
488 }
489 if (delta.keyboard != KEYBOARD_UNDEFINED
490 && keyboard != delta.keyboard) {
491 changed |= ActivityInfo.CONFIG_KEYBOARD;
492 }
493 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
494 && keyboardHidden != delta.keyboardHidden) {
495 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
496 }
497 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
498 && hardKeyboardHidden != delta.hardKeyboardHidden) {
499 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
500 }
501 if (delta.navigation != NAVIGATION_UNDEFINED
502 && navigation != delta.navigation) {
503 changed |= ActivityInfo.CONFIG_NAVIGATION;
504 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700505 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
506 && navigationHidden != delta.navigationHidden) {
507 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (delta.orientation != ORIENTATION_UNDEFINED
510 && orientation != delta.orientation) {
511 changed |= ActivityInfo.CONFIG_ORIENTATION;
512 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700513 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700514 && screenLayout != delta.screenLayout) {
515 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
516 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800517 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100518 && uiMode != delta.uiMode) {
519 changed |= ActivityInfo.CONFIG_UI_MODE;
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
522 return changed;
523 }
524
525 /**
526 * Determine if a new resource needs to be loaded from the bit set of
527 * configuration changes returned by {@link #updateFrom(Configuration)}.
528 *
529 * @param configChanges The mask of changes configurations as returned by
530 * {@link #updateFrom(Configuration)}.
531 * @param interestingChanges The configuration changes that the resource
532 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
533 *
534 * @return Return true if the resource needs to be loaded, else false.
535 */
536 public static boolean needNewResources(int configChanges, int interestingChanges) {
537 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
538 }
539
540 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800541 * @hide Return true if the sequence of 'other' is better than this. Assumes
542 * that 'this' is your current sequence and 'other' is a new one you have
543 * received some how and want to compare with what you have.
544 */
545 public boolean isOtherSeqNewer(Configuration other) {
546 if (other == null) {
547 // Sanity check.
548 return false;
549 }
550 if (other.seq == 0) {
551 // If the other sequence is not specified, then we must assume
552 // it is newer since we don't know any better.
553 return true;
554 }
555 if (seq == 0) {
556 // If this sequence is not specified, then we also consider the
557 // other is better. Yes we have a preference for other. Sue us.
558 return true;
559 }
560 int diff = other.seq - seq;
561 if (diff > 0x10000) {
562 // If there has been a sufficiently large jump, assume the
563 // sequence has wrapped around.
564 return false;
565 }
566 return diff > 0;
567 }
568
569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 * Parcelable methods
571 */
572 public int describeContents() {
573 return 0;
574 }
575
576 public void writeToParcel(Parcel dest, int flags) {
577 dest.writeFloat(fontScale);
578 dest.writeInt(mcc);
579 dest.writeInt(mnc);
580 if (locale == null) {
581 dest.writeInt(0);
582 } else {
583 dest.writeInt(1);
584 dest.writeString(locale.getLanguage());
585 dest.writeString(locale.getCountry());
586 dest.writeString(locale.getVariant());
587 }
588 if(userSetLocale) {
589 dest.writeInt(1);
590 } else {
591 dest.writeInt(0);
592 }
593 dest.writeInt(touchscreen);
594 dest.writeInt(keyboard);
595 dest.writeInt(keyboardHidden);
596 dest.writeInt(hardKeyboardHidden);
597 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700598 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700600 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100601 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800602 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700605 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 fontScale = source.readFloat();
607 mcc = source.readInt();
608 mnc = source.readInt();
609 if (source.readInt() != 0) {
610 locale = new Locale(source.readString(), source.readString(),
611 source.readString());
612 }
613 userSetLocale = (source.readInt()==1);
614 touchscreen = source.readInt();
615 keyboard = source.readInt();
616 keyboardHidden = source.readInt();
617 hardKeyboardHidden = source.readInt();
618 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700619 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700621 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100622 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800623 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700625
626 public static final Parcelable.Creator<Configuration> CREATOR
627 = new Parcelable.Creator<Configuration>() {
628 public Configuration createFromParcel(Parcel source) {
629 return new Configuration(source);
630 }
631
632 public Configuration[] newArray(int size) {
633 return new Configuration[size];
634 }
635 };
636
637 /**
638 * Construct this Configuration object, reading from the Parcel.
639 */
640 private Configuration(Parcel source) {
641 readFromParcel(source);
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
644 public int compareTo(Configuration that) {
645 int n;
646 float a = this.fontScale;
647 float b = that.fontScale;
648 if (a < b) return -1;
649 if (a > b) return 1;
650 n = this.mcc - that.mcc;
651 if (n != 0) return n;
652 n = this.mnc - that.mnc;
653 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800654 if (this.locale == null) {
655 if (that.locale != null) return 1;
656 } else if (that.locale == null) {
657 return -1;
658 } else {
659 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
660 if (n != 0) return n;
661 n = this.locale.getCountry().compareTo(that.locale.getCountry());
662 if (n != 0) return n;
663 n = this.locale.getVariant().compareTo(that.locale.getVariant());
664 if (n != 0) return n;
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 n = this.touchscreen - that.touchscreen;
667 if (n != 0) return n;
668 n = this.keyboard - that.keyboard;
669 if (n != 0) return n;
670 n = this.keyboardHidden - that.keyboardHidden;
671 if (n != 0) return n;
672 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
673 if (n != 0) return n;
674 n = this.navigation - that.navigation;
675 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700676 n = this.navigationHidden - that.navigationHidden;
677 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700679 if (n != 0) return n;
680 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100681 if (n != 0) return n;
682 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 //if (n != 0) return n;
684 return n;
685 }
686
687 public boolean equals(Configuration that) {
688 if (that == null) return false;
689 if (that == this) return true;
690 return this.compareTo(that) == 0;
691 }
692
693 public boolean equals(Object that) {
694 try {
695 return equals((Configuration)that);
696 } catch (ClassCastException e) {
697 }
698 return false;
699 }
700
701 public int hashCode() {
702 return ((int)this.fontScale) + this.mcc + this.mnc
Dianne Hackborna8397032010-03-12 10:52:22 -0800703 + (this.locale != null ? this.locale.hashCode() : 0)
704 + this.touchscreen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700706 + this.navigation + this.navigationHidden
Tobias Haamel27b28b32010-02-09 23:09:17 +0100707 + this.orientation + this.screenLayout + this.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709}