blob: 577aa600a19e1e576a004ee30f274bc589d75190 [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.
63 * One of: {@link #KEYBOARD_QWERTY}, {@link #KEYBOARD_12KEY}.
64 */
65 public int keyboard;
66
67 public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
68 public static final int KEYBOARDHIDDEN_NO = 1;
69 public static final int KEYBOARDHIDDEN_YES = 2;
70 /** Constant matching actual resource implementation. {@hide} */
71 public static final int KEYBOARDHIDDEN_SOFT = 3;
72
73 /**
74 * A flag indicating whether any keyboard is available. Unlike
75 * {@link #hardKeyboardHidden}, this also takes into account a soft
76 * keyboard, so if the hard keyboard is hidden but there is soft
77 * keyboard available, it will be set to NO. Value is one of:
78 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
79 */
80 public int keyboardHidden;
81
82 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
83 public static final int HARDKEYBOARDHIDDEN_NO = 1;
84 public static final int HARDKEYBOARDHIDDEN_YES = 2;
85
86 /**
87 * A flag indicating whether the hard keyboard has been hidden. This will
88 * be set on a device with a mechanism to hide the keyboard from the
89 * user, when that mechanism is closed. One of:
90 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
91 */
92 public int hardKeyboardHidden;
93
94 public static final int NAVIGATION_UNDEFINED = 0;
95 public static final int NAVIGATION_NONAV = 1;
96 public static final int NAVIGATION_DPAD = 2;
97 public static final int NAVIGATION_TRACKBALL = 3;
98 public static final int NAVIGATION_WHEEL = 4;
99
100 /**
101 * The kind of navigation method available on the device.
102 * One of: {@link #NAVIGATION_DPAD}, {@link #NAVIGATION_TRACKBALL},
103 * {@link #NAVIGATION_WHEEL}.
104 */
105 public int navigation;
106
107 public static final int ORIENTATION_UNDEFINED = 0;
108 public static final int ORIENTATION_PORTRAIT = 1;
109 public static final int ORIENTATION_LANDSCAPE = 2;
110 public static final int ORIENTATION_SQUARE = 3;
111
112 /**
113 * Overall orientation of the screen. May be one of
114 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
115 * or {@link #ORIENTATION_SQUARE}.
116 */
117 public int orientation;
118
Dianne Hackborn723738c2009-06-25 19:48:04 -0700119 public static final int SCREENLAYOUT_UNDEFINED = 0;
120 public static final int SCREENLAYOUT_SMALL = 1;
121 public static final int SCREENLAYOUT_NORMAL = 2;
122 public static final int SCREENLAYOUT_LARGE = 3;
123
124 /**
125 * Overall layout of the screen. May be one of
126 * {@link #SCREENLAYOUT_SMALL}, {@link #SCREENLAYOUT_NORMAL},
127 * or {@link #SCREENLAYOUT_LARGE}.
128 */
129 public int screenLayout;
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 /**
132 * Construct an invalid Configuration. You must call {@link #setToDefaults}
133 * for this object to be valid. {@more}
134 */
135 public Configuration() {
136 setToDefaults();
137 }
138
139 /**
140 * Makes a deep copy suitable for modification.
141 */
142 public Configuration(Configuration o) {
143 fontScale = o.fontScale;
144 mcc = o.mcc;
145 mnc = o.mnc;
146 if (o.locale != null) {
147 locale = (Locale) o.locale.clone();
148 }
149 userSetLocale = o.userSetLocale;
150 touchscreen = o.touchscreen;
151 keyboard = o.keyboard;
152 keyboardHidden = o.keyboardHidden;
153 hardKeyboardHidden = o.hardKeyboardHidden;
154 navigation = o.navigation;
155 orientation = o.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700156 screenLayout = o.screenLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158
159 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700160 StringBuilder sb = new StringBuilder(128);
161 sb.append("{ scale=");
162 sb.append(fontScale);
163 sb.append(" imsi=");
164 sb.append(mcc);
165 sb.append("/");
166 sb.append(mnc);
167 sb.append(" loc=");
168 sb.append(locale);
169 sb.append(" touch=");
170 sb.append(touchscreen);
171 sb.append(" keys=");
172 sb.append(keyboard);
173 sb.append("/");
174 sb.append(keyboardHidden);
175 sb.append("/");
176 sb.append(hardKeyboardHidden);
177 sb.append(" nav=");
178 sb.append(navigation);
179 sb.append(" orien=");
180 sb.append(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700181 sb.append(" layout=");
182 sb.append(screenLayout);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700183 sb.append('}');
184 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
186
187 /**
188 * Set this object to the system defaults.
189 */
190 public void setToDefaults() {
191 fontScale = 1;
192 mcc = mnc = 0;
193 locale = Locale.getDefault();
194 userSetLocale = false;
195 touchscreen = TOUCHSCREEN_UNDEFINED;
196 keyboard = KEYBOARD_UNDEFINED;
197 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
198 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
199 navigation = NAVIGATION_UNDEFINED;
200 orientation = ORIENTATION_UNDEFINED;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700201 screenLayout = SCREENLAYOUT_UNDEFINED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
203
204 /** {@hide} */
205 @Deprecated public void makeDefault() {
206 setToDefaults();
207 }
208
209 /**
210 * Copy the fields from delta into this Configuration object, keeping
211 * track of which ones have changed. Any undefined fields in
212 * <var>delta</var> are ignored and not copied in to the current
213 * Configuration.
214 * @return Returns a bit mask of the changed fields, as per
215 * {@link #diff}.
216 */
217 public int updateFrom(Configuration delta) {
218 int changed = 0;
219 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
220 changed |= ActivityInfo.CONFIG_FONT_SCALE;
221 fontScale = delta.fontScale;
222 }
223 if (delta.mcc != 0 && mcc != delta.mcc) {
224 changed |= ActivityInfo.CONFIG_MCC;
225 mcc = delta.mcc;
226 }
227 if (delta.mnc != 0 && mnc != delta.mnc) {
228 changed |= ActivityInfo.CONFIG_MNC;
229 mnc = delta.mnc;
230 }
231 if (delta.locale != null
232 && (locale == null || !locale.equals(delta.locale))) {
233 changed |= ActivityInfo.CONFIG_LOCALE;
234 locale = delta.locale != null
235 ? (Locale) delta.locale.clone() : null;
236 }
237 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
238 {
239 userSetLocale = true;
240 changed |= ActivityInfo.CONFIG_LOCALE;
241 }
242 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
243 && touchscreen != delta.touchscreen) {
244 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
245 touchscreen = delta.touchscreen;
246 }
247 if (delta.keyboard != KEYBOARD_UNDEFINED
248 && keyboard != delta.keyboard) {
249 changed |= ActivityInfo.CONFIG_KEYBOARD;
250 keyboard = delta.keyboard;
251 }
252 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
253 && keyboardHidden != delta.keyboardHidden) {
254 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
255 keyboardHidden = delta.keyboardHidden;
256 }
257 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
258 && hardKeyboardHidden != delta.hardKeyboardHidden) {
259 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
260 hardKeyboardHidden = delta.hardKeyboardHidden;
261 }
262 if (delta.navigation != NAVIGATION_UNDEFINED
263 && navigation != delta.navigation) {
264 changed |= ActivityInfo.CONFIG_NAVIGATION;
265 navigation = delta.navigation;
266 }
267 if (delta.orientation != ORIENTATION_UNDEFINED
268 && orientation != delta.orientation) {
269 changed |= ActivityInfo.CONFIG_ORIENTATION;
270 orientation = delta.orientation;
271 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700272 if (delta.screenLayout != SCREENLAYOUT_UNDEFINED
273 && screenLayout != delta.screenLayout) {
274 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
275 screenLayout = delta.screenLayout;
276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
278 return changed;
279 }
280
281 /**
282 * Return a bit mask of the differences between this Configuration
283 * object and the given one. Does not change the values of either. Any
284 * undefined fields in <var>delta</var> are ignored.
285 * @return Returns a bit mask indicating which configuration
286 * values has changed, containing any combination of
287 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
288 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
289 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
290 * PackageManager.ActivityInfo.CONFIG_MCC},
291 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
292 * PackageManager.ActivityInfo.CONFIG_MNC},
293 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
294 * PackageManager.ActivityInfo.CONFIG_LOCALE},
295 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
296 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
297 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
298 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
299 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700300 * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
Dianne Hackborn723738c2009-06-25 19:48:04 -0700302 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
303 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
304 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
306 public int diff(Configuration delta) {
307 int changed = 0;
308 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
309 changed |= ActivityInfo.CONFIG_FONT_SCALE;
310 }
311 if (delta.mcc != 0 && mcc != delta.mcc) {
312 changed |= ActivityInfo.CONFIG_MCC;
313 }
314 if (delta.mnc != 0 && mnc != delta.mnc) {
315 changed |= ActivityInfo.CONFIG_MNC;
316 }
317 if (delta.locale != null
318 && (locale == null || !locale.equals(delta.locale))) {
319 changed |= ActivityInfo.CONFIG_LOCALE;
320 }
321 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
322 && touchscreen != delta.touchscreen) {
323 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
324 }
325 if (delta.keyboard != KEYBOARD_UNDEFINED
326 && keyboard != delta.keyboard) {
327 changed |= ActivityInfo.CONFIG_KEYBOARD;
328 }
329 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
330 && keyboardHidden != delta.keyboardHidden) {
331 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
332 }
333 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
334 && hardKeyboardHidden != delta.hardKeyboardHidden) {
335 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
336 }
337 if (delta.navigation != NAVIGATION_UNDEFINED
338 && navigation != delta.navigation) {
339 changed |= ActivityInfo.CONFIG_NAVIGATION;
340 }
341 if (delta.orientation != ORIENTATION_UNDEFINED
342 && orientation != delta.orientation) {
343 changed |= ActivityInfo.CONFIG_ORIENTATION;
344 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700345 if (delta.screenLayout != SCREENLAYOUT_UNDEFINED
346 && screenLayout != delta.screenLayout) {
347 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349
350 return changed;
351 }
352
353 /**
354 * Determine if a new resource needs to be loaded from the bit set of
355 * configuration changes returned by {@link #updateFrom(Configuration)}.
356 *
357 * @param configChanges The mask of changes configurations as returned by
358 * {@link #updateFrom(Configuration)}.
359 * @param interestingChanges The configuration changes that the resource
360 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
361 *
362 * @return Return true if the resource needs to be loaded, else false.
363 */
364 public static boolean needNewResources(int configChanges, int interestingChanges) {
365 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
366 }
367
368 /**
369 * Parcelable methods
370 */
371 public int describeContents() {
372 return 0;
373 }
374
375 public void writeToParcel(Parcel dest, int flags) {
376 dest.writeFloat(fontScale);
377 dest.writeInt(mcc);
378 dest.writeInt(mnc);
379 if (locale == null) {
380 dest.writeInt(0);
381 } else {
382 dest.writeInt(1);
383 dest.writeString(locale.getLanguage());
384 dest.writeString(locale.getCountry());
385 dest.writeString(locale.getVariant());
386 }
387 if(userSetLocale) {
388 dest.writeInt(1);
389 } else {
390 dest.writeInt(0);
391 }
392 dest.writeInt(touchscreen);
393 dest.writeInt(keyboard);
394 dest.writeInt(keyboardHidden);
395 dest.writeInt(hardKeyboardHidden);
396 dest.writeInt(navigation);
397 dest.writeInt(orientation);
Dianne Hackborn723738c2009-06-25 19:48:04 -0700398 dest.writeInt(screenLayout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400
401 public static final Parcelable.Creator<Configuration> CREATOR
402 = new Parcelable.Creator<Configuration>() {
403 public Configuration createFromParcel(Parcel source) {
404 return new Configuration(source);
405 }
406
407 public Configuration[] newArray(int size) {
408 return new Configuration[size];
409 }
410 };
411
412 /**
413 * Construct this Configuration object, reading from the Parcel.
414 */
415 private Configuration(Parcel source) {
416 fontScale = source.readFloat();
417 mcc = source.readInt();
418 mnc = source.readInt();
419 if (source.readInt() != 0) {
420 locale = new Locale(source.readString(), source.readString(),
421 source.readString());
422 }
423 userSetLocale = (source.readInt()==1);
424 touchscreen = source.readInt();
425 keyboard = source.readInt();
426 keyboardHidden = source.readInt();
427 hardKeyboardHidden = source.readInt();
428 navigation = source.readInt();
429 orientation = source.readInt();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700430 screenLayout = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432
433 public int compareTo(Configuration that) {
434 int n;
435 float a = this.fontScale;
436 float b = that.fontScale;
437 if (a < b) return -1;
438 if (a > b) return 1;
439 n = this.mcc - that.mcc;
440 if (n != 0) return n;
441 n = this.mnc - that.mnc;
442 if (n != 0) return n;
443 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
444 if (n != 0) return n;
445 n = this.locale.getCountry().compareTo(that.locale.getCountry());
446 if (n != 0) return n;
447 n = this.locale.getVariant().compareTo(that.locale.getVariant());
448 if (n != 0) return n;
449 n = this.touchscreen - that.touchscreen;
450 if (n != 0) return n;
451 n = this.keyboard - that.keyboard;
452 if (n != 0) return n;
453 n = this.keyboardHidden - that.keyboardHidden;
454 if (n != 0) return n;
455 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
456 if (n != 0) return n;
457 n = this.navigation - that.navigation;
458 if (n != 0) return n;
459 n = this.orientation - that.orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700460 if (n != 0) return n;
461 n = this.screenLayout - that.screenLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 //if (n != 0) return n;
463 return n;
464 }
465
466 public boolean equals(Configuration that) {
467 if (that == null) return false;
468 if (that == this) return true;
469 return this.compareTo(that) == 0;
470 }
471
472 public boolean equals(Object that) {
473 try {
474 return equals((Configuration)that);
475 } catch (ClassCastException e) {
476 }
477 return false;
478 }
479
480 public int hashCode() {
481 return ((int)this.fontScale) + this.mcc + this.mnc
482 + this.locale.hashCode() + this.touchscreen
483 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
Dianne Hackborn723738c2009-06-25 19:48:04 -0700484 + this.navigation + this.orientation + this.screenLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
486}