blob: 0de08f26c0b3f9cffdda3b23532745f488cf9c9e [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
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700248 public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
249
250 /**
251 * The current width of the available screen space, in dp units.
252 */
253 public int screenWidthDp;
254
255 public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0;
256
257 /**
258 * The current height of the available screen space, in dp units.
259 */
260 public int screenHeightDp;
261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800263 * @hide Internal book-keeping.
264 */
265 public int seq;
266
267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * Construct an invalid Configuration. You must call {@link #setToDefaults}
269 * for this object to be valid. {@more}
270 */
271 public Configuration() {
272 setToDefaults();
273 }
274
275 /**
276 * Makes a deep copy suitable for modification.
277 */
278 public Configuration(Configuration o) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700279 setTo(o);
280 }
281
282 public void setTo(Configuration o) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 fontScale = o.fontScale;
284 mcc = o.mcc;
285 mnc = o.mnc;
286 if (o.locale != null) {
287 locale = (Locale) o.locale.clone();
288 }
289 userSetLocale = o.userSetLocale;
290 touchscreen = o.touchscreen;
291 keyboard = o.keyboard;
292 keyboardHidden = o.keyboardHidden;
293 hardKeyboardHidden = o.hardKeyboardHidden;
294 navigation = o.navigation;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700295 navigationHidden = o.navigationHidden;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700297 screenLayout = o.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100298 uiMode = o.uiMode;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700299 screenWidthDp = o.screenWidthDp;
300 screenHeightDp = o.screenHeightDp;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800301 seq = o.seq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700305 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn9d132642011-04-21 17:26:39 -0700306 sb.append("{");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700307 sb.append(fontScale);
Dianne Hackborn9d132642011-04-21 17:26:39 -0700308 sb.append("x imsi=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700309 sb.append(mcc);
310 sb.append("/");
311 sb.append(mnc);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700312 if (locale != null) {
313 sb.append(" ");
314 sb.append(locale);
315 } else {
316 sb.append(" (no locale)");
Daniel Sandler7d6bddc2010-07-22 16:11:55 -0400317 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700318 switch (touchscreen) {
319 case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
320 case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
321 case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
322 case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
323 default: sb.append(" touch="); sb.append(touchscreen); break;
324 }
325 switch (keyboard) {
326 case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
327 case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
328 case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
329 case KEYBOARD_12KEY: sb.append(" 12key"); break;
330 default: sb.append(" keys="); sb.append(keyboard); break;
331 }
332 switch (keyboardHidden) {
333 case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
334 case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
335 case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
336 case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
337 default: sb.append("/"); sb.append(keyboardHidden); break;
338 }
339 switch (hardKeyboardHidden) {
340 case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
341 case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
342 case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
343 default: sb.append("/"); sb.append(hardKeyboardHidden); break;
344 }
345 switch (navigation) {
346 case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
347 case NAVIGATION_NONAV: sb.append(" -nav"); break;
348 case NAVIGATION_DPAD: sb.append(" dpad"); break;
349 case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
350 case NAVIGATION_WHEEL: sb.append(" wheel"); break;
351 default: sb.append(" nav="); sb.append(navigation); break;
352 }
353 switch (navigationHidden) {
354 case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
355 case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
356 case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
357 default: sb.append("/"); sb.append(navigationHidden); break;
358 }
359 switch (orientation) {
360 case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
361 case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
362 case ORIENTATION_PORTRAIT: sb.append(" port"); break;
363 default: sb.append(" orien="); sb.append(orientation); break;
364 }
365 switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
366 case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
367 case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
368 case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
369 case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
370 case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
371 default: sb.append(" layoutSize=");
372 sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
373 }
374 switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
375 case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
376 case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
377 case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
378 default: sb.append(" layoutLong=");
379 sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
380 }
381 switch ((uiMode&UI_MODE_TYPE_MASK)) {
382 case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
383 case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
384 case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
385 case UI_MODE_TYPE_CAR: sb.append(" car"); break;
386 default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
387 }
388 switch ((uiMode&UI_MODE_NIGHT_MASK)) {
389 case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
390 case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
391 case UI_MODE_NIGHT_YES: sb.append(" night"); break;
392 default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
393 }
394 if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
395 sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
396 } else {
397 sb.append("?wdp");
398 }
399 if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
400 sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
401 } else {
402 sb.append("?hdp");
403 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800404 if (seq != 0) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700405 sb.append(" s.");
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800406 sb.append(seq);
407 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700408 sb.append('}');
409 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411
412 /**
413 * Set this object to the system defaults.
414 */
415 public void setToDefaults() {
416 fontScale = 1;
417 mcc = mnc = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800418 locale = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 userSetLocale = false;
420 touchscreen = TOUCHSCREEN_UNDEFINED;
421 keyboard = KEYBOARD_UNDEFINED;
422 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
423 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
424 navigation = NAVIGATION_UNDEFINED;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700425 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 orientation = ORIENTATION_UNDEFINED;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700427 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800428 uiMode = UI_MODE_TYPE_UNDEFINED;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700429 screenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
430 screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800431 seq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433
434 /** {@hide} */
435 @Deprecated public void makeDefault() {
436 setToDefaults();
437 }
438
439 /**
440 * Copy the fields from delta into this Configuration object, keeping
441 * track of which ones have changed. Any undefined fields in
442 * <var>delta</var> are ignored and not copied in to the current
443 * Configuration.
444 * @return Returns a bit mask of the changed fields, as per
445 * {@link #diff}.
446 */
447 public int updateFrom(Configuration delta) {
448 int changed = 0;
449 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
450 changed |= ActivityInfo.CONFIG_FONT_SCALE;
451 fontScale = delta.fontScale;
452 }
453 if (delta.mcc != 0 && mcc != delta.mcc) {
454 changed |= ActivityInfo.CONFIG_MCC;
455 mcc = delta.mcc;
456 }
457 if (delta.mnc != 0 && mnc != delta.mnc) {
458 changed |= ActivityInfo.CONFIG_MNC;
459 mnc = delta.mnc;
460 }
461 if (delta.locale != null
462 && (locale == null || !locale.equals(delta.locale))) {
463 changed |= ActivityInfo.CONFIG_LOCALE;
464 locale = delta.locale != null
465 ? (Locale) delta.locale.clone() : null;
466 }
467 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
468 {
469 userSetLocale = true;
470 changed |= ActivityInfo.CONFIG_LOCALE;
471 }
472 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
473 && touchscreen != delta.touchscreen) {
474 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
475 touchscreen = delta.touchscreen;
476 }
477 if (delta.keyboard != KEYBOARD_UNDEFINED
478 && keyboard != delta.keyboard) {
479 changed |= ActivityInfo.CONFIG_KEYBOARD;
480 keyboard = delta.keyboard;
481 }
482 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
483 && keyboardHidden != delta.keyboardHidden) {
484 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
485 keyboardHidden = delta.keyboardHidden;
486 }
487 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
488 && hardKeyboardHidden != delta.hardKeyboardHidden) {
489 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
490 hardKeyboardHidden = delta.hardKeyboardHidden;
491 }
492 if (delta.navigation != NAVIGATION_UNDEFINED
493 && navigation != delta.navigation) {
494 changed |= ActivityInfo.CONFIG_NAVIGATION;
495 navigation = delta.navigation;
496 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700497 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
498 && navigationHidden != delta.navigationHidden) {
499 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
500 navigationHidden = delta.navigationHidden;
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 if (delta.orientation != ORIENTATION_UNDEFINED
503 && orientation != delta.orientation) {
504 changed |= ActivityInfo.CONFIG_ORIENTATION;
505 orientation = delta.orientation;
506 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700507 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700508 && screenLayout != delta.screenLayout) {
509 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
510 screenLayout = delta.screenLayout;
511 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800512 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100513 && uiMode != delta.uiMode) {
514 changed |= ActivityInfo.CONFIG_UI_MODE;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800515 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
516 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
517 | (delta.uiMode&UI_MODE_TYPE_MASK);
518 }
519 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
520 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
521 | (delta.uiMode&UI_MODE_NIGHT_MASK);
522 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100523 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700524 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
525 && screenWidthDp != delta.screenWidthDp) {
526 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
527 screenWidthDp = delta.screenWidthDp;
528 }
529 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
530 && screenHeightDp != delta.screenHeightDp) {
531 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
532 screenHeightDp = delta.screenHeightDp;
533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800535 if (delta.seq != 0) {
536 seq = delta.seq;
537 }
538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 return changed;
540 }
541
542 /**
543 * Return a bit mask of the differences between this Configuration
544 * object and the given one. Does not change the values of either. Any
545 * undefined fields in <var>delta</var> are ignored.
546 * @return Returns a bit mask indicating which configuration
547 * values has changed, containing any combination of
548 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
549 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
550 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
551 * PackageManager.ActivityInfo.CONFIG_MCC},
552 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
553 * PackageManager.ActivityInfo.CONFIG_MNC},
554 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
555 * PackageManager.ActivityInfo.CONFIG_LOCALE},
556 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
557 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
558 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
559 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
560 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700561 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700563 * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
Dianne Hackborn723738c2009-06-25 19:48:04 -0700564 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700565 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
566 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
567 * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 */
569 public int diff(Configuration delta) {
570 int changed = 0;
571 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
572 changed |= ActivityInfo.CONFIG_FONT_SCALE;
573 }
574 if (delta.mcc != 0 && mcc != delta.mcc) {
575 changed |= ActivityInfo.CONFIG_MCC;
576 }
577 if (delta.mnc != 0 && mnc != delta.mnc) {
578 changed |= ActivityInfo.CONFIG_MNC;
579 }
580 if (delta.locale != null
581 && (locale == null || !locale.equals(delta.locale))) {
582 changed |= ActivityInfo.CONFIG_LOCALE;
583 }
584 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
585 && touchscreen != delta.touchscreen) {
586 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
587 }
588 if (delta.keyboard != KEYBOARD_UNDEFINED
589 && keyboard != delta.keyboard) {
590 changed |= ActivityInfo.CONFIG_KEYBOARD;
591 }
592 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
593 && keyboardHidden != delta.keyboardHidden) {
594 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
595 }
596 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
597 && hardKeyboardHidden != delta.hardKeyboardHidden) {
598 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
599 }
600 if (delta.navigation != NAVIGATION_UNDEFINED
601 && navigation != delta.navigation) {
602 changed |= ActivityInfo.CONFIG_NAVIGATION;
603 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700604 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
605 && navigationHidden != delta.navigationHidden) {
606 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 if (delta.orientation != ORIENTATION_UNDEFINED
609 && orientation != delta.orientation) {
610 changed |= ActivityInfo.CONFIG_ORIENTATION;
611 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700612 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
Dianne Hackborn723738c2009-06-25 19:48:04 -0700613 && screenLayout != delta.screenLayout) {
614 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
615 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800616 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
Tobias Haamel27b28b32010-02-09 23:09:17 +0100617 && uiMode != delta.uiMode) {
618 changed |= ActivityInfo.CONFIG_UI_MODE;
619 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700620 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
621 && screenWidthDp != delta.screenWidthDp) {
622 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
623 }
624 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
625 && screenHeightDp != delta.screenHeightDp) {
626 changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 return changed;
630 }
631
632 /**
633 * Determine if a new resource needs to be loaded from the bit set of
634 * configuration changes returned by {@link #updateFrom(Configuration)}.
635 *
636 * @param configChanges The mask of changes configurations as returned by
637 * {@link #updateFrom(Configuration)}.
638 * @param interestingChanges The configuration changes that the resource
639 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
640 *
641 * @return Return true if the resource needs to be loaded, else false.
642 */
643 public static boolean needNewResources(int configChanges, int interestingChanges) {
644 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
645 }
646
647 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800648 * @hide Return true if the sequence of 'other' is better than this. Assumes
649 * that 'this' is your current sequence and 'other' is a new one you have
650 * received some how and want to compare with what you have.
651 */
652 public boolean isOtherSeqNewer(Configuration other) {
653 if (other == null) {
654 // Sanity check.
655 return false;
656 }
657 if (other.seq == 0) {
658 // If the other sequence is not specified, then we must assume
659 // it is newer since we don't know any better.
660 return true;
661 }
662 if (seq == 0) {
663 // If this sequence is not specified, then we also consider the
664 // other is better. Yes we have a preference for other. Sue us.
665 return true;
666 }
667 int diff = other.seq - seq;
668 if (diff > 0x10000) {
669 // If there has been a sufficiently large jump, assume the
670 // sequence has wrapped around.
671 return false;
672 }
673 return diff > 0;
674 }
675
676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * Parcelable methods
678 */
679 public int describeContents() {
680 return 0;
681 }
682
683 public void writeToParcel(Parcel dest, int flags) {
684 dest.writeFloat(fontScale);
685 dest.writeInt(mcc);
686 dest.writeInt(mnc);
687 if (locale == null) {
688 dest.writeInt(0);
689 } else {
690 dest.writeInt(1);
691 dest.writeString(locale.getLanguage());
692 dest.writeString(locale.getCountry());
693 dest.writeString(locale.getVariant());
694 }
695 if(userSetLocale) {
696 dest.writeInt(1);
697 } else {
698 dest.writeInt(0);
699 }
700 dest.writeInt(touchscreen);
701 dest.writeInt(keyboard);
702 dest.writeInt(keyboardHidden);
703 dest.writeInt(hardKeyboardHidden);
704 dest.writeInt(navigation);
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700705 dest.writeInt(navigationHidden);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700707 dest.writeInt(screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100708 dest.writeInt(uiMode);
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700709 dest.writeInt(screenWidthDp);
710 dest.writeInt(screenHeightDp);
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800711 dest.writeInt(seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700714 public void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 fontScale = source.readFloat();
716 mcc = source.readInt();
717 mnc = source.readInt();
718 if (source.readInt() != 0) {
719 locale = new Locale(source.readString(), source.readString(),
720 source.readString());
721 }
722 userSetLocale = (source.readInt()==1);
723 touchscreen = source.readInt();
724 keyboard = source.readInt();
725 keyboardHidden = source.readInt();
726 hardKeyboardHidden = source.readInt();
727 navigation = source.readInt();
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700728 navigationHidden = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700730 screenLayout = source.readInt();
Tobias Haamel27b28b32010-02-09 23:09:17 +0100731 uiMode = source.readInt();
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700732 screenWidthDp = source.readInt();
733 screenHeightDp = source.readInt();
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800734 seq = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700736
737 public static final Parcelable.Creator<Configuration> CREATOR
738 = new Parcelable.Creator<Configuration>() {
739 public Configuration createFromParcel(Parcel source) {
740 return new Configuration(source);
741 }
742
743 public Configuration[] newArray(int size) {
744 return new Configuration[size];
745 }
746 };
747
748 /**
749 * Construct this Configuration object, reading from the Parcel.
750 */
751 private Configuration(Parcel source) {
752 readFromParcel(source);
753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754
755 public int compareTo(Configuration that) {
756 int n;
757 float a = this.fontScale;
758 float b = that.fontScale;
759 if (a < b) return -1;
760 if (a > b) return 1;
761 n = this.mcc - that.mcc;
762 if (n != 0) return n;
763 n = this.mnc - that.mnc;
764 if (n != 0) return n;
Dianne Hackborna8397032010-03-12 10:52:22 -0800765 if (this.locale == null) {
766 if (that.locale != null) return 1;
767 } else if (that.locale == null) {
768 return -1;
769 } else {
770 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
771 if (n != 0) return n;
772 n = this.locale.getCountry().compareTo(that.locale.getCountry());
773 if (n != 0) return n;
774 n = this.locale.getVariant().compareTo(that.locale.getVariant());
775 if (n != 0) return n;
776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 n = this.touchscreen - that.touchscreen;
778 if (n != 0) return n;
779 n = this.keyboard - that.keyboard;
780 if (n != 0) return n;
781 n = this.keyboardHidden - that.keyboardHidden;
782 if (n != 0) return n;
783 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
784 if (n != 0) return n;
785 n = this.navigation - that.navigation;
786 if (n != 0) return n;
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700787 n = this.navigationHidden - that.navigationHidden;
788 if (n != 0) return n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700790 if (n != 0) return n;
791 n = this.screenLayout - that.screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100792 if (n != 0) return n;
793 n = this.uiMode - that.uiMode;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700794 if (n != 0) return n;
795 n = this.screenWidthDp - that.screenWidthDp;
796 if (n != 0) return n;
797 n = this.screenHeightDp - that.screenHeightDp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 //if (n != 0) return n;
799 return n;
800 }
801
802 public boolean equals(Configuration that) {
803 if (that == null) return false;
804 if (that == this) return true;
805 return this.compareTo(that) == 0;
806 }
807
808 public boolean equals(Object that) {
809 try {
810 return equals((Configuration)that);
811 } catch (ClassCastException e) {
812 }
813 return false;
814 }
815
816 public int hashCode() {
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400817 int result = 17;
818 result = 31 * result + Float.floatToIntBits(fontScale);
819 result = 31 * result + mcc;
820 result = 31 * result + mnc;
821 result = 31 * result + (locale != null ? locale.hashCode() : 0);
822 result = 31 * result + touchscreen;
823 result = 31 * result + keyboard;
824 result = 31 * result + keyboardHidden;
825 result = 31 * result + hardKeyboardHidden;
826 result = 31 * result + navigation;
827 result = 31 * result + navigationHidden;
828 result = 31 * result + orientation;
829 result = 31 * result + screenLayout;
830 result = 31 * result + uiMode;
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700831 result = 31 * result + screenWidthDp;
832 result = 31 * result + screenHeightDp;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400833 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 }
835}