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