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