blob: 956b15a788285c28637486ade8f7a265994911da [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
119 /**
120 * Construct an invalid Configuration. You must call {@link #setToDefaults}
121 * for this object to be valid. {@more}
122 */
123 public Configuration() {
124 setToDefaults();
125 }
126
127 /**
128 * Makes a deep copy suitable for modification.
129 */
130 public Configuration(Configuration o) {
131 fontScale = o.fontScale;
132 mcc = o.mcc;
133 mnc = o.mnc;
134 if (o.locale != null) {
135 locale = (Locale) o.locale.clone();
136 }
137 userSetLocale = o.userSetLocale;
138 touchscreen = o.touchscreen;
139 keyboard = o.keyboard;
140 keyboardHidden = o.keyboardHidden;
141 hardKeyboardHidden = o.hardKeyboardHidden;
142 navigation = o.navigation;
143 orientation = o.orientation;
144 }
145
146 public String toString() {
147 return "{ scale=" + fontScale + " imsi=" + mcc + "/" + mnc
148 + " locale=" + locale
149 + " touch=" + touchscreen + " key=" + keyboard + "/"
150 + keyboardHidden + "/" + hardKeyboardHidden
151 + " nav=" + navigation + " orien=" + orientation + " }";
152 }
153
154 /**
155 * Set this object to the system defaults.
156 */
157 public void setToDefaults() {
158 fontScale = 1;
159 mcc = mnc = 0;
160 locale = Locale.getDefault();
161 userSetLocale = false;
162 touchscreen = TOUCHSCREEN_UNDEFINED;
163 keyboard = KEYBOARD_UNDEFINED;
164 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
165 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
166 navigation = NAVIGATION_UNDEFINED;
167 orientation = ORIENTATION_UNDEFINED;
168 }
169
170 /** {@hide} */
171 @Deprecated public void makeDefault() {
172 setToDefaults();
173 }
174
175 /**
176 * Copy the fields from delta into this Configuration object, keeping
177 * track of which ones have changed. Any undefined fields in
178 * <var>delta</var> are ignored and not copied in to the current
179 * Configuration.
180 * @return Returns a bit mask of the changed fields, as per
181 * {@link #diff}.
182 */
183 public int updateFrom(Configuration delta) {
184 int changed = 0;
185 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
186 changed |= ActivityInfo.CONFIG_FONT_SCALE;
187 fontScale = delta.fontScale;
188 }
189 if (delta.mcc != 0 && mcc != delta.mcc) {
190 changed |= ActivityInfo.CONFIG_MCC;
191 mcc = delta.mcc;
192 }
193 if (delta.mnc != 0 && mnc != delta.mnc) {
194 changed |= ActivityInfo.CONFIG_MNC;
195 mnc = delta.mnc;
196 }
197 if (delta.locale != null
198 && (locale == null || !locale.equals(delta.locale))) {
199 changed |= ActivityInfo.CONFIG_LOCALE;
200 locale = delta.locale != null
201 ? (Locale) delta.locale.clone() : null;
202 }
203 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
204 {
205 userSetLocale = true;
206 changed |= ActivityInfo.CONFIG_LOCALE;
207 }
208 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
209 && touchscreen != delta.touchscreen) {
210 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
211 touchscreen = delta.touchscreen;
212 }
213 if (delta.keyboard != KEYBOARD_UNDEFINED
214 && keyboard != delta.keyboard) {
215 changed |= ActivityInfo.CONFIG_KEYBOARD;
216 keyboard = delta.keyboard;
217 }
218 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
219 && keyboardHidden != delta.keyboardHidden) {
220 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
221 keyboardHidden = delta.keyboardHidden;
222 }
223 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
224 && hardKeyboardHidden != delta.hardKeyboardHidden) {
225 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
226 hardKeyboardHidden = delta.hardKeyboardHidden;
227 }
228 if (delta.navigation != NAVIGATION_UNDEFINED
229 && navigation != delta.navigation) {
230 changed |= ActivityInfo.CONFIG_NAVIGATION;
231 navigation = delta.navigation;
232 }
233 if (delta.orientation != ORIENTATION_UNDEFINED
234 && orientation != delta.orientation) {
235 changed |= ActivityInfo.CONFIG_ORIENTATION;
236 orientation = delta.orientation;
237 }
238
239 return changed;
240 }
241
242 /**
243 * Return a bit mask of the differences between this Configuration
244 * object and the given one. Does not change the values of either. Any
245 * undefined fields in <var>delta</var> are ignored.
246 * @return Returns a bit mask indicating which configuration
247 * values has changed, containing any combination of
248 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
249 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
250 * {@link android.content.pm.ActivityInfo#CONFIG_MCC
251 * PackageManager.ActivityInfo.CONFIG_MCC},
252 * {@link android.content.pm.ActivityInfo#CONFIG_MNC
253 * PackageManager.ActivityInfo.CONFIG_MNC},
254 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
255 * PackageManager.ActivityInfo.CONFIG_LOCALE},
256 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
257 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
258 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
259 * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
260 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
261 * PackageManager.ActivityInfo.CONFIG_NAVIGATION}, or
262 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
263 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}.
264 */
265 public int diff(Configuration delta) {
266 int changed = 0;
267 if (delta.fontScale > 0 && fontScale != delta.fontScale) {
268 changed |= ActivityInfo.CONFIG_FONT_SCALE;
269 }
270 if (delta.mcc != 0 && mcc != delta.mcc) {
271 changed |= ActivityInfo.CONFIG_MCC;
272 }
273 if (delta.mnc != 0 && mnc != delta.mnc) {
274 changed |= ActivityInfo.CONFIG_MNC;
275 }
276 if (delta.locale != null
277 && (locale == null || !locale.equals(delta.locale))) {
278 changed |= ActivityInfo.CONFIG_LOCALE;
279 }
280 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
281 && touchscreen != delta.touchscreen) {
282 changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
283 }
284 if (delta.keyboard != KEYBOARD_UNDEFINED
285 && keyboard != delta.keyboard) {
286 changed |= ActivityInfo.CONFIG_KEYBOARD;
287 }
288 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
289 && keyboardHidden != delta.keyboardHidden) {
290 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
291 }
292 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
293 && hardKeyboardHidden != delta.hardKeyboardHidden) {
294 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
295 }
296 if (delta.navigation != NAVIGATION_UNDEFINED
297 && navigation != delta.navigation) {
298 changed |= ActivityInfo.CONFIG_NAVIGATION;
299 }
300 if (delta.orientation != ORIENTATION_UNDEFINED
301 && orientation != delta.orientation) {
302 changed |= ActivityInfo.CONFIG_ORIENTATION;
303 }
304
305 return changed;
306 }
307
308 /**
309 * Determine if a new resource needs to be loaded from the bit set of
310 * configuration changes returned by {@link #updateFrom(Configuration)}.
311 *
312 * @param configChanges The mask of changes configurations as returned by
313 * {@link #updateFrom(Configuration)}.
314 * @param interestingChanges The configuration changes that the resource
315 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
316 *
317 * @return Return true if the resource needs to be loaded, else false.
318 */
319 public static boolean needNewResources(int configChanges, int interestingChanges) {
320 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
321 }
322
323 /**
324 * Parcelable methods
325 */
326 public int describeContents() {
327 return 0;
328 }
329
330 public void writeToParcel(Parcel dest, int flags) {
331 dest.writeFloat(fontScale);
332 dest.writeInt(mcc);
333 dest.writeInt(mnc);
334 if (locale == null) {
335 dest.writeInt(0);
336 } else {
337 dest.writeInt(1);
338 dest.writeString(locale.getLanguage());
339 dest.writeString(locale.getCountry());
340 dest.writeString(locale.getVariant());
341 }
342 if(userSetLocale) {
343 dest.writeInt(1);
344 } else {
345 dest.writeInt(0);
346 }
347 dest.writeInt(touchscreen);
348 dest.writeInt(keyboard);
349 dest.writeInt(keyboardHidden);
350 dest.writeInt(hardKeyboardHidden);
351 dest.writeInt(navigation);
352 dest.writeInt(orientation);
353 }
354
355 public static final Parcelable.Creator<Configuration> CREATOR
356 = new Parcelable.Creator<Configuration>() {
357 public Configuration createFromParcel(Parcel source) {
358 return new Configuration(source);
359 }
360
361 public Configuration[] newArray(int size) {
362 return new Configuration[size];
363 }
364 };
365
366 /**
367 * Construct this Configuration object, reading from the Parcel.
368 */
369 private Configuration(Parcel source) {
370 fontScale = source.readFloat();
371 mcc = source.readInt();
372 mnc = source.readInt();
373 if (source.readInt() != 0) {
374 locale = new Locale(source.readString(), source.readString(),
375 source.readString());
376 }
377 userSetLocale = (source.readInt()==1);
378 touchscreen = source.readInt();
379 keyboard = source.readInt();
380 keyboardHidden = source.readInt();
381 hardKeyboardHidden = source.readInt();
382 navigation = source.readInt();
383 orientation = source.readInt();
384 }
385
386 public int compareTo(Configuration that) {
387 int n;
388 float a = this.fontScale;
389 float b = that.fontScale;
390 if (a < b) return -1;
391 if (a > b) return 1;
392 n = this.mcc - that.mcc;
393 if (n != 0) return n;
394 n = this.mnc - that.mnc;
395 if (n != 0) return n;
396 n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
397 if (n != 0) return n;
398 n = this.locale.getCountry().compareTo(that.locale.getCountry());
399 if (n != 0) return n;
400 n = this.locale.getVariant().compareTo(that.locale.getVariant());
401 if (n != 0) return n;
402 n = this.touchscreen - that.touchscreen;
403 if (n != 0) return n;
404 n = this.keyboard - that.keyboard;
405 if (n != 0) return n;
406 n = this.keyboardHidden - that.keyboardHidden;
407 if (n != 0) return n;
408 n = this.hardKeyboardHidden - that.hardKeyboardHidden;
409 if (n != 0) return n;
410 n = this.navigation - that.navigation;
411 if (n != 0) return n;
412 n = this.orientation - that.orientation;
413 //if (n != 0) return n;
414 return n;
415 }
416
417 public boolean equals(Configuration that) {
418 if (that == null) return false;
419 if (that == this) return true;
420 return this.compareTo(that) == 0;
421 }
422
423 public boolean equals(Object that) {
424 try {
425 return equals((Configuration)that);
426 } catch (ClassCastException e) {
427 }
428 return false;
429 }
430
431 public int hashCode() {
432 return ((int)this.fontScale) + this.mcc + this.mnc
433 + this.locale.hashCode() + this.touchscreen
434 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden
435 + this.navigation + this.orientation;
436 }
437}