Configuration.java revision 5fd2169eabd77e6bfafaf456e58051a3bafb2bca
1/*
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
17package 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
29 * 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>
34 */
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    /**
58     * 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
61     */
62    public boolean userSetLocale;
63
64    /** Constant for {@link #screenLayout}: bits that encode the size. */
65    public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
66    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
67     * value indicating that no size has been set. */
68    public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
69    /** 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. */
73    public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
74    /** 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. */
78    public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
79    /** 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. */
83    public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
84    /** 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.*/
88    public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
89
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},
109     * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
110     *
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}.
114     *
115     * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
116     * Multiple Screens</a> for more information.
117     */
118    public int screenLayout;
119
120    /**
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;
133        return cur >= size;
134    }
135
136    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.
155     * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
156     * {@link #KEYBOARD_12KEY}.
157     */
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.
195     * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
196     * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
197     */
198    public int navigation;
199
200    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
212    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;
223
224    public static final int UI_MODE_TYPE_MASK = 0x0f;
225    public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
226    public static final int UI_MODE_TYPE_NORMAL = 0x01;
227    public static final int UI_MODE_TYPE_DESK = 0x02;
228    public static final int UI_MODE_TYPE_CAR = 0x03;
229    public static final int UI_MODE_TYPE_TELEVISION = 0x04;
230
231    public static final int UI_MODE_NIGHT_MASK = 0x30;
232    public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
233    public static final int UI_MODE_NIGHT_NO = 0x10;
234    public static final int UI_MODE_NIGHT_YES = 0x20;
235
236    /**
237     * Bit mask of the ui mode.  Currently there are two fields:
238     * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
239     * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
240     * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
241     * or {@link #UI_MODE_TYPE_CAR}.
242     *
243     * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
244     * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
245     * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
246     */
247    public int uiMode;
248
249    public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
250
251    /**
252     * The current width of the available screen space, in dp units.
253     */
254    public int screenWidthDp;
255
256    public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0;
257
258    /**
259     * The current height of the available screen space, in dp units.
260     */
261    public int screenHeightDp;
262
263    public static final int SMALLEST_SCREEN_WIDTH_DP_UNDEFINED = 0;
264
265    /**
266     * The smallest screen size an application will see in normal operation.
267     * This is the smallest value of both screenWidthDp and screenHeightDp
268     * in both portrait and landscape.
269     */
270    public int smallestScreenWidthDp;
271
272    /** @hide Hack to get this information from WM to app running in compat mode. */
273    public int compatScreenWidthDp;
274    /** @hide Hack to get this information from WM to app running in compat mode. */
275    public int compatScreenHeightDp;
276    /** @hide Hack to get this information from WM to app running in compat mode. */
277    public int compatSmallestScreenWidthDp;
278
279    /**
280     * @hide Internal book-keeping.
281     */
282    public int seq;
283
284    /**
285     * Construct an invalid Configuration.  You must call {@link #setToDefaults}
286     * for this object to be valid.  {@more}
287     */
288    public Configuration() {
289        setToDefaults();
290    }
291
292    /**
293     * Makes a deep copy suitable for modification.
294     */
295    public Configuration(Configuration o) {
296        setTo(o);
297    }
298
299    public void setTo(Configuration o) {
300        fontScale = o.fontScale;
301        mcc = o.mcc;
302        mnc = o.mnc;
303        if (o.locale != null) {
304            locale = (Locale) o.locale.clone();
305        }
306        userSetLocale = o.userSetLocale;
307        touchscreen = o.touchscreen;
308        keyboard = o.keyboard;
309        keyboardHidden = o.keyboardHidden;
310        hardKeyboardHidden = o.hardKeyboardHidden;
311        navigation = o.navigation;
312        navigationHidden = o.navigationHidden;
313        orientation = o.orientation;
314        screenLayout = o.screenLayout;
315        uiMode = o.uiMode;
316        screenWidthDp = o.screenWidthDp;
317        screenHeightDp = o.screenHeightDp;
318        smallestScreenWidthDp = o.smallestScreenWidthDp;
319        compatScreenWidthDp = o.compatScreenWidthDp;
320        compatScreenHeightDp = o.compatScreenHeightDp;
321        compatSmallestScreenWidthDp = o.compatSmallestScreenWidthDp;
322        seq = o.seq;
323    }
324
325    public String toString() {
326        StringBuilder sb = new StringBuilder(128);
327        sb.append("{");
328        sb.append(fontScale);
329        sb.append(" ");
330        sb.append(mcc);
331        sb.append("mcc");
332        sb.append(mnc);
333        sb.append("mnc");
334        if (locale != null) {
335            sb.append(" ");
336            sb.append(locale);
337        } else {
338            sb.append(" (no locale)");
339        }
340        if (smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
341            sb.append(" sw"); sb.append(smallestScreenWidthDp); sb.append("dp");
342        } else {
343            sb.append(" ?swdp");
344        }
345        if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
346            sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
347        } else {
348            sb.append(" ?wdp");
349        }
350        if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
351            sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
352        } else {
353            sb.append(" ?hdp");
354        }
355        switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
356            case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
357            case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
358            case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
359            case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
360            case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
361            default: sb.append(" layoutSize=");
362                    sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
363        }
364        switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
365            case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
366            case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
367            case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
368            default: sb.append(" layoutLong=");
369                    sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
370        }
371        switch (orientation) {
372            case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
373            case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
374            case ORIENTATION_PORTRAIT: sb.append(" port"); break;
375            default: sb.append(" orien="); sb.append(orientation); break;
376        }
377        switch ((uiMode&UI_MODE_TYPE_MASK)) {
378            case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
379            case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
380            case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
381            case UI_MODE_TYPE_CAR: sb.append(" car"); break;
382            case UI_MODE_TYPE_TELEVISION: sb.append(" television"); break;
383            default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
384        }
385        switch ((uiMode&UI_MODE_NIGHT_MASK)) {
386            case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
387            case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
388            case UI_MODE_NIGHT_YES: sb.append(" night"); break;
389            default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
390        }
391        switch (touchscreen) {
392            case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
393            case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
394            case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
395            case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
396            default: sb.append(" touch="); sb.append(touchscreen); break;
397        }
398        switch (keyboard) {
399            case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
400            case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
401            case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
402            case KEYBOARD_12KEY: sb.append(" 12key"); break;
403            default: sb.append(" keys="); sb.append(keyboard); break;
404        }
405        switch (keyboardHidden) {
406            case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
407            case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
408            case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
409            case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
410            default: sb.append("/"); sb.append(keyboardHidden); break;
411        }
412        switch (hardKeyboardHidden) {
413            case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
414            case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
415            case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
416            default: sb.append("/"); sb.append(hardKeyboardHidden); break;
417        }
418        switch (navigation) {
419            case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
420            case NAVIGATION_NONAV: sb.append(" -nav"); break;
421            case NAVIGATION_DPAD: sb.append(" dpad"); break;
422            case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
423            case NAVIGATION_WHEEL: sb.append(" wheel"); break;
424            default: sb.append(" nav="); sb.append(navigation); break;
425        }
426        switch (navigationHidden) {
427            case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
428            case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
429            case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
430            default: sb.append("/"); sb.append(navigationHidden); break;
431        }
432        if (seq != 0) {
433            sb.append(" s.");
434            sb.append(seq);
435        }
436        sb.append('}');
437        return sb.toString();
438    }
439
440    /**
441     * Set this object to the system defaults.
442     */
443    public void setToDefaults() {
444        fontScale = 1;
445        mcc = mnc = 0;
446        locale = null;
447        userSetLocale = false;
448        touchscreen = TOUCHSCREEN_UNDEFINED;
449        keyboard = KEYBOARD_UNDEFINED;
450        keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
451        hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
452        navigation = NAVIGATION_UNDEFINED;
453        navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
454        orientation = ORIENTATION_UNDEFINED;
455        screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
456        uiMode = UI_MODE_TYPE_UNDEFINED;
457        screenWidthDp = compatScreenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
458        screenHeightDp = compatScreenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
459        smallestScreenWidthDp = compatSmallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
460        seq = 0;
461    }
462
463    /** {@hide} */
464    @Deprecated public void makeDefault() {
465        setToDefaults();
466    }
467
468    /**
469     * Copy the fields from delta into this Configuration object, keeping
470     * track of which ones have changed.  Any undefined fields in
471     * <var>delta</var> are ignored and not copied in to the current
472     * Configuration.
473     * @return Returns a bit mask of the changed fields, as per
474     * {@link #diff}.
475     */
476    public int updateFrom(Configuration delta) {
477        int changed = 0;
478        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
479            changed |= ActivityInfo.CONFIG_FONT_SCALE;
480            fontScale = delta.fontScale;
481        }
482        if (delta.mcc != 0 && mcc != delta.mcc) {
483            changed |= ActivityInfo.CONFIG_MCC;
484            mcc = delta.mcc;
485        }
486        if (delta.mnc != 0 && mnc != delta.mnc) {
487            changed |= ActivityInfo.CONFIG_MNC;
488            mnc = delta.mnc;
489        }
490        if (delta.locale != null
491                && (locale == null || !locale.equals(delta.locale))) {
492            changed |= ActivityInfo.CONFIG_LOCALE;
493            locale = delta.locale != null
494                    ? (Locale) delta.locale.clone() : null;
495        }
496        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
497        {
498            userSetLocale = true;
499            changed |= ActivityInfo.CONFIG_LOCALE;
500        }
501        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
502                && touchscreen != delta.touchscreen) {
503            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
504            touchscreen = delta.touchscreen;
505        }
506        if (delta.keyboard != KEYBOARD_UNDEFINED
507                && keyboard != delta.keyboard) {
508            changed |= ActivityInfo.CONFIG_KEYBOARD;
509            keyboard = delta.keyboard;
510        }
511        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
512                && keyboardHidden != delta.keyboardHidden) {
513            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
514            keyboardHidden = delta.keyboardHidden;
515        }
516        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
517                && hardKeyboardHidden != delta.hardKeyboardHidden) {
518            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
519            hardKeyboardHidden = delta.hardKeyboardHidden;
520        }
521        if (delta.navigation != NAVIGATION_UNDEFINED
522                && navigation != delta.navigation) {
523            changed |= ActivityInfo.CONFIG_NAVIGATION;
524            navigation = delta.navigation;
525        }
526        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
527                && navigationHidden != delta.navigationHidden) {
528            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
529            navigationHidden = delta.navigationHidden;
530        }
531        if (delta.orientation != ORIENTATION_UNDEFINED
532                && orientation != delta.orientation) {
533            changed |= ActivityInfo.CONFIG_ORIENTATION;
534            orientation = delta.orientation;
535        }
536        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
537                && screenLayout != delta.screenLayout) {
538            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
539            screenLayout = delta.screenLayout;
540        }
541        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
542                && uiMode != delta.uiMode) {
543            changed |= ActivityInfo.CONFIG_UI_MODE;
544            if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
545                uiMode = (uiMode&~UI_MODE_TYPE_MASK)
546                        | (delta.uiMode&UI_MODE_TYPE_MASK);
547            }
548            if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
549                uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
550                        | (delta.uiMode&UI_MODE_NIGHT_MASK);
551            }
552        }
553        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
554                && screenWidthDp != delta.screenWidthDp) {
555            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
556            screenWidthDp = delta.screenWidthDp;
557        }
558        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
559                && screenHeightDp != delta.screenHeightDp) {
560            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
561            screenHeightDp = delta.screenHeightDp;
562        }
563        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
564            smallestScreenWidthDp = delta.smallestScreenWidthDp;
565        }
566        if (delta.compatScreenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
567            compatScreenWidthDp = delta.compatScreenWidthDp;
568        }
569        if (delta.compatScreenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
570            compatScreenHeightDp = delta.compatScreenHeightDp;
571        }
572        if (delta.compatSmallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
573            compatSmallestScreenWidthDp = delta.compatSmallestScreenWidthDp;
574        }
575
576        if (delta.seq != 0) {
577            seq = delta.seq;
578        }
579
580        return changed;
581    }
582
583    /**
584     * Return a bit mask of the differences between this Configuration
585     * object and the given one.  Does not change the values of either.  Any
586     * undefined fields in <var>delta</var> are ignored.
587     * @return Returns a bit mask indicating which configuration
588     * values has changed, containing any combination of
589     * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
590     * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
591     * {@link android.content.pm.ActivityInfo#CONFIG_MCC
592     * PackageManager.ActivityInfo.CONFIG_MCC},
593     * {@link android.content.pm.ActivityInfo#CONFIG_MNC
594     * PackageManager.ActivityInfo.CONFIG_MNC},
595     * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
596     * PackageManager.ActivityInfo.CONFIG_LOCALE},
597     * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
598     * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
599     * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
600     * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
601     * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
602     * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
603     * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
604     * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
605     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
606     * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
607     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
608     * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or
609     * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE
610     * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}.
611     */
612    public int diff(Configuration delta) {
613        int changed = 0;
614        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
615            changed |= ActivityInfo.CONFIG_FONT_SCALE;
616        }
617        if (delta.mcc != 0 && mcc != delta.mcc) {
618            changed |= ActivityInfo.CONFIG_MCC;
619        }
620        if (delta.mnc != 0 && mnc != delta.mnc) {
621            changed |= ActivityInfo.CONFIG_MNC;
622        }
623        if (delta.locale != null
624                && (locale == null || !locale.equals(delta.locale))) {
625            changed |= ActivityInfo.CONFIG_LOCALE;
626        }
627        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
628                && touchscreen != delta.touchscreen) {
629            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
630        }
631        if (delta.keyboard != KEYBOARD_UNDEFINED
632                && keyboard != delta.keyboard) {
633            changed |= ActivityInfo.CONFIG_KEYBOARD;
634        }
635        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
636                && keyboardHidden != delta.keyboardHidden) {
637            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
638        }
639        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
640                && hardKeyboardHidden != delta.hardKeyboardHidden) {
641            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
642        }
643        if (delta.navigation != NAVIGATION_UNDEFINED
644                && navigation != delta.navigation) {
645            changed |= ActivityInfo.CONFIG_NAVIGATION;
646        }
647        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
648                && navigationHidden != delta.navigationHidden) {
649            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
650        }
651        if (delta.orientation != ORIENTATION_UNDEFINED
652                && orientation != delta.orientation) {
653            changed |= ActivityInfo.CONFIG_ORIENTATION;
654        }
655        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
656                && screenLayout != delta.screenLayout) {
657            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
658        }
659        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
660                && uiMode != delta.uiMode) {
661            changed |= ActivityInfo.CONFIG_UI_MODE;
662        }
663        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
664                && screenWidthDp != delta.screenWidthDp) {
665            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
666        }
667        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
668                && screenHeightDp != delta.screenHeightDp) {
669            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
670        }
671        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
672                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
673            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
674        }
675
676        return changed;
677    }
678
679    /**
680     * Determine if a new resource needs to be loaded from the bit set of
681     * configuration changes returned by {@link #updateFrom(Configuration)}.
682     *
683     * @param configChanges The mask of changes configurations as returned by
684     * {@link #updateFrom(Configuration)}.
685     * @param interestingChanges The configuration changes that the resource
686     * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
687     *
688     * @return Return true if the resource needs to be loaded, else false.
689     */
690    public static boolean needNewResources(int configChanges, int interestingChanges) {
691        return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
692    }
693
694    /**
695     * @hide Return true if the sequence of 'other' is better than this.  Assumes
696     * that 'this' is your current sequence and 'other' is a new one you have
697     * received some how and want to compare with what you have.
698     */
699    public boolean isOtherSeqNewer(Configuration other) {
700        if (other == null) {
701            // Sanity check.
702            return false;
703        }
704        if (other.seq == 0) {
705            // If the other sequence is not specified, then we must assume
706            // it is newer since we don't know any better.
707            return true;
708        }
709        if (seq == 0) {
710            // If this sequence is not specified, then we also consider the
711            // other is better.  Yes we have a preference for other.  Sue us.
712            return true;
713        }
714        int diff = other.seq - seq;
715        if (diff > 0x10000) {
716            // If there has been a sufficiently large jump, assume the
717            // sequence has wrapped around.
718            return false;
719        }
720        return diff > 0;
721    }
722
723    /**
724     * Parcelable methods
725     */
726    public int describeContents() {
727        return 0;
728    }
729
730    public void writeToParcel(Parcel dest, int flags) {
731        dest.writeFloat(fontScale);
732        dest.writeInt(mcc);
733        dest.writeInt(mnc);
734        if (locale == null) {
735            dest.writeInt(0);
736        } else {
737            dest.writeInt(1);
738            dest.writeString(locale.getLanguage());
739            dest.writeString(locale.getCountry());
740            dest.writeString(locale.getVariant());
741        }
742        if(userSetLocale) {
743            dest.writeInt(1);
744        } else {
745            dest.writeInt(0);
746        }
747        dest.writeInt(touchscreen);
748        dest.writeInt(keyboard);
749        dest.writeInt(keyboardHidden);
750        dest.writeInt(hardKeyboardHidden);
751        dest.writeInt(navigation);
752        dest.writeInt(navigationHidden);
753        dest.writeInt(orientation);
754        dest.writeInt(screenLayout);
755        dest.writeInt(uiMode);
756        dest.writeInt(screenWidthDp);
757        dest.writeInt(screenHeightDp);
758        dest.writeInt(smallestScreenWidthDp);
759        dest.writeInt(compatScreenWidthDp);
760        dest.writeInt(compatScreenHeightDp);
761        dest.writeInt(compatSmallestScreenWidthDp);
762        dest.writeInt(seq);
763    }
764
765    public void readFromParcel(Parcel source) {
766        fontScale = source.readFloat();
767        mcc = source.readInt();
768        mnc = source.readInt();
769        if (source.readInt() != 0) {
770            locale = new Locale(source.readString(), source.readString(),
771                    source.readString());
772        }
773        userSetLocale = (source.readInt()==1);
774        touchscreen = source.readInt();
775        keyboard = source.readInt();
776        keyboardHidden = source.readInt();
777        hardKeyboardHidden = source.readInt();
778        navigation = source.readInt();
779        navigationHidden = source.readInt();
780        orientation = source.readInt();
781        screenLayout = source.readInt();
782        uiMode = source.readInt();
783        screenWidthDp = source.readInt();
784        screenHeightDp = source.readInt();
785        smallestScreenWidthDp = source.readInt();
786        compatScreenWidthDp = source.readInt();
787        compatScreenHeightDp = source.readInt();
788        compatSmallestScreenWidthDp = source.readInt();
789        seq = source.readInt();
790    }
791
792    public static final Parcelable.Creator<Configuration> CREATOR
793            = new Parcelable.Creator<Configuration>() {
794        public Configuration createFromParcel(Parcel source) {
795            return new Configuration(source);
796        }
797
798        public Configuration[] newArray(int size) {
799            return new Configuration[size];
800        }
801    };
802
803    /**
804     * Construct this Configuration object, reading from the Parcel.
805     */
806    private Configuration(Parcel source) {
807        readFromParcel(source);
808    }
809
810    public int compareTo(Configuration that) {
811        int n;
812        float a = this.fontScale;
813        float b = that.fontScale;
814        if (a < b) return -1;
815        if (a > b) return 1;
816        n = this.mcc - that.mcc;
817        if (n != 0) return n;
818        n = this.mnc - that.mnc;
819        if (n != 0) return n;
820        if (this.locale == null) {
821            if (that.locale != null) return 1;
822        } else if (that.locale == null) {
823            return -1;
824        } else {
825            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
826            if (n != 0) return n;
827            n = this.locale.getCountry().compareTo(that.locale.getCountry());
828            if (n != 0) return n;
829            n = this.locale.getVariant().compareTo(that.locale.getVariant());
830            if (n != 0) return n;
831        }
832        n = this.touchscreen - that.touchscreen;
833        if (n != 0) return n;
834        n = this.keyboard - that.keyboard;
835        if (n != 0) return n;
836        n = this.keyboardHidden - that.keyboardHidden;
837        if (n != 0) return n;
838        n = this.hardKeyboardHidden - that.hardKeyboardHidden;
839        if (n != 0) return n;
840        n = this.navigation - that.navigation;
841        if (n != 0) return n;
842        n = this.navigationHidden - that.navigationHidden;
843        if (n != 0) return n;
844        n = this.orientation - that.orientation;
845        if (n != 0) return n;
846        n = this.screenLayout - that.screenLayout;
847        if (n != 0) return n;
848        n = this.uiMode - that.uiMode;
849        if (n != 0) return n;
850        n = this.screenWidthDp - that.screenWidthDp;
851        if (n != 0) return n;
852        n = this.screenHeightDp - that.screenHeightDp;
853        if (n != 0) return n;
854        n = this.smallestScreenWidthDp - that.smallestScreenWidthDp;
855        //if (n != 0) return n;
856        return n;
857    }
858
859    public boolean equals(Configuration that) {
860        if (that == null) return false;
861        if (that == this) return true;
862        return this.compareTo(that) == 0;
863    }
864
865    public boolean equals(Object that) {
866        try {
867            return equals((Configuration)that);
868        } catch (ClassCastException e) {
869        }
870        return false;
871    }
872
873    public int hashCode() {
874        int result = 17;
875        result = 31 * result + Float.floatToIntBits(fontScale);
876        result = 31 * result + mcc;
877        result = 31 * result + mnc;
878        result = 31 * result + (locale != null ? locale.hashCode() : 0);
879        result = 31 * result + touchscreen;
880        result = 31 * result + keyboard;
881        result = 31 * result + keyboardHidden;
882        result = 31 * result + hardKeyboardHidden;
883        result = 31 * result + navigation;
884        result = 31 * result + navigationHidden;
885        result = 31 * result + orientation;
886        result = 31 * result + screenLayout;
887        result = 31 * result + uiMode;
888        result = 31 * result + screenWidthDp;
889        result = 31 * result + screenHeightDp;
890        result = 31 * result + smallestScreenWidthDp;
891        return result;
892    }
893}
894