blob: 908db118245edac9572114ac82dcf8042a8ed04b [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
Scott Main63848e32011-04-20 22:20:46 -070029 * as device configurations (such as input modes, screen size and screen orientation).
30 * <p>You can acquire this object from {@link Resources}, using {@link
31 * Resources#getConfiguration}. Thus, from an activity, you can get it by chaining the request
32 * with {@link android.app.Activity#getResources}:</p>
33 * <pre>Configuration config = getResources().getConfiguration();</pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 */
35public final class Configuration implements Parcelable, Comparable<Configuration> {
36 /**
37 * Current user preference for the scaling factor for fonts, relative
38 * to the base density scaling.
39 */
40 public float fontScale;
41
42 /**
43 * IMSI MCC (Mobile Country Code). 0 if undefined.
44 */
45 public int mcc;
46
47 /**
48 * IMSI MNC (Mobile Network Code). 0 if undefined.
49 */
50 public int mnc;
51
52 /**
53 * Current user preference for the locale.
54 */
55 public Locale locale;
56
57 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070058 * Locale should persist on setting. This is hidden because it is really
59 * questionable whether this is the right way to expose the functionality.
60 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 */
62 public boolean userSetLocale;
63
Dianne Hackborn2f98f262011-03-28 18:28:35 -070064 /** Constant for {@link #screenLayout}: bits that encode the size. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070065 public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070066 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
67 * value indicating that no size has been set. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070068 public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070069 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
70 * value indicating the screen is at least approximately 320x426 dp units.
71 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
72 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070073 public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070074 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
75 * value indicating the screen is at least approximately 320x470 dp units.
76 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
77 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070078 public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070079 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
80 * value indicating the screen is at least approximately 480x640 dp units.
81 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
82 * Multiple Screens</a> for more information. */
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070083 public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
Dianne Hackborn2f98f262011-03-28 18:28:35 -070084 /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
85 * value indicating the screen is at least approximately 720x960 dp units.
86 * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
87 * Multiple Screens</a> for more information.*/
Dianne Hackborn14cee9f2010-04-23 17:51:26 -070088 public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -070089
90 public static final int SCREENLAYOUT_LONG_MASK = 0x30;
91 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
92 public static final int SCREENLAYOUT_LONG_NO = 0x10;
93 public static final int SCREENLAYOUT_LONG_YES = 0x20;
94
95 /**
96 * Special flag we generate to indicate that the screen layout requires
97 * us to use a compatibility mode for apps that are not modern layout
98 * aware.
99 * @hide
100 */
101 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
102
103 /**
104 * Bit mask of overall layout of the screen. Currently there are two
105 * fields:
106 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
107 * of the screen. They may be one of
108 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
Dianne Hackborn14cee9f2010-04-23 17:51:26 -0700109 * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700110 *
111 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
112 * is wider/taller than normal. They may be one of
113 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
Dianne Hackborn2f98f262011-03-28 18:28:35 -0700114 *
115 * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
116 * Multiple Screens</a> for more information.
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700117 */
118 public int screenLayout;
119
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800120 /**
121 * Check if the Configuration's current {@link #screenLayout} is at
122 * least the given size.
123 *
124 * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
125 * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
126 * {@link #SCREENLAYOUT_SIZE_XLARGE}.
127 * @return Returns true if the current screen layout size is at least
128 * the given size.
129 */
130 public boolean isLayoutSizeAtLeast(int size) {
131 int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
132 if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800133 return cur >= size;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public static final int TOUCHSCREEN_UNDEFINED = 0;
137 public static final int TOUCHSCREEN_NOTOUCH = 1;
138 public static final int TOUCHSCREEN_STYLUS = 2;
139 public static final int TOUCHSCREEN_FINGER = 3;
140
141 /**
142 * The kind of touch screen attached to the device.
143 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
144 * {@link #TOUCHSCREEN_FINGER}.
145 */
146 public int touchscreen;
147
148 public static final int KEYBOARD_UNDEFINED = 0;
149 public static final int KEYBOARD_NOKEYS = 1;
150 public static final int KEYBOARD_QWERTY = 2;
151 public static final int KEYBOARD_12KEY = 3;
152
153 /**
154 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500155 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
156 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 */
158 public int keyboard;
159
160 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
161 public static final int KEYBOARDHIDDEN_NO = 1;
162 public static final int KEYBOARDHIDDEN_YES = 2;
163 /** Constant matching actual resource implementation. {@hide} */
164 public static final int KEYBOARDHIDDEN_SOFT = 3;
165
166 /**
167 * A flag indicating whether any keyboard is available. Unlike
168 * {@link #hardKeyboardHidden}, this also takes into account a soft
169 * keyboard, so if the hard keyboard is hidden but there is soft
170 * keyboard available, it will be set to NO. Value is one of:
171 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
172 */
173 public int keyboardHidden;
174
175 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
176 public static final int HARDKEYBOARDHIDDEN_NO = 1;
177 public static final int HARDKEYBOARDHIDDEN_YES = 2;
178
179 /**
180 * A flag indicating whether the hard keyboard has been hidden. This will
181 * be set on a device with a mechanism to hide the keyboard from the
182 * user, when that mechanism is closed. One of:
183 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
184 */
185 public int hardKeyboardHidden;
186
187 public static final int NAVIGATION_UNDEFINED = 0;
188 public static final int NAVIGATION_NONAV = 1;
189 public static final int NAVIGATION_DPAD = 2;
190 public static final int NAVIGATION_TRACKBALL = 3;
191 public static final int NAVIGATION_WHEEL = 4;
192
193 /**
194 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500195 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
196 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 */
198 public int navigation;
199
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700200 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
201 public static final int NAVIGATIONHIDDEN_NO = 1;
202 public static final int NAVIGATIONHIDDEN_YES = 2;
203
204 /**
205 * A flag indicating whether any 5-way or DPAD navigation available.
206 * This will be set on a device with a mechanism to hide the navigation
207 * controls from the user, when that mechanism is closed. One of:
208 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
209 */
210 public int navigationHidden;
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public static final int ORIENTATION_UNDEFINED = 0;
213 public static final int ORIENTATION_PORTRAIT = 1;
214 public static final int ORIENTATION_LANDSCAPE = 2;
215 public static final int ORIENTATION_SQUARE = 3;
216
217 /**
218 * Overall orientation of the screen. May be one of
219 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
220 * or {@link #ORIENTATION_SQUARE}.
221 */
222 public int orientation;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100223
Tobias Haamel27b28b32010-02-09 23:09:17 +0100224 public static final int UI_MODE_TYPE_MASK = 0x0f;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800225 public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
226 public static final int UI_MODE_TYPE_NORMAL = 0x01;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800227 public static final int UI_MODE_TYPE_DESK = 0x02;
228 public static final int UI_MODE_TYPE_CAR = 0x03;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100229
Tobias Haamel27b28b32010-02-09 23:09:17 +0100230 public static final int UI_MODE_NIGHT_MASK = 0x30;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100231 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100232 public static final int UI_MODE_NIGHT_NO = 0x10;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100233 public static final int UI_MODE_NIGHT_YES = 0x20;
234
235 /**
236 * Bit mask of the ui mode. Currently there are two fields:
237 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
Dianne Hackborn7299c412010-03-04 18:41:49 -0800238 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
239 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
240 * or {@link #UI_MODE_TYPE_CAR}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100241 *
242 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
Dianne Hackborn7299c412010-03-04 18:41:49 -0800243 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
Tobias Haamel27b28b32010-02-09 23:09:17 +0100244 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
Tobias Haamel27b28b32010-02-09 23:09:17 +0100245 */
246 public int uiMode;
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800249 * @hide Internal book-keeping.
250 */
251 public int seq;
252
253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * Construct an invalid Configuration. You must call {@link #setToDefaults}
255 * for this object to be valid. {@more}
256 */
257 public Configuration() {
258 setToDefaults();
259 }
260
261 /**
262 * Makes a deep copy suitable for modification.
263 */
264 public Configuration(Configuration o) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700265 setTo(o);
266 }
267
268 public void setTo(Configuration o) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 fontScale = o.fontScale;
270 mcc = o.mcc;
271 mnc = o.mnc;
272 if (o.locale != null) {
273 locale = (Locale) o.locale.clone();
274 }
275 userSetLocale = o.userSetLocale;
276 touchscreen = o.touchscreen;
277 keyboard = o.keyboard;
278 keyboardHidden = o.keyboardHidden;
279 hardKeyboardHidden = o.hardKeyboardHidden;
280 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700281 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700283 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100284 uiMode = o.uiMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800285 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700289 StringBuilder sb = new StringBuilder(128);
290 sb.append("{ scale=");
291 sb.append(fontScale);
292 sb.append(" imsi=");
293 sb.append(mcc);
294 sb.append("/");
295 sb.append(mnc);
296 sb.append(" loc=");
297 sb.append(locale);
298 sb.append(" touch=");
299 sb.append(touchscreen);
300 sb.append(" keys=");
301 sb.append(keyboard);
302 sb.append("/");
303 sb.append(keyboardHidden);
304 sb.append("/");
305 sb.append(hardKeyboardHidden);
306 sb.append(" nav=");
307 sb.append(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700308 sb.append("/");
309 sb.append(navigationHidden);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700310 sb.append(" orien=");
Daniel Sandler7d6bddc2010-07-22 16:11:55 -0400311 switch(orientation) {
312 case ORIENTATION_LANDSCAPE:
313 sb.append("L"); break;
314 case ORIENTATION_PORTRAIT:
315 sb.append("P"); break;
316 default:
317 sb.append(orientation);
318 }
319 sb.append(" layout=0x");
320 sb.append(java.lang.Integer.toHexString(screenLayout));
321 sb.append(" uiMode=0x");
322 sb.append(java.lang.Integer.toHexString(uiMode));
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800323 if (seq != 0) {
324 sb.append(" seq=");
325 sb.append(seq);
326 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700327 sb.append('}');
328 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330
331 /**
332 * Set this object to the system defaults.
333 */
334 public void setToDefaults() {
335 fontScale = 1;
336 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800337 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 userSetLocale = false;
339 touchscreen = TOUCHSCREEN_UNDEFINED;
340 keyboard = KEYBOARD_UNDEFINED;
341 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
342 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
343 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700344 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700346 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800347 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800348 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350
351 /** {@hide} */
352 @Deprecated public void makeDefault() {
353 setToDefaults();
354 }
355
356 /**
357 * Copy the fields from delta into this Configuration object, keeping
358 * track of which ones have changed. Any undefined fields in
359 * <var>delta</var> are ignored and not copied in to the current
360 * Configuration.
361 * @return Returns a bit mask of the changed fields, as per
362 * {@link #diff}.
363 */
364 public int updateFrom(Configuration delta) {
365 int changed = 0;
366 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
367 changed |= ActivityInfo.CONFIG_FONT_SCALE;
368 fontScale = delta.fontScale;
369 }
370 if (delta.mcc != 0 && mcc != delta.mcc) {
371 changed |= ActivityInfo.CONFIG_MCC;
372 mcc = delta.mcc;
373 }
374 if (delta.mnc != 0 && mnc != delta.mnc) {
375 changed |= ActivityInfo.CONFIG_MNC;
376 mnc = delta.mnc;
377 }
378 if (delta.locale != null
379 && (locale == null || !locale.equals(delta.locale))) {
380 changed |= ActivityInfo.CONFIG_LOCALE;
381 locale = delta.locale != null
382 ? (Locale) delta.locale.clone() : null;
383 }
384 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
385 {
386 userSetLocale = true;
387 changed |= ActivityInfo.CONFIG_LOCALE;
388 }
389 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
390 && touchscreen != delta.touchscreen) {
391 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
392 touchscreen = delta.touchscreen;
393 }
394 if (delta.keyboard != KEYBOARD_UNDEFINED
395 && keyboard != delta.keyboard) {
396 changed |= ActivityInfo.CONFIG_KEYBOARD;
397 keyboard = delta.keyboard;
398 }
399 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
400 && keyboardHidden != delta.keyboardHidden) {
401 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
402 keyboardHidden = delta.keyboardHidden;
403 }
404 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
405 && hardKeyboardHidden != delta.hardKeyboardHidden) {
406 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
407 hardKeyboardHidden = delta.hardKeyboardHidden;
408 }
409 if (delta.navigation != NAVIGATION_UNDEFINED
410 && navigation != delta.navigation) {
411 changed |= ActivityInfo.CONFIG_NAVIGATION;
412 navigation = delta.navigation;
413 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700414 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
415 && navigationHidden != delta.navigationHidden) {
416 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
417 navigationHidden = delta.navigationHidden;
418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 if (delta.orientation != ORIENTATION_UNDEFINED
420 && orientation != delta.orientation) {
421 changed |= ActivityInfo.CONFIG_ORIENTATION;
422 orientation = delta.orientation;
423 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700424 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700425 && screenLayout != delta.screenLayout) {
426 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
427 screenLayout = delta.screenLayout;
428 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800429 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100430 && uiMode != delta.uiMode) {
431 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800432 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
433 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
434 | (delta.uiMode&UI_MODE_TYPE_MASK);
435 }
436 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
437 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
438 | (delta.uiMode&UI_MODE_NIGHT_MASK);
439 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800442 if (delta.seq != 0) {
443 seq = delta.seq;
444 }
445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 return changed;
447 }
448
449 /**
450 * Return a bit mask of the differences between this Configuration
451 * object and the given one. Does not change the values of either. Any
452 * undefined fields in <var>delta</var> are ignored.
453 * @return Returns a bit mask indicating which configuration
454 * values has changed, containing any combination of
455 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
456 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
457 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
458 * PackageManager.ActivityInfo.CONFIG_MCC},
459 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
460 * PackageManager.ActivityInfo.CONFIG_MNC},
461 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
462 * PackageManager.ActivityInfo.CONFIG_LOCALE},
463 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
464 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
465 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
466 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
467 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700468 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700470 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
471 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
472 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
474 public int diff(Configuration delta) {
475 int changed = 0;
476 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
477 changed |= ActivityInfo.CONFIG_FONT_SCALE;
478 }
479 if (delta.mcc != 0 && mcc != delta.mcc) {
480 changed |= ActivityInfo.CONFIG_MCC;
481 }
482 if (delta.mnc != 0 && mnc != delta.mnc) {
483 changed |= ActivityInfo.CONFIG_MNC;
484 }
485 if (delta.locale != null
486 && (locale == null || !locale.equals(delta.locale))) {
487 changed |= ActivityInfo.CONFIG_LOCALE;
488 }
489 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
490 && touchscreen != delta.touchscreen) {
491 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
492 }
493 if (delta.keyboard != KEYBOARD_UNDEFINED
494 && keyboard != delta.keyboard) {
495 changed |= ActivityInfo.CONFIG_KEYBOARD;
496 }
497 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
498 && keyboardHidden != delta.keyboardHidden) {
499 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
500 }
501 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
502 && hardKeyboardHidden != delta.hardKeyboardHidden) {
503 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
504 }
505 if (delta.navigation != NAVIGATION_UNDEFINED
506 && navigation != delta.navigation) {
507 changed |= ActivityInfo.CONFIG_NAVIGATION;
508 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700509 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
510 && navigationHidden != delta.navigationHidden) {
511 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 if (delta.orientation != ORIENTATION_UNDEFINED
514 && orientation != delta.orientation) {
515 changed |= ActivityInfo.CONFIG_ORIENTATION;
516 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700517 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700518 && screenLayout != delta.screenLayout) {
519 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
520 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800521 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100522 && uiMode != delta.uiMode) {
523 changed |= ActivityInfo.CONFIG_UI_MODE;
524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525
526 return changed;
527 }
528
529 /**
530 * Determine if a new resource needs to be loaded from the bit set of
531 * configuration changes returned by {@link #updateFrom(Configuration)}.
532 *
533 * @param configChanges The mask of changes configurations as returned by
534 * {@link #updateFrom(Configuration)}.
535 * @param interestingChanges The configuration changes that the resource
536 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
537 *
538 * @return Return true if the resource needs to be loaded, else false.
539 */
540 public static boolean needNewResources(int configChanges, int interestingChanges) {
541 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
542 }
543
544 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800545 * @hide Return true if the sequence of 'other' is better than this. Assumes
546 * that 'this' is your current sequence and 'other' is a new one you have
547 * received some how and want to compare with what you have.
548 */
549 public boolean isOtherSeqNewer(Configuration other) {
550 if (other == null) {
551 // Sanity check.
552 return false;
553 }
554 if (other.seq == 0) {
555 // If the other sequence is not specified, then we must assume
556 // it is newer since we don't know any better.
557 return true;
558 }
559 if (seq == 0) {
560 // If this sequence is not specified, then we also consider the
561 // other is better. Yes we have a preference for other. Sue us.
562 return true;
563 }
564 int diff = other.seq - seq;
565 if (diff > 0x10000) {
566 // If there has been a sufficiently large jump, assume the
567 // sequence has wrapped around.
568 return false;
569 }
570 return diff > 0;
571 }
572
573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 * Parcelable methods
575 */
576 public int describeContents() {
577 return 0;
578 }
579
580 public void writeToParcel(Parcel dest, int flags) {
581 dest.writeFloat(fontScale);
582 dest.writeInt(mcc);
583 dest.writeInt(mnc);
584 if (locale == null) {
585 dest.writeInt(0);
586 } else {
587 dest.writeInt(1);
588 dest.writeString(locale.getLanguage());
589 dest.writeString(locale.getCountry());
590 dest.writeString(locale.getVariant());
591 }
592 if(userSetLocale) {
593 dest.writeInt(1);
594 } else {
595 dest.writeInt(0);
596 }
597 dest.writeInt(touchscreen);
598 dest.writeInt(keyboard);
599 dest.writeInt(keyboardHidden);
600 dest.writeInt(hardKeyboardHidden);
601 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700602 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700604 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100605 dest.writeInt(uiMode);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800606 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
608
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700609 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 fontScale = source.readFloat();
611 mcc = source.readInt();
612 mnc = source.readInt();
613 if (source.readInt() != 0) {
614 locale = new Locale(source.readString(), source.readString(),
615 source.readString());
616 }
617 userSetLocale = (source.readInt()==1);
618 touchscreen = source.readInt();
619 keyboard = source.readInt();
620 keyboardHidden = source.readInt();
621 hardKeyboardHidden = source.readInt();
622 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700623 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700625 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100626 uiMode = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800627 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700629
630 public static final Parcelable.Creator<Configuration> CREATOR
631 = new Parcelable.Creator<Configuration>() {
632 public Configuration createFromParcel(Parcel source) {
633 return new Configuration(source);
634 }
635
636 public Configuration[] newArray(int size) {
637 return new Configuration[size];
638 }
639 };
640
641 /**
642 * Construct this Configuration object, reading from the Parcel.
643 */
644 private Configuration(Parcel source) {
645 readFromParcel(source);
646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
648 public int compareTo(Configuration that) {
649 int n;
650 float a = this.fontScale;
651 float b = that.fontScale;
652 if (a < b) return -1;
653 if (a > b) return 1;
654 n = this.mcc - that.mcc;
655 if (n != 0) return n;
656 n = this.mnc - that.mnc;
657 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800658 if (this.locale == null) {
659 if (that.locale != null) return 1;
660 } else if (that.locale == null) {
661 return -1;
662 } else {
663 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
664 if (n != 0) return n;
665 n = this.locale.getCountry().compareTo(that.locale.getCountry());
666 if (n != 0) return n;
667 n = this.locale.getVariant().compareTo(that.locale.getVariant());
668 if (n != 0) return n;
669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 n = this.touchscreen - that.touchscreen;
671 if (n != 0) return n;
672 n = this.keyboard - that.keyboard;
673 if (n != 0) return n;
674 n = this.keyboardHidden - that.keyboardHidden;
675 if (n != 0) return n;
676 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
677 if (n != 0) return n;
678 n = this.navigation - that.navigation;
679 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700680 n = this.navigationHidden - that.navigationHidden;
681 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700683 if (n != 0) return n;
684 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100685 if (n != 0) return n;
686 n = this.uiMode - that.uiMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 //if (n != 0) return n;
688 return n;
689 }
690
691 public boolean equals(Configuration that) {
692 if (that == null) return false;
693 if (that == this) return true;
694 return this.compareTo(that) == 0;
695 }
696
697 public boolean equals(Object that) {
698 try {
699 return equals((Configuration)that);
700 } catch (ClassCastException e) {
701 }
702 return false;
703 }
704
705 public int hashCode() {
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400706 int result = 17;
707 result = 31 * result + Float.floatToIntBits(fontScale);
708 result = 31 * result + mcc;
709 result = 31 * result + mnc;
710 result = 31 * result + (locale != null ? locale.hashCode() : 0);
711 result = 31 * result + touchscreen;
712 result = 31 * result + keyboard;
713 result = 31 * result + keyboardHidden;
714 result = 31 * result + hardKeyboardHidden;
715 result = 31 * result + navigation;
716 result = 31 * result + navigationHidden;
717 result = 31 * result + orientation;
718 result = 31 * result + screenLayout;
719 result = 31 * result + uiMode;
720 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722}