Configuration.java revision 7a736fbf69dd6f03db968d7d8182024eebc0e508
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 Do not use. Implementation not finished.
281     */
282    public static final int TEXT_LAYOUT_DIRECTION_UNDEFINED_DO_NOT_USE = -1;
283
284    /**
285     * @hide Do not use. Implementation not finished.
286     */
287    public static final int TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE = 0;
288
289    /**
290     * @hide Do not use. Implementation not finished.
291     */
292    public static final int TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE = 1;
293
294    /**
295     * @hide The text layout direction associated to the current Locale
296     */
297    public int textLayoutDirection;
298
299    /**
300     * @hide Internal book-keeping.
301     */
302    public int seq;
303
304    /**
305     * Construct an invalid Configuration.  You must call {@link #setToDefaults}
306     * for this object to be valid.  {@more}
307     */
308    public Configuration() {
309        setToDefaults();
310    }
311
312    /**
313     * Makes a deep copy suitable for modification.
314     */
315    public Configuration(Configuration o) {
316        setTo(o);
317    }
318
319    public void setTo(Configuration o) {
320        fontScale = o.fontScale;
321        mcc = o.mcc;
322        mnc = o.mnc;
323        if (o.locale != null) {
324            locale = (Locale) o.locale.clone();
325            textLayoutDirection = o.textLayoutDirection;
326        }
327        userSetLocale = o.userSetLocale;
328        touchscreen = o.touchscreen;
329        keyboard = o.keyboard;
330        keyboardHidden = o.keyboardHidden;
331        hardKeyboardHidden = o.hardKeyboardHidden;
332        navigation = o.navigation;
333        navigationHidden = o.navigationHidden;
334        orientation = o.orientation;
335        screenLayout = o.screenLayout;
336        uiMode = o.uiMode;
337        screenWidthDp = o.screenWidthDp;
338        screenHeightDp = o.screenHeightDp;
339        smallestScreenWidthDp = o.smallestScreenWidthDp;
340        compatScreenWidthDp = o.compatScreenWidthDp;
341        compatScreenHeightDp = o.compatScreenHeightDp;
342        compatSmallestScreenWidthDp = o.compatSmallestScreenWidthDp;
343        seq = o.seq;
344    }
345
346    public String toString() {
347        StringBuilder sb = new StringBuilder(128);
348        sb.append("{");
349        sb.append(fontScale);
350        sb.append(" ");
351        sb.append(mcc);
352        sb.append("mcc");
353        sb.append(mnc);
354        sb.append("mnc");
355        if (locale != null) {
356            sb.append(" ");
357            sb.append(locale);
358        } else {
359            sb.append(" (no locale)");
360        }
361        switch (textLayoutDirection) {
362            case TEXT_LAYOUT_DIRECTION_UNDEFINED_DO_NOT_USE: sb.append(" ?layoutdir"); break;
363            case TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE: sb.append(" rtl"); break;
364            default: sb.append(" layoutdir="); sb.append(textLayoutDirection); break;
365        }
366        if (smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
367            sb.append(" sw"); sb.append(smallestScreenWidthDp); sb.append("dp");
368        } else {
369            sb.append(" ?swdp");
370        }
371        if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
372            sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
373        } else {
374            sb.append(" ?wdp");
375        }
376        if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
377            sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
378        } else {
379            sb.append(" ?hdp");
380        }
381        switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
382            case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
383            case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
384            case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
385            case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
386            case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
387            default: sb.append(" layoutSize=");
388                    sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
389        }
390        switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
391            case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
392            case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
393            case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
394            default: sb.append(" layoutLong=");
395                    sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
396        }
397        switch (orientation) {
398            case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
399            case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
400            case ORIENTATION_PORTRAIT: sb.append(" port"); break;
401            default: sb.append(" orien="); sb.append(orientation); break;
402        }
403        switch ((uiMode&UI_MODE_TYPE_MASK)) {
404            case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
405            case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
406            case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
407            case UI_MODE_TYPE_CAR: sb.append(" car"); break;
408            case UI_MODE_TYPE_TELEVISION: sb.append(" television"); break;
409            default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
410        }
411        switch ((uiMode&UI_MODE_NIGHT_MASK)) {
412            case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
413            case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
414            case UI_MODE_NIGHT_YES: sb.append(" night"); break;
415            default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
416        }
417        switch (touchscreen) {
418            case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
419            case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
420            case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
421            case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
422            default: sb.append(" touch="); sb.append(touchscreen); break;
423        }
424        switch (keyboard) {
425            case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
426            case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
427            case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
428            case KEYBOARD_12KEY: sb.append(" 12key"); break;
429            default: sb.append(" keys="); sb.append(keyboard); break;
430        }
431        switch (keyboardHidden) {
432            case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
433            case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
434            case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
435            case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
436            default: sb.append("/"); sb.append(keyboardHidden); break;
437        }
438        switch (hardKeyboardHidden) {
439            case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
440            case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
441            case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
442            default: sb.append("/"); sb.append(hardKeyboardHidden); break;
443        }
444        switch (navigation) {
445            case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
446            case NAVIGATION_NONAV: sb.append(" -nav"); break;
447            case NAVIGATION_DPAD: sb.append(" dpad"); break;
448            case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
449            case NAVIGATION_WHEEL: sb.append(" wheel"); break;
450            default: sb.append(" nav="); sb.append(navigation); break;
451        }
452        switch (navigationHidden) {
453            case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
454            case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
455            case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
456            default: sb.append("/"); sb.append(navigationHidden); break;
457        }
458        if (seq != 0) {
459            sb.append(" s.");
460            sb.append(seq);
461        }
462        sb.append('}');
463        return sb.toString();
464    }
465
466    /**
467     * Set this object to the system defaults.
468     */
469    public void setToDefaults() {
470        fontScale = 1;
471        mcc = mnc = 0;
472        locale = null;
473        userSetLocale = false;
474        touchscreen = TOUCHSCREEN_UNDEFINED;
475        keyboard = KEYBOARD_UNDEFINED;
476        keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
477        hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
478        navigation = NAVIGATION_UNDEFINED;
479        navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
480        orientation = ORIENTATION_UNDEFINED;
481        screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
482        uiMode = UI_MODE_TYPE_UNDEFINED;
483        screenWidthDp = compatScreenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
484        screenHeightDp = compatScreenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
485        smallestScreenWidthDp = compatSmallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
486        textLayoutDirection = TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE;
487        seq = 0;
488    }
489
490    /** {@hide} */
491    @Deprecated public void makeDefault() {
492        setToDefaults();
493    }
494
495    /**
496     * Copy the fields from delta into this Configuration object, keeping
497     * track of which ones have changed.  Any undefined fields in
498     * <var>delta</var> are ignored and not copied in to the current
499     * Configuration.
500     * @return Returns a bit mask of the changed fields, as per
501     * {@link #diff}.
502     */
503    public int updateFrom(Configuration delta) {
504        int changed = 0;
505        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
506            changed |= ActivityInfo.CONFIG_FONT_SCALE;
507            fontScale = delta.fontScale;
508        }
509        if (delta.mcc != 0 && mcc != delta.mcc) {
510            changed |= ActivityInfo.CONFIG_MCC;
511            mcc = delta.mcc;
512        }
513        if (delta.mnc != 0 && mnc != delta.mnc) {
514            changed |= ActivityInfo.CONFIG_MNC;
515            mnc = delta.mnc;
516        }
517        if (delta.locale != null
518                && (locale == null || !locale.equals(delta.locale))) {
519            changed |= ActivityInfo.CONFIG_LOCALE;
520            locale = delta.locale != null
521                    ? (Locale) delta.locale.clone() : null;
522            textLayoutDirection = getLayoutDirectionFromLocale(locale);
523        }
524        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
525        {
526            userSetLocale = true;
527            changed |= ActivityInfo.CONFIG_LOCALE;
528        }
529        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
530                && touchscreen != delta.touchscreen) {
531            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
532            touchscreen = delta.touchscreen;
533        }
534        if (delta.keyboard != KEYBOARD_UNDEFINED
535                && keyboard != delta.keyboard) {
536            changed |= ActivityInfo.CONFIG_KEYBOARD;
537            keyboard = delta.keyboard;
538        }
539        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
540                && keyboardHidden != delta.keyboardHidden) {
541            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
542            keyboardHidden = delta.keyboardHidden;
543        }
544        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
545                && hardKeyboardHidden != delta.hardKeyboardHidden) {
546            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
547            hardKeyboardHidden = delta.hardKeyboardHidden;
548        }
549        if (delta.navigation != NAVIGATION_UNDEFINED
550                && navigation != delta.navigation) {
551            changed |= ActivityInfo.CONFIG_NAVIGATION;
552            navigation = delta.navigation;
553        }
554        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
555                && navigationHidden != delta.navigationHidden) {
556            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
557            navigationHidden = delta.navigationHidden;
558        }
559        if (delta.orientation != ORIENTATION_UNDEFINED
560                && orientation != delta.orientation) {
561            changed |= ActivityInfo.CONFIG_ORIENTATION;
562            orientation = delta.orientation;
563        }
564        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
565                && screenLayout != delta.screenLayout) {
566            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
567            screenLayout = delta.screenLayout;
568        }
569        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
570                && uiMode != delta.uiMode) {
571            changed |= ActivityInfo.CONFIG_UI_MODE;
572            if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
573                uiMode = (uiMode&~UI_MODE_TYPE_MASK)
574                        | (delta.uiMode&UI_MODE_TYPE_MASK);
575            }
576            if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
577                uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
578                        | (delta.uiMode&UI_MODE_NIGHT_MASK);
579            }
580        }
581        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
582                && screenWidthDp != delta.screenWidthDp) {
583            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
584            screenWidthDp = delta.screenWidthDp;
585        }
586        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
587                && screenHeightDp != delta.screenHeightDp) {
588            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
589            screenHeightDp = delta.screenHeightDp;
590        }
591        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
592            smallestScreenWidthDp = delta.smallestScreenWidthDp;
593        }
594        if (delta.compatScreenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
595            compatScreenWidthDp = delta.compatScreenWidthDp;
596        }
597        if (delta.compatScreenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
598            compatScreenHeightDp = delta.compatScreenHeightDp;
599        }
600        if (delta.compatSmallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
601            compatSmallestScreenWidthDp = delta.compatSmallestScreenWidthDp;
602        }
603
604        if (delta.seq != 0) {
605            seq = delta.seq;
606        }
607
608        return changed;
609    }
610
611    /**
612     * Return the layout direction for a given Locale
613     * @param locale the Locale for which we want the layout direction. Can be null.
614     * @return the layout direction. This may be one of:
615     * {@link #TEXT_LAYOUT_DIRECTION_UNDEFINED_DO_NOT_USE} or
616     * {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or
617     * {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}.
618     *
619     * @hide
620     */
621    public static int getLayoutDirectionFromLocale(Locale locale) {
622        if (locale == null || locale.equals(Locale.ROOT)) return TEXT_LAYOUT_DIRECTION_UNDEFINED_DO_NOT_USE;
623        // Be careful: this code will need to be changed when vertical scripts will be supported
624        // OR if ICU4C is updated to have the "likelySubtags" file
625        switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
626            case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
627                return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE;
628            case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
629            case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
630                return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE;
631            default:
632                return TEXT_LAYOUT_DIRECTION_UNDEFINED_DO_NOT_USE;
633        }
634    }
635
636    /**
637     * Return a bit mask of the differences between this Configuration
638     * object and the given one.  Does not change the values of either.  Any
639     * undefined fields in <var>delta</var> are ignored.
640     * @return Returns a bit mask indicating which configuration
641     * values has changed, containing any combination of
642     * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
643     * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
644     * {@link android.content.pm.ActivityInfo#CONFIG_MCC
645     * PackageManager.ActivityInfo.CONFIG_MCC},
646     * {@link android.content.pm.ActivityInfo#CONFIG_MNC
647     * PackageManager.ActivityInfo.CONFIG_MNC},
648     * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
649     * PackageManager.ActivityInfo.CONFIG_LOCALE},
650     * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
651     * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
652     * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
653     * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
654     * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
655     * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
656     * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
657     * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
658     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
659     * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
660     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
661     * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or
662     * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE
663     * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}.
664     */
665    public int diff(Configuration delta) {
666        int changed = 0;
667        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
668            changed |= ActivityInfo.CONFIG_FONT_SCALE;
669        }
670        if (delta.mcc != 0 && mcc != delta.mcc) {
671            changed |= ActivityInfo.CONFIG_MCC;
672        }
673        if (delta.mnc != 0 && mnc != delta.mnc) {
674            changed |= ActivityInfo.CONFIG_MNC;
675        }
676        if (delta.locale != null
677                && (locale == null || !locale.equals(delta.locale))) {
678            changed |= ActivityInfo.CONFIG_LOCALE;
679        }
680        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
681                && touchscreen != delta.touchscreen) {
682            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
683        }
684        if (delta.keyboard != KEYBOARD_UNDEFINED
685                && keyboard != delta.keyboard) {
686            changed |= ActivityInfo.CONFIG_KEYBOARD;
687        }
688        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
689                && keyboardHidden != delta.keyboardHidden) {
690            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
691        }
692        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
693                && hardKeyboardHidden != delta.hardKeyboardHidden) {
694            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
695        }
696        if (delta.navigation != NAVIGATION_UNDEFINED
697                && navigation != delta.navigation) {
698            changed |= ActivityInfo.CONFIG_NAVIGATION;
699        }
700        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
701                && navigationHidden != delta.navigationHidden) {
702            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
703        }
704        if (delta.orientation != ORIENTATION_UNDEFINED
705                && orientation != delta.orientation) {
706            changed |= ActivityInfo.CONFIG_ORIENTATION;
707        }
708        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
709                && screenLayout != delta.screenLayout) {
710            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
711        }
712        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
713                && uiMode != delta.uiMode) {
714            changed |= ActivityInfo.CONFIG_UI_MODE;
715        }
716        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
717                && screenWidthDp != delta.screenWidthDp) {
718            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
719        }
720        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
721                && screenHeightDp != delta.screenHeightDp) {
722            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
723        }
724        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
725                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
726            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
727        }
728
729        return changed;
730    }
731
732    /**
733     * Determine if a new resource needs to be loaded from the bit set of
734     * configuration changes returned by {@link #updateFrom(Configuration)}.
735     *
736     * @param configChanges The mask of changes configurations as returned by
737     * {@link #updateFrom(Configuration)}.
738     * @param interestingChanges The configuration changes that the resource
739     * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
740     *
741     * @return Return true if the resource needs to be loaded, else false.
742     */
743    public static boolean needNewResources(int configChanges, int interestingChanges) {
744        return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
745    }
746
747    /**
748     * @hide Return true if the sequence of 'other' is better than this.  Assumes
749     * that 'this' is your current sequence and 'other' is a new one you have
750     * received some how and want to compare with what you have.
751     */
752    public boolean isOtherSeqNewer(Configuration other) {
753        if (other == null) {
754            // Sanity check.
755            return false;
756        }
757        if (other.seq == 0) {
758            // If the other sequence is not specified, then we must assume
759            // it is newer since we don't know any better.
760            return true;
761        }
762        if (seq == 0) {
763            // If this sequence is not specified, then we also consider the
764            // other is better.  Yes we have a preference for other.  Sue us.
765            return true;
766        }
767        int diff = other.seq - seq;
768        if (diff > 0x10000) {
769            // If there has been a sufficiently large jump, assume the
770            // sequence has wrapped around.
771            return false;
772        }
773        return diff > 0;
774    }
775
776    /**
777     * Parcelable methods
778     */
779    public int describeContents() {
780        return 0;
781    }
782
783    public void writeToParcel(Parcel dest, int flags) {
784        dest.writeFloat(fontScale);
785        dest.writeInt(mcc);
786        dest.writeInt(mnc);
787        if (locale == null) {
788            dest.writeInt(0);
789        } else {
790            dest.writeInt(1);
791            dest.writeString(locale.getLanguage());
792            dest.writeString(locale.getCountry());
793            dest.writeString(locale.getVariant());
794        }
795        if(userSetLocale) {
796            dest.writeInt(1);
797        } else {
798            dest.writeInt(0);
799        }
800        dest.writeInt(touchscreen);
801        dest.writeInt(keyboard);
802        dest.writeInt(keyboardHidden);
803        dest.writeInt(hardKeyboardHidden);
804        dest.writeInt(navigation);
805        dest.writeInt(navigationHidden);
806        dest.writeInt(orientation);
807        dest.writeInt(screenLayout);
808        dest.writeInt(uiMode);
809        dest.writeInt(screenWidthDp);
810        dest.writeInt(screenHeightDp);
811        dest.writeInt(smallestScreenWidthDp);
812        dest.writeInt(compatScreenWidthDp);
813        dest.writeInt(compatScreenHeightDp);
814        dest.writeInt(compatSmallestScreenWidthDp);
815        dest.writeInt(textLayoutDirection);
816        dest.writeInt(seq);
817    }
818
819    public void readFromParcel(Parcel source) {
820        fontScale = source.readFloat();
821        mcc = source.readInt();
822        mnc = source.readInt();
823        if (source.readInt() != 0) {
824            locale = new Locale(source.readString(), source.readString(),
825                    source.readString());
826        }
827        userSetLocale = (source.readInt()==1);
828        touchscreen = source.readInt();
829        keyboard = source.readInt();
830        keyboardHidden = source.readInt();
831        hardKeyboardHidden = source.readInt();
832        navigation = source.readInt();
833        navigationHidden = source.readInt();
834        orientation = source.readInt();
835        screenLayout = source.readInt();
836        uiMode = source.readInt();
837        screenWidthDp = source.readInt();
838        screenHeightDp = source.readInt();
839        smallestScreenWidthDp = source.readInt();
840        compatScreenWidthDp = source.readInt();
841        compatScreenHeightDp = source.readInt();
842        compatSmallestScreenWidthDp = source.readInt();
843        textLayoutDirection = source.readInt();
844        seq = source.readInt();
845    }
846
847    public static final Parcelable.Creator<Configuration> CREATOR
848            = new Parcelable.Creator<Configuration>() {
849        public Configuration createFromParcel(Parcel source) {
850            return new Configuration(source);
851        }
852
853        public Configuration[] newArray(int size) {
854            return new Configuration[size];
855        }
856    };
857
858    /**
859     * Construct this Configuration object, reading from the Parcel.
860     */
861    private Configuration(Parcel source) {
862        readFromParcel(source);
863    }
864
865    public int compareTo(Configuration that) {
866        int n;
867        float a = this.fontScale;
868        float b = that.fontScale;
869        if (a < b) return -1;
870        if (a > b) return 1;
871        n = this.mcc - that.mcc;
872        if (n != 0) return n;
873        n = this.mnc - that.mnc;
874        if (n != 0) return n;
875        if (this.locale == null) {
876            if (that.locale != null) return 1;
877        } else if (that.locale == null) {
878            return -1;
879        } else {
880            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
881            if (n != 0) return n;
882            n = this.locale.getCountry().compareTo(that.locale.getCountry());
883            if (n != 0) return n;
884            n = this.locale.getVariant().compareTo(that.locale.getVariant());
885            if (n != 0) return n;
886        }
887        n = this.touchscreen - that.touchscreen;
888        if (n != 0) return n;
889        n = this.keyboard - that.keyboard;
890        if (n != 0) return n;
891        n = this.keyboardHidden - that.keyboardHidden;
892        if (n != 0) return n;
893        n = this.hardKeyboardHidden - that.hardKeyboardHidden;
894        if (n != 0) return n;
895        n = this.navigation - that.navigation;
896        if (n != 0) return n;
897        n = this.navigationHidden - that.navigationHidden;
898        if (n != 0) return n;
899        n = this.orientation - that.orientation;
900        if (n != 0) return n;
901        n = this.screenLayout - that.screenLayout;
902        if (n != 0) return n;
903        n = this.uiMode - that.uiMode;
904        if (n != 0) return n;
905        n = this.screenWidthDp - that.screenWidthDp;
906        if (n != 0) return n;
907        n = this.screenHeightDp - that.screenHeightDp;
908        if (n != 0) return n;
909        n = this.smallestScreenWidthDp - that.smallestScreenWidthDp;
910        //if (n != 0) return n;
911        return n;
912    }
913
914    public boolean equals(Configuration that) {
915        if (that == null) return false;
916        if (that == this) return true;
917        return this.compareTo(that) == 0;
918    }
919
920    public boolean equals(Object that) {
921        try {
922            return equals((Configuration)that);
923        } catch (ClassCastException e) {
924        }
925        return false;
926    }
927
928    public int hashCode() {
929        int result = 17;
930        result = 31 * result + Float.floatToIntBits(fontScale);
931        result = 31 * result + mcc;
932        result = 31 * result + mnc;
933        result = 31 * result + (locale != null ? locale.hashCode() : 0);
934        result = 31 * result + touchscreen;
935        result = 31 * result + keyboard;
936        result = 31 * result + keyboardHidden;
937        result = 31 * result + hardKeyboardHidden;
938        result = 31 * result + navigation;
939        result = 31 * result + navigationHidden;
940        result = 31 * result + orientation;
941        result = 31 * result + screenLayout;
942        result = 31 * result + uiMode;
943        result = 31 * result + screenWidthDp;
944        result = 31 * result + screenHeightDp;
945        result = 31 * result + smallestScreenWidthDp;
946        return result;
947    }
948}
949