blob: 4928e93484446375709f93d82b46207bd80eb729 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001package android.content.res;
2
3import android.content.pm.ActivityInfo;
4import android.os.Parcel;
5import android.os.Parcelable;
6
7import java.util.Locale;
8
9/**
10 * This class describes all device configuration information that can
11 * impact the resources the application retrieves. This includes both
12 * user-specified configuration options (locale and scaling) as well
13 * as dynamic device configuration (various types of input devices).
14 */
15public final class Configuration implements Parcelable, Comparable<Configuration> {
16 /**
17 * Current user preference for the scaling factor for fonts, relative
18 * to the base density scaling.
19 */
20 public float fontScale;
21
22 /**
23 * IMSI MCC (Mobile Country Code). 0 if undefined.
24 */
25 public int mcc;
26
27 /**
28 * IMSI MNC (Mobile Network Code). 0 if undefined.
29 */
30 public int mnc;
31
32 /**
33 * Current user preference for the locale.
34 */
35 public Locale locale;
36
37 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070038 * Locale should persist on setting. This is hidden because it is really
39 * questionable whether this is the right way to expose the functionality.
40 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 */
42 public boolean userSetLocale;
43
44 public static final int TOUCHSCREEN_UNDEFINED = 0;
45 public static final int TOUCHSCREEN_NOTOUCH = 1;
46 public static final int TOUCHSCREEN_STYLUS = 2;
47 public static final int TOUCHSCREEN_FINGER = 3;
48
49 /**
50 * The kind of touch screen attached to the device.
51 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
52 * {@link #TOUCHSCREEN_FINGER}.
53 */
54 public int touchscreen;
55
56 public static final int KEYBOARD_UNDEFINED = 0;
57 public static final int KEYBOARD_NOKEYS = 1;
58 public static final int KEYBOARD_QWERTY = 2;
59 public static final int KEYBOARD_12KEY = 3;
60
61 /**
62 * The kind of keyboard attached to the device.
Kenny Root507f8ed2009-06-09 11:21:11 -050063 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
64 * {@link #KEYBOARD_12KEY}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
66 public int keyboard;
67
68 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
69 public static final int KEYBOARDHIDDEN_NO = 1;
70 public static final int KEYBOARDHIDDEN_YES = 2;
71 /** Constant matching actual resource implementation. {@hide} */
72 public static final int KEYBOARDHIDDEN_SOFT = 3;
73
74 /**
75 * A flag indicating whether any keyboard is available. Unlike
76 * {@link #hardKeyboardHidden}, this also takes into account a soft
77 * keyboard, so if the hard keyboard is hidden but there is soft
78 * keyboard available, it will be set to NO. Value is one of:
79 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
80 */
81 public int keyboardHidden;
82
83 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
84 public static final int HARDKEYBOARDHIDDEN_NO = 1;
85 public static final int HARDKEYBOARDHIDDEN_YES = 2;
86
87 /**
88 * A flag indicating whether the hard keyboard has been hidden. This will
89 * be set on a device with a mechanism to hide the keyboard from the
90 * user, when that mechanism is closed. One of:
91 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
92 */
93 public int hardKeyboardHidden;
94
95 public static final int NAVIGATION_UNDEFINED = 0;
96 public static final int NAVIGATION_NONAV = 1;
97 public static final int NAVIGATION_DPAD = 2;
98 public static final int NAVIGATION_TRACKBALL = 3;
99 public static final int NAVIGATION_WHEEL = 4;
100
101 /**
102 * The kind of navigation method available on the device.
Kenny Root507f8ed2009-06-09 11:21:11 -0500103 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
104 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 */
106 public int navigation;
107
108 public static final int ORIENTATION_UNDEFINED = 0;
109 public static final int ORIENTATION_PORTRAIT = 1;
110 public static final int ORIENTATION_LANDSCAPE = 2;
111 public static final int ORIENTATION_SQUARE = 3;
112
113 /**
114 * Overall orientation of the screen. May be one of
115 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
116 * or {@link #ORIENTATION_SQUARE}.
117 */
118 public int orientation;
119
Dianne Hackborn723738c2009-06-25 19:48:04 -0700120 public static final int SCREENLAYOUT_UNDEFINED = 0;
121 public static final int SCREENLAYOUT_SMALL = 1;
122 public static final int SCREENLAYOUT_NORMAL = 2;
123 public static final int SCREENLAYOUT_LARGE = 3;
124
125 /**
126 * Overall layout of the screen. May be one of
127 * {@link #SCREENLAYOUT_SMALL}, {@link #SCREENLAYOUT_NORMAL},
128 * or {@link #SCREENLAYOUT_LARGE}.
129 */
130 public int screenLayout;
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 /**
133 * Construct an invalid Configuration. You must call {@link #setToDefaults}
134 * for this object to be valid. {@more}
135 */
136 public Configuration() {
137 setToDefaults();
138 }
139
140 /**
141 * Makes a deep copy suitable for modification.
142 */
143 public Configuration(Configuration o) {
144 fontScale = o.fontScale;
145 mcc = o.mcc;
146 mnc = o.mnc;
147 if (o.locale != null) {
148 locale = (Locale) o.locale.clone();
149 }
150 userSetLocale = o.userSetLocale;
151 touchscreen = o.touchscreen;
152 keyboard = o.keyboard;
153 keyboardHidden = o.keyboardHidden;
154 hardKeyboardHidden = o.hardKeyboardHidden;
155 navigation = o.navigation;
156 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700157 screenLayout = o.screenLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 }
159
160 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700161 StringBuilder sb = new StringBuilder(128);
162 sb.append("{ scale=");
163 sb.append(fontScale);
164 sb.append(" imsi=");
165 sb.append(mcc);
166 sb.append("/");
167 sb.append(mnc);
168 sb.append(" loc=");
169 sb.append(locale);
170 sb.append(" touch=");
171 sb.append(touchscreen);
172 sb.append(" keys=");
173 sb.append(keyboard);
174 sb.append("/");
175 sb.append(keyboardHidden);
176 sb.append("/");
177 sb.append(hardKeyboardHidden);
178 sb.append(" nav=");
179 sb.append(navigation);
180 sb.append(" orien=");
181 sb.append(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700182 sb.append(" layout=");
183 sb.append(screenLayout);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700184 sb.append('}');
185 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
187
188 /**
189 * Set this object to the system defaults.
190 */
191 public void setToDefaults() {
192 fontScale = 1;
193 mcc = mnc = 0;
194 locale = Locale.getDefault();
195 userSetLocale = false;
196 touchscreen = TOUCHSCREEN_UNDEFINED;
197 keyboard = KEYBOARD_UNDEFINED;
198 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
199 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
200 navigation = NAVIGATION_UNDEFINED;
201 orientation = ORIENTATION_UNDEFINED;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700202 screenLayout = SCREENLAYOUT_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204
205 /** {@hide} */
206 @Deprecated public void makeDefault() {
207 setToDefaults();
208 }
209
210 /**
211 * Copy the fields from delta into this Configuration object, keeping
212 * track of which ones have changed. Any undefined fields in
213 * <var>delta</var> are ignored and not copied in to the current
214 * Configuration.
215 * @return Returns a bit mask of the changed fields, as per
216 * {@link #diff}.
217 */
218 public int updateFrom(Configuration delta) {
219 int changed = 0;
220 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
221 changed |= ActivityInfo.CONFIG_FONT_SCALE;
222 fontScale = delta.fontScale;
223 }
224 if (delta.mcc != 0 && mcc != delta.mcc) {
225 changed |= ActivityInfo.CONFIG_MCC;
226 mcc = delta.mcc;
227 }
228 if (delta.mnc != 0 && mnc != delta.mnc) {
229 changed |= ActivityInfo.CONFIG_MNC;
230 mnc = delta.mnc;
231 }
232 if (delta.locale != null
233 && (locale == null || !locale.equals(delta.locale))) {
234 changed |= ActivityInfo.CONFIG_LOCALE;
235 locale = delta.locale != null
236 ? (Locale) delta.locale.clone() : null;
237 }
238 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
239 {
240 userSetLocale = true;
241 changed |= ActivityInfo.CONFIG_LOCALE;
242 }
243 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
244 && touchscreen != delta.touchscreen) {
245 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
246 touchscreen = delta.touchscreen;
247 }
248 if (delta.keyboard != KEYBOARD_UNDEFINED
249 && keyboard != delta.keyboard) {
250 changed |= ActivityInfo.CONFIG_KEYBOARD;
251 keyboard = delta.keyboard;
252 }
253 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
254 && keyboardHidden != delta.keyboardHidden) {
255 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
256 keyboardHidden = delta.keyboardHidden;
257 }
258 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
259 && hardKeyboardHidden != delta.hardKeyboardHidden) {
260 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
261 hardKeyboardHidden = delta.hardKeyboardHidden;
262 }
263 if (delta.navigation != NAVIGATION_UNDEFINED
264 && navigation != delta.navigation) {
265 changed |= ActivityInfo.CONFIG_NAVIGATION;
266 navigation = delta.navigation;
267 }
268 if (delta.orientation != ORIENTATION_UNDEFINED
269 && orientation != delta.orientation) {
270 changed |= ActivityInfo.CONFIG_ORIENTATION;
271 orientation = delta.orientation;
272 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700273 if (delta.screenLayout != SCREENLAYOUT_UNDEFINED
274 && screenLayout != delta.screenLayout) {
275 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
276 screenLayout = delta.screenLayout;
277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
279 return changed;
280 }
281
282 /**
283 * Return a bit mask of the differences between this Configuration
284 * object and the given one. Does not change the values of either. Any
285 * undefined fields in <var>delta</var> are ignored.
286 * @return Returns a bit mask indicating which configuration
287 * values has changed, containing any combination of
288 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
289 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
290 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
291 * PackageManager.ActivityInfo.CONFIG_MCC},
292 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
293 * PackageManager.ActivityInfo.CONFIG_MNC},
294 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
295 * PackageManager.ActivityInfo.CONFIG_LOCALE},
296 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
297 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
298 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
299 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
300 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700301 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700303 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
304 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
305 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 */
307 public int diff(Configuration delta) {
308 int changed = 0;
309 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
310 changed |= ActivityInfo.CONFIG_FONT_SCALE;
311 }
312 if (delta.mcc != 0 && mcc != delta.mcc) {
313 changed |= ActivityInfo.CONFIG_MCC;
314 }
315 if (delta.mnc != 0 && mnc != delta.mnc) {
316 changed |= ActivityInfo.CONFIG_MNC;
317 }
318 if (delta.locale != null
319 && (locale == null || !locale.equals(delta.locale))) {
320 changed |= ActivityInfo.CONFIG_LOCALE;
321 }
322 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
323 && touchscreen != delta.touchscreen) {
324 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
325 }
326 if (delta.keyboard != KEYBOARD_UNDEFINED
327 && keyboard != delta.keyboard) {
328 changed |= ActivityInfo.CONFIG_KEYBOARD;
329 }
330 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
331 && keyboardHidden != delta.keyboardHidden) {
332 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
333 }
334 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
335 && hardKeyboardHidden != delta.hardKeyboardHidden) {
336 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
337 }
338 if (delta.navigation != NAVIGATION_UNDEFINED
339 && navigation != delta.navigation) {
340 changed |= ActivityInfo.CONFIG_NAVIGATION;
341 }
342 if (delta.orientation != ORIENTATION_UNDEFINED
343 && orientation != delta.orientation) {
344 changed |= ActivityInfo.CONFIG_ORIENTATION;
345 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700346 if (delta.screenLayout != SCREENLAYOUT_UNDEFINED
347 && screenLayout != delta.screenLayout) {
348 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 return changed;
352 }
353
354 /**
355 * Determine if a new resource needs to be loaded from the bit set of
356 * configuration changes returned by {@link #updateFrom(Configuration)}.
357 *
358 * @param configChanges The mask of changes configurations as returned by
359 * {@link #updateFrom(Configuration)}.
360 * @param interestingChanges The configuration changes that the resource
361 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
362 *
363 * @return Return true if the resource needs to be loaded, else false.
364 */
365 public static boolean needNewResources(int configChanges, int interestingChanges) {
366 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
367 }
368
369 /**
370 * Parcelable methods
371 */
372 public int describeContents() {
373 return 0;
374 }
375
376 public void writeToParcel(Parcel dest, int flags) {
377 dest.writeFloat(fontScale);
378 dest.writeInt(mcc);
379 dest.writeInt(mnc);
380 if (locale == null) {
381 dest.writeInt(0);
382 } else {
383 dest.writeInt(1);
384 dest.writeString(locale.getLanguage());
385 dest.writeString(locale.getCountry());
386 dest.writeString(locale.getVariant());
387 }
388 if(userSetLocale) {
389 dest.writeInt(1);
390 } else {
391 dest.writeInt(0);
392 }
393 dest.writeInt(touchscreen);
394 dest.writeInt(keyboard);
395 dest.writeInt(keyboardHidden);
396 dest.writeInt(hardKeyboardHidden);
397 dest.writeInt(navigation);
398 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700399 dest.writeInt(screenLayout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
401
402 public static final Parcelable.Creator<Configuration> CREATOR
403 = new Parcelable.Creator<Configuration>() {
404 public Configuration createFromParcel(Parcel source) {
405 return new Configuration(source);
406 }
407
408 public Configuration[] newArray(int size) {
409 return new Configuration[size];
410 }
411 };
412
413 /**
414 * Construct this Configuration object, reading from the Parcel.
415 */
416 private Configuration(Parcel source) {
417 fontScale = source.readFloat();
418 mcc = source.readInt();
419 mnc = source.readInt();
420 if (source.readInt() != 0) {
421 locale = new Locale(source.readString(), source.readString(),
422 source.readString());
423 }
424 userSetLocale = (source.readInt()==1);
425 touchscreen = source.readInt();
426 keyboard = source.readInt();
427 keyboardHidden = source.readInt();
428 hardKeyboardHidden = source.readInt();
429 navigation = source.readInt();
430 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700431 screenLayout = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433
434 public int compareTo(Configuration that) {
435 int n;
436 float a = this.fontScale;
437 float b = that.fontScale;
438 if (a < b) return -1;
439 if (a > b) return 1;
440 n = this.mcc - that.mcc;
441 if (n != 0) return n;
442 n = this.mnc - that.mnc;
443 if (n != 0) return n;
444 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
445 if (n != 0) return n;
446 n = this.locale.getCountry().compareTo(that.locale.getCountry());
447 if (n != 0) return n;
448 n = this.locale.getVariant().compareTo(that.locale.getVariant());
449 if (n != 0) return n;
450 n = this.touchscreen - that.touchscreen;
451 if (n != 0) return n;
452 n = this.keyboard - that.keyboard;
453 if (n != 0) return n;
454 n = this.keyboardHidden - that.keyboardHidden;
455 if (n != 0) return n;
456 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
457 if (n != 0) return n;
458 n = this.navigation - that.navigation;
459 if (n != 0) return n;
460 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700461 if (n != 0) return n;
462 n = this.screenLayout - that.screenLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 //if (n != 0) return n;
464 return n;
465 }
466
467 public boolean equals(Configuration that) {
468 if (that == null) return false;
469 if (that == this) return true;
470 return this.compareTo(that) == 0;
471 }
472
473 public boolean equals(Object that) {
474 try {
475 return equals((Configuration)that);
476 } catch (ClassCastException e) {
477 }
478 return false;
479 }
480
481 public int hashCode() {
482 return ((int)this.fontScale) + this.mcc + this.mnc
483 + this.locale.hashCode() + this.touchscreen
484 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn723738c2009-06-25 19:48:04 -0700485 + this.navigation + this.orientation + this.screenLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487}