Configuration.java revision 27b28b3f62bd3b54fa13acd5d035940b9be464f3
1package android.content.res; 2 3import android.content.pm.ActivityInfo; 4import android.os.Parcel; 5import android.os.Parcelable; 6 7import java.util.Locale; 8 9/** 10 * This class describes all device configuration information that can 11 * impact the resources the application retrieves. This includes both 12 * user-specified configuration options (locale and scaling) as well 13 * as dynamic device configuration (various types of input devices). 14 */ 15public final class Configuration implements Parcelable, Comparable<Configuration> { 16 /** 17 * Current user preference for the scaling factor for fonts, relative 18 * to the base density scaling. 19 */ 20 public float fontScale; 21 22 /** 23 * IMSI MCC (Mobile Country Code). 0 if undefined. 24 */ 25 public int mcc; 26 27 /** 28 * IMSI MNC (Mobile Network Code). 0 if undefined. 29 */ 30 public int mnc; 31 32 /** 33 * Current user preference for the locale. 34 */ 35 public Locale locale; 36 37 /** 38 * Locale should persist on setting. This is hidden because it is really 39 * questionable whether this is the right way to expose the functionality. 40 * @hide 41 */ 42 public boolean userSetLocale; 43 44 public static final int SCREENLAYOUT_SIZE_MASK = 0x0f; 45 public static final int SCREENLAYOUT_SIZE_UNDEFINED = 0x00; 46 public static final int SCREENLAYOUT_SIZE_SMALL = 0x01; 47 public static final int SCREENLAYOUT_SIZE_NORMAL = 0x02; 48 public static final int SCREENLAYOUT_SIZE_LARGE = 0x03; 49 50 public static final int SCREENLAYOUT_LONG_MASK = 0x30; 51 public static final int SCREENLAYOUT_LONG_UNDEFINED = 0x00; 52 public static final int SCREENLAYOUT_LONG_NO = 0x10; 53 public static final int SCREENLAYOUT_LONG_YES = 0x20; 54 55 /** 56 * Special flag we generate to indicate that the screen layout requires 57 * us to use a compatibility mode for apps that are not modern layout 58 * aware. 59 * @hide 60 */ 61 public static final int SCREENLAYOUT_COMPAT_NEEDED = 0x10000000; 62 63 /** 64 * Bit mask of overall layout of the screen. Currently there are two 65 * fields: 66 * <p>The {@link #SCREENLAYOUT_SIZE_MASK} bits define the overall size 67 * of the screen. They may be one of 68 * {@link #SCREENLAYOUT_SIZE_SMALL}, {@link #SCREENLAYOUT_SIZE_NORMAL}, 69 * or {@link #SCREENLAYOUT_SIZE_LARGE}. 70 * 71 * <p>The {@link #SCREENLAYOUT_LONG_MASK} defines whether the screen 72 * is wider/taller than normal. They may be one of 73 * {@link #SCREENLAYOUT_LONG_NO} or {@link #SCREENLAYOUT_LONG_YES}. 74 */ 75 public int screenLayout; 76 77 public static final int TOUCHSCREEN_UNDEFINED = 0; 78 public static final int TOUCHSCREEN_NOTOUCH = 1; 79 public static final int TOUCHSCREEN_STYLUS = 2; 80 public static final int TOUCHSCREEN_FINGER = 3; 81 82 /** 83 * The kind of touch screen attached to the device. 84 * One of: {@link #TOUCHSCREEN_NOTOUCH}, {@link #TOUCHSCREEN_STYLUS}, 85 * {@link #TOUCHSCREEN_FINGER}. 86 */ 87 public int touchscreen; 88 89 public static final int KEYBOARD_UNDEFINED = 0; 90 public static final int KEYBOARD_NOKEYS = 1; 91 public static final int KEYBOARD_QWERTY = 2; 92 public static final int KEYBOARD_12KEY = 3; 93 94 /** 95 * The kind of keyboard attached to the device. 96 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY}, 97 * {@link #KEYBOARD_12KEY}. 98 */ 99 public int keyboard; 100 101 public static final int KEYBOARDHIDDEN_UNDEFINED = 0; 102 public static final int KEYBOARDHIDDEN_NO = 1; 103 public static final int KEYBOARDHIDDEN_YES = 2; 104 /** Constant matching actual resource implementation. {@hide} */ 105 public static final int KEYBOARDHIDDEN_SOFT = 3; 106 107 /** 108 * A flag indicating whether any keyboard is available. Unlike 109 * {@link #hardKeyboardHidden}, this also takes into account a soft 110 * keyboard, so if the hard keyboard is hidden but there is soft 111 * keyboard available, it will be set to NO. Value is one of: 112 * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}. 113 */ 114 public int keyboardHidden; 115 116 public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0; 117 public static final int HARDKEYBOARDHIDDEN_NO = 1; 118 public static final int HARDKEYBOARDHIDDEN_YES = 2; 119 120 /** 121 * A flag indicating whether the hard keyboard has been hidden. This will 122 * be set on a device with a mechanism to hide the keyboard from the 123 * user, when that mechanism is closed. One of: 124 * {@link #HARDKEYBOARDHIDDEN_NO}, {@link #HARDKEYBOARDHIDDEN_YES}. 125 */ 126 public int hardKeyboardHidden; 127 128 public static final int NAVIGATION_UNDEFINED = 0; 129 public static final int NAVIGATION_NONAV = 1; 130 public static final int NAVIGATION_DPAD = 2; 131 public static final int NAVIGATION_TRACKBALL = 3; 132 public static final int NAVIGATION_WHEEL = 4; 133 134 /** 135 * The kind of navigation method available on the device. 136 * One of: {@link #NAVIGATION_NONAV}, {@link #NAVIGATION_DPAD}, 137 * {@link #NAVIGATION_TRACKBALL}, {@link #NAVIGATION_WHEEL}. 138 */ 139 public int navigation; 140 141 public static final int NAVIGATIONHIDDEN_UNDEFINED = 0; 142 public static final int NAVIGATIONHIDDEN_NO = 1; 143 public static final int NAVIGATIONHIDDEN_YES = 2; 144 145 /** 146 * A flag indicating whether any 5-way or DPAD navigation available. 147 * This will be set on a device with a mechanism to hide the navigation 148 * controls from the user, when that mechanism is closed. One of: 149 * {@link #NAVIGATIONHIDDEN_NO}, {@link #NAVIGATIONHIDDEN_YES}. 150 */ 151 public int navigationHidden; 152 153 public static final int ORIENTATION_UNDEFINED = 0; 154 public static final int ORIENTATION_PORTRAIT = 1; 155 public static final int ORIENTATION_LANDSCAPE = 2; 156 public static final int ORIENTATION_SQUARE = 3; 157 158 /** 159 * Overall orientation of the screen. May be one of 160 * {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT}, 161 * or {@link #ORIENTATION_SQUARE}. 162 */ 163 public int orientation; 164 165 /** @hide (UIMODE) Pending API council approval */ 166 public static final int UI_MODE_TYPE_MASK = 0x0f; 167 /** @hide (UIMODE) Pending API council approval */ 168 public static final int UI_MODE_TYPE_NORMAL = 0x00; 169 /** @hide (UIMODE) Pending API council approval */ 170 public static final int UI_MODE_TYPE_CAR = 0x01; 171 172 /** @hide (UIMODE) Pending API council approval */ 173 public static final int UI_MODE_NIGHT_MASK = 0x30; 174 /** @hide (UIMODE) Pending API council approval */ 175 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00; 176 /** @hide (UIMODE) Pending API council approval */ 177 public static final int UI_MODE_NIGHT_NO = 0x10; 178 /** @hide (UIMODE) Pending API council approval */ 179 public static final int UI_MODE_NIGHT_YES = 0x20; 180 181 /** 182 * Bit mask of the ui mode. Currently there are two fields: 183 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the 184 * device. They may be one of 185 * {@link #UI_MODE_TYPE_NORMAL} or {@link #UI_MODE_TYPE_CAR}. 186 * 187 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen 188 * is in a special mode. They may be one of 189 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}. 190 * 191 * @hide (UIMODE) Pending API council approval 192 */ 193 public int uiMode; 194 195 /** 196 * Construct an invalid Configuration. You must call {@link #setToDefaults} 197 * for this object to be valid. {@more} 198 */ 199 public Configuration() { 200 setToDefaults(); 201 } 202 203 /** 204 * Makes a deep copy suitable for modification. 205 */ 206 public Configuration(Configuration o) { 207 fontScale = o.fontScale; 208 mcc = o.mcc; 209 mnc = o.mnc; 210 if (o.locale != null) { 211 locale = (Locale) o.locale.clone(); 212 } 213 userSetLocale = o.userSetLocale; 214 touchscreen = o.touchscreen; 215 keyboard = o.keyboard; 216 keyboardHidden = o.keyboardHidden; 217 hardKeyboardHidden = o.hardKeyboardHidden; 218 navigation = o.navigation; 219 navigationHidden = o.navigationHidden; 220 orientation = o.orientation; 221 screenLayout = o.screenLayout; 222 uiMode = o.uiMode; 223 } 224 225 public String toString() { 226 StringBuilder sb = new StringBuilder(128); 227 sb.append("{ scale="); 228 sb.append(fontScale); 229 sb.append(" imsi="); 230 sb.append(mcc); 231 sb.append("/"); 232 sb.append(mnc); 233 sb.append(" loc="); 234 sb.append(locale); 235 sb.append(" touch="); 236 sb.append(touchscreen); 237 sb.append(" keys="); 238 sb.append(keyboard); 239 sb.append("/"); 240 sb.append(keyboardHidden); 241 sb.append("/"); 242 sb.append(hardKeyboardHidden); 243 sb.append(" nav="); 244 sb.append(navigation); 245 sb.append("/"); 246 sb.append(navigationHidden); 247 sb.append(" orien="); 248 sb.append(orientation); 249 sb.append(" layout="); 250 sb.append(screenLayout); 251 sb.append(" uiMode="); 252 sb.append(uiMode); 253 sb.append('}'); 254 return sb.toString(); 255 } 256 257 /** 258 * Set this object to the system defaults. 259 */ 260 public void setToDefaults() { 261 fontScale = 1; 262 mcc = mnc = 0; 263 locale = Locale.getDefault(); 264 userSetLocale = false; 265 touchscreen = TOUCHSCREEN_UNDEFINED; 266 keyboard = KEYBOARD_UNDEFINED; 267 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED; 268 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED; 269 navigation = NAVIGATION_UNDEFINED; 270 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED; 271 orientation = ORIENTATION_UNDEFINED; 272 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED; 273 uiMode = UI_MODE_TYPE_NORMAL; 274 } 275 276 /** {@hide} */ 277 @Deprecated public void makeDefault() { 278 setToDefaults(); 279 } 280 281 /** 282 * Copy the fields from delta into this Configuration object, keeping 283 * track of which ones have changed. Any undefined fields in 284 * <var>delta</var> are ignored and not copied in to the current 285 * Configuration. 286 * @return Returns a bit mask of the changed fields, as per 287 * {@link #diff}. 288 */ 289 public int updateFrom(Configuration delta) { 290 int changed = 0; 291 if (delta.fontScale > 0 && fontScale != delta.fontScale) { 292 changed |= ActivityInfo.CONFIG_FONT_SCALE; 293 fontScale = delta.fontScale; 294 } 295 if (delta.mcc != 0 && mcc != delta.mcc) { 296 changed |= ActivityInfo.CONFIG_MCC; 297 mcc = delta.mcc; 298 } 299 if (delta.mnc != 0 && mnc != delta.mnc) { 300 changed |= ActivityInfo.CONFIG_MNC; 301 mnc = delta.mnc; 302 } 303 if (delta.locale != null 304 && (locale == null || !locale.equals(delta.locale))) { 305 changed |= ActivityInfo.CONFIG_LOCALE; 306 locale = delta.locale != null 307 ? (Locale) delta.locale.clone() : null; 308 } 309 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0))) 310 { 311 userSetLocale = true; 312 changed |= ActivityInfo.CONFIG_LOCALE; 313 } 314 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED 315 && touchscreen != delta.touchscreen) { 316 changed |= ActivityInfo.CONFIG_TOUCHSCREEN; 317 touchscreen = delta.touchscreen; 318 } 319 if (delta.keyboard != KEYBOARD_UNDEFINED 320 && keyboard != delta.keyboard) { 321 changed |= ActivityInfo.CONFIG_KEYBOARD; 322 keyboard = delta.keyboard; 323 } 324 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED 325 && keyboardHidden != delta.keyboardHidden) { 326 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 327 keyboardHidden = delta.keyboardHidden; 328 } 329 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED 330 && hardKeyboardHidden != delta.hardKeyboardHidden) { 331 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 332 hardKeyboardHidden = delta.hardKeyboardHidden; 333 } 334 if (delta.navigation != NAVIGATION_UNDEFINED 335 && navigation != delta.navigation) { 336 changed |= ActivityInfo.CONFIG_NAVIGATION; 337 navigation = delta.navigation; 338 } 339 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED 340 && navigationHidden != delta.navigationHidden) { 341 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 342 navigationHidden = delta.navigationHidden; 343 } 344 if (delta.orientation != ORIENTATION_UNDEFINED 345 && orientation != delta.orientation) { 346 changed |= ActivityInfo.CONFIG_ORIENTATION; 347 orientation = delta.orientation; 348 } 349 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED 350 && screenLayout != delta.screenLayout) { 351 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT; 352 screenLayout = delta.screenLayout; 353 } 354 if (delta.uiMode != UI_MODE_TYPE_NORMAL 355 && uiMode != delta.uiMode) { 356 changed |= ActivityInfo.CONFIG_UI_MODE; 357 uiMode = delta.uiMode; 358 } 359 360 return changed; 361 } 362 363 /** 364 * Return a bit mask of the differences between this Configuration 365 * object and the given one. Does not change the values of either. Any 366 * undefined fields in <var>delta</var> are ignored. 367 * @return Returns a bit mask indicating which configuration 368 * values has changed, containing any combination of 369 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE 370 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE}, 371 * {@link android.content.pm.ActivityInfo#CONFIG_MCC 372 * PackageManager.ActivityInfo.CONFIG_MCC}, 373 * {@link android.content.pm.ActivityInfo#CONFIG_MNC 374 * PackageManager.ActivityInfo.CONFIG_MNC}, 375 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE 376 * PackageManager.ActivityInfo.CONFIG_LOCALE}, 377 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN 378 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN}, 379 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD 380 * PackageManager.ActivityInfo.CONFIG_KEYBOARD}, 381 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION 382 * PackageManager.ActivityInfo.CONFIG_NAVIGATION}, 383 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION 384 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, or 385 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT 386 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}. 387 */ 388 public int diff(Configuration delta) { 389 int changed = 0; 390 if (delta.fontScale > 0 && fontScale != delta.fontScale) { 391 changed |= ActivityInfo.CONFIG_FONT_SCALE; 392 } 393 if (delta.mcc != 0 && mcc != delta.mcc) { 394 changed |= ActivityInfo.CONFIG_MCC; 395 } 396 if (delta.mnc != 0 && mnc != delta.mnc) { 397 changed |= ActivityInfo.CONFIG_MNC; 398 } 399 if (delta.locale != null 400 && (locale == null || !locale.equals(delta.locale))) { 401 changed |= ActivityInfo.CONFIG_LOCALE; 402 } 403 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED 404 && touchscreen != delta.touchscreen) { 405 changed |= ActivityInfo.CONFIG_TOUCHSCREEN; 406 } 407 if (delta.keyboard != KEYBOARD_UNDEFINED 408 && keyboard != delta.keyboard) { 409 changed |= ActivityInfo.CONFIG_KEYBOARD; 410 } 411 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED 412 && keyboardHidden != delta.keyboardHidden) { 413 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 414 } 415 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED 416 && hardKeyboardHidden != delta.hardKeyboardHidden) { 417 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 418 } 419 if (delta.navigation != NAVIGATION_UNDEFINED 420 && navigation != delta.navigation) { 421 changed |= ActivityInfo.CONFIG_NAVIGATION; 422 } 423 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED 424 && navigationHidden != delta.navigationHidden) { 425 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 426 } 427 if (delta.orientation != ORIENTATION_UNDEFINED 428 && orientation != delta.orientation) { 429 changed |= ActivityInfo.CONFIG_ORIENTATION; 430 } 431 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED 432 && screenLayout != delta.screenLayout) { 433 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT; 434 } 435 if (delta.uiMode != UI_MODE_TYPE_NORMAL 436 && uiMode != delta.uiMode) { 437 changed |= ActivityInfo.CONFIG_UI_MODE; 438 } 439 440 return changed; 441 } 442 443 /** 444 * Determine if a new resource needs to be loaded from the bit set of 445 * configuration changes returned by {@link #updateFrom(Configuration)}. 446 * 447 * @param configChanges The mask of changes configurations as returned by 448 * {@link #updateFrom(Configuration)}. 449 * @param interestingChanges The configuration changes that the resource 450 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}. 451 * 452 * @return Return true if the resource needs to be loaded, else false. 453 */ 454 public static boolean needNewResources(int configChanges, int interestingChanges) { 455 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0; 456 } 457 458 /** 459 * Parcelable methods 460 */ 461 public int describeContents() { 462 return 0; 463 } 464 465 public void writeToParcel(Parcel dest, int flags) { 466 dest.writeFloat(fontScale); 467 dest.writeInt(mcc); 468 dest.writeInt(mnc); 469 if (locale == null) { 470 dest.writeInt(0); 471 } else { 472 dest.writeInt(1); 473 dest.writeString(locale.getLanguage()); 474 dest.writeString(locale.getCountry()); 475 dest.writeString(locale.getVariant()); 476 } 477 if(userSetLocale) { 478 dest.writeInt(1); 479 } else { 480 dest.writeInt(0); 481 } 482 dest.writeInt(touchscreen); 483 dest.writeInt(keyboard); 484 dest.writeInt(keyboardHidden); 485 dest.writeInt(hardKeyboardHidden); 486 dest.writeInt(navigation); 487 dest.writeInt(navigationHidden); 488 dest.writeInt(orientation); 489 dest.writeInt(screenLayout); 490 dest.writeInt(uiMode); 491 } 492 493 public static final Parcelable.Creator<Configuration> CREATOR 494 = new Parcelable.Creator<Configuration>() { 495 public Configuration createFromParcel(Parcel source) { 496 return new Configuration(source); 497 } 498 499 public Configuration[] newArray(int size) { 500 return new Configuration[size]; 501 } 502 }; 503 504 /** 505 * Construct this Configuration object, reading from the Parcel. 506 */ 507 private Configuration(Parcel source) { 508 fontScale = source.readFloat(); 509 mcc = source.readInt(); 510 mnc = source.readInt(); 511 if (source.readInt() != 0) { 512 locale = new Locale(source.readString(), source.readString(), 513 source.readString()); 514 } 515 userSetLocale = (source.readInt()==1); 516 touchscreen = source.readInt(); 517 keyboard = source.readInt(); 518 keyboardHidden = source.readInt(); 519 hardKeyboardHidden = source.readInt(); 520 navigation = source.readInt(); 521 navigationHidden = source.readInt(); 522 orientation = source.readInt(); 523 screenLayout = source.readInt(); 524 uiMode = source.readInt(); 525 } 526 527 public int compareTo(Configuration that) { 528 int n; 529 float a = this.fontScale; 530 float b = that.fontScale; 531 if (a < b) return -1; 532 if (a > b) return 1; 533 n = this.mcc - that.mcc; 534 if (n != 0) return n; 535 n = this.mnc - that.mnc; 536 if (n != 0) return n; 537 n = this.locale.getLanguage().compareTo(that.locale.getLanguage()); 538 if (n != 0) return n; 539 n = this.locale.getCountry().compareTo(that.locale.getCountry()); 540 if (n != 0) return n; 541 n = this.locale.getVariant().compareTo(that.locale.getVariant()); 542 if (n != 0) return n; 543 n = this.touchscreen - that.touchscreen; 544 if (n != 0) return n; 545 n = this.keyboard - that.keyboard; 546 if (n != 0) return n; 547 n = this.keyboardHidden - that.keyboardHidden; 548 if (n != 0) return n; 549 n = this.hardKeyboardHidden - that.hardKeyboardHidden; 550 if (n != 0) return n; 551 n = this.navigation - that.navigation; 552 if (n != 0) return n; 553 n = this.navigationHidden - that.navigationHidden; 554 if (n != 0) return n; 555 n = this.orientation - that.orientation; 556 if (n != 0) return n; 557 n = this.screenLayout - that.screenLayout; 558 if (n != 0) return n; 559 n = this.uiMode - that.uiMode; 560 //if (n != 0) return n; 561 return n; 562 } 563 564 public boolean equals(Configuration that) { 565 if (that == null) return false; 566 if (that == this) return true; 567 return this.compareTo(that) == 0; 568 } 569 570 public boolean equals(Object that) { 571 try { 572 return equals((Configuration)that); 573 } catch (ClassCastException e) { 574 } 575 return false; 576 } 577 578 public int hashCode() { 579 return ((int)this.fontScale) + this.mcc + this.mnc 580 + this.locale.hashCode() + this.touchscreen 581 + this.keyboard + this.keyboardHidden + this.hardKeyboardHidden 582 + this.navigation + this.navigationHidden 583 + this.orientation + this.screenLayout + this.uiMode; 584 } 585} 586