Configuration.java revision d8415f4bf061000b049143b4f48b96b2005450bb
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        if (delta.screenLayout != 0 && screenLayout != delta.screenLayout) {
790            changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
791            setLayoutDirection(locale);
792        }
793        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
794        {
795            changed |= ActivityInfo.CONFIG_LOCALE;
796            userSetLocale = true;
797        }
798        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
799                && touchscreen != delta.touchscreen) {
800            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
801            touchscreen = delta.touchscreen;
802        }
803        if (delta.keyboard != KEYBOARD_UNDEFINED
804                && keyboard != delta.keyboard) {
805            changed |= ActivityInfo.CONFIG_KEYBOARD;
806            keyboard = delta.keyboard;
807        }
808        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
809                && keyboardHidden != delta.keyboardHidden) {
810            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
811            keyboardHidden = delta.keyboardHidden;
812        }
813        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
814                && hardKeyboardHidden != delta.hardKeyboardHidden) {
815            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
816            hardKeyboardHidden = delta.hardKeyboardHidden;
817        }
818        if (delta.navigation != NAVIGATION_UNDEFINED
819                && navigation != delta.navigation) {
820            changed |= ActivityInfo.CONFIG_NAVIGATION;
821            navigation = delta.navigation;
822        }
823        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
824                && navigationHidden != delta.navigationHidden) {
825            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
826            navigationHidden = delta.navigationHidden;
827        }
828        if (delta.orientation != ORIENTATION_UNDEFINED
829                && orientation != delta.orientation) {
830            changed |= ActivityInfo.CONFIG_ORIENTATION;
831            orientation = delta.orientation;
832        }
833        if (getScreenLayoutNoDirection(delta.screenLayout) !=
834                    (SCREENLAYOUT_SIZE_UNDEFINED | SCREENLAYOUT_LONG_UNDEFINED)
835                && (getScreenLayoutNoDirection(screenLayout) !=
836                    getScreenLayoutNoDirection(delta.screenLayout))) {
837            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
838            // We need to preserve the previous layout dir bits if they were defined
839            if ((delta.screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == 0) {
840                screenLayout = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK)|delta.screenLayout;
841            } else {
842                screenLayout = delta.screenLayout;
843            }
844        }
845        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
846                && uiMode != delta.uiMode) {
847            changed |= ActivityInfo.CONFIG_UI_MODE;
848            if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
849                uiMode = (uiMode&~UI_MODE_TYPE_MASK)
850                        | (delta.uiMode&UI_MODE_TYPE_MASK);
851            }
852            if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
853                uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
854                        | (delta.uiMode&UI_MODE_NIGHT_MASK);
855            }
856        }
857        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
858                && screenWidthDp != delta.screenWidthDp) {
859            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
860            screenWidthDp = delta.screenWidthDp;
861        }
862        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
863                && screenHeightDp != delta.screenHeightDp) {
864            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
865            screenHeightDp = delta.screenHeightDp;
866        }
867        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
868                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
869            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
870            smallestScreenWidthDp = delta.smallestScreenWidthDp;
871        }
872        if (delta.densityDpi != DENSITY_DPI_UNDEFINED &&
873                densityDpi != delta.densityDpi) {
874            changed |= ActivityInfo.CONFIG_DENSITY;
875            densityDpi = delta.densityDpi;
876        }
877        if (delta.compatScreenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
878            compatScreenWidthDp = delta.compatScreenWidthDp;
879        }
880        if (delta.compatScreenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
881            compatScreenHeightDp = delta.compatScreenHeightDp;
882        }
883        if (delta.compatSmallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
884            compatSmallestScreenWidthDp = delta.compatSmallestScreenWidthDp;
885        }
886        if (delta.seq != 0) {
887            seq = delta.seq;
888        }
889
890        return changed;
891    }
892
893    /**
894     * Return a bit mask of the differences between this Configuration
895     * object and the given one.  Does not change the values of either.  Any
896     * undefined fields in <var>delta</var> are ignored.
897     * @return Returns a bit mask indicating which configuration
898     * values has changed, containing any combination of
899     * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
900     * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
901     * {@link android.content.pm.ActivityInfo#CONFIG_MCC
902     * PackageManager.ActivityInfo.CONFIG_MCC},
903     * {@link android.content.pm.ActivityInfo#CONFIG_MNC
904     * PackageManager.ActivityInfo.CONFIG_MNC},
905     * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
906     * PackageManager.ActivityInfo.CONFIG_LOCALE},
907     * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
908     * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
909     * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
910     * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
911     * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
912     * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
913     * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
914     * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
915     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
916     * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
917     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
918     * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or
919     * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE
920     * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}.
921     * {@link android.content.pm.ActivityInfo#CONFIG_LAYOUT_DIRECTION
922     * PackageManager.ActivityInfo.CONFIG_LAYOUT_DIRECTION}.
923     */
924    public int diff(Configuration delta) {
925        int changed = 0;
926        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
927            changed |= ActivityInfo.CONFIG_FONT_SCALE;
928        }
929        if (delta.mcc != 0 && mcc != delta.mcc) {
930            changed |= ActivityInfo.CONFIG_MCC;
931        }
932        if (delta.mnc != 0 && mnc != delta.mnc) {
933            changed |= ActivityInfo.CONFIG_MNC;
934        }
935        if (delta.locale != null
936                && (locale == null || !locale.equals(delta.locale))) {
937            changed |= ActivityInfo.CONFIG_LOCALE;
938            changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
939        }
940        if (delta.screenLayout != 0 && screenLayout != delta.screenLayout) {
941            changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
942        }
943        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
944                && touchscreen != delta.touchscreen) {
945            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
946        }
947        if (delta.keyboard != KEYBOARD_UNDEFINED
948                && keyboard != delta.keyboard) {
949            changed |= ActivityInfo.CONFIG_KEYBOARD;
950        }
951        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
952                && keyboardHidden != delta.keyboardHidden) {
953            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
954        }
955        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
956                && hardKeyboardHidden != delta.hardKeyboardHidden) {
957            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
958        }
959        if (delta.navigation != NAVIGATION_UNDEFINED
960                && navigation != delta.navigation) {
961            changed |= ActivityInfo.CONFIG_NAVIGATION;
962        }
963        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
964                && navigationHidden != delta.navigationHidden) {
965            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
966        }
967        if (delta.orientation != ORIENTATION_UNDEFINED
968                && orientation != delta.orientation) {
969            changed |= ActivityInfo.CONFIG_ORIENTATION;
970        }
971        if (getScreenLayoutNoDirection(delta.screenLayout) !=
972                    (SCREENLAYOUT_SIZE_UNDEFINED | SCREENLAYOUT_LONG_UNDEFINED)
973                && getScreenLayoutNoDirection(screenLayout) !=
974                    getScreenLayoutNoDirection(delta.screenLayout)) {
975            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
976        }
977        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
978                && uiMode != delta.uiMode) {
979            changed |= ActivityInfo.CONFIG_UI_MODE;
980        }
981        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
982                && screenWidthDp != delta.screenWidthDp) {
983            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
984        }
985        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
986                && screenHeightDp != delta.screenHeightDp) {
987            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
988        }
989        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
990                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
991            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
992        }
993        if (delta.densityDpi != DENSITY_DPI_UNDEFINED
994                && densityDpi != delta.densityDpi) {
995            changed |= ActivityInfo.CONFIG_DENSITY;
996        }
997
998        return changed;
999    }
1000
1001    /**
1002     * Determine if a new resource needs to be loaded from the bit set of
1003     * configuration changes returned by {@link #updateFrom(Configuration)}.
1004     *
1005     * @param configChanges The mask of changes configurations as returned by
1006     * {@link #updateFrom(Configuration)}.
1007     * @param interestingChanges The configuration changes that the resource
1008     * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
1009     *
1010     * @return Return true if the resource needs to be loaded, else false.
1011     */
1012    public static boolean needNewResources(int configChanges, int interestingChanges) {
1013        return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
1014    }
1015
1016    /**
1017     * @hide Return true if the sequence of 'other' is better than this.  Assumes
1018     * that 'this' is your current sequence and 'other' is a new one you have
1019     * received some how and want to compare with what you have.
1020     */
1021    public boolean isOtherSeqNewer(Configuration other) {
1022        if (other == null) {
1023            // Sanity check.
1024            return false;
1025        }
1026        if (other.seq == 0) {
1027            // If the other sequence is not specified, then we must assume
1028            // it is newer since we don't know any better.
1029            return true;
1030        }
1031        if (seq == 0) {
1032            // If this sequence is not specified, then we also consider the
1033            // other is better.  Yes we have a preference for other.  Sue us.
1034            return true;
1035        }
1036        int diff = other.seq - seq;
1037        if (diff > 0x10000) {
1038            // If there has been a sufficiently large jump, assume the
1039            // sequence has wrapped around.
1040            return false;
1041        }
1042        return diff > 0;
1043    }
1044
1045    /**
1046     * Parcelable methods
1047     */
1048    public int describeContents() {
1049        return 0;
1050    }
1051
1052    public void writeToParcel(Parcel dest, int flags) {
1053        dest.writeFloat(fontScale);
1054        dest.writeInt(mcc);
1055        dest.writeInt(mnc);
1056        if (locale == null) {
1057            dest.writeInt(0);
1058        } else {
1059            dest.writeInt(1);
1060            dest.writeString(locale.getLanguage());
1061            dest.writeString(locale.getCountry());
1062            dest.writeString(locale.getVariant());
1063        }
1064        if(userSetLocale) {
1065            dest.writeInt(1);
1066        } else {
1067            dest.writeInt(0);
1068        }
1069        dest.writeInt(touchscreen);
1070        dest.writeInt(keyboard);
1071        dest.writeInt(keyboardHidden);
1072        dest.writeInt(hardKeyboardHidden);
1073        dest.writeInt(navigation);
1074        dest.writeInt(navigationHidden);
1075        dest.writeInt(orientation);
1076        dest.writeInt(screenLayout);
1077        dest.writeInt(uiMode);
1078        dest.writeInt(screenWidthDp);
1079        dest.writeInt(screenHeightDp);
1080        dest.writeInt(smallestScreenWidthDp);
1081        dest.writeInt(densityDpi);
1082        dest.writeInt(compatScreenWidthDp);
1083        dest.writeInt(compatScreenHeightDp);
1084        dest.writeInt(compatSmallestScreenWidthDp);
1085        dest.writeInt(seq);
1086    }
1087
1088    public void readFromParcel(Parcel source) {
1089        fontScale = source.readFloat();
1090        mcc = source.readInt();
1091        mnc = source.readInt();
1092        if (source.readInt() != 0) {
1093            locale = new Locale(source.readString(), source.readString(),
1094                    source.readString());
1095        }
1096        userSetLocale = (source.readInt()==1);
1097        touchscreen = source.readInt();
1098        keyboard = source.readInt();
1099        keyboardHidden = source.readInt();
1100        hardKeyboardHidden = source.readInt();
1101        navigation = source.readInt();
1102        navigationHidden = source.readInt();
1103        orientation = source.readInt();
1104        screenLayout = source.readInt();
1105        uiMode = source.readInt();
1106        screenWidthDp = source.readInt();
1107        screenHeightDp = source.readInt();
1108        smallestScreenWidthDp = source.readInt();
1109        densityDpi = source.readInt();
1110        compatScreenWidthDp = source.readInt();
1111        compatScreenHeightDp = source.readInt();
1112        compatSmallestScreenWidthDp = source.readInt();
1113        seq = source.readInt();
1114    }
1115
1116    public static final Parcelable.Creator<Configuration> CREATOR
1117            = new Parcelable.Creator<Configuration>() {
1118        public Configuration createFromParcel(Parcel source) {
1119            return new Configuration(source);
1120        }
1121
1122        public Configuration[] newArray(int size) {
1123            return new Configuration[size];
1124        }
1125    };
1126
1127    /**
1128     * Construct this Configuration object, reading from the Parcel.
1129     */
1130    private Configuration(Parcel source) {
1131        readFromParcel(source);
1132    }
1133
1134    public int compareTo(Configuration that) {
1135        int n;
1136        float a = this.fontScale;
1137        float b = that.fontScale;
1138        if (a < b) return -1;
1139        if (a > b) return 1;
1140        n = this.mcc - that.mcc;
1141        if (n != 0) return n;
1142        n = this.mnc - that.mnc;
1143        if (n != 0) return n;
1144        if (this.locale == null) {
1145            if (that.locale != null) return 1;
1146        } else if (that.locale == null) {
1147            return -1;
1148        } else {
1149            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
1150            if (n != 0) return n;
1151            n = this.locale.getCountry().compareTo(that.locale.getCountry());
1152            if (n != 0) return n;
1153            n = this.locale.getVariant().compareTo(that.locale.getVariant());
1154            if (n != 0) return n;
1155        }
1156        n = this.touchscreen - that.touchscreen;
1157        if (n != 0) return n;
1158        n = this.keyboard - that.keyboard;
1159        if (n != 0) return n;
1160        n = this.keyboardHidden - that.keyboardHidden;
1161        if (n != 0) return n;
1162        n = this.hardKeyboardHidden - that.hardKeyboardHidden;
1163        if (n != 0) return n;
1164        n = this.navigation - that.navigation;
1165        if (n != 0) return n;
1166        n = this.navigationHidden - that.navigationHidden;
1167        if (n != 0) return n;
1168        n = this.orientation - that.orientation;
1169        if (n != 0) return n;
1170        n = this.screenLayout - that.screenLayout;
1171        if (n != 0) return n;
1172        n = this.uiMode - that.uiMode;
1173        if (n != 0) return n;
1174        n = this.screenWidthDp - that.screenWidthDp;
1175        if (n != 0) return n;
1176        n = this.screenHeightDp - that.screenHeightDp;
1177        if (n != 0) return n;
1178        n = this.smallestScreenWidthDp - that.smallestScreenWidthDp;
1179        if (n != 0) return n;
1180        n = this.densityDpi - that.densityDpi;
1181        //if (n != 0) return n;
1182        return n;
1183    }
1184
1185    public boolean equals(Configuration that) {
1186        if (that == null) return false;
1187        if (that == this) return true;
1188        return this.compareTo(that) == 0;
1189    }
1190
1191    public boolean equals(Object that) {
1192        try {
1193            return equals((Configuration)that);
1194        } catch (ClassCastException e) {
1195        }
1196        return false;
1197    }
1198
1199    public int hashCode() {
1200        int result = 17;
1201        result = 31 * result + Float.floatToIntBits(fontScale);
1202        result = 31 * result + mcc;
1203        result = 31 * result + mnc;
1204        result = 31 * result + (locale != null ? locale.hashCode() : 0);
1205        result = 31 * result + touchscreen;
1206        result = 31 * result + keyboard;
1207        result = 31 * result + keyboardHidden;
1208        result = 31 * result + hardKeyboardHidden;
1209        result = 31 * result + navigation;
1210        result = 31 * result + navigationHidden;
1211        result = 31 * result + orientation;
1212        result = 31 * result + screenLayout;
1213        result = 31 * result + uiMode;
1214        result = 31 * result + screenWidthDp;
1215        result = 31 * result + screenHeightDp;
1216        result = 31 * result + smallestScreenWidthDp;
1217        result = 31 * result + densityDpi;
1218        return result;
1219    }
1220
1221    /**
1222     * Set the locale. This is the preferred way for setting up the locale (instead of using the
1223     * direct accessor). This will also set the userLocale and layout direction according to
1224     * the locale.
1225     *
1226     * @param loc The locale. Can be null.
1227     */
1228    public void setLocale(Locale loc) {
1229        locale = loc;
1230        userSetLocale = true;
1231        setLayoutDirection(locale);
1232    }
1233
1234    /**
1235     * Return the layout direction. Will be either {@link View#LAYOUT_DIRECTION_LTR} or
1236     * {@link View#LAYOUT_DIRECTION_RTL}.
1237     *
1238     * @return Returns {@link View#LAYOUT_DIRECTION_RTL} if the configuration
1239     * is {@link #SCREENLAYOUT_LAYOUTDIR_RTL}, otherwise {@link View#LAYOUT_DIRECTION_LTR}.
1240     */
1241    public int getLayoutDirection() {
1242        return (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == SCREENLAYOUT_LAYOUTDIR_RTL
1243                ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
1244    }
1245
1246    /**
1247     * Set the layout direction from the Locale.
1248     *
1249     * @param locale The Locale. If null will set the layout direction to
1250     * {@link View#LAYOUT_DIRECTION_LTR}. If not null will set it to the layout direction
1251     * corresponding to the Locale.
1252     *
1253     * @see {@link View#LAYOUT_DIRECTION_LTR} and {@link View#LAYOUT_DIRECTION_RTL}
1254     */
1255    public void setLayoutDirection(Locale locale) {
1256        // There is a "1" difference between the configuration values for
1257        // layout direction and View constants for layout direction, just add "1".
1258        final int layoutDirection = 1 + TextUtils.getLayoutDirectionFromLocale(locale);
1259        screenLayout = (screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK)|
1260                (layoutDirection << SCREENLAYOUT_LAYOUTDIR_SHIFT);
1261    }
1262
1263    private static int getScreenLayoutNoDirection(int screenLayout) {
1264        return screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK;
1265    }
1266}
1267