Configuration.java revision e2515eebf42c763c0a2d9f873a153711778cfc17
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    /**
249     * @hide Internal book-keeping.
250     */
251    public int seq;
252
253    /**
254     * Construct an invalid Configuration.  You must call {@link #setToDefaults}
255     * for this object to be valid.  {@more}
256     */
257    public Configuration() {
258        setToDefaults();
259    }
260
261    /**
262     * Makes a deep copy suitable for modification.
263     */
264    public Configuration(Configuration o) {
265        setTo(o);
266    }
267
268    public void setTo(Configuration o) {
269        fontScale = o.fontScale;
270        mcc = o.mcc;
271        mnc = o.mnc;
272        if (o.locale != null) {
273            locale = (Locale) o.locale.clone();
274        }
275        userSetLocale = o.userSetLocale;
276        touchscreen = o.touchscreen;
277        keyboard = o.keyboard;
278        keyboardHidden = o.keyboardHidden;
279        hardKeyboardHidden = o.hardKeyboardHidden;
280        navigation = o.navigation;
281        navigationHidden = o.navigationHidden;
282        orientation = o.orientation;
283        screenLayout = o.screenLayout;
284        uiMode = o.uiMode;
285        seq = o.seq;
286    }
287
288    public String toString() {
289        StringBuilder sb = new StringBuilder(128);
290        sb.append("{ scale=");
291        sb.append(fontScale);
292        sb.append(" imsi=");
293        sb.append(mcc);
294        sb.append("/");
295        sb.append(mnc);
296        sb.append(" loc=");
297        sb.append(locale);
298        sb.append(" touch=");
299        sb.append(touchscreen);
300        sb.append(" keys=");
301        sb.append(keyboard);
302        sb.append("/");
303        sb.append(keyboardHidden);
304        sb.append("/");
305        sb.append(hardKeyboardHidden);
306        sb.append(" nav=");
307        sb.append(navigation);
308        sb.append("/");
309        sb.append(navigationHidden);
310        sb.append(" orien=");
311        switch(orientation) {
312            case ORIENTATION_LANDSCAPE:
313                sb.append("L"); break;
314            case ORIENTATION_PORTRAIT:
315                sb.append("P"); break;
316            default:
317                sb.append(orientation);
318        }
319        sb.append(" layout=0x");
320        sb.append(java.lang.Integer.toHexString(screenLayout));
321        sb.append(" uiMode=0x");
322        sb.append(java.lang.Integer.toHexString(uiMode));
323        if (seq != 0) {
324            sb.append(" seq=");
325            sb.append(seq);
326        }
327        sb.append('}');
328        return sb.toString();
329    }
330
331    /**
332     * Set this object to the system defaults.
333     */
334    public void setToDefaults() {
335        fontScale = 1;
336        mcc = mnc = 0;
337        locale = null;
338        userSetLocale = false;
339        touchscreen = TOUCHSCREEN_UNDEFINED;
340        keyboard = KEYBOARD_UNDEFINED;
341        keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
342        hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
343        navigation = NAVIGATION_UNDEFINED;
344        navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
345        orientation = ORIENTATION_UNDEFINED;
346        screenLayout = SCREENLAYOUT_SIZE_UNDEFINED;
347        uiMode = UI_MODE_TYPE_UNDEFINED;
348        seq = 0;
349    }
350
351    /** {@hide} */
352    @Deprecated public void makeDefault() {
353        setToDefaults();
354    }
355
356    /**
357     * Copy the fields from delta into this Configuration object, keeping
358     * track of which ones have changed.  Any undefined fields in
359     * <var>delta</var> are ignored and not copied in to the current
360     * Configuration.
361     * @return Returns a bit mask of the changed fields, as per
362     * {@link #diff}.
363     */
364    public int updateFrom(Configuration delta) {
365        int changed = 0;
366        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
367            changed |= ActivityInfo.CONFIG_FONT_SCALE;
368            fontScale = delta.fontScale;
369        }
370        if (delta.mcc != 0 && mcc != delta.mcc) {
371            changed |= ActivityInfo.CONFIG_MCC;
372            mcc = delta.mcc;
373        }
374        if (delta.mnc != 0 && mnc != delta.mnc) {
375            changed |= ActivityInfo.CONFIG_MNC;
376            mnc = delta.mnc;
377        }
378        if (delta.locale != null
379                && (locale == null || !locale.equals(delta.locale))) {
380            changed |= ActivityInfo.CONFIG_LOCALE;
381            locale = delta.locale != null
382                    ? (Locale) delta.locale.clone() : null;
383        }
384        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
385        {
386            userSetLocale = true;
387            changed |= ActivityInfo.CONFIG_LOCALE;
388        }
389        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
390                && touchscreen != delta.touchscreen) {
391            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
392            touchscreen = delta.touchscreen;
393        }
394        if (delta.keyboard != KEYBOARD_UNDEFINED
395                && keyboard != delta.keyboard) {
396            changed |= ActivityInfo.CONFIG_KEYBOARD;
397            keyboard = delta.keyboard;
398        }
399        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
400                && keyboardHidden != delta.keyboardHidden) {
401            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
402            keyboardHidden = delta.keyboardHidden;
403        }
404        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
405                && hardKeyboardHidden != delta.hardKeyboardHidden) {
406            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
407            hardKeyboardHidden = delta.hardKeyboardHidden;
408        }
409        if (delta.navigation != NAVIGATION_UNDEFINED
410                && navigation != delta.navigation) {
411            changed |= ActivityInfo.CONFIG_NAVIGATION;
412            navigation = delta.navigation;
413        }
414        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
415                && navigationHidden != delta.navigationHidden) {
416            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
417            navigationHidden = delta.navigationHidden;
418        }
419        if (delta.orientation != ORIENTATION_UNDEFINED
420                && orientation != delta.orientation) {
421            changed |= ActivityInfo.CONFIG_ORIENTATION;
422            orientation = delta.orientation;
423        }
424        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
425                && screenLayout != delta.screenLayout) {
426            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
427            screenLayout = delta.screenLayout;
428        }
429        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
430                && uiMode != delta.uiMode) {
431            changed |= ActivityInfo.CONFIG_UI_MODE;
432            if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
433                uiMode = (uiMode&~UI_MODE_TYPE_MASK)
434                        | (delta.uiMode&UI_MODE_TYPE_MASK);
435            }
436            if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
437                uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
438                        | (delta.uiMode&UI_MODE_NIGHT_MASK);
439            }
440        }
441
442        if (delta.seq != 0) {
443            seq = delta.seq;
444        }
445
446        return changed;
447    }
448
449    /**
450     * Return a bit mask of the differences between this Configuration
451     * object and the given one.  Does not change the values of either.  Any
452     * undefined fields in <var>delta</var> are ignored.
453     * @return Returns a bit mask indicating which configuration
454     * values has changed, containing any combination of
455     * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
456     * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
457     * {@link android.content.pm.ActivityInfo#CONFIG_MCC
458     * PackageManager.ActivityInfo.CONFIG_MCC},
459     * {@link android.content.pm.ActivityInfo#CONFIG_MNC
460     * PackageManager.ActivityInfo.CONFIG_MNC},
461     * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
462     * PackageManager.ActivityInfo.CONFIG_LOCALE},
463     * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
464     * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
465     * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
466     * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
467     * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
468     * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
469     * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
470     * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or
471     * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
472     * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}.
473     */
474    public int diff(Configuration delta) {
475        int changed = 0;
476        if (delta.fontScale > 0 && fontScale != delta.fontScale) {
477            changed |= ActivityInfo.CONFIG_FONT_SCALE;
478        }
479        if (delta.mcc != 0 && mcc != delta.mcc) {
480            changed |= ActivityInfo.CONFIG_MCC;
481        }
482        if (delta.mnc != 0 && mnc != delta.mnc) {
483            changed |= ActivityInfo.CONFIG_MNC;
484        }
485        if (delta.locale != null
486                && (locale == null || !locale.equals(delta.locale))) {
487            changed |= ActivityInfo.CONFIG_LOCALE;
488        }
489        if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
490                && touchscreen != delta.touchscreen) {
491            changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
492        }
493        if (delta.keyboard != KEYBOARD_UNDEFINED
494                && keyboard != delta.keyboard) {
495            changed |= ActivityInfo.CONFIG_KEYBOARD;
496        }
497        if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
498                && keyboardHidden != delta.keyboardHidden) {
499            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
500        }
501        if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
502                && hardKeyboardHidden != delta.hardKeyboardHidden) {
503            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
504        }
505        if (delta.navigation != NAVIGATION_UNDEFINED
506                && navigation != delta.navigation) {
507            changed |= ActivityInfo.CONFIG_NAVIGATION;
508        }
509        if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
510                && navigationHidden != delta.navigationHidden) {
511            changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
512        }
513        if (delta.orientation != ORIENTATION_UNDEFINED
514                && orientation != delta.orientation) {
515            changed |= ActivityInfo.CONFIG_ORIENTATION;
516        }
517        if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED
518                && screenLayout != delta.screenLayout) {
519            changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
520        }
521        if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
522                && uiMode != delta.uiMode) {
523            changed |= ActivityInfo.CONFIG_UI_MODE;
524        }
525
526        return changed;
527    }
528
529    /**
530     * Determine if a new resource needs to be loaded from the bit set of
531     * configuration changes returned by {@link #updateFrom(Configuration)}.
532     *
533     * @param configChanges The mask of changes configurations as returned by
534     * {@link #updateFrom(Configuration)}.
535     * @param interestingChanges The configuration changes that the resource
536     * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
537     *
538     * @return Return true if the resource needs to be loaded, else false.
539     */
540    public static boolean needNewResources(int configChanges, int interestingChanges) {
541        return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
542    }
543
544    /**
545     * @hide Return true if the sequence of 'other' is better than this.  Assumes
546     * that 'this' is your current sequence and 'other' is a new one you have
547     * received some how and want to compare with what you have.
548     */
549    public boolean isOtherSeqNewer(Configuration other) {
550        if (other == null) {
551            // Sanity check.
552            return false;
553        }
554        if (other.seq == 0) {
555            // If the other sequence is not specified, then we must assume
556            // it is newer since we don't know any better.
557            return true;
558        }
559        if (seq == 0) {
560            // If this sequence is not specified, then we also consider the
561            // other is better.  Yes we have a preference for other.  Sue us.
562            return true;
563        }
564        int diff = other.seq - seq;
565        if (diff > 0x10000) {
566            // If there has been a sufficiently large jump, assume the
567            // sequence has wrapped around.
568            return false;
569        }
570        return diff > 0;
571    }
572
573    /**
574     * Parcelable methods
575     */
576    public int describeContents() {
577        return 0;
578    }
579
580    public void writeToParcel(Parcel dest, int flags) {
581        dest.writeFloat(fontScale);
582        dest.writeInt(mcc);
583        dest.writeInt(mnc);
584        if (locale == null) {
585            dest.writeInt(0);
586        } else {
587            dest.writeInt(1);
588            dest.writeString(locale.getLanguage());
589            dest.writeString(locale.getCountry());
590            dest.writeString(locale.getVariant());
591        }
592        if(userSetLocale) {
593            dest.writeInt(1);
594        } else {
595            dest.writeInt(0);
596        }
597        dest.writeInt(touchscreen);
598        dest.writeInt(keyboard);
599        dest.writeInt(keyboardHidden);
600        dest.writeInt(hardKeyboardHidden);
601        dest.writeInt(navigation);
602        dest.writeInt(navigationHidden);
603        dest.writeInt(orientation);
604        dest.writeInt(screenLayout);
605        dest.writeInt(uiMode);
606        dest.writeInt(seq);
607    }
608
609    public void readFromParcel(Parcel source) {
610        fontScale = source.readFloat();
611        mcc = source.readInt();
612        mnc = source.readInt();
613        if (source.readInt() != 0) {
614            locale = new Locale(source.readString(), source.readString(),
615                    source.readString());
616        }
617        userSetLocale = (source.readInt()==1);
618        touchscreen = source.readInt();
619        keyboard = source.readInt();
620        keyboardHidden = source.readInt();
621        hardKeyboardHidden = source.readInt();
622        navigation = source.readInt();
623        navigationHidden = source.readInt();
624        orientation = source.readInt();
625        screenLayout = source.readInt();
626        uiMode = source.readInt();
627        seq = source.readInt();
628    }
629
630    public static final Parcelable.Creator<Configuration> CREATOR
631            = new Parcelable.Creator<Configuration>() {
632        public Configuration createFromParcel(Parcel source) {
633            return new Configuration(source);
634        }
635
636        public Configuration[] newArray(int size) {
637            return new Configuration[size];
638        }
639    };
640
641    /**
642     * Construct this Configuration object, reading from the Parcel.
643     */
644    private Configuration(Parcel source) {
645        readFromParcel(source);
646    }
647
648    public int compareTo(Configuration that) {
649        int n;
650        float a = this.fontScale;
651        float b = that.fontScale;
652        if (a < b) return -1;
653        if (a > b) return 1;
654        n = this.mcc - that.mcc;
655        if (n != 0) return n;
656        n = this.mnc - that.mnc;
657        if (n != 0) return n;
658        if (this.locale == null) {
659            if (that.locale != null) return 1;
660        } else if (that.locale == null) {
661            return -1;
662        } else {
663            n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
664            if (n != 0) return n;
665            n = this.locale.getCountry().compareTo(that.locale.getCountry());
666            if (n != 0) return n;
667            n = this.locale.getVariant().compareTo(that.locale.getVariant());
668            if (n != 0) return n;
669        }
670        n = this.touchscreen - that.touchscreen;
671        if (n != 0) return n;
672        n = this.keyboard - that.keyboard;
673        if (n != 0) return n;
674        n = this.keyboardHidden - that.keyboardHidden;
675        if (n != 0) return n;
676        n = this.hardKeyboardHidden - that.hardKeyboardHidden;
677        if (n != 0) return n;
678        n = this.navigation - that.navigation;
679        if (n != 0) return n;
680        n = this.navigationHidden - that.navigationHidden;
681        if (n != 0) return n;
682        n = this.orientation - that.orientation;
683        if (n != 0) return n;
684        n = this.screenLayout - that.screenLayout;
685        if (n != 0) return n;
686        n = this.uiMode - that.uiMode;
687        //if (n != 0) return n;
688        return n;
689    }
690
691    public boolean equals(Configuration that) {
692        if (that == null) return false;
693        if (that == this) return true;
694        return this.compareTo(that) == 0;
695    }
696
697    public boolean equals(Object that) {
698        try {
699            return equals((Configuration)that);
700        } catch (ClassCastException e) {
701        }
702        return false;
703    }
704
705    public int hashCode() {
706        int result = 17;
707        result = 31 * result + Float.floatToIntBits(fontScale);
708        result = 31 * result + mcc;
709        result = 31 * result + mnc;
710        result = 31 * result + (locale != null ? locale.hashCode() : 0);
711        result = 31 * result + touchscreen;
712        result = 31 * result + keyboard;
713        result = 31 * result + keyboardHidden;
714        result = 31 * result + hardKeyboardHidden;
715        result = 31 * result + navigation;
716        result = 31 * result + navigationHidden;
717        result = 31 * result + orientation;
718        result = 31 * result + screenLayout;
719        result = 31 * result + uiMode;
720        return result;
721    }
722}
723