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 
17 package android.content.res;
18 
19 import com.android.internal.util.XmlUtils;
20 
21 import org.xmlpull.v1.XmlPullParser;
22 import org.xmlpull.v1.XmlPullParserException;
23 import org.xmlpull.v1.XmlSerializer;
24 
25 import android.content.pm.ActivityInfo;
26 import android.os.Build;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 import android.text.TextUtils;
30 import android.view.View;
31 
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Locale;
35 
36 /**
37  * This class describes all device configuration information that can
38  * impact the resources the application retrieves.  This includes both
39  * user-specified configuration options (locale and scaling) as well
40  * as device configurations (such as input modes, screen size and screen orientation).
41  * <p>You can acquire this object from {@link Resources}, using {@link
42  * Resources#getConfiguration}. Thus, from an activity, you can get it by chaining the request
43  * with {@link android.app.Activity#getResources}:</p>
44  * <pre>Configuration config = getResources().getConfiguration();</pre>
45  */
46 public final class Configuration implements Parcelable, Comparable<Configuration> {
47     /** @hide */
48     public static final Configuration EMPTY = new Configuration();
49 
50     /**
51      * Current user preference for the scaling factor for fonts, relative
52      * to the base density scaling.
53      */
54     public float fontScale;
55 
56     /**
57      * IMSI MCC (Mobile Country Code), corresponding to
58      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#MccQualifier">mcc</a>
59      * resource qualifier.  0 if undefined.
60      */
61     public int mcc;
62 
63     /**
64      * IMSI MNC (Mobile Network Code), corresponding to
65      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#MccQualifier">mnc</a>
66      * resource qualifier.  0 if undefined. Note that the actual MNC may be 0; in order to check
67      * for this use the {@link #MNC_ZERO} symbol.
68      */
69     public int mnc;
70 
71     /**
72      * Constant used to to represent MNC (Mobile Network Code) zero.
73      * 0 cannot be used, since it is used to represent an undefined MNC.
74      */
75     public static final int MNC_ZERO = 0xffff;
76 
77     /**
78      * Current user preference for the locale, corresponding to
79      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#LocaleQualifier">locale</a>
80      * resource qualifier.
81      */
82     public Locale locale;
83 
84     /**
85      * Locale should persist on setting.  This is hidden because it is really
86      * questionable whether this is the right way to expose the functionality.
87      * @hide
88      */
89     public boolean userSetLocale;
90 
91     /** Constant for {@link #screenLayout}: bits that encode the size. */
92     public static final int SCREENLAYOUT_SIZE_MASK = 0x0f;
93     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
94      * value indicating that no size has been set. */
95     public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00;
96     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
97      * value indicating the screen is at least approximately 320x426 dp units,
98      * corresponds to the
99      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">small</a>
100      * resource qualifier.
101      * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
102      * Multiple Screens</a> for more information. */
103     public static final int SCREENLAYOUT_SIZE_SMALL = 0x01;
104     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
105      * value indicating the screen is at least approximately 320x470 dp units,
106      * corresponds to the
107      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">normal</a>
108      * resource qualifier.
109      * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
110      * Multiple Screens</a> for more information. */
111     public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02;
112     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
113      * value indicating the screen is at least approximately 480x640 dp units,
114      * corresponds to the
115      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">large</a>
116      * resource qualifier.
117      * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
118      * Multiple Screens</a> for more information. */
119     public static final int SCREENLAYOUT_SIZE_LARGE = 0x03;
120     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_SIZE_MASK}
121      * value indicating the screen is at least approximately 720x960 dp units,
122      * corresponds to the
123      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenSizeQualifier">xlarge</a>
124      * resource qualifier.
125      * See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
126      * Multiple Screens</a> for more information.*/
127     public static final int SCREENLAYOUT_SIZE_XLARGE = 0x04;
128 
129     /** Constant for {@link #screenLayout}: bits that encode the aspect ratio. */
130     public static final int SCREENLAYOUT_LONG_MASK = 0x30;
131     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LONG_MASK}
132      * value indicating that no size has been set. */
133     public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00;
134     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LONG_MASK}
135      * value that corresponds to the
136      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenAspectQualifier">notlong</a>
137      * resource qualifier. */
138     public static final int SCREENLAYOUT_LONG_NO = 0x10;
139     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LONG_MASK}
140      * value that corresponds to the
141      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenAspectQualifier">long</a>
142      * resource qualifier. */
143     public static final int SCREENLAYOUT_LONG_YES = 0x20;
144 
145     /** Constant for {@link #screenLayout}: bits that encode the layout direction. */
146     public static final int SCREENLAYOUT_LAYOUTDIR_MASK = 0xC0;
147     /** Constant for {@link #screenLayout}: bits shift to get the layout direction. */
148     public static final int SCREENLAYOUT_LAYOUTDIR_SHIFT = 6;
149     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LAYOUTDIR_MASK}
150      * value indicating that no layout dir has been set. */
151     public static final int SCREENLAYOUT_LAYOUTDIR_UNDEFINED = 0x00;
152     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LAYOUTDIR_MASK}
153      * value indicating that a layout dir has been set to LTR. */
154     public static final int SCREENLAYOUT_LAYOUTDIR_LTR = 0x01 << SCREENLAYOUT_LAYOUTDIR_SHIFT;
155     /** Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_LAYOUTDIR_MASK}
156      * value indicating that a layout dir has been set to RTL. */
157     public static final int SCREENLAYOUT_LAYOUTDIR_RTL = 0x02 << SCREENLAYOUT_LAYOUTDIR_SHIFT;
158 
159     /** Constant for {@link #screenLayout}: bits that encode roundness of the screen. */
160     public static final int SCREENLAYOUT_ROUND_MASK = 0x300;
161     /** @hide Constant for {@link #screenLayout}: bit shift to get to screen roundness bits */
162     public static final int SCREENLAYOUT_ROUND_SHIFT = 8;
163     /**
164      * Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_ROUND_MASK} value indicating
165      * that it is unknown whether or not the screen has a round shape.
166      */
167     public static final int SCREENLAYOUT_ROUND_UNDEFINED = 0x00;
168     /**
169      * Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_ROUND_MASK} value indicating
170      * that the screen does not have a rounded shape.
171      */
172     public static final int SCREENLAYOUT_ROUND_NO = 0x1 << SCREENLAYOUT_ROUND_SHIFT;
173     /**
174      * Constant for {@link #screenLayout}: a {@link #SCREENLAYOUT_ROUND_MASK} value indicating
175      * that the screen has a rounded shape. Corners may not be visible to the user;
176      * developers should pay special attention to the {@link android.view.WindowInsets} delivered
177      * to views for more information about ensuring content is not obscured.
178      *
179      * <p>Corresponds to the <code>-round</code> resource qualifier.</p>
180      */
181     public static final int SCREENLAYOUT_ROUND_YES = 0x2 << SCREENLAYOUT_ROUND_SHIFT;
182 
183     /** Constant for {@link #screenLayout}: a value indicating that screenLayout is undefined */
184     public static final int SCREENLAYOUT_UNDEFINED = SCREENLAYOUT_SIZE_UNDEFINED |
185             SCREENLAYOUT_LONG_UNDEFINED | SCREENLAYOUT_LAYOUTDIR_UNDEFINED |
186             SCREENLAYOUT_ROUND_UNDEFINED;
187 
188     /**
189      * Special flag we generate to indicate that the screen layout requires
190      * us to use a compatibility mode for apps that are not modern layout
191      * aware.
192      * @hide
193      */
194     public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000;
195 
196     /**
197      * Bit mask of overall layout of the screen.  Currently there are two
198      * fields:
199      * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size
200      * of the screen.  They may be one of
201      * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL},
202      * {@link #SCREENLAYOUT_SIZE_LARGE}, or {@link #SCREENLAYOUT_SIZE_XLARGE}.</p>
203      *
204      * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen
205      * is wider/taller than normal.  They may be one of
206      * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}.</p>
207      *
208      * <p>The {@link #SCREENLAYOUT_LAYOUTDIR_MASK} defines whether the screen layout
209      * is either LTR or RTL.  They may be one of
210      * {@link #SCREENLAYOUT_LAYOUTDIR_LTR} or {@link #SCREENLAYOUT_LAYOUTDIR_RTL}.</p>
211      *
212      * <p>The {@link #SCREENLAYOUT_ROUND_MASK} defines whether the screen has a rounded
213      * shape. They may be one of {@link #SCREENLAYOUT_ROUND_NO} or {@link #SCREENLAYOUT_ROUND_YES}.
214      * </p>
215      *
216      * <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting
217      * Multiple Screens</a> for more information.</p>
218      */
219     public int screenLayout;
220 
221     /** @hide */
resetScreenLayout(int curLayout)222     static public int resetScreenLayout(int curLayout) {
223         return (curLayout&~(SCREENLAYOUT_LONG_MASK | SCREENLAYOUT_SIZE_MASK
224                         | SCREENLAYOUT_COMPAT_NEEDED))
225                 | (SCREENLAYOUT_LONG_YES | SCREENLAYOUT_SIZE_XLARGE);
226     }
227 
228     /** @hide */
reduceScreenLayout(int curLayout, int longSizeDp, int shortSizeDp)229     static public int reduceScreenLayout(int curLayout, int longSizeDp, int shortSizeDp) {
230         int screenLayoutSize;
231         boolean screenLayoutLong;
232         boolean screenLayoutCompatNeeded;
233 
234         // These semi-magic numbers define our compatibility modes for
235         // applications with different screens.  These are guarantees to
236         // app developers about the space they can expect for a particular
237         // configuration.  DO NOT CHANGE!
238         if (longSizeDp < 470) {
239             // This is shorter than an HVGA normal density screen (which
240             // is 480 pixels on its long side).
241             screenLayoutSize = SCREENLAYOUT_SIZE_SMALL;
242             screenLayoutLong = false;
243             screenLayoutCompatNeeded = false;
244         } else {
245             // What size is this screen screen?
246             if (longSizeDp >= 960 && shortSizeDp >= 720) {
247                 // 1.5xVGA or larger screens at medium density are the point
248                 // at which we consider it to be an extra large screen.
249                 screenLayoutSize = SCREENLAYOUT_SIZE_XLARGE;
250             } else if (longSizeDp >= 640 && shortSizeDp >= 480) {
251                 // VGA or larger screens at medium density are the point
252                 // at which we consider it to be a large screen.
253                 screenLayoutSize = SCREENLAYOUT_SIZE_LARGE;
254             } else {
255                 screenLayoutSize = SCREENLAYOUT_SIZE_NORMAL;
256             }
257 
258             // If this screen is wider than normal HVGA, or taller
259             // than FWVGA, then for old apps we want to run in size
260             // compatibility mode.
261             if (shortSizeDp > 321 || longSizeDp > 570) {
262                 screenLayoutCompatNeeded = true;
263             } else {
264                 screenLayoutCompatNeeded = false;
265             }
266 
267             // Is this a long screen?
268             if (((longSizeDp*3)/5) >= (shortSizeDp-1)) {
269                 // Anything wider than WVGA (5:3) is considering to be long.
270                 screenLayoutLong = true;
271             } else {
272                 screenLayoutLong = false;
273             }
274         }
275 
276         // Now reduce the last screenLayout to not be better than what we
277         // have found.
278         if (!screenLayoutLong) {
279             curLayout = (curLayout&~SCREENLAYOUT_LONG_MASK) | SCREENLAYOUT_LONG_NO;
280         }
281         if (screenLayoutCompatNeeded) {
282             curLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
283         }
284         int curSize = curLayout&SCREENLAYOUT_SIZE_MASK;
285         if (screenLayoutSize < curSize) {
286             curLayout = (curLayout&~SCREENLAYOUT_SIZE_MASK) | screenLayoutSize;
287         }
288         return curLayout;
289     }
290 
291     /**
292      * Check if the Configuration's current {@link #screenLayout} is at
293      * least the given size.
294      *
295      * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
296      * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
297      * {@link #SCREENLAYOUT_SIZE_XLARGE}.
298      * @return Returns true if the current screen layout size is at least
299      * the given size.
300      */
isLayoutSizeAtLeast(int size)301     public boolean isLayoutSizeAtLeast(int size) {
302         int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
303         if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
304         return cur >= size;
305     }
306 
307     /** Constant for {@link #touchscreen}: a value indicating that no value has been set. */
308     public static final int TOUCHSCREEN_UNDEFINED = 0;
309     /** Constant for {@link #touchscreen}, value corresponding to the
310      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#TouchscreenQualifier">notouch</a>
311      * resource qualifier. */
312     public static final int TOUCHSCREEN_NOTOUCH = 1;
313     /** @deprecated Not currently supported or used. */
314     @Deprecated public static final int TOUCHSCREEN_STYLUS = 2;
315     /** Constant for {@link #touchscreen}, value corresponding to the
316      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#TouchscreenQualifier">finger</a>
317      * resource qualifier. */
318     public static final int TOUCHSCREEN_FINGER = 3;
319 
320     /**
321      * The kind of touch screen attached to the device.
322      * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_FINGER}.
323      */
324     public int touchscreen;
325 
326     /** Constant for {@link #keyboard}: a value indicating that no value has been set. */
327     public static final int KEYBOARD_UNDEFINED = 0;
328     /** Constant for {@link #keyboard}, value corresponding to the
329      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ImeQualifier">nokeys</a>
330      * resource qualifier. */
331     public static final int KEYBOARD_NOKEYS = 1;
332     /** Constant for {@link #keyboard}, value corresponding to the
333      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ImeQualifier">qwerty</a>
334      * resource qualifier. */
335     public static final int KEYBOARD_QWERTY = 2;
336     /** Constant for {@link #keyboard}, value corresponding to the
337      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ImeQualifier">12key</a>
338      * resource qualifier. */
339     public static final int KEYBOARD_12KEY = 3;
340 
341     /**
342      * The kind of keyboard attached to the device.
343      * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
344      * {@link #KEYBOARD_12KEY}.
345      */
346     public int keyboard;
347 
348     /** Constant for {@link #keyboardHidden}: a value indicating that no value has been set. */
349     public static final int KEYBOARDHIDDEN_UNDEFINED = 0;
350     /** Constant for {@link #keyboardHidden}, value corresponding to the
351      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#KeyboardAvailQualifier">keysexposed</a>
352      * resource qualifier. */
353     public static final int KEYBOARDHIDDEN_NO = 1;
354     /** Constant for {@link #keyboardHidden}, value corresponding to the
355      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#KeyboardAvailQualifier">keyshidden</a>
356      * resource qualifier. */
357     public static final int KEYBOARDHIDDEN_YES = 2;
358     /** Constant matching actual resource implementation. {@hide} */
359     public static final int KEYBOARDHIDDEN_SOFT = 3;
360 
361     /**
362      * A flag indicating whether any keyboard is available.  Unlike
363      * {@link #hardKeyboardHidden}, this also takes into account a soft
364      * keyboard, so if the hard keyboard is hidden but there is soft
365      * keyboard available, it will be set to NO.  Value is one of:
366      * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
367      */
368     public int keyboardHidden;
369 
370     /** Constant for {@link #hardKeyboardHidden}: a value indicating that no value has been set. */
371     public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0;
372     /** Constant for {@link #hardKeyboardHidden}, value corresponding to the
373      * physical keyboard being exposed. */
374     public static final int HARDKEYBOARDHIDDEN_NO = 1;
375     /** Constant for {@link #hardKeyboardHidden}, value corresponding to the
376      * physical keyboard being hidden. */
377     public static final int HARDKEYBOARDHIDDEN_YES = 2;
378 
379     /**
380      * A flag indicating whether the hard keyboard has been hidden.  This will
381      * be set on a device with a mechanism to hide the keyboard from the
382      * user, when that mechanism is closed.  One of:
383      * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}.
384      */
385     public int hardKeyboardHidden;
386 
387     /** Constant for {@link #navigation}: a value indicating that no value has been set. */
388     public static final int NAVIGATION_UNDEFINED = 0;
389     /** Constant for {@link #navigation}, value corresponding to the
390      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">nonav</a>
391      * resource qualifier. */
392     public static final int NAVIGATION_NONAV = 1;
393     /** Constant for {@link #navigation}, value corresponding to the
394      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">dpad</a>
395      * resource qualifier. */
396     public static final int NAVIGATION_DPAD = 2;
397     /** Constant for {@link #navigation}, value corresponding to the
398      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">trackball</a>
399      * resource qualifier. */
400     public static final int NAVIGATION_TRACKBALL = 3;
401     /** Constant for {@link #navigation}, value corresponding to the
402      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavigationQualifier">wheel</a>
403      * resource qualifier. */
404     public static final int NAVIGATION_WHEEL = 4;
405 
406     /**
407      * The kind of navigation method available on the device.
408      * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD},
409      * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}.
410      */
411     public int navigation;
412 
413     /** Constant for {@link #navigationHidden}: a value indicating that no value has been set. */
414     public static final int NAVIGATIONHIDDEN_UNDEFINED = 0;
415     /** Constant for {@link #navigationHidden}, value corresponding to the
416      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavAvailQualifier">navexposed</a>
417      * resource qualifier. */
418     public static final int NAVIGATIONHIDDEN_NO = 1;
419     /** Constant for {@link #navigationHidden}, value corresponding to the
420      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NavAvailQualifier">navhidden</a>
421      * resource qualifier. */
422     public static final int NAVIGATIONHIDDEN_YES = 2;
423 
424     /**
425      * A flag indicating whether any 5-way or DPAD navigation available.
426      * This will be set on a device with a mechanism to hide the navigation
427      * controls from the user, when that mechanism is closed.  One of:
428      * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}.
429      */
430     public int navigationHidden;
431 
432     /** Constant for {@link #orientation}: a value indicating that no value has been set. */
433     public static final int ORIENTATION_UNDEFINED = 0;
434     /** Constant for {@link #orientation}, value corresponding to the
435      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#OrientationQualifier">port</a>
436      * resource qualifier. */
437     public static final int ORIENTATION_PORTRAIT = 1;
438     /** Constant for {@link #orientation}, value corresponding to the
439      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#OrientationQualifier">land</a>
440      * resource qualifier. */
441     public static final int ORIENTATION_LANDSCAPE = 2;
442     /** @deprecated Not currently supported or used. */
443     @Deprecated public static final int ORIENTATION_SQUARE = 3;
444 
445     /**
446      * Overall orientation of the screen.  May be one of
447      * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT}.
448      */
449     public int orientation;
450 
451     /** Constant for {@link #uiMode}: bits that encode the mode type. */
452     public static final int UI_MODE_TYPE_MASK = 0x0f;
453     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
454      * value indicating that no mode type has been set. */
455     public static final int UI_MODE_TYPE_UNDEFINED = 0x00;
456     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
457      * value that corresponds to
458      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">no
459      * UI mode</a> resource qualifier specified. */
460     public static final int UI_MODE_TYPE_NORMAL = 0x01;
461     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
462      * value that corresponds to the
463      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">desk</a>
464      * resource qualifier. */
465     public static final int UI_MODE_TYPE_DESK = 0x02;
466     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
467      * value that corresponds to the
468      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">car</a>
469      * resource qualifier. */
470     public static final int UI_MODE_TYPE_CAR = 0x03;
471     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
472      * value that corresponds to the
473      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">television</a>
474      * resource qualifier. */
475     public static final int UI_MODE_TYPE_TELEVISION = 0x04;
476     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
477      * value that corresponds to the
478      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">appliance</a>
479      * resource qualifier. */
480     public static final int UI_MODE_TYPE_APPLIANCE = 0x05;
481     /** Constant for {@link #uiMode}: a {@link #UI_MODE_TYPE_MASK}
482      * value that corresponds to the
483      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#UiModeQualifier">watch</a>
484      * resource qualifier. */
485     public static final int UI_MODE_TYPE_WATCH = 0x06;
486 
487     /** Constant for {@link #uiMode}: bits that encode the night mode. */
488     public static final int UI_MODE_NIGHT_MASK = 0x30;
489     /** Constant for {@link #uiMode}: a {@link #UI_MODE_NIGHT_MASK}
490      * value indicating that no mode type has been set. */
491     public static final int UI_MODE_NIGHT_UNDEFINED = 0x00;
492     /** Constant for {@link #uiMode}: a {@link #UI_MODE_NIGHT_MASK}
493      * value that corresponds to the
494      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NightQualifier">notnight</a>
495      * resource qualifier. */
496     public static final int UI_MODE_NIGHT_NO = 0x10;
497     /** Constant for {@link #uiMode}: a {@link #UI_MODE_NIGHT_MASK}
498      * value that corresponds to the
499      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#NightQualifier">night</a>
500      * resource qualifier. */
501     public static final int UI_MODE_NIGHT_YES = 0x20;
502 
503     /**
504      * Bit mask of the ui mode.  Currently there are two fields:
505      * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
506      * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
507      * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
508      * {@link #UI_MODE_TYPE_CAR}, {@link #UI_MODE_TYPE_TELEVISION},
509      * {@link #UI_MODE_TYPE_APPLIANCE}, or {@link #UI_MODE_TYPE_WATCH}.
510      *
511      * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
512      * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
513      * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
514      */
515     public int uiMode;
516 
517     /**
518      * Default value for {@link #screenWidthDp} indicating that no width
519      * has been specified.
520      */
521     public static final int SCREEN_WIDTH_DP_UNDEFINED = 0;
522 
523     /**
524      * The current width of the available screen space, in dp units,
525      * corresponding to
526      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenWidthQualifier">screen
527      * width</a> resource qualifier.  Set to
528      * {@link #SCREEN_WIDTH_DP_UNDEFINED} if no width is specified.
529      */
530     public int screenWidthDp;
531 
532     /**
533      * Default value for {@link #screenHeightDp} indicating that no width
534      * has been specified.
535      */
536     public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0;
537 
538     /**
539      * The current height of the available screen space, in dp units,
540      * corresponding to
541      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#ScreenHeightQualifier">screen
542      * height</a> resource qualifier.  Set to
543      * {@link #SCREEN_HEIGHT_DP_UNDEFINED} if no height is specified.
544      */
545     public int screenHeightDp;
546 
547     /**
548      * Default value for {@link #smallestScreenWidthDp} indicating that no width
549      * has been specified.
550      */
551     public static final int SMALLEST_SCREEN_WIDTH_DP_UNDEFINED = 0;
552 
553     /**
554      * The smallest screen size an application will see in normal operation,
555      * corresponding to
556      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#SmallestScreenWidthQualifier">smallest
557      * screen width</a> resource qualifier.
558      * This is the smallest value of both screenWidthDp and screenHeightDp
559      * in both portrait and landscape.  Set to
560      * {@link #SMALLEST_SCREEN_WIDTH_DP_UNDEFINED} if no width is specified.
561      */
562     public int smallestScreenWidthDp;
563 
564     /**
565      * Default value for {@link #densityDpi} indicating that no width
566      * has been specified.
567      */
568     public static final int DENSITY_DPI_UNDEFINED = 0;
569 
570     /**
571      * Value for {@link #densityDpi} for resources that scale to any density (vector drawables).
572      * {@hide}
573      */
574     public static final int DENSITY_DPI_ANY = 0xfffe;
575 
576     /**
577      * Value for {@link #densityDpi} for resources that are not meant to be scaled.
578      * {@hide}
579      */
580     public static final int DENSITY_DPI_NONE = 0xffff;
581 
582     /**
583      * The target screen density being rendered to,
584      * corresponding to
585      * <a href="{@docRoot}guide/topics/resources/providing-resources.html#DensityQualifier">density</a>
586      * resource qualifier.  Set to
587      * {@link #DENSITY_DPI_UNDEFINED} if no density is specified.
588      */
589     public int densityDpi;
590 
591     /** @hide Hack to get this information from WM to app running in compat mode. */
592     public int compatScreenWidthDp;
593     /** @hide Hack to get this information from WM to app running in compat mode. */
594     public int compatScreenHeightDp;
595     /** @hide Hack to get this information from WM to app running in compat mode. */
596     public int compatSmallestScreenWidthDp;
597 
598     /**
599      * @hide Internal book-keeping.
600      */
601     public int seq;
602 
603     /** @hide Native-specific bit mask for MCC config; DO NOT USE UNLESS YOU ARE SURE. */
604     public static final int NATIVE_CONFIG_MCC = 0x0001;
605     /** @hide Native-specific bit mask for MNC config; DO NOT USE UNLESS YOU ARE SURE. */
606     public static final int NATIVE_CONFIG_MNC = 0x0002;
607     /** @hide Native-specific bit mask for LOCALE config; DO NOT USE UNLESS YOU ARE SURE. */
608     public static final int NATIVE_CONFIG_LOCALE = 0x0004;
609     /** @hide Native-specific bit mask for TOUCHSCREEN config; DO NOT USE UNLESS YOU ARE SURE. */
610     public static final int NATIVE_CONFIG_TOUCHSCREEN = 0x0008;
611     /** @hide Native-specific bit mask for KEYBOARD config; DO NOT USE UNLESS YOU ARE SURE. */
612     public static final int NATIVE_CONFIG_KEYBOARD = 0x0010;
613     /** @hide Native-specific bit mask for KEYBOARD_HIDDEN config; DO NOT USE UNLESS YOU
614      * ARE SURE. */
615     public static final int NATIVE_CONFIG_KEYBOARD_HIDDEN = 0x0020;
616     /** @hide Native-specific bit mask for NAVIGATION config; DO NOT USE UNLESS YOU ARE SURE. */
617     public static final int NATIVE_CONFIG_NAVIGATION = 0x0040;
618     /** @hide Native-specific bit mask for ORIENTATION config; DO NOT USE UNLESS YOU ARE SURE. */
619     public static final int NATIVE_CONFIG_ORIENTATION = 0x0080;
620     /** @hide Native-specific bit mask for DENSITY config; DO NOT USE UNLESS YOU ARE SURE. */
621     public static final int NATIVE_CONFIG_DENSITY = 0x0100;
622     /** @hide Native-specific bit mask for SCREEN_SIZE config; DO NOT USE UNLESS YOU ARE SURE. */
623     public static final int NATIVE_CONFIG_SCREEN_SIZE = 0x0200;
624     /** @hide Native-specific bit mask for VERSION config; DO NOT USE UNLESS YOU ARE SURE. */
625     public static final int NATIVE_CONFIG_VERSION = 0x0400;
626     /** @hide Native-specific bit mask for SCREEN_LAYOUT config; DO NOT USE UNLESS YOU ARE SURE. */
627     public static final int NATIVE_CONFIG_SCREEN_LAYOUT = 0x0800;
628     /** @hide Native-specific bit mask for UI_MODE config; DO NOT USE UNLESS YOU ARE SURE. */
629     public static final int NATIVE_CONFIG_UI_MODE = 0x1000;
630     /** @hide Native-specific bit mask for SMALLEST_SCREEN_SIZE config; DO NOT USE UNLESS YOU
631      * ARE SURE. */
632     public static final int NATIVE_CONFIG_SMALLEST_SCREEN_SIZE = 0x2000;
633     /** @hide Native-specific bit mask for LAYOUTDIR config ; DO NOT USE UNLESS YOU ARE SURE.*/
634     public static final int NATIVE_CONFIG_LAYOUTDIR = 0x4000;
635 
636     /**
637      * Construct an invalid Configuration.  You must call {@link #setToDefaults}
638      * for this object to be valid.  {@more}
639      */
Configuration()640     public Configuration() {
641         setToDefaults();
642     }
643 
644     /**
645      * Makes a deep copy suitable for modification.
646      */
Configuration(Configuration o)647     public Configuration(Configuration o) {
648         setTo(o);
649     }
650 
setTo(Configuration o)651     public void setTo(Configuration o) {
652         fontScale = o.fontScale;
653         mcc = o.mcc;
654         mnc = o.mnc;
655         if (o.locale != null) {
656             locale = (Locale) o.locale.clone();
657         }
658         userSetLocale = o.userSetLocale;
659         touchscreen = o.touchscreen;
660         keyboard = o.keyboard;
661         keyboardHidden = o.keyboardHidden;
662         hardKeyboardHidden = o.hardKeyboardHidden;
663         navigation = o.navigation;
664         navigationHidden = o.navigationHidden;
665         orientation = o.orientation;
666         screenLayout = o.screenLayout;
667         uiMode = o.uiMode;
668         screenWidthDp = o.screenWidthDp;
669         screenHeightDp = o.screenHeightDp;
670         smallestScreenWidthDp = o.smallestScreenWidthDp;
671         densityDpi = o.densityDpi;
672         compatScreenWidthDp = o.compatScreenWidthDp;
673         compatScreenHeightDp = o.compatScreenHeightDp;
674         compatSmallestScreenWidthDp = o.compatSmallestScreenWidthDp;
675         seq = o.seq;
676     }
677 
toString()678     public String toString() {
679         StringBuilder sb = new StringBuilder(128);
680         sb.append("{");
681         sb.append(fontScale);
682         sb.append(" ");
683         if (mcc != 0) {
684             sb.append(mcc);
685             sb.append("mcc");
686         } else {
687             sb.append("?mcc");
688         }
689         if (mnc != 0) {
690             sb.append(mnc);
691             sb.append("mnc");
692         } else {
693             sb.append("?mnc");
694         }
695         if (locale != null) {
696             sb.append(" ");
697             sb.append(locale);
698         } else {
699             sb.append(" ?locale");
700         }
701         int layoutDir = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK);
702         switch (layoutDir) {
703             case SCREENLAYOUT_LAYOUTDIR_UNDEFINED: sb.append(" ?layoutDir"); break;
704             case SCREENLAYOUT_LAYOUTDIR_LTR: sb.append(" ldltr"); break;
705             case SCREENLAYOUT_LAYOUTDIR_RTL: sb.append(" ldrtl"); break;
706             default: sb.append(" layoutDir=");
707                 sb.append(layoutDir >> SCREENLAYOUT_LAYOUTDIR_SHIFT); break;
708         }
709         if (smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
710             sb.append(" sw"); sb.append(smallestScreenWidthDp); sb.append("dp");
711         } else {
712             sb.append(" ?swdp");
713         }
714         if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
715             sb.append(" w"); sb.append(screenWidthDp); sb.append("dp");
716         } else {
717             sb.append(" ?wdp");
718         }
719         if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
720             sb.append(" h"); sb.append(screenHeightDp); sb.append("dp");
721         } else {
722             sb.append(" ?hdp");
723         }
724         if (densityDpi != DENSITY_DPI_UNDEFINED) {
725             sb.append(" "); sb.append(densityDpi); sb.append("dpi");
726         } else {
727             sb.append(" ?density");
728         }
729         switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) {
730             case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break;
731             case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break;
732             case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break;
733             case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break;
734             case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break;
735             default: sb.append(" layoutSize=");
736                     sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break;
737         }
738         switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) {
739             case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break;
740             case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break;
741             case SCREENLAYOUT_LONG_YES: sb.append(" long"); break;
742             default: sb.append(" layoutLong=");
743                     sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break;
744         }
745         switch (orientation) {
746             case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break;
747             case ORIENTATION_LANDSCAPE: sb.append(" land"); break;
748             case ORIENTATION_PORTRAIT: sb.append(" port"); break;
749             default: sb.append(" orien="); sb.append(orientation); break;
750         }
751         switch ((uiMode&UI_MODE_TYPE_MASK)) {
752             case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break;
753             case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break;
754             case UI_MODE_TYPE_DESK: sb.append(" desk"); break;
755             case UI_MODE_TYPE_CAR: sb.append(" car"); break;
756             case UI_MODE_TYPE_TELEVISION: sb.append(" television"); break;
757             case UI_MODE_TYPE_APPLIANCE: sb.append(" appliance"); break;
758             case UI_MODE_TYPE_WATCH: sb.append(" watch"); break;
759             default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break;
760         }
761         switch ((uiMode&UI_MODE_NIGHT_MASK)) {
762             case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break;
763             case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break;
764             case UI_MODE_NIGHT_YES: sb.append(" night"); break;
765             default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break;
766         }
767         switch (touchscreen) {
768             case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break;
769             case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break;
770             case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break;
771             case TOUCHSCREEN_FINGER: sb.append(" finger"); break;
772             default: sb.append(" touch="); sb.append(touchscreen); break;
773         }
774         switch (keyboard) {
775             case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break;
776             case KEYBOARD_NOKEYS: sb.append(" -keyb"); break;
777             case KEYBOARD_QWERTY: sb.append(" qwerty"); break;
778             case KEYBOARD_12KEY: sb.append(" 12key"); break;
779             default: sb.append(" keys="); sb.append(keyboard); break;
780         }
781         switch (keyboardHidden) {
782             case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
783             case KEYBOARDHIDDEN_NO: sb.append("/v"); break;
784             case KEYBOARDHIDDEN_YES: sb.append("/h"); break;
785             case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break;
786             default: sb.append("/"); sb.append(keyboardHidden); break;
787         }
788         switch (hardKeyboardHidden) {
789             case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break;
790             case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break;
791             case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break;
792             default: sb.append("/"); sb.append(hardKeyboardHidden); break;
793         }
794         switch (navigation) {
795             case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break;
796             case NAVIGATION_NONAV: sb.append(" -nav"); break;
797             case NAVIGATION_DPAD: sb.append(" dpad"); break;
798             case NAVIGATION_TRACKBALL: sb.append(" tball"); break;
799             case NAVIGATION_WHEEL: sb.append(" wheel"); break;
800             default: sb.append(" nav="); sb.append(navigation); break;
801         }
802         switch (navigationHidden) {
803             case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break;
804             case NAVIGATIONHIDDEN_NO: sb.append("/v"); break;
805             case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
806             default: sb.append("/"); sb.append(navigationHidden); break;
807         }
808         if (seq != 0) {
809             sb.append(" s.");
810             sb.append(seq);
811         }
812         sb.append('}');
813         return sb.toString();
814     }
815 
816     /**
817      * Set this object to the system defaults.
818      */
setToDefaults()819     public void setToDefaults() {
820         fontScale = 1;
821         mcc = mnc = 0;
822         locale = null;
823         userSetLocale = false;
824         touchscreen = TOUCHSCREEN_UNDEFINED;
825         keyboard = KEYBOARD_UNDEFINED;
826         keyboardHidden = KEYBOARDHIDDEN_UNDEFINED;
827         hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED;
828         navigation = NAVIGATION_UNDEFINED;
829         navigationHidden = NAVIGATIONHIDDEN_UNDEFINED;
830         orientation = ORIENTATION_UNDEFINED;
831         screenLayout = SCREENLAYOUT_UNDEFINED;
832         uiMode = UI_MODE_TYPE_UNDEFINED;
833         screenWidthDp = compatScreenWidthDp = SCREEN_WIDTH_DP_UNDEFINED;
834         screenHeightDp = compatScreenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
835         smallestScreenWidthDp = compatSmallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
836         densityDpi = DENSITY_DPI_UNDEFINED;
837         seq = 0;
838     }
839 
840     /** {@hide} */
makeDefault()841     @Deprecated public void makeDefault() {
842         setToDefaults();
843     }
844 
845     /**
846      * Copy the fields from delta into this Configuration object, keeping
847      * track of which ones have changed.  Any undefined fields in
848      * <var>delta</var> are ignored and not copied in to the current
849      * Configuration.
850      * @return Returns a bit mask of the changed fields, as per
851      * {@link #diff}.
852      */
updateFrom(Configuration delta)853     public int updateFrom(Configuration delta) {
854         int changed = 0;
855         if (delta.fontScale > 0 && fontScale != delta.fontScale) {
856             changed |= ActivityInfo.CONFIG_FONT_SCALE;
857             fontScale = delta.fontScale;
858         }
859         if (delta.mcc != 0 && mcc != delta.mcc) {
860             changed |= ActivityInfo.CONFIG_MCC;
861             mcc = delta.mcc;
862         }
863         if (delta.mnc != 0 && mnc != delta.mnc) {
864             changed |= ActivityInfo.CONFIG_MNC;
865             mnc = delta.mnc;
866         }
867         if (delta.locale != null
868                 && (locale == null || !locale.equals(delta.locale))) {
869             changed |= ActivityInfo.CONFIG_LOCALE;
870             locale = delta.locale != null
871                     ? (Locale) delta.locale.clone() : null;
872             // If locale has changed, then layout direction is also changed ...
873             changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
874             // ... and we need to update the layout direction (represented by the first
875             // 2 most significant bits in screenLayout).
876             setLayoutDirection(locale);
877         }
878         final int deltaScreenLayoutDir = delta.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK;
879         if (deltaScreenLayoutDir != SCREENLAYOUT_LAYOUTDIR_UNDEFINED &&
880                 deltaScreenLayoutDir != (screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK)) {
881             screenLayout = (screenLayout & ~SCREENLAYOUT_LAYOUTDIR_MASK) | deltaScreenLayoutDir;
882             changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
883         }
884         if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
885         {
886             changed |= ActivityInfo.CONFIG_LOCALE;
887             userSetLocale = true;
888         }
889         if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
890                 && touchscreen != delta.touchscreen) {
891             changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
892             touchscreen = delta.touchscreen;
893         }
894         if (delta.keyboard != KEYBOARD_UNDEFINED
895                 && keyboard != delta.keyboard) {
896             changed |= ActivityInfo.CONFIG_KEYBOARD;
897             keyboard = delta.keyboard;
898         }
899         if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
900                 && keyboardHidden != delta.keyboardHidden) {
901             changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
902             keyboardHidden = delta.keyboardHidden;
903         }
904         if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
905                 && hardKeyboardHidden != delta.hardKeyboardHidden) {
906             changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
907             hardKeyboardHidden = delta.hardKeyboardHidden;
908         }
909         if (delta.navigation != NAVIGATION_UNDEFINED
910                 && navigation != delta.navigation) {
911             changed |= ActivityInfo.CONFIG_NAVIGATION;
912             navigation = delta.navigation;
913         }
914         if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
915                 && navigationHidden != delta.navigationHidden) {
916             changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
917             navigationHidden = delta.navigationHidden;
918         }
919         if (delta.orientation != ORIENTATION_UNDEFINED
920                 && orientation != delta.orientation) {
921             changed |= ActivityInfo.CONFIG_ORIENTATION;
922             orientation = delta.orientation;
923         }
924         if (getScreenLayoutNoDirection(delta.screenLayout) !=
925                     (SCREENLAYOUT_SIZE_UNDEFINED | SCREENLAYOUT_LONG_UNDEFINED)
926                 && (getScreenLayoutNoDirection(screenLayout) !=
927                     getScreenLayoutNoDirection(delta.screenLayout))) {
928             changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
929             // We need to preserve the previous layout dir bits if they were defined
930             if ((delta.screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == 0) {
931                 screenLayout = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK)|delta.screenLayout;
932             } else {
933                 screenLayout = delta.screenLayout;
934             }
935         }
936         if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
937                 && uiMode != delta.uiMode) {
938             changed |= ActivityInfo.CONFIG_UI_MODE;
939             if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) {
940                 uiMode = (uiMode&~UI_MODE_TYPE_MASK)
941                         | (delta.uiMode&UI_MODE_TYPE_MASK);
942             }
943             if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) {
944                 uiMode = (uiMode&~UI_MODE_NIGHT_MASK)
945                         | (delta.uiMode&UI_MODE_NIGHT_MASK);
946             }
947         }
948         if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
949                 && screenWidthDp != delta.screenWidthDp) {
950             changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
951             screenWidthDp = delta.screenWidthDp;
952         }
953         if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
954                 && screenHeightDp != delta.screenHeightDp) {
955             changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
956             screenHeightDp = delta.screenHeightDp;
957         }
958         if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
959                 && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
960             changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
961             smallestScreenWidthDp = delta.smallestScreenWidthDp;
962         }
963         if (delta.densityDpi != DENSITY_DPI_UNDEFINED &&
964                 densityDpi != delta.densityDpi) {
965             changed |= ActivityInfo.CONFIG_DENSITY;
966             densityDpi = delta.densityDpi;
967         }
968         if (delta.compatScreenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
969             compatScreenWidthDp = delta.compatScreenWidthDp;
970         }
971         if (delta.compatScreenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
972             compatScreenHeightDp = delta.compatScreenHeightDp;
973         }
974         if (delta.compatSmallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
975             compatSmallestScreenWidthDp = delta.compatSmallestScreenWidthDp;
976         }
977         if (delta.seq != 0) {
978             seq = delta.seq;
979         }
980 
981         return changed;
982     }
983 
984     /**
985      * Return a bit mask of the differences between this Configuration
986      * object and the given one.  Does not change the values of either.  Any
987      * undefined fields in <var>delta</var> are ignored.
988      * @return Returns a bit mask indicating which configuration
989      * values has changed, containing any combination of
990      * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE
991      * PackageManager.ActivityInfo.CONFIG_FONT_SCALE},
992      * {@link android.content.pm.ActivityInfo#CONFIG_MCC
993      * PackageManager.ActivityInfo.CONFIG_MCC},
994      * {@link android.content.pm.ActivityInfo#CONFIG_MNC
995      * PackageManager.ActivityInfo.CONFIG_MNC},
996      * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE
997      * PackageManager.ActivityInfo.CONFIG_LOCALE},
998      * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN
999      * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN},
1000      * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD
1001      * PackageManager.ActivityInfo.CONFIG_KEYBOARD},
1002      * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION
1003      * PackageManager.ActivityInfo.CONFIG_NAVIGATION},
1004      * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION
1005      * PackageManager.ActivityInfo.CONFIG_ORIENTATION},
1006      * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT
1007      * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or
1008      * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE
1009      * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or
1010      * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE
1011      * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}.
1012      * {@link android.content.pm.ActivityInfo#CONFIG_LAYOUT_DIRECTION
1013      * PackageManager.ActivityInfo.CONFIG_LAYOUT_DIRECTION}.
1014      */
diff(Configuration delta)1015     public int diff(Configuration delta) {
1016         int changed = 0;
1017         if (delta.fontScale > 0 && fontScale != delta.fontScale) {
1018             changed |= ActivityInfo.CONFIG_FONT_SCALE;
1019         }
1020         if (delta.mcc != 0 && mcc != delta.mcc) {
1021             changed |= ActivityInfo.CONFIG_MCC;
1022         }
1023         if (delta.mnc != 0 && mnc != delta.mnc) {
1024             changed |= ActivityInfo.CONFIG_MNC;
1025         }
1026         if (delta.locale != null
1027                 && (locale == null || !locale.equals(delta.locale))) {
1028             changed |= ActivityInfo.CONFIG_LOCALE;
1029             changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
1030         }
1031         final int deltaScreenLayoutDir = delta.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK;
1032         if (deltaScreenLayoutDir != SCREENLAYOUT_LAYOUTDIR_UNDEFINED &&
1033                 deltaScreenLayoutDir != (screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK)) {
1034             changed |= ActivityInfo.CONFIG_LAYOUT_DIRECTION;
1035         }
1036         if (delta.touchscreen != TOUCHSCREEN_UNDEFINED
1037                 && touchscreen != delta.touchscreen) {
1038             changed |= ActivityInfo.CONFIG_TOUCHSCREEN;
1039         }
1040         if (delta.keyboard != KEYBOARD_UNDEFINED
1041                 && keyboard != delta.keyboard) {
1042             changed |= ActivityInfo.CONFIG_KEYBOARD;
1043         }
1044         if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED
1045                 && keyboardHidden != delta.keyboardHidden) {
1046             changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
1047         }
1048         if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED
1049                 && hardKeyboardHidden != delta.hardKeyboardHidden) {
1050             changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
1051         }
1052         if (delta.navigation != NAVIGATION_UNDEFINED
1053                 && navigation != delta.navigation) {
1054             changed |= ActivityInfo.CONFIG_NAVIGATION;
1055         }
1056         if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED
1057                 && navigationHidden != delta.navigationHidden) {
1058             changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN;
1059         }
1060         if (delta.orientation != ORIENTATION_UNDEFINED
1061                 && orientation != delta.orientation) {
1062             changed |= ActivityInfo.CONFIG_ORIENTATION;
1063         }
1064         if (getScreenLayoutNoDirection(delta.screenLayout) !=
1065                     (SCREENLAYOUT_SIZE_UNDEFINED | SCREENLAYOUT_LONG_UNDEFINED)
1066                 && getScreenLayoutNoDirection(screenLayout) !=
1067                     getScreenLayoutNoDirection(delta.screenLayout)) {
1068             changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT;
1069         }
1070         if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED)
1071                 && uiMode != delta.uiMode) {
1072             changed |= ActivityInfo.CONFIG_UI_MODE;
1073         }
1074         if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED
1075                 && screenWidthDp != delta.screenWidthDp) {
1076             changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
1077         }
1078         if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED
1079                 && screenHeightDp != delta.screenHeightDp) {
1080             changed |= ActivityInfo.CONFIG_SCREEN_SIZE;
1081         }
1082         if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED
1083                 && smallestScreenWidthDp != delta.smallestScreenWidthDp) {
1084             changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
1085         }
1086         if (delta.densityDpi != DENSITY_DPI_UNDEFINED
1087                 && densityDpi != delta.densityDpi) {
1088             changed |= ActivityInfo.CONFIG_DENSITY;
1089         }
1090 
1091         return changed;
1092     }
1093 
1094     /**
1095      * Determine if a new resource needs to be loaded from the bit set of
1096      * configuration changes returned by {@link #updateFrom(Configuration)}.
1097      *
1098      * @param configChanges The mask of changes configurations as returned by
1099      * {@link #updateFrom(Configuration)}.
1100      * @param interestingChanges The configuration changes that the resource
1101      * can handled, as given in {@link android.util.TypedValue#changingConfigurations}.
1102      *
1103      * @return Return true if the resource needs to be loaded, else false.
1104      */
needNewResources(int configChanges, int interestingChanges)1105     public static boolean needNewResources(int configChanges, int interestingChanges) {
1106         return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0;
1107     }
1108 
1109     /**
1110      * @hide Return true if the sequence of 'other' is better than this.  Assumes
1111      * that 'this' is your current sequence and 'other' is a new one you have
1112      * received some how and want to compare with what you have.
1113      */
isOtherSeqNewer(Configuration other)1114     public boolean isOtherSeqNewer(Configuration other) {
1115         if (other == null) {
1116             // Sanity check.
1117             return false;
1118         }
1119         if (other.seq == 0) {
1120             // If the other sequence is not specified, then we must assume
1121             // it is newer since we don't know any better.
1122             return true;
1123         }
1124         if (seq == 0) {
1125             // If this sequence is not specified, then we also consider the
1126             // other is better.  Yes we have a preference for other.  Sue us.
1127             return true;
1128         }
1129         int diff = other.seq - seq;
1130         if (diff > 0x10000) {
1131             // If there has been a sufficiently large jump, assume the
1132             // sequence has wrapped around.
1133             return false;
1134         }
1135         return diff > 0;
1136     }
1137 
1138     /**
1139      * Parcelable methods
1140      */
describeContents()1141     public int describeContents() {
1142         return 0;
1143     }
1144 
writeToParcel(Parcel dest, int flags)1145     public void writeToParcel(Parcel dest, int flags) {
1146         dest.writeFloat(fontScale);
1147         dest.writeInt(mcc);
1148         dest.writeInt(mnc);
1149         if (locale == null) {
1150             dest.writeInt(0);
1151         } else {
1152             dest.writeInt(1);
1153             dest.writeString(locale.getLanguage());
1154             dest.writeString(locale.getCountry());
1155             dest.writeString(locale.getVariant());
1156         }
1157         if(userSetLocale) {
1158             dest.writeInt(1);
1159         } else {
1160             dest.writeInt(0);
1161         }
1162         dest.writeInt(touchscreen);
1163         dest.writeInt(keyboard);
1164         dest.writeInt(keyboardHidden);
1165         dest.writeInt(hardKeyboardHidden);
1166         dest.writeInt(navigation);
1167         dest.writeInt(navigationHidden);
1168         dest.writeInt(orientation);
1169         dest.writeInt(screenLayout);
1170         dest.writeInt(uiMode);
1171         dest.writeInt(screenWidthDp);
1172         dest.writeInt(screenHeightDp);
1173         dest.writeInt(smallestScreenWidthDp);
1174         dest.writeInt(densityDpi);
1175         dest.writeInt(compatScreenWidthDp);
1176         dest.writeInt(compatScreenHeightDp);
1177         dest.writeInt(compatSmallestScreenWidthDp);
1178         dest.writeInt(seq);
1179     }
1180 
readFromParcel(Parcel source)1181     public void readFromParcel(Parcel source) {
1182         fontScale = source.readFloat();
1183         mcc = source.readInt();
1184         mnc = source.readInt();
1185         if (source.readInt() != 0) {
1186             locale = new Locale(source.readString(), source.readString(),
1187                     source.readString());
1188         }
1189         userSetLocale = (source.readInt()==1);
1190         touchscreen = source.readInt();
1191         keyboard = source.readInt();
1192         keyboardHidden = source.readInt();
1193         hardKeyboardHidden = source.readInt();
1194         navigation = source.readInt();
1195         navigationHidden = source.readInt();
1196         orientation = source.readInt();
1197         screenLayout = source.readInt();
1198         uiMode = source.readInt();
1199         screenWidthDp = source.readInt();
1200         screenHeightDp = source.readInt();
1201         smallestScreenWidthDp = source.readInt();
1202         densityDpi = source.readInt();
1203         compatScreenWidthDp = source.readInt();
1204         compatScreenHeightDp = source.readInt();
1205         compatSmallestScreenWidthDp = source.readInt();
1206         seq = source.readInt();
1207     }
1208 
1209     public static final Parcelable.Creator<Configuration> CREATOR
1210             = new Parcelable.Creator<Configuration>() {
1211         public Configuration createFromParcel(Parcel source) {
1212             return new Configuration(source);
1213         }
1214 
1215         public Configuration[] newArray(int size) {
1216             return new Configuration[size];
1217         }
1218     };
1219 
1220     /**
1221      * Construct this Configuration object, reading from the Parcel.
1222      */
Configuration(Parcel source)1223     private Configuration(Parcel source) {
1224         readFromParcel(source);
1225     }
1226 
compareTo(Configuration that)1227     public int compareTo(Configuration that) {
1228         int n;
1229         float a = this.fontScale;
1230         float b = that.fontScale;
1231         if (a < b) return -1;
1232         if (a > b) return 1;
1233         n = this.mcc - that.mcc;
1234         if (n != 0) return n;
1235         n = this.mnc - that.mnc;
1236         if (n != 0) return n;
1237         if (this.locale == null) {
1238             if (that.locale != null) return 1;
1239         } else if (that.locale == null) {
1240             return -1;
1241         } else {
1242             n = this.locale.getLanguage().compareTo(that.locale.getLanguage());
1243             if (n != 0) return n;
1244             n = this.locale.getCountry().compareTo(that.locale.getCountry());
1245             if (n != 0) return n;
1246             n = this.locale.getVariant().compareTo(that.locale.getVariant());
1247             if (n != 0) return n;
1248         }
1249         n = this.touchscreen - that.touchscreen;
1250         if (n != 0) return n;
1251         n = this.keyboard - that.keyboard;
1252         if (n != 0) return n;
1253         n = this.keyboardHidden - that.keyboardHidden;
1254         if (n != 0) return n;
1255         n = this.hardKeyboardHidden - that.hardKeyboardHidden;
1256         if (n != 0) return n;
1257         n = this.navigation - that.navigation;
1258         if (n != 0) return n;
1259         n = this.navigationHidden - that.navigationHidden;
1260         if (n != 0) return n;
1261         n = this.orientation - that.orientation;
1262         if (n != 0) return n;
1263         n = this.screenLayout - that.screenLayout;
1264         if (n != 0) return n;
1265         n = this.uiMode - that.uiMode;
1266         if (n != 0) return n;
1267         n = this.screenWidthDp - that.screenWidthDp;
1268         if (n != 0) return n;
1269         n = this.screenHeightDp - that.screenHeightDp;
1270         if (n != 0) return n;
1271         n = this.smallestScreenWidthDp - that.smallestScreenWidthDp;
1272         if (n != 0) return n;
1273         n = this.densityDpi - that.densityDpi;
1274         //if (n != 0) return n;
1275         return n;
1276     }
1277 
equals(Configuration that)1278     public boolean equals(Configuration that) {
1279         if (that == null) return false;
1280         if (that == this) return true;
1281         return this.compareTo(that) == 0;
1282     }
1283 
equals(Object that)1284     public boolean equals(Object that) {
1285         try {
1286             return equals((Configuration)that);
1287         } catch (ClassCastException e) {
1288         }
1289         return false;
1290     }
1291 
hashCode()1292     public int hashCode() {
1293         int result = 17;
1294         result = 31 * result + Float.floatToIntBits(fontScale);
1295         result = 31 * result + mcc;
1296         result = 31 * result + mnc;
1297         result = 31 * result + (locale != null ? locale.hashCode() : 0);
1298         result = 31 * result + touchscreen;
1299         result = 31 * result + keyboard;
1300         result = 31 * result + keyboardHidden;
1301         result = 31 * result + hardKeyboardHidden;
1302         result = 31 * result + navigation;
1303         result = 31 * result + navigationHidden;
1304         result = 31 * result + orientation;
1305         result = 31 * result + screenLayout;
1306         result = 31 * result + uiMode;
1307         result = 31 * result + screenWidthDp;
1308         result = 31 * result + screenHeightDp;
1309         result = 31 * result + smallestScreenWidthDp;
1310         result = 31 * result + densityDpi;
1311         return result;
1312     }
1313 
1314     /**
1315      * Set the locale. This is the preferred way for setting up the locale (instead of using the
1316      * direct accessor). This will also set the layout direction according to the locale.
1317      *
1318      * @param loc The locale. Can be null.
1319      */
setLocale(Locale loc)1320     public void setLocale(Locale loc) {
1321         locale = loc;
1322         setLayoutDirection(locale);
1323     }
1324 
1325     /**
1326      * Return the layout direction. Will be either {@link View#LAYOUT_DIRECTION_LTR} or
1327      * {@link View#LAYOUT_DIRECTION_RTL}.
1328      *
1329      * @return Returns {@link View#LAYOUT_DIRECTION_RTL} if the configuration
1330      * is {@link #SCREENLAYOUT_LAYOUTDIR_RTL}, otherwise {@link View#LAYOUT_DIRECTION_LTR}.
1331      */
getLayoutDirection()1332     public int getLayoutDirection() {
1333         return (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == SCREENLAYOUT_LAYOUTDIR_RTL
1334                 ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
1335     }
1336 
1337     /**
1338      * Set the layout direction from the Locale.
1339      *
1340      * @param locale The Locale. If null will set the layout direction to
1341      * {@link View#LAYOUT_DIRECTION_LTR}. If not null will set it to the layout direction
1342      * corresponding to the Locale.
1343      *
1344      * @see View#LAYOUT_DIRECTION_LTR
1345      * @see View#LAYOUT_DIRECTION_RTL
1346      */
setLayoutDirection(Locale locale)1347     public void setLayoutDirection(Locale locale) {
1348         // There is a "1" difference between the configuration values for
1349         // layout direction and View constants for layout direction, just add "1".
1350         final int layoutDirection = 1 + TextUtils.getLayoutDirectionFromLocale(locale);
1351         screenLayout = (screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK)|
1352                 (layoutDirection << SCREENLAYOUT_LAYOUTDIR_SHIFT);
1353     }
1354 
getScreenLayoutNoDirection(int screenLayout)1355     private static int getScreenLayoutNoDirection(int screenLayout) {
1356         return screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK;
1357     }
1358 
1359     /**
1360      * Return whether the screen has a round shape. Apps may choose to change styling based
1361      * on this property, such as the alignment or layout of text or informational icons.
1362      *
1363      * @return true if the screen is rounded, false otherwise
1364      */
isScreenRound()1365     public boolean isScreenRound() {
1366         return (screenLayout & SCREENLAYOUT_ROUND_MASK) == SCREENLAYOUT_ROUND_YES;
1367     }
1368 
1369     /**
1370      *
1371      * @hide
1372      */
localeToResourceQualifier(Locale locale)1373     public static String localeToResourceQualifier(Locale locale) {
1374         StringBuilder sb = new StringBuilder();
1375         boolean l = (locale.getLanguage().length() != 0);
1376         boolean c = (locale.getCountry().length() != 0);
1377         boolean s = (locale.getScript().length() != 0);
1378         boolean v = (locale.getVariant().length() != 0);
1379 
1380         if (l) {
1381             sb.append(locale.getLanguage());
1382             if (c) {
1383                 sb.append("-r").append(locale.getCountry());
1384                 if (s) {
1385                     sb.append("-s").append(locale.getScript());
1386                     if (v) {
1387                         sb.append("-v").append(locale.getVariant());
1388                     }
1389                 }
1390             }
1391         }
1392         return sb.toString();
1393     }
1394 
1395 
1396     /**
1397      * Returns a string representation of the configuration that can be parsed
1398      * by build tools (like AAPT).
1399      *
1400      * @hide
1401      */
resourceQualifierString(Configuration config)1402     public static String resourceQualifierString(Configuration config) {
1403         ArrayList<String> parts = new ArrayList<String>();
1404 
1405         if (config.mcc != 0) {
1406             parts.add("mcc" + config.mcc);
1407             if (config.mnc != 0) {
1408                 parts.add("mnc" + config.mnc);
1409             }
1410         }
1411 
1412         if (config.locale != null && !config.locale.getLanguage().isEmpty()) {
1413             parts.add(localeToResourceQualifier(config.locale));
1414         }
1415 
1416         switch (config.screenLayout & Configuration.SCREENLAYOUT_LAYOUTDIR_MASK) {
1417             case Configuration.SCREENLAYOUT_LAYOUTDIR_LTR:
1418                 parts.add("ldltr");
1419                 break;
1420             case Configuration.SCREENLAYOUT_LAYOUTDIR_RTL:
1421                 parts.add("ldrtl");
1422                 break;
1423             default:
1424                 break;
1425         }
1426 
1427         if (config.smallestScreenWidthDp != 0) {
1428             parts.add("sw" + config.smallestScreenWidthDp + "dp");
1429         }
1430 
1431         if (config.screenWidthDp != 0) {
1432             parts.add("w" + config.screenWidthDp + "dp");
1433         }
1434 
1435         if (config.screenHeightDp != 0) {
1436             parts.add("h" + config.screenHeightDp + "dp");
1437         }
1438 
1439         switch (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) {
1440             case Configuration.SCREENLAYOUT_SIZE_SMALL:
1441                 parts.add("small");
1442                 break;
1443             case Configuration.SCREENLAYOUT_SIZE_NORMAL:
1444                 parts.add("normal");
1445                 break;
1446             case Configuration.SCREENLAYOUT_SIZE_LARGE:
1447                 parts.add("large");
1448                 break;
1449             case Configuration.SCREENLAYOUT_SIZE_XLARGE:
1450                 parts.add("xlarge");
1451                 break;
1452             default:
1453                 break;
1454         }
1455 
1456         switch (config.screenLayout & Configuration.SCREENLAYOUT_LONG_MASK) {
1457             case Configuration.SCREENLAYOUT_LONG_YES:
1458                 parts.add("long");
1459                 break;
1460             case Configuration.SCREENLAYOUT_LONG_NO:
1461                 parts.add("notlong");
1462                 break;
1463             default:
1464                 break;
1465         }
1466 
1467         switch (config.screenLayout & Configuration.SCREENLAYOUT_ROUND_MASK) {
1468             case Configuration.SCREENLAYOUT_ROUND_YES:
1469                 parts.add("round");
1470                 break;
1471             case Configuration.SCREENLAYOUT_ROUND_NO:
1472                 parts.add("notround");
1473                 break;
1474             default:
1475                 break;
1476         }
1477 
1478         switch (config.orientation) {
1479             case Configuration.ORIENTATION_LANDSCAPE:
1480                 parts.add("land");
1481                 break;
1482             case Configuration.ORIENTATION_PORTRAIT:
1483                 parts.add("port");
1484                 break;
1485             default:
1486                 break;
1487         }
1488 
1489         switch (config.uiMode & Configuration.UI_MODE_TYPE_MASK) {
1490             case Configuration.UI_MODE_TYPE_APPLIANCE:
1491                 parts.add("appliance");
1492                 break;
1493             case Configuration.UI_MODE_TYPE_DESK:
1494                 parts.add("desk");
1495                 break;
1496             case Configuration.UI_MODE_TYPE_TELEVISION:
1497                 parts.add("television");
1498                 break;
1499             case Configuration.UI_MODE_TYPE_CAR:
1500                 parts.add("car");
1501                 break;
1502             case Configuration.UI_MODE_TYPE_WATCH:
1503                 parts.add("watch");
1504                 break;
1505             default:
1506                 break;
1507         }
1508 
1509         switch (config.uiMode & Configuration.UI_MODE_NIGHT_MASK) {
1510             case Configuration.UI_MODE_NIGHT_YES:
1511                 parts.add("night");
1512                 break;
1513             case Configuration.UI_MODE_NIGHT_NO:
1514                 parts.add("notnight");
1515                 break;
1516             default:
1517                 break;
1518         }
1519 
1520         switch (config.densityDpi) {
1521             case DENSITY_DPI_UNDEFINED:
1522                 break;
1523             case 120:
1524                 parts.add("ldpi");
1525                 break;
1526             case 160:
1527                 parts.add("mdpi");
1528                 break;
1529             case 213:
1530                 parts.add("tvdpi");
1531                 break;
1532             case 240:
1533                 parts.add("hdpi");
1534                 break;
1535             case 320:
1536                 parts.add("xhdpi");
1537                 break;
1538             case 480:
1539                 parts.add("xxhdpi");
1540                 break;
1541             case 640:
1542                 parts.add("xxxhdpi");
1543                 break;
1544             case DENSITY_DPI_ANY:
1545                 parts.add("anydpi");
1546                 break;
1547             case DENSITY_DPI_NONE:
1548                 parts.add("nodpi");
1549             default:
1550                 parts.add(config.densityDpi + "dpi");
1551                 break;
1552         }
1553 
1554         switch (config.touchscreen) {
1555             case Configuration.TOUCHSCREEN_NOTOUCH:
1556                 parts.add("notouch");
1557                 break;
1558             case Configuration.TOUCHSCREEN_FINGER:
1559                 parts.add("finger");
1560                 break;
1561             default:
1562                 break;
1563         }
1564 
1565         switch (config.keyboardHidden) {
1566             case Configuration.KEYBOARDHIDDEN_NO:
1567                 parts.add("keysexposed");
1568                 break;
1569             case Configuration.KEYBOARDHIDDEN_YES:
1570                 parts.add("keyshidden");
1571                 break;
1572             case Configuration.KEYBOARDHIDDEN_SOFT:
1573                 parts.add("keyssoft");
1574                 break;
1575             default:
1576                 break;
1577         }
1578 
1579         switch (config.keyboard) {
1580             case Configuration.KEYBOARD_NOKEYS:
1581                 parts.add("nokeys");
1582                 break;
1583             case Configuration.KEYBOARD_QWERTY:
1584                 parts.add("qwerty");
1585                 break;
1586             case Configuration.KEYBOARD_12KEY:
1587                 parts.add("12key");
1588                 break;
1589             default:
1590                 break;
1591         }
1592 
1593         switch (config.navigationHidden) {
1594             case Configuration.NAVIGATIONHIDDEN_NO:
1595                 parts.add("navexposed");
1596                 break;
1597             case Configuration.NAVIGATIONHIDDEN_YES:
1598                 parts.add("navhidden");
1599                 break;
1600             default:
1601                 break;
1602         }
1603 
1604         switch (config.navigation) {
1605             case Configuration.NAVIGATION_NONAV:
1606                 parts.add("nonav");
1607                 break;
1608             case Configuration.NAVIGATION_DPAD:
1609                 parts.add("dpad");
1610                 break;
1611             case Configuration.NAVIGATION_TRACKBALL:
1612                 parts.add("trackball");
1613                 break;
1614             case Configuration.NAVIGATION_WHEEL:
1615                 parts.add("wheel");
1616                 break;
1617             default:
1618                 break;
1619         }
1620 
1621         parts.add("v" + Build.VERSION.RESOURCES_SDK_INT);
1622         return TextUtils.join("-", parts);
1623     }
1624 
1625     /**
1626      * Generate a delta Configuration between <code>base</code> and <code>change</code>. The
1627      * resulting delta can be used with {@link #updateFrom(Configuration)}.
1628      * <p />
1629      * Caveat: If the any of the Configuration's members becomes undefined, then
1630      * {@link #updateFrom(Configuration)} will treat it as a no-op and not update that member.
1631      *
1632      * This is fine for device configurations as no member is ever undefined.
1633      * {@hide}
1634      */
generateDelta(Configuration base, Configuration change)1635     public static Configuration generateDelta(Configuration base, Configuration change) {
1636         final Configuration delta = new Configuration();
1637         if (base.fontScale != change.fontScale) {
1638             delta.fontScale = change.fontScale;
1639         }
1640 
1641         if (base.mcc != change.mcc) {
1642             delta.mcc = change.mcc;
1643         }
1644 
1645         if (base.mnc != change.mnc) {
1646             delta.mnc = change.mnc;
1647         }
1648 
1649         if ((base.locale == null && change.locale != null) ||
1650                 (base.locale != null && !base.locale.equals(change.locale)))  {
1651             delta.locale = change.locale;
1652         }
1653 
1654         if (base.touchscreen != change.touchscreen) {
1655             delta.touchscreen = change.touchscreen;
1656         }
1657 
1658         if (base.keyboard != change.keyboard) {
1659             delta.keyboard = change.keyboard;
1660         }
1661 
1662         if (base.keyboardHidden != change.keyboardHidden) {
1663             delta.keyboardHidden = change.keyboardHidden;
1664         }
1665 
1666         if (base.navigation != change.navigation) {
1667             delta.navigation = change.navigation;
1668         }
1669 
1670         if (base.navigationHidden != change.navigationHidden) {
1671             delta.navigationHidden = change.navigationHidden;
1672         }
1673 
1674         if (base.orientation != change.orientation) {
1675             delta.orientation = change.orientation;
1676         }
1677 
1678         if ((base.screenLayout & SCREENLAYOUT_SIZE_MASK) !=
1679                 (change.screenLayout & SCREENLAYOUT_SIZE_MASK)) {
1680             delta.screenLayout |= change.screenLayout & SCREENLAYOUT_SIZE_MASK;
1681         }
1682 
1683         if ((base.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK) !=
1684                 (change.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK)) {
1685             delta.screenLayout |= change.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK;
1686         }
1687 
1688         if ((base.screenLayout & SCREENLAYOUT_LONG_MASK) !=
1689                 (change.screenLayout & SCREENLAYOUT_LONG_MASK)) {
1690             delta.screenLayout |= change.screenLayout & SCREENLAYOUT_LONG_MASK;
1691         }
1692 
1693         if ((base.screenLayout & SCREENLAYOUT_ROUND_MASK) !=
1694                 (change.screenLayout & SCREENLAYOUT_ROUND_MASK)) {
1695             delta.screenLayout |= change.screenLayout & SCREENLAYOUT_ROUND_MASK;
1696         }
1697 
1698         if ((base.uiMode & UI_MODE_TYPE_MASK) != (change.uiMode & UI_MODE_TYPE_MASK)) {
1699             delta.uiMode |= change.uiMode & UI_MODE_TYPE_MASK;
1700         }
1701 
1702         if ((base.uiMode & UI_MODE_NIGHT_MASK) != (change.uiMode & UI_MODE_NIGHT_MASK)) {
1703             delta.uiMode |= change.uiMode & UI_MODE_NIGHT_MASK;
1704         }
1705 
1706         if (base.screenWidthDp != change.screenWidthDp) {
1707             delta.screenWidthDp = change.screenWidthDp;
1708         }
1709 
1710         if (base.screenHeightDp != change.screenHeightDp) {
1711             delta.screenHeightDp = change.screenHeightDp;
1712         }
1713 
1714         if (base.smallestScreenWidthDp != change.smallestScreenWidthDp) {
1715             delta.smallestScreenWidthDp = change.smallestScreenWidthDp;
1716         }
1717 
1718         if (base.densityDpi != change.densityDpi) {
1719             delta.densityDpi = change.densityDpi;
1720         }
1721         return delta;
1722     }
1723 
1724     private static final String XML_ATTR_FONT_SCALE = "fs";
1725     private static final String XML_ATTR_MCC = "mcc";
1726     private static final String XML_ATTR_MNC = "mnc";
1727     private static final String XML_ATTR_LOCALE = "locale";
1728     private static final String XML_ATTR_TOUCHSCREEN = "touch";
1729     private static final String XML_ATTR_KEYBOARD = "key";
1730     private static final String XML_ATTR_KEYBOARD_HIDDEN = "keyHid";
1731     private static final String XML_ATTR_HARD_KEYBOARD_HIDDEN = "hardKeyHid";
1732     private static final String XML_ATTR_NAVIGATION = "nav";
1733     private static final String XML_ATTR_NAVIGATION_HIDDEN = "navHid";
1734     private static final String XML_ATTR_ORIENTATION = "ori";
1735     private static final String XML_ATTR_SCREEN_LAYOUT = "scrLay";
1736     private static final String XML_ATTR_UI_MODE = "ui";
1737     private static final String XML_ATTR_SCREEN_WIDTH = "width";
1738     private static final String XML_ATTR_SCREEN_HEIGHT = "height";
1739     private static final String XML_ATTR_SMALLEST_WIDTH = "sw";
1740     private static final String XML_ATTR_DENSITY = "density";
1741 
1742     /**
1743      * Reads the attributes corresponding to Configuration member fields from the Xml parser.
1744      * The parser is expected to be on a tag which has Configuration attributes.
1745      *
1746      * @param parser The Xml parser from which to read attributes.
1747      * @param configOut The Configuration to populate from the Xml attributes.
1748      * {@hide}
1749      */
readXmlAttrs(XmlPullParser parser, Configuration configOut)1750     public static void readXmlAttrs(XmlPullParser parser, Configuration configOut)
1751             throws XmlPullParserException, IOException {
1752         configOut.fontScale = Float.intBitsToFloat(
1753                 XmlUtils.readIntAttribute(parser, XML_ATTR_FONT_SCALE, 0));
1754         configOut.mcc = XmlUtils.readIntAttribute(parser, XML_ATTR_MCC, 0);
1755         configOut.mnc = XmlUtils.readIntAttribute(parser, XML_ATTR_MNC, 0);
1756 
1757         final String localeStr = XmlUtils.readStringAttribute(parser, XML_ATTR_LOCALE);
1758         if (localeStr != null) {
1759             configOut.locale = Locale.forLanguageTag(localeStr);
1760         }
1761 
1762         configOut.touchscreen = XmlUtils.readIntAttribute(parser, XML_ATTR_TOUCHSCREEN,
1763                 TOUCHSCREEN_UNDEFINED);
1764         configOut.keyboard = XmlUtils.readIntAttribute(parser, XML_ATTR_KEYBOARD,
1765                 KEYBOARD_UNDEFINED);
1766         configOut.keyboardHidden = XmlUtils.readIntAttribute(parser, XML_ATTR_KEYBOARD_HIDDEN,
1767                 KEYBOARDHIDDEN_UNDEFINED);
1768         configOut.hardKeyboardHidden =
1769                 XmlUtils.readIntAttribute(parser, XML_ATTR_HARD_KEYBOARD_HIDDEN,
1770                         HARDKEYBOARDHIDDEN_UNDEFINED);
1771         configOut.navigation = XmlUtils.readIntAttribute(parser, XML_ATTR_NAVIGATION,
1772                 NAVIGATION_UNDEFINED);
1773         configOut.navigationHidden = XmlUtils.readIntAttribute(parser, XML_ATTR_NAVIGATION_HIDDEN,
1774                 NAVIGATIONHIDDEN_UNDEFINED);
1775         configOut.orientation = XmlUtils.readIntAttribute(parser, XML_ATTR_ORIENTATION,
1776                 ORIENTATION_UNDEFINED);
1777         configOut.screenLayout = XmlUtils.readIntAttribute(parser, XML_ATTR_SCREEN_LAYOUT,
1778                 SCREENLAYOUT_UNDEFINED);
1779         configOut.uiMode = XmlUtils.readIntAttribute(parser, XML_ATTR_UI_MODE, 0);
1780         configOut.screenWidthDp = XmlUtils.readIntAttribute(parser, XML_ATTR_SCREEN_WIDTH,
1781                 SCREEN_WIDTH_DP_UNDEFINED);
1782         configOut.screenHeightDp = XmlUtils.readIntAttribute(parser, XML_ATTR_SCREEN_HEIGHT,
1783                 SCREEN_HEIGHT_DP_UNDEFINED);
1784         configOut.smallestScreenWidthDp =
1785                 XmlUtils.readIntAttribute(parser, XML_ATTR_SMALLEST_WIDTH,
1786                         SMALLEST_SCREEN_WIDTH_DP_UNDEFINED);
1787         configOut.densityDpi = XmlUtils.readIntAttribute(parser, XML_ATTR_DENSITY,
1788                 DENSITY_DPI_UNDEFINED);
1789     }
1790 
1791 
1792     /**
1793      * Writes the Configuration's member fields as attributes into the XmlSerializer.
1794      * The serializer is expected to have already started a tag so that attributes can be
1795      * immediately written.
1796      *
1797      * @param xml The serializer to which to write the attributes.
1798      * @param config The Configuration whose member fields to write.
1799      * {@hide}
1800      */
writeXmlAttrs(XmlSerializer xml, Configuration config)1801     public static void writeXmlAttrs(XmlSerializer xml, Configuration config) throws IOException {
1802         XmlUtils.writeIntAttribute(xml, XML_ATTR_FONT_SCALE,
1803                 Float.floatToIntBits(config.fontScale));
1804         if (config.mcc != 0) {
1805             XmlUtils.writeIntAttribute(xml, XML_ATTR_MCC, config.mcc);
1806         }
1807         if (config.mnc != 0) {
1808             XmlUtils.writeIntAttribute(xml, XML_ATTR_MNC, config.mnc);
1809         }
1810         if (config.locale != null) {
1811             XmlUtils.writeStringAttribute(xml, XML_ATTR_LOCALE, config.locale.toLanguageTag());
1812         }
1813         if (config.touchscreen != TOUCHSCREEN_UNDEFINED) {
1814             XmlUtils.writeIntAttribute(xml, XML_ATTR_TOUCHSCREEN, config.touchscreen);
1815         }
1816         if (config.keyboard != KEYBOARD_UNDEFINED) {
1817             XmlUtils.writeIntAttribute(xml, XML_ATTR_KEYBOARD, config.keyboard);
1818         }
1819         if (config.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED) {
1820             XmlUtils.writeIntAttribute(xml, XML_ATTR_KEYBOARD_HIDDEN, config.keyboardHidden);
1821         }
1822         if (config.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED) {
1823             XmlUtils.writeIntAttribute(xml, XML_ATTR_HARD_KEYBOARD_HIDDEN,
1824                     config.hardKeyboardHidden);
1825         }
1826         if (config.navigation != NAVIGATION_UNDEFINED) {
1827             XmlUtils.writeIntAttribute(xml, XML_ATTR_NAVIGATION, config.navigation);
1828         }
1829         if (config.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED) {
1830             XmlUtils.writeIntAttribute(xml, XML_ATTR_NAVIGATION_HIDDEN, config.navigationHidden);
1831         }
1832         if (config.orientation != ORIENTATION_UNDEFINED) {
1833             XmlUtils.writeIntAttribute(xml, XML_ATTR_ORIENTATION, config.orientation);
1834         }
1835         if (config.screenLayout != SCREENLAYOUT_UNDEFINED) {
1836             XmlUtils.writeIntAttribute(xml, XML_ATTR_SCREEN_LAYOUT, config.screenLayout);
1837         }
1838         if (config.uiMode != 0) {
1839             XmlUtils.writeIntAttribute(xml, XML_ATTR_UI_MODE, config.uiMode);
1840         }
1841         if (config.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) {
1842             XmlUtils.writeIntAttribute(xml, XML_ATTR_SCREEN_WIDTH, config.screenWidthDp);
1843         }
1844         if (config.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) {
1845             XmlUtils.writeIntAttribute(xml, XML_ATTR_SCREEN_HEIGHT, config.screenHeightDp);
1846         }
1847         if (config.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
1848             XmlUtils.writeIntAttribute(xml, XML_ATTR_SMALLEST_WIDTH, config.smallestScreenWidthDp);
1849         }
1850         if (config.densityDpi != DENSITY_DPI_UNDEFINED) {
1851             XmlUtils.writeIntAttribute(xml, XML_ATTR_DENSITY, config.densityDpi);
1852         }
1853     }
1854 }
1855