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