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