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