Configuration.java revision 5f7979993979466c79ab4f38d83c6f2aca361662
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;
22import android.util.LocaleUtil;
23import android.view.View;
24
25import java.util.Locale;
26
27/**
28 * This class describes all device configuration information that can
29 * impact the resources the application retrieves.  This includes both
30 * user-specified configuration options (locale and scaling) as well
31 * as device configurations (such as input modes, screen size and screen orientation).
32 * <p>You can acquire this object from {@link Resources}, using {@link
33 * Resources#getConfiguration}. Thus, from an activity, you can get it by chaining the request
34 * with {@link android.app.Activity#getResources}:</p>
35 * <pre>Configuration config = getResources().getConfiguration();</pre>
36 */
37public final class Configuration implements Parcelable, Comparable<Configuration> {
38    /** @hide */
39    public static final Configuration EMPTY = new Configuration();
40
41    /**
42     * Current user preference for the scaling factor for fonts, relative
43     * to the base density scaling.
44     */
45    public float fontScale;
46
47    /**
48     * IMSI MCC (Mobile Country Code), corresponding to
49     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#MccQualifier">mcc</a>
50     * resource qualifier.  0 if undefined.
51     */
52    public int mcc;
53
54    /**
55     * IMSI MNC (Mobile Network Code), corresponding to
56     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#MccQualifier">mnc</a>
57     * resource qualifier.  0 if undefined.
58     */
59    public int mnc;
60
61    /**
62     * Current user preference for the locale, corresponding to
63     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#LocaleQualifier">locale</a>
64     * resource qualifier.
65     */
66    public Locale locale;
67
68    /**
69     * Locale should persist on setting.  This is hidden because it is really
70     * questionable whether this is the right way to expose the functionality.
71     * @hide
72     */
73    public boolean userSetLocale;
74
75    /** Constant for {@link #screenLayout}: bits that encode the size. */
76    public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
77    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
78     * value indicating that no size has been set. */
79    public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
80    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
81     * value indicating the screen is at least approximately 320x426 dp units,
82     * corresponds to the
83     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">small</a>
84     * resource qualifier.
85     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
86     * Multiple Screens</a> for more information. */
87    public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
88    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
89     * value indicating the screen is at least approximately 320x470 dp units,
90     * corresponds to the
91     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">normal</a>
92     * resource qualifier.
93     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
94     * Multiple Screens</a> for more information. */
95    public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
96    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
97     * value indicating the screen is at least approximately 480x640 dp units,
98     * corresponds to the
99     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">large</a>
100     * resource qualifier.
101     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
102     * Multiple Screens</a> for more information. */
103    public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
104    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
105     * value indicating the screen is at least approximately 720x960 dp units,
106     * corresponds to the
107     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">xlarge</a>
108     * resource qualifier.
109     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
110     * Multiple Screens</a> for more information.*/
111    public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
112
113    /** Constant for {@link #screenLayout}: bits that encode the aspect ratio. */
114    public static final int SCREENLAYOUT_LONG_MASK = 0x30;
115    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LONG_MASK}
116     * value indicating that no size has been set. */
117    public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
118    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LONG_MASK}
119     * value that corresponds to the
120     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenAspectQualifier">notlong</a>
121     * resource qualifier. */
122    public static final int SCREENLAYOUT_LONG_NO = 0x10;
123    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LONG_MASK}
124     * value that corresponds to the
125     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenAspectQualifier">long</a>
126     * resource qualifier. */
127    public static final int SCREENLAYOUT_LONG_YES = 0x20;
128
129    /** Constant for {@link #screenLayout}: bits that encode the layout direction. */
130    public static final int SCREENLAYOUT_LAYOUTDIR_MASK = 0xC0;
131    /** Constant for {@link #screenLayout}: bits shift to get the layout direction. */
132    public static final int SCREENLAYOUT_LAYOUTDIR_SHIFT = 6;
133    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LAYOUTDIR_MASK}
134     * value indicating that no layout dir has been set. */
135    public static final int SCREENLAYOUT_LAYOUTDIR_UNDEFINED = 0x00;
136    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LAYOUTDIR_MASK}
137     * value indicating that a layout dir has been set to LTR. */
138    public static final int SCREENLAYOUT_LAYOUTDIR_LTR = 0x01 << SCREENLAYOUT_LAYOUTDIR_SHIFT;
139    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LAYOUTDIR_MASK}
140     * value indicating that a layout dir has been set to RTL. */
141    public static final int SCREENLAYOUT_LAYOUTDIR_RTL = 0x02 << SCREENLAYOUT_LAYOUTDIR_SHIFT;
142
143    /** Constant for {@link #screenLayout}: a value indicating that screenLayout is undefined */
144    public static final int SCREENLAYOUT_UNDEFINED = SCREENLAYOUT_SIZE_UNDEFINED |
145            SCREENLAYOUT_LONG_UNDEFINED | SCREENLAYOUT_LAYOUTDIR_UNDEFINED;
146
147    /**
148     * Special flag we generate to indicate that the screen layout requires
149     * us to use a compatibility mode for apps that are not modern layout
150     * aware.
151     * @hide
152     */
153    public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
154
155    /**
156     * Bit mask of overall layout of the screen.  Currently there are two
157     * fields:
158     * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
159     * of the screen.  They may be one of
160     * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
161     * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
162     *
163     * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
164     * is wider/taller than normal.  They may be one of
165     * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
166     *
167     * <p>The {@link #SCREENLAYOUT_LAYOUTDIR_MASK} defines whether the screen layout
168     * is either LTR or RTL.  They may be one of
169     * {@link #SCREENLAYOUT_LAYOUTDIR_LTR} or {@link #SCREENLAYOUT_LAYOUTDIR_RTL}.
170     *
171     * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
172     * Multiple Screens</a> for more information.
173     */
174    public int screenLayout;
175
176    /**
177     * Check if the Configuration's current {@link #screenLayout} is at
178     * least the given size.
179     *
180     * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
181     * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
182     * {@link #SCREENLAYOUT_SIZE_XLARGE}.
183     * @return Returns true if the current screen layout size is at least
184     * the given size.
185     */
186    public boolean isLayoutSizeAtLeast(int size) {
187        int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
188        if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
189        return cur >= size;
190    }
191
192    /** Constant for {@link #touchscreen}: a value indicating that no value has been set. */
193    public static final int TOUCHSCREEN_UNDEFINED = 0;
194    /** Constant for {@link #touchscreen}, value corresponding to the
195     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#TouchscreenQualifier">notouch</a>
196     * resource qualifier. */
197    public static final int TOUCHSCREEN_NOTOUCH = 1;
198    /** @deprecated Not currently supported or used. */
199    @Deprecated public static final int TOUCHSCREEN_STYLUS = 2;
200    /** Constant for {@link #touchscreen}, value corresponding to the
201     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#TouchscreenQualifier">finger</a>
202     * resource qualifier. */
203    public static final int TOUCHSCREEN_FINGER = 3;
204
205    /**
206     * The kind of touch screen attached to the device.
207     * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_FINGER}.
208     */
209    public int touchscreen;
210
211    /** Constant for {@link #keyboard}: a value indicating that no value has been set. */
212    public static final int KEYBOARD_UNDEFINED = 0;
213    /** Constant for {@link #keyboard}, value corresponding to the
214     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ImeQualifier">nokeys</a>
215     * resource qualifier. */
216    public static final int KEYBOARD_NOKEYS = 1;
217    /** Constant for {@link #keyboard}, value corresponding to the
218     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ImeQualifier">qwerty</a>
219     * resource qualifier. */
220    public static final int KEYBOARD_QWERTY = 2;
221    /** Constant for {@link #keyboard}, value corresponding to the
222     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ImeQualifier">12key</a>
223     * resource qualifier. */
224    public static final int KEYBOARD_12KEY = 3;
225
226    /**
227     * The kind of keyboard attached to the device.
228     * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
229     * {@link #KEYBOARD_12KEY}.
230     */
231    public int keyboard;
232
233    /** Constant for {@link #keyboardHidden}: a value indicating that no value has been set. */
234    public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
235    /** Constant for {@link #keyboardHidden}, value corresponding to the
236     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#KeyboardAvailQualifier">keysexposed</a>
237     * resource qualifier. */
238    public static final int KEYBOARDHIDDEN_NO = 1;
239    /** Constant for {@link #keyboardHidden}, value corresponding to the
240     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#KeyboardAvailQualifier">keyshidden</a>
241     * resource qualifier. */
242    public static final int KEYBOARDHIDDEN_YES = 2;
243    /** Constant matching actual resource implementation. {@hide} */
244    public static final int KEYBOARDHIDDEN_SOFT = 3;
245
246    /**
247     * A flag indicating whether any keyboard is available.  Unlike
248     * {@link #hardKeyboardHidden}, this also takes into account a soft
249     * keyboard, so if the hard keyboard is hidden but there is soft
250     * keyboard available, it will be set to NO.  Value is one of:
251     * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
252     */
253    public int keyboardHidden;
254
255    /** Constant for {@link #hardKeyboardHidden}: a value indicating that no value has been set. */
256    public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
257    /** Constant for {@link #hardKeyboardHidden}, value corresponding to the
258     * physical keyboard being exposed. */
259    public static final int HARDKEYBOARDHIDDEN_NO = 1;
260    /** Constant for {@link #hardKeyboardHidden}, value corresponding to the
261     * physical keyboard being hidden. */
262    public static final int HARDKEYBOARDHIDDEN_YES = 2;
263
264    /**
265     * A flag indicating whether the hard keyboard has been hidden.  This will
266     * be set on a device with a mechanism to hide the keyboard from the
267     * user, when that mechanism is closed.  One of:
268     * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
269     */
270    public int hardKeyboardHidden;
271
272    /** Constant for {@link #navigation}: a value indicating that no value has been set. */
273    public static final int NAVIGATION_UNDEFINED = 0;
274    /** Constant for {@link #navigation}, value corresponding to the
275     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">nonav</a>
276     * resource qualifier. */
277    public static final int NAVIGATION_NONAV = 1;
278    /** Constant for {@link #navigation}, value corresponding to the
279     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">dpad</a>
280     * resource qualifier. */
281    public static final int NAVIGATION_DPAD = 2;
282    /** Constant for {@link #navigation}, value corresponding to the
283     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">trackball</a>
284     * resource qualifier. */
285    public static final int NAVIGATION_TRACKBALL = 3;
286    /** Constant for {@link #navigation}, value corresponding to the
287     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">wheel</a>
288     * resource qualifier. */
289    public static final int NAVIGATION_WHEEL = 4;
290
291    /**
292     * The kind of navigation method available on the device.
293     * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
294     * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
295     */
296    public int navigation;
297
298    /** Constant for {@link #navigationHidden}: a value indicating that no value has been set. */
299    public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
300    /** Constant for {@link #navigationHidden}, value corresponding to the
301     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavAvailQualifier">navexposed</a>
302     * resource qualifier. */
303    public static final int NAVIGATIONHIDDEN_NO = 1;
304    /** Constant for {@link #navigationHidden}, value corresponding to the
305     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavAvailQualifier">navhidden</a>
306     * resource qualifier. */
307    public static final int NAVIGATIONHIDDEN_YES = 2;
308
309    /**
310     * A flag indicating whether any 5-way or DPAD navigation available.
311     * This will be set on a device with a mechanism to hide the navigation
312     * controls from the user, when that mechanism is closed.  One of:
313     * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
314     */
315    public int navigationHidden;
316
317    /** Constant for {@link #orientation}: a value indicating that no value has been set. */
318    public static final int ORIENTATION_UNDEFINED = 0;
319    /** Constant for {@link #orientation}, value corresponding to the
320     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#OrientationQualifier">port</a>
321     * resource qualifier. */
322    public static final int ORIENTATION_PORTRAIT = 1;
323    /** Constant for {@link #orientation}, value corresponding to the
324     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#OrientationQualifier">land</a>
325     * resource qualifier. */
326    public static final int ORIENTATION_LANDSCAPE = 2;
327    /** @deprecated Not currently supported or used. */
328    @Deprecated public static final int ORIENTATION_SQUARE = 3;
329
330    /**
331     * Overall orientation of the screen.  May be one of
332     * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT}.
333     */
334    public int orientation;
335
336    /** Constant for {@link #uiMode}: bits that encode the mode type. */
337    public static final int UI_MODE_TYPE_MASK = 0x0f;
338    /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
339     * value indicating that no mode type has been set. */
340    public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
341    /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
342     * value that corresponds to
343     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">no
344     * UI mode</a> resource qualifier specified. */
345    public static final int UI_MODE_TYPE_NORMAL = 0x01;
346    /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
347     * value that corresponds to the
348     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">desk</a>
349     * resource qualifier. */
350    public static final int UI_MODE_TYPE_DESK = 0x02;
351    /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
352     * value that corresponds to the
353     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">car</a>
354     * resource qualifier. */
355    public static final int UI_MODE_TYPE_CAR = 0x03;
356    /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
357     * value that corresponds to the
358     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">television</a>
359     * resource qualifier. */
360    public static final int UI_MODE_TYPE_TELEVISION = 0x04;
361    /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
362     * value that corresponds to the
363     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">appliance</a>
364     * resource qualifier. */
365    public static final int UI_MODE_TYPE_APPLIANCE = 0x05;
366
367    /** Constant for {@link #uiMode}: bits that encode the night mode. */
368    public static final int UI_MODE_NIGHT_MASK = 0x30;
369    /** Constant for {@link #uiMode}: a {@link #UI_MODE_NIGHT_MASK}
370     * value indicating that no mode type has been set. */
371    public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
372    /** Constant for {@link #uiMode}: a {@link #UI_MODE_NIGHT_MASK}
373     * value that corresponds to the
374     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NightQualifier">notnight</a>
375     * resource qualifier. */
376    public static final int UI_MODE_NIGHT_NO = 0x10;
377    /** Constant for {@link #uiMode}: a {@link #UI_MODE_NIGHT_MASK}
378     * value that corresponds to the
379     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NightQualifier">night</a>
380     * resource qualifier. */
381    public static final int UI_MODE_NIGHT_YES = 0x20;
382
383    /**
384     * Bit mask of the ui mode.  Currently there are two fields:
385     * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
386     * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
387     * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
388     * {@link #UI_MODE_TYPE_CAR}, {@link #UI_MODE_TYPE_TELEVISION}, or
389     * {@link #UI_MODE_TYPE_APPLIANCE}.
390     *
391     * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
392     * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
393     * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
394     */
395    public int uiMode;
396
397    /**
398     * Default value for {@link #screenWidthDp} indicating that no width
399     * has been specified.
400     */
401    public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
402
403    /**
404     * The current width of the available screen space, in dp units,
405     * corresponding to
406     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenWidthQualifier">screen
407     * width</a> resource qualifier.  Set to
408     * {@link #SCREEN_WIDTH_DP_UNDEFINED} if no width is specified.
409     */
410    public int screenWidthDp;
411
412    /**
413     * Default value for {@link #screenHeightDp} indicating that no width
414     * has been specified.
415     */
416    public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0;
417
418    /**
419     * The current height of the available screen space, in dp units,
420     * corresponding to
421     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenHeightQualifier">screen
422     * height</a> resource qualifier.  Set to
423     * {@link #SCREEN_HEIGHT_DP_UNDEFINED} if no height is specified.
424     */
425    public int screenHeightDp;
426
427    /**
428     * Default value for {@link #smallestScreenWidthDp} indicating that no width
429     * has been specified.
430     */
431    public static final int SMALLEST_SCREEN_WIDTH_DP_UNDEFINED = 0;
432
433    /**
434     * The smallest screen size an application will see in normal operation,
435     * corresponding to
436     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#SmallestScreenWidthQualifier">smallest
437     * screen width</a> resource qualifier.
438     * This is the smallest value of both screenWidthDp and screenHeightDp
439     * in both portrait and landscape.  Set to
440     * {@link #SMALLEST_SCREEN_WIDTH_DP_UNDEFINED} if no width is specified.
441     */
442    public int smallestScreenWidthDp;
443
444    /**
445     * Default value for {@link #densityDpi} indicating that no width
446     * has been specified.
447     */
448    public static final int DENSITY_DPI_UNDEFINED = 0;
449
450    /**
451     * The target screen density being rendered to,
452     * corresponding to
453     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#DensityQualifier">density</a>
454     * resource qualifier.  Set to
455     * {@link #DENSITY_DPI_UNDEFINED} if no density is specified.
456     */
457    public int densityDpi;
458
459    /** @hide Hack to get this information from WM to app running in compat mode. */
460    public int compatScreenWidthDp;
461    /** @hide Hack to get this information from WM to app running in compat mode. */
462    public int compatScreenHeightDp;
463    /** @hide Hack to get this information from WM to app running in compat mode. */
464    public int compatSmallestScreenWidthDp;
465
466    /**
467     * @hide Internal book-keeping.
468     */
469    public int seq;
470
471    /**
472     * Construct an invalid Configuration.  You must call {@link #setToDefaults}
473     * for this object to be valid.  {@more}
474     */
475    public Configuration() {
476        setToDefaults();
477    }
478
479    /**
480     * Makes a deep copy suitable for modification.
481     */
482    public Configuration(Configuration o) {
483        setTo(o);
484    }
485
486    public void setTo(Configuration o) {
487        fontScale = o.fontScale;
488        mcc = o.mcc;
489        mnc = o.mnc;
490        if (o.locale != null) {
491            locale = (Locale) o.locale.clone();
492        }
493        userSetLocale = o.userSetLocale;
494        touchscreen = o.touchscreen;
495        keyboard = o.keyboard;
496        keyboardHidden = o.keyboardHidden;
497        hardKeyboardHidden = o.hardKeyboardHidden;
498        navigation = o.navigation;
499        navigationHidden = o.navigationHidden;
500        orientation = o.orientation;
501        screenLayout = o.screenLayout;
502        uiMode = o.uiMode;
503        screenWidthDp = o.screenWidthDp;
504        screenHeightDp = o.screenHeightDp;
505        smallestScreenWidthDp = o.smallestScreenWidthDp;
506        densityDpi = o.densityDpi;
507        compatScreenWidthDp = o.compatScreenWidthDp;
508        compatScreenHeightDp = o.compatScreenHeightDp;
509        compatSmallestScreenWidthDp = o.compatSmallestScreenWidthDp;
510        seq = o.seq;
511    }
512
513    public String toString() {
514        StringBuilder sb = new StringBuilder(128);
515        sb.append("{");
516        sb.append(fontScale);
517        sb.append(" ");
518        if (mcc != 0) {
519            sb.append(mcc);
520            sb.append("mcc");
521        } else {
522            sb.append("?mcc");
523        }
524        if (mnc != 0) {
525            sb.append(mnc);
526            sb.append("mnc");
527        } else {
528            sb.append("?mnc");
529        }
530        if (locale != null) {
531            sb.append(" ");
532            sb.append(locale);
533        } else {
534            sb.append(" ?locale");
535        }
536        int layoutDir = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK);
537        switch (layoutDir) {
538            case SCREENLAYOUT_LAYOUTDIR_UNDEFINED: sb.append(" ?layoutDir"); break;
539            case SCREENLAYOUT_LAYOUTDIR_LTR: sb.append(" ltr"); break;
540            case SCREENLAYOUT_LAYOUTDIR_RTL: sb.append(" rtl"); break;
541            default: sb.append(" layoutDir=");
542                sb.append(layoutDir >> SCREENLAYOUT_LAYOUTDIR_SHIFT); break;
543        }
544        if (smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
545            sb.append(" sw"); sb.append(smallestScreenWidthDp); sb.append("dp");
546        } else {
547            sb.append(" ?swdp");
548        }
549        if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
550            sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
551        } else {
552            sb.append(" ?wdp");
553        }
554        if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
555            sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
556        } else {
557            sb.append(" ?hdp");
558        }
559        if (densityDpi != DENSITY_DPI_UNDEFINED) {
560            sb.append(" "); sb.append(densityDpi); sb.append("dpi");
561        } else {
562            sb.append(" ?density");
563        }
564        switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
565            case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
566            case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
567            case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
568            case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
569            case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
570            default: sb.append(" layoutSize=");
571                    sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
572        }
573        switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
574            case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
575            case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
576            case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
577            default: sb.append(" layoutLong=");
578                    sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
579        }
580        switch (orientation) {
581            case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
582            case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
583            case ORIENTATION_PORTRAIT: sb.append(" port"); break;
584            default: sb.append(" orien="); sb.append(orientation); break;
585        }
586        switch ((uiMode&UI_MODE_TYPE_MASK)) {
587            case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
588            case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
589            case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
590            case UI_MODE_TYPE_CAR: sb.append(" car"); break;
591            case UI_MODE_TYPE_TELEVISION: sb.append(" television"); break;
592            case UI_MODE_TYPE_APPLIANCE: sb.append(" appliance"); break;
593            default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
594        }
595        switch ((uiMode&UI_MODE_NIGHT_MASK)) {
596            case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
597            case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
598            case UI_MODE_NIGHT_YES: sb.append(" night"); break;
599            default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
600        }
601        switch (touchscreen) {
602            case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
603            case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
604            case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
605            case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
606            default: sb.append(" touch="); sb.append(touchscreen); break;
607        }
608        switch (keyboard) {
609            case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
610            case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
611            case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
612            case KEYBOARD_12KEY: sb.append(" 12key"); break;
613            default: sb.append(" keys="); sb.append(keyboard); break;
614        }
615        switch (keyboardHidden) {
616            case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
617            case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
618            case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
619            case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
620            default: sb.append("/"); sb.append(keyboardHidden); break;
621        }
622        switch (hardKeyboardHidden) {
623            case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
624            case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
625            case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
626            default: sb.append("/"); sb.append(hardKeyboardHidden); break;
627        }
628        switch (navigation) {
629            case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
630            case NAVIGATION_NONAV: sb.append(" -nav"); break;
631            case NAVIGATION_DPAD: sb.append(" dpad"); break;
632            case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
633            case NAVIGATION_WHEEL: sb.append(" wheel"); break;
634            default: sb.append(" nav="); sb.append(navigation); break;
635        }
636        switch (navigationHidden) {
637            case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
638            case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
639            case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
640            default: sb.append("/"); sb.append(navigationHidden); break;
641        }
642        if (seq != 0) {
643            sb.append(" s.");
644            sb.append(seq);
645        }
646        sb.append('}');
647        return sb.toString();
648    }
649
650    /**
651     * Set this object to the system defaults.
652     */
653    public void setToDefaults() {
654        fontScale = 1;
655        mcc = mnc = 0;
656        locale = null;
657        userSetLocale = false;
658        touchscreen = TOUCHSCREEN_UNDEFINED;
659        keyboard = KEYBOARD_UNDEFINED;
660        keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
661        hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
662        navigation = NAVIGATION_UNDEFINED;
663        navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
664        orientation = ORIENTATION_UNDEFINED;
665        screenLayout = SCREENLAYOUT_UNDEFINED;
666        uiMode = UI_MODE_TYPE_UNDEFINED;
667        screenWidthDp = compatScreenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
668        screenHeightDp = compatScreenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
669        smallestScreenWidthDp = compatSmallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
670        densityDpi = DENSITY_DPI_UNDEFINED;
671        seq = 0;
672    }
673
674    /** {@hide} */
675    @Deprecated public void makeDefault() {
676        setToDefaults();
677    }
678
679    /**
680     * Copy the fields from delta into this Configuration object, keeping
681     * track of which ones have changed.  Any undefined fields in
682     * <var>delta</var> are ignored and not copied in to the current
683     * Configuration.
684     * @return Returns a bit mask of the changed fields, as per
685     * {@link #diff}.
686     */
687    public int updateFrom(Configuration delta) {
688        int changed = 0;
689        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
690            changed |= ActivityInfo.CONFIG_FONT_SCALE;
691            fontScale = delta.fontScale;
692        }
693        if (delta.mcc != 0 && mcc != delta.mcc) {
694            changed |= ActivityInfo.CONFIG_MCC;
695            mcc = delta.mcc;
696        }
697        if (delta.mnc != 0 && mnc != delta.mnc) {
698            changed |= ActivityInfo.CONFIG_MNC;
699            mnc = delta.mnc;
700        }
701        if (delta.locale != null
702                && (locale == null || !locale.equals(delta.locale))) {
703            changed |= ActivityInfo.CONFIG_LOCALE;
704            locale = delta.locale != null
705                    ? (Locale) delta.locale.clone() : null;
706            // If locale has changed, then layout direction is also changed ...
707            changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
708            // ... and we need to update the layout direction (represented by the first
709            // 2 most significant bits in screenLayout).
710            setLayoutDirection(locale);
711        }
712        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
713        {
714            userSetLocale = true;
715            changed |= ActivityInfo.CONFIG_LOCALE;
716        }
717        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
718                && touchscreen != delta.touchscreen) {
719            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
720            touchscreen = delta.touchscreen;
721        }
722        if (delta.keyboard != KEYBOARD_UNDEFINED
723                && keyboard != delta.keyboard) {
724            changed |= ActivityInfo.CONFIG_KEYBOARD;
725            keyboard = delta.keyboard;
726        }
727        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
728                && keyboardHidden != delta.keyboardHidden) {
729            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
730            keyboardHidden = delta.keyboardHidden;
731        }
732        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
733                && hardKeyboardHidden != delta.hardKeyboardHidden) {
734            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
735            hardKeyboardHidden = delta.hardKeyboardHidden;
736        }
737        if (delta.navigation != NAVIGATION_UNDEFINED
738                && navigation != delta.navigation) {
739            changed |= ActivityInfo.CONFIG_NAVIGATION;
740            navigation = delta.navigation;
741        }
742        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
743                && navigationHidden != delta.navigationHidden) {
744            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
745            navigationHidden = delta.navigationHidden;
746        }
747        if (delta.orientation != ORIENTATION_UNDEFINED
748                && orientation != delta.orientation) {
749            changed |= ActivityInfo.CONFIG_ORIENTATION;
750            orientation = delta.orientation;
751        }
752        if (getScreenLayoutNoDirection(delta.screenLayout) !=
753                    (SCREENLAYOUT_SIZE_UNDEFINED | SCREENLAYOUT_LONG_UNDEFINED)
754                && (getScreenLayoutNoDirection(screenLayout) !=
755                    getScreenLayoutNoDirection(delta.screenLayout))) {
756            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
757            // We need to preserve the previous layout dir bits if they were defined
758            if ((delta.screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == 0) {
759                screenLayout = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK)|delta.screenLayout;
760            } else {
761                screenLayout = delta.screenLayout;
762            }
763        }
764        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
765                && uiMode != delta.uiMode) {
766            changed |= ActivityInfo.CONFIG_UI_MODE;
767            if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
768                uiMode = (uiMode&~UI_MODE_TYPE_MASK)
769                        | (delta.uiMode&UI_MODE_TYPE_MASK);
770            }
771            if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
772                uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
773                        | (delta.uiMode&UI_MODE_NIGHT_MASK);
774            }
775        }
776        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
777                && screenWidthDp != delta.screenWidthDp) {
778            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
779            screenWidthDp = delta.screenWidthDp;
780        }
781        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
782                && screenHeightDp != delta.screenHeightDp) {
783            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
784            screenHeightDp = delta.screenHeightDp;
785        }
786        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
787            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
788            smallestScreenWidthDp = delta.smallestScreenWidthDp;
789        }
790        if (delta.densityDpi != DENSITY_DPI_UNDEFINED) {
791            changed |= ActivityInfo.CONFIG_DENSITY;
792            densityDpi = delta.densityDpi;
793        }
794        if (delta.compatScreenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
795            compatScreenWidthDp = delta.compatScreenWidthDp;
796        }
797        if (delta.compatScreenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
798            compatScreenHeightDp = delta.compatScreenHeightDp;
799        }
800        if (delta.compatSmallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
801            compatSmallestScreenWidthDp = delta.compatSmallestScreenWidthDp;
802        }
803        if (delta.seq != 0) {
804            seq = delta.seq;
805        }
806
807        return changed;
808    }
809
810    /**
811     * Return a bit mask of the differences between this Configuration
812     * object and the given one.  Does not change the values of either.  Any
813     * undefined fields in <var>delta</var> are ignored.
814     * @return Returns a bit mask indicating which configuration
815     * values has changed, containing any combination of
816     * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
817     * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
818     * {@link android.content.pm.ActivityInfo#CONFIG_MCC
819     * PackageManager.ActivityInfo.CONFIG_MCC},
820     * {@link android.content.pm.ActivityInfo#CONFIG_MNC
821     * PackageManager.ActivityInfo.CONFIG_MNC},
822     * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
823     * PackageManager.ActivityInfo.CONFIG_LOCALE},
824     * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
825     * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
826     * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
827     * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
828     * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
829     * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
830     * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
831     * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
832     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
833     * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
834     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
835     * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or
836     * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE
837     * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}.
838     * {@link android.content.pm.ActivityInfo#CONFIG_LAYOUT_DIRECTION
839     * PackageManager.ActivityInfo.CONFIG_LAYOUT_DIRECTION}.
840     */
841    public int diff(Configuration delta) {
842        int changed = 0;
843        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
844            changed |= ActivityInfo.CONFIG_FONT_SCALE;
845        }
846        if (delta.mcc != 0 && mcc != delta.mcc) {
847            changed |= ActivityInfo.CONFIG_MCC;
848        }
849        if (delta.mnc != 0 && mnc != delta.mnc) {
850            changed |= ActivityInfo.CONFIG_MNC;
851        }
852        if (delta.locale != null
853                && (locale == null || !locale.equals(delta.locale))) {
854            changed |= ActivityInfo.CONFIG_LOCALE;
855            changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
856        }
857        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
858                && touchscreen != delta.touchscreen) {
859            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
860        }
861        if (delta.keyboard != KEYBOARD_UNDEFINED
862                && keyboard != delta.keyboard) {
863            changed |= ActivityInfo.CONFIG_KEYBOARD;
864        }
865        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
866                && keyboardHidden != delta.keyboardHidden) {
867            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
868        }
869        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
870                && hardKeyboardHidden != delta.hardKeyboardHidden) {
871            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
872        }
873        if (delta.navigation != NAVIGATION_UNDEFINED
874                && navigation != delta.navigation) {
875            changed |= ActivityInfo.CONFIG_NAVIGATION;
876        }
877        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
878                && navigationHidden != delta.navigationHidden) {
879            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
880        }
881        if (delta.orientation != ORIENTATION_UNDEFINED
882                && orientation != delta.orientation) {
883            changed |= ActivityInfo.CONFIG_ORIENTATION;
884        }
885        if (getScreenLayoutNoDirection(delta.screenLayout) !=
886                    (SCREENLAYOUT_SIZE_UNDEFINED | SCREENLAYOUT_LONG_UNDEFINED)
887                && getScreenLayoutNoDirection(screenLayout) !=
888                    getScreenLayoutNoDirection(delta.screenLayout)) {
889            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
890        }
891        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
892                && uiMode != delta.uiMode) {
893            changed |= ActivityInfo.CONFIG_UI_MODE;
894        }
895        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
896                && screenWidthDp != delta.screenWidthDp) {
897            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
898        }
899        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
900                && screenHeightDp != delta.screenHeightDp) {
901            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
902        }
903        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
904                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
905            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
906        }
907        if (delta.densityDpi != DENSITY_DPI_UNDEFINED
908                && densityDpi != delta.densityDpi) {
909            changed |= ActivityInfo.CONFIG_DENSITY;
910        }
911
912        return changed;
913    }
914
915    /**
916     * Determine if a new resource needs to be loaded from the bit set of
917     * configuration changes returned by {@link #updateFrom(Configuration)}.
918     *
919     * @param configChanges The mask of changes configurations as returned by
920     * {@link #updateFrom(Configuration)}.
921     * @param interestingChanges The configuration changes that the resource
922     * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
923     *
924     * @return Return true if the resource needs to be loaded, else false.
925     */
926    public static boolean needNewResources(int configChanges, int interestingChanges) {
927        return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
928    }
929
930    /**
931     * @hide Return true if the sequence of 'other' is better than this.  Assumes
932     * that 'this' is your current sequence and 'other' is a new one you have
933     * received some how and want to compare with what you have.
934     */
935    public boolean isOtherSeqNewer(Configuration other) {
936        if (other == null) {
937            // Sanity check.
938            return false;
939        }
940        if (other.seq == 0) {
941            // If the other sequence is not specified, then we must assume
942            // it is newer since we don't know any better.
943            return true;
944        }
945        if (seq == 0) {
946            // If this sequence is not specified, then we also consider the
947            // other is better.  Yes we have a preference for other.  Sue us.
948            return true;
949        }
950        int diff = other.seq - seq;
951        if (diff > 0x10000) {
952            // If there has been a sufficiently large jump, assume the
953            // sequence has wrapped around.
954            return false;
955        }
956        return diff > 0;
957    }
958
959    /**
960     * Parcelable methods
961     */
962    public int describeContents() {
963        return 0;
964    }
965
966    public void writeToParcel(Parcel dest, int flags) {
967        dest.writeFloat(fontScale);
968        dest.writeInt(mcc);
969        dest.writeInt(mnc);
970        if (locale == null) {
971            dest.writeInt(0);
972        } else {
973            dest.writeInt(1);
974            dest.writeString(locale.getLanguage());
975            dest.writeString(locale.getCountry());
976            dest.writeString(locale.getVariant());
977        }
978        if(userSetLocale) {
979            dest.writeInt(1);
980        } else {
981            dest.writeInt(0);
982        }
983        dest.writeInt(touchscreen);
984        dest.writeInt(keyboard);
985        dest.writeInt(keyboardHidden);
986        dest.writeInt(hardKeyboardHidden);
987        dest.writeInt(navigation);
988        dest.writeInt(navigationHidden);
989        dest.writeInt(orientation);
990        dest.writeInt(screenLayout);
991        dest.writeInt(uiMode);
992        dest.writeInt(screenWidthDp);
993        dest.writeInt(screenHeightDp);
994        dest.writeInt(smallestScreenWidthDp);
995        dest.writeInt(densityDpi);
996        dest.writeInt(compatScreenWidthDp);
997        dest.writeInt(compatScreenHeightDp);
998        dest.writeInt(compatSmallestScreenWidthDp);
999        dest.writeInt(seq);
1000    }
1001
1002    public void readFromParcel(Parcel source) {
1003        fontScale = source.readFloat();
1004        mcc = source.readInt();
1005        mnc = source.readInt();
1006        if (source.readInt() != 0) {
1007            locale = new Locale(source.readString(), source.readString(),
1008                    source.readString());
1009        }
1010        userSetLocale = (source.readInt()==1);
1011        touchscreen = source.readInt();
1012        keyboard = source.readInt();
1013        keyboardHidden = source.readInt();
1014        hardKeyboardHidden = source.readInt();
1015        navigation = source.readInt();
1016        navigationHidden = source.readInt();
1017        orientation = source.readInt();
1018        screenLayout = source.readInt();
1019        uiMode = source.readInt();
1020        screenWidthDp = source.readInt();
1021        screenHeightDp = source.readInt();
1022        smallestScreenWidthDp = source.readInt();
1023        densityDpi = source.readInt();
1024        compatScreenWidthDp = source.readInt();
1025        compatScreenHeightDp = source.readInt();
1026        compatSmallestScreenWidthDp = source.readInt();
1027        seq = source.readInt();
1028    }
1029
1030    public static final Parcelable.Creator<Configuration> CREATOR
1031            = new Parcelable.Creator<Configuration>() {
1032        public Configuration createFromParcel(Parcel source) {
1033            return new Configuration(source);
1034        }
1035
1036        public Configuration[] newArray(int size) {
1037            return new Configuration[size];
1038        }
1039    };
1040
1041    /**
1042     * Construct this Configuration object, reading from the Parcel.
1043     */
1044    private Configuration(Parcel source) {
1045        readFromParcel(source);
1046    }
1047
1048    public int compareTo(Configuration that) {
1049        int n;
1050        float a = this.fontScale;
1051        float b = that.fontScale;
1052        if (a < b) return -1;
1053        if (a > b) return 1;
1054        n = this.mcc - that.mcc;
1055        if (n != 0) return n;
1056        n = this.mnc - that.mnc;
1057        if (n != 0) return n;
1058        if (this.locale == null) {
1059            if (that.locale != null) return 1;
1060        } else if (that.locale == null) {
1061            return -1;
1062        } else {
1063            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
1064            if (n != 0) return n;
1065            n = this.locale.getCountry().compareTo(that.locale.getCountry());
1066            if (n != 0) return n;
1067            n = this.locale.getVariant().compareTo(that.locale.getVariant());
1068            if (n != 0) return n;
1069        }
1070        n = this.touchscreen - that.touchscreen;
1071        if (n != 0) return n;
1072        n = this.keyboard - that.keyboard;
1073        if (n != 0) return n;
1074        n = this.keyboardHidden - that.keyboardHidden;
1075        if (n != 0) return n;
1076        n = this.hardKeyboardHidden - that.hardKeyboardHidden;
1077        if (n != 0) return n;
1078        n = this.navigation - that.navigation;
1079        if (n != 0) return n;
1080        n = this.navigationHidden - that.navigationHidden;
1081        if (n != 0) return n;
1082        n = this.orientation - that.orientation;
1083        if (n != 0) return n;
1084        n = this.screenLayout - that.screenLayout;
1085        if (n != 0) return n;
1086        n = this.uiMode - that.uiMode;
1087        if (n != 0) return n;
1088        n = this.screenWidthDp - that.screenWidthDp;
1089        if (n != 0) return n;
1090        n = this.screenHeightDp - that.screenHeightDp;
1091        if (n != 0) return n;
1092        n = this.smallestScreenWidthDp - that.smallestScreenWidthDp;
1093        if (n != 0) return n;
1094        n = this.densityDpi - that.densityDpi;
1095        //if (n != 0) return n;
1096        return n;
1097    }
1098
1099    public boolean equals(Configuration that) {
1100        if (that == null) return false;
1101        if (that == this) return true;
1102        return this.compareTo(that) == 0;
1103    }
1104
1105    public boolean equals(Object that) {
1106        try {
1107            return equals((Configuration)that);
1108        } catch (ClassCastException e) {
1109        }
1110        return false;
1111    }
1112
1113    public int hashCode() {
1114        int result = 17;
1115        result = 31 * result + Float.floatToIntBits(fontScale);
1116        result = 31 * result + mcc;
1117        result = 31 * result + mnc;
1118        result = 31 * result + (locale != null ? locale.hashCode() : 0);
1119        result = 31 * result + touchscreen;
1120        result = 31 * result + keyboard;
1121        result = 31 * result + keyboardHidden;
1122        result = 31 * result + hardKeyboardHidden;
1123        result = 31 * result + navigation;
1124        result = 31 * result + navigationHidden;
1125        result = 31 * result + orientation;
1126        result = 31 * result + screenLayout;
1127        result = 31 * result + uiMode;
1128        result = 31 * result + screenWidthDp;
1129        result = 31 * result + screenHeightDp;
1130        result = 31 * result + smallestScreenWidthDp;
1131        result = 31 * result + densityDpi;
1132        return result;
1133    }
1134
1135    /**
1136     * Set the locale. This is the preferred way for setting up the locale (instead of using the
1137     * direct accessor). This will also set the userLocale and layout direction according to
1138     * the locale.
1139     *
1140     * @param loc The locale. Can be null.
1141     */
1142    public void setLocale(Locale loc) {
1143        locale = loc;
1144        userSetLocale = true;
1145        setLayoutDirection(locale);
1146    }
1147
1148    /**
1149     * Return the layout direction. Will be either {@link View#LAYOUT_DIRECTION_LTR} or
1150     * {@link View#LAYOUT_DIRECTION_RTL}.
1151     *
1152     * @return the layout direction
1153     */
1154    public int getLayoutDirection() {
1155        // We need to substract one here as the configuration values are using "0" as undefined thus
1156        // having LRT set to "1" and RTL set to "2"
1157        return ((screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) >> SCREENLAYOUT_LAYOUTDIR_SHIFT) - 1;
1158    }
1159
1160    /**
1161     * Set the layout direction from the Locale.
1162     *
1163     * @param locale The Locale. If null will set the layout direction to
1164     * {@link View#LAYOUT_DIRECTION_LTR}. If not null will set it to the layout direction
1165     * corresponding to the Locale.
1166     *
1167     * @see {@link View#LAYOUT_DIRECTION_LTR} and {@link View#LAYOUT_DIRECTION_RTL}
1168     */
1169    public void setLayoutDirection(Locale locale) {
1170        // There is a "1" difference between the configuration values for
1171        // layout direction and View constants for layout direction, just add "1".
1172        final int layoutDirection = 1 + LocaleUtil.getLayoutDirectionFromLocale(locale);
1173        screenLayout = (screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK)|
1174                (layoutDirection << SCREENLAYOUT_LAYOUTDIR_SHIFT);
1175    }
1176
1177    private static int getScreenLayoutNoDirection(int screenLayout) {
1178        return screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK;
1179    }
1180}
1181