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