Configuration.java revision e360bb6bd8ea84b8d37604fc11dfc744b18fb0de
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.res;
18
19import android.content.pm.ActivityInfo;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Locale;
24
25/**
26 * This class describes all device configuration information that can
27 * impact the resources the application retrieves.  This includes both
28 * user-specified configuration options (locale and scaling) as well
29 * as device configurations (such as input modes, screen size and screen orientation).
30 * <p>You can acquire this object from {@link Resources}, using {@link
31 * Resources#getConfiguration}. Thus, from an activity, you can get it by chaining the request
32 * with {@link android.app.Activity#getResources}:</p>
33 * <pre>Configuration config = getResources().getConfiguration();</pre>
34 */
35public final class Configuration implements Parcelable, Comparable<Configuration> {
36    /**
37     * Current user preference for the scaling factor for fonts, relative
38     * to the base density scaling.
39     */
40    public float fontScale;
41
42    /**
43     * IMSI MCC (Mobile Country Code).  0 if undefined.
44     */
45    public int mcc;
46
47    /**
48     * IMSI MNC (Mobile Network Code).  0 if undefined.
49     */
50    public int mnc;
51
52    /**
53     * Current user preference for the locale.
54     */
55    public Locale locale;
56
57    /**
58     * Locale should persist on setting.  This is hidden because it is really
59     * questionable whether this is the right way to expose the functionality.
60     * @hide
61     */
62    public boolean userSetLocale;
63
64    /** Constant for {@link #screenLayout}: bits that encode the size. */
65    public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
66    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
67     * value indicating that no size has been set. */
68    public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
69    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
70     * value indicating the screen is at least approximately 320x426 dp units.
71     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
72     * Multiple Screens</a> for more information. */
73    public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
74    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
75     * value indicating the screen is at least approximately 320x470 dp units.
76     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
77     * Multiple Screens</a> for more information. */
78    public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
79    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
80     * value indicating the screen is at least approximately 480x640 dp units.
81     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
82     * Multiple Screens</a> for more information. */
83    public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
84    /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
85     * value indicating the screen is at least approximately 720x960 dp units.
86     * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
87     * Multiple Screens</a> for more information.*/
88    public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
89
90    public static final int SCREENLAYOUT_LONG_MASK = 0x30;
91    public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
92    public static final int SCREENLAYOUT_LONG_NO = 0x10;
93    public static final int SCREENLAYOUT_LONG_YES = 0x20;
94
95    /**
96     * Special flag we generate to indicate that the screen layout requires
97     * us to use a compatibility mode for apps that are not modern layout
98     * aware.
99     * @hide
100     */
101    public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
102
103    /**
104     * Bit mask of overall layout of the screen.  Currently there are two
105     * fields:
106     * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
107     * of the screen.  They may be one of
108     * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
109     * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.
110     *
111     * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
112     * is wider/taller than normal.  They may be one of
113     * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.
114     *
115     * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
116     * Multiple Screens</a> for more information.
117     */
118    public int screenLayout;
119
120    /**
121     * Check if the Configuration's current {@link #screenLayout} is at
122     * least the given size.
123     *
124     * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
125     * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
126     * {@link #SCREENLAYOUT_SIZE_XLARGE}.
127     * @return Returns true if the current screen layout size is at least
128     * the given size.
129     */
130    public boolean isLayoutSizeAtLeast(int size) {
131        int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
132        if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
133        return cur >= size;
134    }
135
136    public static final int TOUCHSCREEN_UNDEFINED = 0;
137    public static final int TOUCHSCREEN_NOTOUCH = 1;
138    public static final int TOUCHSCREEN_STYLUS = 2;
139    public static final int TOUCHSCREEN_FINGER = 3;
140
141    /**
142     * The kind of touch screen attached to the device.
143     * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS},
144     * {@link #TOUCHSCREEN_FINGER}.
145     */
146    public int touchscreen;
147
148    public static final int KEYBOARD_UNDEFINED = 0;
149    public static final int KEYBOARD_NOKEYS = 1;
150    public static final int KEYBOARD_QWERTY = 2;
151    public static final int KEYBOARD_12KEY = 3;
152
153    /**
154     * The kind of keyboard attached to the device.
155     * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
156     * {@link #KEYBOARD_12KEY}.
157     */
158    public int keyboard;
159
160    public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
161    public static final int KEYBOARDHIDDEN_NO = 1;
162    public static final int KEYBOARDHIDDEN_YES = 2;
163    /** Constant matching actual resource implementation. {@hide} */
164    public static final int KEYBOARDHIDDEN_SOFT = 3;
165
166    /**
167     * A flag indicating whether any keyboard is available.  Unlike
168     * {@link #hardKeyboardHidden}, this also takes into account a soft
169     * keyboard, so if the hard keyboard is hidden but there is soft
170     * keyboard available, it will be set to NO.  Value is one of:
171     * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
172     */
173    public int keyboardHidden;
174
175    public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
176    public static final int HARDKEYBOARDHIDDEN_NO = 1;
177    public static final int HARDKEYBOARDHIDDEN_YES = 2;
178
179    /**
180     * A flag indicating whether the hard keyboard has been hidden.  This will
181     * be set on a device with a mechanism to hide the keyboard from the
182     * user, when that mechanism is closed.  One of:
183     * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
184     */
185    public int hardKeyboardHidden;
186
187    public static final int NAVIGATION_UNDEFINED = 0;
188    public static final int NAVIGATION_NONAV = 1;
189    public static final int NAVIGATION_DPAD = 2;
190    public static final int NAVIGATION_TRACKBALL = 3;
191    public static final int NAVIGATION_WHEEL = 4;
192
193    /**
194     * The kind of navigation method available on the device.
195     * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
196     * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
197     */
198    public int navigation;
199
200    public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
201    public static final int NAVIGATIONHIDDEN_NO = 1;
202    public static final int NAVIGATIONHIDDEN_YES = 2;
203
204    /**
205     * A flag indicating whether any 5-way or DPAD navigation available.
206     * This will be set on a device with a mechanism to hide the navigation
207     * controls from the user, when that mechanism is closed.  One of:
208     * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
209     */
210    public int navigationHidden;
211
212    public static final int ORIENTATION_UNDEFINED = 0;
213    public static final int ORIENTATION_PORTRAIT = 1;
214    public static final int ORIENTATION_LANDSCAPE = 2;
215    public static final int ORIENTATION_SQUARE = 3;
216
217    /**
218     * Overall orientation of the screen.  May be one of
219     * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
220     * or {@link #ORIENTATION_SQUARE}.
221     */
222    public int orientation;
223
224    public static final int UI_MODE_TYPE_MASK = 0x0f;
225    public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
226    public static final int UI_MODE_TYPE_NORMAL = 0x01;
227    public static final int UI_MODE_TYPE_DESK = 0x02;
228    public static final int UI_MODE_TYPE_CAR = 0x03;
229    public static final int UI_MODE_TYPE_TELEVISION = 0x04;
230
231    public static final int UI_MODE_NIGHT_MASK = 0x30;
232    public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
233    public static final int UI_MODE_NIGHT_NO = 0x10;
234    public static final int UI_MODE_NIGHT_YES = 0x20;
235
236    /**
237     * Bit mask of the ui mode.  Currently there are two fields:
238     * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
239     * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
240     * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
241     * or {@link #UI_MODE_TYPE_CAR}.
242     *
243     * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
244     * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
245     * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
246     */
247    public int uiMode;
248
249    public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
250
251    /**
252     * The current width of the available screen space, in dp units.
253     */
254    public int screenWidthDp;
255
256    public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0;
257
258    /**
259     * The current height of the available screen space, in dp units.
260     */
261    public int screenHeightDp;
262
263    public static final int SMALLEST_SCREEN_WIDTH_DP_UNDEFINED = 0;
264
265    /**
266     * The smallest screen size an application will see in normal operation.
267     * This is the smallest value of both screenWidthDp and screenHeightDp
268     * in both portrait and landscape.
269     */
270    public int smallestScreenWidthDp;
271
272    /**
273     * @hide Internal book-keeping.
274     */
275    public int seq;
276
277    /**
278     * Construct an invalid Configuration.  You must call {@link #setToDefaults}
279     * for this object to be valid.  {@more}
280     */
281    public Configuration() {
282        setToDefaults();
283    }
284
285    /**
286     * Makes a deep copy suitable for modification.
287     */
288    public Configuration(Configuration o) {
289        setTo(o);
290    }
291
292    public void setTo(Configuration o) {
293        fontScale = o.fontScale;
294        mcc = o.mcc;
295        mnc = o.mnc;
296        if (o.locale != null) {
297            locale = (Locale) o.locale.clone();
298        }
299        userSetLocale = o.userSetLocale;
300        touchscreen = o.touchscreen;
301        keyboard = o.keyboard;
302        keyboardHidden = o.keyboardHidden;
303        hardKeyboardHidden = o.hardKeyboardHidden;
304        navigation = o.navigation;
305        navigationHidden = o.navigationHidden;
306        orientation = o.orientation;
307        screenLayout = o.screenLayout;
308        uiMode = o.uiMode;
309        screenWidthDp = o.screenWidthDp;
310        screenHeightDp = o.screenHeightDp;
311        smallestScreenWidthDp = o.smallestScreenWidthDp;
312        seq = o.seq;
313    }
314
315    public String toString() {
316        StringBuilder sb = new StringBuilder(128);
317        sb.append("{");
318        sb.append(fontScale);
319        sb.append("x imsi=");
320        sb.append(mcc);
321        sb.append("/");
322        sb.append(mnc);
323        if (locale != null) {
324            sb.append(" ");
325            sb.append(locale);
326        } else {
327            sb.append(" (no locale)");
328        }
329        if (smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
330            sb.append(" sw"); sb.append(smallestScreenWidthDp); sb.append("dp");
331        } else {
332            sb.append("?swdp");
333        }
334        if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
335            sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
336        } else {
337            sb.append("?wdp");
338        }
339        if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
340            sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
341        } else {
342            sb.append("?hdp");
343        }
344        switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
345            case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
346            case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
347            case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
348            case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
349            case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
350            default: sb.append(" layoutSize=");
351                    sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
352        }
353        switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
354            case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
355            case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
356            case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
357            default: sb.append(" layoutLong=");
358                    sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
359        }
360        switch (orientation) {
361            case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
362            case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
363            case ORIENTATION_PORTRAIT: sb.append(" port"); break;
364            default: sb.append(" orien="); sb.append(orientation); break;
365        }
366        switch ((uiMode&UI_MODE_TYPE_MASK)) {
367            case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
368            case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
369            case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
370            case UI_MODE_TYPE_CAR: sb.append(" car"); break;
371            case UI_MODE_TYPE_TELEVISION: sb.append(" television"); break;
372            default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
373        }
374        switch ((uiMode&UI_MODE_NIGHT_MASK)) {
375            case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
376            case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
377            case UI_MODE_NIGHT_YES: sb.append(" night"); break;
378            default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
379        }
380        switch (touchscreen) {
381            case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
382            case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
383            case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
384            case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
385            default: sb.append(" touch="); sb.append(touchscreen); break;
386        }
387        switch (keyboard) {
388            case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
389            case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
390            case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
391            case KEYBOARD_12KEY: sb.append(" 12key"); break;
392            default: sb.append(" keys="); sb.append(keyboard); break;
393        }
394        switch (keyboardHidden) {
395            case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
396            case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
397            case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
398            case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
399            default: sb.append("/"); sb.append(keyboardHidden); break;
400        }
401        switch (hardKeyboardHidden) {
402            case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
403            case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
404            case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
405            default: sb.append("/"); sb.append(hardKeyboardHidden); break;
406        }
407        switch (navigation) {
408            case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
409            case NAVIGATION_NONAV: sb.append(" -nav"); break;
410            case NAVIGATION_DPAD: sb.append(" dpad"); break;
411            case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
412            case NAVIGATION_WHEEL: sb.append(" wheel"); break;
413            default: sb.append(" nav="); sb.append(navigation); break;
414        }
415        switch (navigationHidden) {
416            case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
417            case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
418            case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
419            default: sb.append("/"); sb.append(navigationHidden); break;
420        }
421        if (seq != 0) {
422            sb.append(" s.");
423            sb.append(seq);
424        }
425        sb.append('}');
426        return sb.toString();
427    }
428
429    /**
430     * Set this object to the system defaults.
431     */
432    public void setToDefaults() {
433        fontScale = 1;
434        mcc = mnc = 0;
435        locale = null;
436        userSetLocale = false;
437        touchscreen = TOUCHSCREEN_UNDEFINED;
438        keyboard = KEYBOARD_UNDEFINED;
439        keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
440        hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
441        navigation = NAVIGATION_UNDEFINED;
442        navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
443        orientation = ORIENTATION_UNDEFINED;
444        screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
445        uiMode = UI_MODE_TYPE_UNDEFINED;
446        screenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
447        screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
448        smallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
449        seq = 0;
450    }
451
452    /** {@hide} */
453    @Deprecated public void makeDefault() {
454        setToDefaults();
455    }
456
457    /**
458     * Copy the fields from delta into this Configuration object, keeping
459     * track of which ones have changed.  Any undefined fields in
460     * <var>delta</var> are ignored and not copied in to the current
461     * Configuration.
462     * @return Returns a bit mask of the changed fields, as per
463     * {@link #diff}.
464     */
465    public int updateFrom(Configuration delta) {
466        int changed = 0;
467        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
468            changed |= ActivityInfo.CONFIG_FONT_SCALE;
469            fontScale = delta.fontScale;
470        }
471        if (delta.mcc != 0 && mcc != delta.mcc) {
472            changed |= ActivityInfo.CONFIG_MCC;
473            mcc = delta.mcc;
474        }
475        if (delta.mnc != 0 && mnc != delta.mnc) {
476            changed |= ActivityInfo.CONFIG_MNC;
477            mnc = delta.mnc;
478        }
479        if (delta.locale != null
480                && (locale == null || !locale.equals(delta.locale))) {
481            changed |= ActivityInfo.CONFIG_LOCALE;
482            locale = delta.locale != null
483                    ? (Locale) delta.locale.clone() : null;
484        }
485        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
486        {
487            userSetLocale = true;
488            changed |= ActivityInfo.CONFIG_LOCALE;
489        }
490        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
491                && touchscreen != delta.touchscreen) {
492            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
493            touchscreen = delta.touchscreen;
494        }
495        if (delta.keyboard != KEYBOARD_UNDEFINED
496                && keyboard != delta.keyboard) {
497            changed |= ActivityInfo.CONFIG_KEYBOARD;
498            keyboard = delta.keyboard;
499        }
500        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
501                && keyboardHidden != delta.keyboardHidden) {
502            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
503            keyboardHidden = delta.keyboardHidden;
504        }
505        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
506                && hardKeyboardHidden != delta.hardKeyboardHidden) {
507            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
508            hardKeyboardHidden = delta.hardKeyboardHidden;
509        }
510        if (delta.navigation != NAVIGATION_UNDEFINED
511                && navigation != delta.navigation) {
512            changed |= ActivityInfo.CONFIG_NAVIGATION;
513            navigation = delta.navigation;
514        }
515        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
516                && navigationHidden != delta.navigationHidden) {
517            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
518            navigationHidden = delta.navigationHidden;
519        }
520        if (delta.orientation != ORIENTATION_UNDEFINED
521                && orientation != delta.orientation) {
522            changed |= ActivityInfo.CONFIG_ORIENTATION;
523            orientation = delta.orientation;
524        }
525        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
526                && screenLayout != delta.screenLayout) {
527            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
528            screenLayout = delta.screenLayout;
529        }
530        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
531                && uiMode != delta.uiMode) {
532            changed |= ActivityInfo.CONFIG_UI_MODE;
533            if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
534                uiMode = (uiMode&~UI_MODE_TYPE_MASK)
535                        | (delta.uiMode&UI_MODE_TYPE_MASK);
536            }
537            if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
538                uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
539                        | (delta.uiMode&UI_MODE_NIGHT_MASK);
540            }
541        }
542        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
543                && screenWidthDp != delta.screenWidthDp) {
544            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
545            screenWidthDp = delta.screenWidthDp;
546        }
547        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
548                && screenHeightDp != delta.screenHeightDp) {
549            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
550            screenHeightDp = delta.screenHeightDp;
551        }
552        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
553                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
554            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
555            smallestScreenWidthDp = delta.smallestScreenWidthDp;
556        }
557
558        if (delta.seq != 0) {
559            seq = delta.seq;
560        }
561
562        return changed;
563    }
564
565    /**
566     * Return a bit mask of the differences between this Configuration
567     * object and the given one.  Does not change the values of either.  Any
568     * undefined fields in <var>delta</var> are ignored.
569     * @return Returns a bit mask indicating which configuration
570     * values has changed, containing any combination of
571     * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
572     * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
573     * {@link android.content.pm.ActivityInfo#CONFIG_MCC
574     * PackageManager.ActivityInfo.CONFIG_MCC},
575     * {@link android.content.pm.ActivityInfo#CONFIG_MNC
576     * PackageManager.ActivityInfo.CONFIG_MNC},
577     * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
578     * PackageManager.ActivityInfo.CONFIG_LOCALE},
579     * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
580     * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
581     * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
582     * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
583     * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
584     * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
585     * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
586     * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
587     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
588     * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
589     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
590     * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or
591     * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE
592     * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}.
593     */
594    public int diff(Configuration delta) {
595        int changed = 0;
596        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
597            changed |= ActivityInfo.CONFIG_FONT_SCALE;
598        }
599        if (delta.mcc != 0 && mcc != delta.mcc) {
600            changed |= ActivityInfo.CONFIG_MCC;
601        }
602        if (delta.mnc != 0 && mnc != delta.mnc) {
603            changed |= ActivityInfo.CONFIG_MNC;
604        }
605        if (delta.locale != null
606                && (locale == null || !locale.equals(delta.locale))) {
607            changed |= ActivityInfo.CONFIG_LOCALE;
608        }
609        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
610                && touchscreen != delta.touchscreen) {
611            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
612        }
613        if (delta.keyboard != KEYBOARD_UNDEFINED
614                && keyboard != delta.keyboard) {
615            changed |= ActivityInfo.CONFIG_KEYBOARD;
616        }
617        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
618                && keyboardHidden != delta.keyboardHidden) {
619            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
620        }
621        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
622                && hardKeyboardHidden != delta.hardKeyboardHidden) {
623            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
624        }
625        if (delta.navigation != NAVIGATION_UNDEFINED
626                && navigation != delta.navigation) {
627            changed |= ActivityInfo.CONFIG_NAVIGATION;
628        }
629        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
630                && navigationHidden != delta.navigationHidden) {
631            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
632        }
633        if (delta.orientation != ORIENTATION_UNDEFINED
634                && orientation != delta.orientation) {
635            changed |= ActivityInfo.CONFIG_ORIENTATION;
636        }
637        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
638                && screenLayout != delta.screenLayout) {
639            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
640        }
641        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
642                && uiMode != delta.uiMode) {
643            changed |= ActivityInfo.CONFIG_UI_MODE;
644        }
645        if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
646                && screenWidthDp != delta.screenWidthDp) {
647            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
648        }
649        if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
650                && screenHeightDp != delta.screenHeightDp) {
651            changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
652        }
653        if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
654                && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
655            changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
656        }
657
658        return changed;
659    }
660
661    /**
662     * Determine if a new resource needs to be loaded from the bit set of
663     * configuration changes returned by {@link #updateFrom(Configuration)}.
664     *
665     * @param configChanges The mask of changes configurations as returned by
666     * {@link #updateFrom(Configuration)}.
667     * @param interestingChanges The configuration changes that the resource
668     * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
669     *
670     * @return Return true if the resource needs to be loaded, else false.
671     */
672    public static boolean needNewResources(int configChanges, int interestingChanges) {
673        return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
674    }
675
676    /**
677     * @hide Return true if the sequence of 'other' is better than this.  Assumes
678     * that 'this' is your current sequence and 'other' is a new one you have
679     * received some how and want to compare with what you have.
680     */
681    public boolean isOtherSeqNewer(Configuration other) {
682        if (other == null) {
683            // Sanity check.
684            return false;
685        }
686        if (other.seq == 0) {
687            // If the other sequence is not specified, then we must assume
688            // it is newer since we don't know any better.
689            return true;
690        }
691        if (seq == 0) {
692            // If this sequence is not specified, then we also consider the
693            // other is better.  Yes we have a preference for other.  Sue us.
694            return true;
695        }
696        int diff = other.seq - seq;
697        if (diff > 0x10000) {
698            // If there has been a sufficiently large jump, assume the
699            // sequence has wrapped around.
700            return false;
701        }
702        return diff > 0;
703    }
704
705    /**
706     * Parcelable methods
707     */
708    public int describeContents() {
709        return 0;
710    }
711
712    public void writeToParcel(Parcel dest, int flags) {
713        dest.writeFloat(fontScale);
714        dest.writeInt(mcc);
715        dest.writeInt(mnc);
716        if (locale == null) {
717            dest.writeInt(0);
718        } else {
719            dest.writeInt(1);
720            dest.writeString(locale.getLanguage());
721            dest.writeString(locale.getCountry());
722            dest.writeString(locale.getVariant());
723        }
724        if(userSetLocale) {
725            dest.writeInt(1);
726        } else {
727            dest.writeInt(0);
728        }
729        dest.writeInt(touchscreen);
730        dest.writeInt(keyboard);
731        dest.writeInt(keyboardHidden);
732        dest.writeInt(hardKeyboardHidden);
733        dest.writeInt(navigation);
734        dest.writeInt(navigationHidden);
735        dest.writeInt(orientation);
736        dest.writeInt(screenLayout);
737        dest.writeInt(uiMode);
738        dest.writeInt(screenWidthDp);
739        dest.writeInt(screenHeightDp);
740        dest.writeInt(smallestScreenWidthDp);
741        dest.writeInt(seq);
742    }
743
744    public void readFromParcel(Parcel source) {
745        fontScale = source.readFloat();
746        mcc = source.readInt();
747        mnc = source.readInt();
748        if (source.readInt() != 0) {
749            locale = new Locale(source.readString(), source.readString(),
750                    source.readString());
751        }
752        userSetLocale = (source.readInt()==1);
753        touchscreen = source.readInt();
754        keyboard = source.readInt();
755        keyboardHidden = source.readInt();
756        hardKeyboardHidden = source.readInt();
757        navigation = source.readInt();
758        navigationHidden = source.readInt();
759        orientation = source.readInt();
760        screenLayout = source.readInt();
761        uiMode = source.readInt();
762        screenWidthDp = source.readInt();
763        screenHeightDp = source.readInt();
764        smallestScreenWidthDp = source.readInt();
765        seq = source.readInt();
766    }
767
768    public static final Parcelable.Creator<Configuration> CREATOR
769            = new Parcelable.Creator<Configuration>() {
770        public Configuration createFromParcel(Parcel source) {
771            return new Configuration(source);
772        }
773
774        public Configuration[] newArray(int size) {
775            return new Configuration[size];
776        }
777    };
778
779    /**
780     * Construct this Configuration object, reading from the Parcel.
781     */
782    private Configuration(Parcel source) {
783        readFromParcel(source);
784    }
785
786    public int compareTo(Configuration that) {
787        int n;
788        float a = this.fontScale;
789        float b = that.fontScale;
790        if (a < b) return -1;
791        if (a > b) return 1;
792        n = this.mcc - that.mcc;
793        if (n != 0) return n;
794        n = this.mnc - that.mnc;
795        if (n != 0) return n;
796        if (this.locale == null) {
797            if (that.locale != null) return 1;
798        } else if (that.locale == null) {
799            return -1;
800        } else {
801            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
802            if (n != 0) return n;
803            n = this.locale.getCountry().compareTo(that.locale.getCountry());
804            if (n != 0) return n;
805            n = this.locale.getVariant().compareTo(that.locale.getVariant());
806            if (n != 0) return n;
807        }
808        n = this.touchscreen - that.touchscreen;
809        if (n != 0) return n;
810        n = this.keyboard - that.keyboard;
811        if (n != 0) return n;
812        n = this.keyboardHidden - that.keyboardHidden;
813        if (n != 0) return n;
814        n = this.hardKeyboardHidden - that.hardKeyboardHidden;
815        if (n != 0) return n;
816        n = this.navigation - that.navigation;
817        if (n != 0) return n;
818        n = this.navigationHidden - that.navigationHidden;
819        if (n != 0) return n;
820        n = this.orientation - that.orientation;
821        if (n != 0) return n;
822        n = this.screenLayout - that.screenLayout;
823        if (n != 0) return n;
824        n = this.uiMode - that.uiMode;
825        if (n != 0) return n;
826        n = this.screenWidthDp - that.screenWidthDp;
827        if (n != 0) return n;
828        n = this.screenHeightDp - that.screenHeightDp;
829        if (n != 0) return n;
830        n = this.smallestScreenWidthDp - that.smallestScreenWidthDp;
831        //if (n != 0) return n;
832        return n;
833    }
834
835    public boolean equals(Configuration that) {
836        if (that == null) return false;
837        if (that == this) return true;
838        return this.compareTo(that) == 0;
839    }
840
841    public boolean equals(Object that) {
842        try {
843            return equals((Configuration)that);
844        } catch (ClassCastException e) {
845        }
846        return false;
847    }
848
849    public int hashCode() {
850        int result = 17;
851        result = 31 * result + Float.floatToIntBits(fontScale);
852        result = 31 * result + mcc;
853        result = 31 * result + mnc;
854        result = 31 * result + (locale != null ? locale.hashCode() : 0);
855        result = 31 * result + touchscreen;
856        result = 31 * result + keyboard;
857        result = 31 * result + keyboardHidden;
858        result = 31 * result + hardKeyboardHidden;
859        result = 31 * result + navigation;
860        result = 31 * result + navigationHidden;
861        result = 31 * result + orientation;
862        result = 31 * result + screenLayout;
863        result = 31 * result + uiMode;
864        result = 31 * result + screenWidthDp;
865        result = 31 * result + screenHeightDp;
866        result = 31 * result + smallestScreenWidthDp;
867        return result;
868    }
869}
870