Configuration.java revision 5be8de3420ba4c9d816b98e29bdec11715f6b626
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 public static final int UI_MODE_TYPE_TELEVISION = 0x04; 230 231 public static final int UI_MODE_NIGHT_MASK = 0x30; 232 public static final int UI_MODE_NIGHT_UNDEFINED = 0x00; 233 public static final int UI_MODE_NIGHT_NO = 0x10; 234 public static final int UI_MODE_NIGHT_YES = 0x20; 235 236 /** 237 * Bit mask of the ui mode. Currently there are two fields: 238 * <p>The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the 239 * device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED}, 240 * {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK}, 241 * or {@link #UI_MODE_TYPE_CAR}. 242 * 243 * <p>The {@link #UI_MODE_NIGHT_MASK} defines whether the screen 244 * is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED}, 245 * {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}. 246 */ 247 public int uiMode; 248 249 public static final int SCREEN_WIDTH_DP_UNDEFINED = 0; 250 251 /** 252 * The current width of the available screen space, in dp units. 253 */ 254 public int screenWidthDp; 255 256 public static final int SCREEN_HEIGHT_DP_UNDEFINED = 0; 257 258 /** 259 * The current height of the available screen space, in dp units. 260 */ 261 public int screenHeightDp; 262 263 public static final int SMALLEST_SCREEN_WIDTH_DP_UNDEFINED = 0; 264 265 /** 266 * The smallest screen size an application will see in normal operation. 267 * This is the smallest value of both screenWidthDp and screenHeightDp 268 * in both portrait and landscape. 269 */ 270 public int smallestScreenWidthDp; 271 272 /** 273 * @hide Internal book-keeping. 274 */ 275 public int seq; 276 277 /** 278 * Construct an invalid Configuration. You must call {@link #setToDefaults} 279 * for this object to be valid. {@more} 280 */ 281 public Configuration() { 282 setToDefaults(); 283 } 284 285 /** 286 * Makes a deep copy suitable for modification. 287 */ 288 public Configuration(Configuration o) { 289 setTo(o); 290 } 291 292 public void setTo(Configuration o) { 293 fontScale = o.fontScale; 294 mcc = o.mcc; 295 mnc = o.mnc; 296 if (o.locale != null) { 297 locale = (Locale) o.locale.clone(); 298 } 299 userSetLocale = o.userSetLocale; 300 touchscreen = o.touchscreen; 301 keyboard = o.keyboard; 302 keyboardHidden = o.keyboardHidden; 303 hardKeyboardHidden = o.hardKeyboardHidden; 304 navigation = o.navigation; 305 navigationHidden = o.navigationHidden; 306 orientation = o.orientation; 307 screenLayout = o.screenLayout; 308 uiMode = o.uiMode; 309 screenWidthDp = o.screenWidthDp; 310 screenHeightDp = o.screenHeightDp; 311 smallestScreenWidthDp = o.smallestScreenWidthDp; 312 seq = o.seq; 313 } 314 315 public String toString() { 316 StringBuilder sb = new StringBuilder(128); 317 sb.append("{"); 318 sb.append(fontScale); 319 sb.append(" "); 320 sb.append(mcc); 321 sb.append("mcc"); 322 sb.append(mnc); 323 sb.append("mnc"); 324 if (locale != null) { 325 sb.append(" "); 326 sb.append(locale); 327 } else { 328 sb.append(" (no locale)"); 329 } 330 if (smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) { 331 sb.append(" sw"); sb.append(smallestScreenWidthDp); sb.append("dp"); 332 } else { 333 sb.append("?swdp"); 334 } 335 if (screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED) { 336 sb.append(" w"); sb.append(screenWidthDp); sb.append("dp"); 337 } else { 338 sb.append("?wdp"); 339 } 340 if (screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED) { 341 sb.append(" h"); sb.append(screenHeightDp); sb.append("dp"); 342 } else { 343 sb.append("?hdp"); 344 } 345 switch ((screenLayout&SCREENLAYOUT_SIZE_MASK)) { 346 case SCREENLAYOUT_SIZE_UNDEFINED: sb.append(" ?lsize"); break; 347 case SCREENLAYOUT_SIZE_SMALL: sb.append(" smll"); break; 348 case SCREENLAYOUT_SIZE_NORMAL: sb.append(" nrml"); break; 349 case SCREENLAYOUT_SIZE_LARGE: sb.append(" lrg"); break; 350 case SCREENLAYOUT_SIZE_XLARGE: sb.append(" xlrg"); break; 351 default: sb.append(" layoutSize="); 352 sb.append(screenLayout&SCREENLAYOUT_SIZE_MASK); break; 353 } 354 switch ((screenLayout&SCREENLAYOUT_LONG_MASK)) { 355 case SCREENLAYOUT_LONG_UNDEFINED: sb.append(" ?long"); break; 356 case SCREENLAYOUT_LONG_NO: /* not-long is not interesting to print */ break; 357 case SCREENLAYOUT_LONG_YES: sb.append(" long"); break; 358 default: sb.append(" layoutLong="); 359 sb.append(screenLayout&SCREENLAYOUT_LONG_MASK); break; 360 } 361 switch (orientation) { 362 case ORIENTATION_UNDEFINED: sb.append(" ?orien"); break; 363 case ORIENTATION_LANDSCAPE: sb.append(" land"); break; 364 case ORIENTATION_PORTRAIT: sb.append(" port"); break; 365 default: sb.append(" orien="); sb.append(orientation); break; 366 } 367 switch ((uiMode&UI_MODE_TYPE_MASK)) { 368 case UI_MODE_TYPE_UNDEFINED: sb.append(" ?uimode"); break; 369 case UI_MODE_TYPE_NORMAL: /* normal is not interesting to print */ break; 370 case UI_MODE_TYPE_DESK: sb.append(" desk"); break; 371 case UI_MODE_TYPE_CAR: sb.append(" car"); break; 372 case UI_MODE_TYPE_TELEVISION: sb.append(" television"); break; 373 default: sb.append(" uimode="); sb.append(uiMode&UI_MODE_TYPE_MASK); break; 374 } 375 switch ((uiMode&UI_MODE_NIGHT_MASK)) { 376 case UI_MODE_NIGHT_UNDEFINED: sb.append(" ?night"); break; 377 case UI_MODE_NIGHT_NO: /* not-night is not interesting to print */ break; 378 case UI_MODE_NIGHT_YES: sb.append(" night"); break; 379 default: sb.append(" night="); sb.append(uiMode&UI_MODE_NIGHT_MASK); break; 380 } 381 switch (touchscreen) { 382 case TOUCHSCREEN_UNDEFINED: sb.append(" ?touch"); break; 383 case TOUCHSCREEN_NOTOUCH: sb.append(" -touch"); break; 384 case TOUCHSCREEN_STYLUS: sb.append(" stylus"); break; 385 case TOUCHSCREEN_FINGER: sb.append(" finger"); break; 386 default: sb.append(" touch="); sb.append(touchscreen); break; 387 } 388 switch (keyboard) { 389 case KEYBOARD_UNDEFINED: sb.append(" ?keyb"); break; 390 case KEYBOARD_NOKEYS: sb.append(" -keyb"); break; 391 case KEYBOARD_QWERTY: sb.append(" qwerty"); break; 392 case KEYBOARD_12KEY: sb.append(" 12key"); break; 393 default: sb.append(" keys="); sb.append(keyboard); break; 394 } 395 switch (keyboardHidden) { 396 case KEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break; 397 case KEYBOARDHIDDEN_NO: sb.append("/v"); break; 398 case KEYBOARDHIDDEN_YES: sb.append("/h"); break; 399 case KEYBOARDHIDDEN_SOFT: sb.append("/s"); break; 400 default: sb.append("/"); sb.append(keyboardHidden); break; 401 } 402 switch (hardKeyboardHidden) { 403 case HARDKEYBOARDHIDDEN_UNDEFINED: sb.append("/?"); break; 404 case HARDKEYBOARDHIDDEN_NO: sb.append("/v"); break; 405 case HARDKEYBOARDHIDDEN_YES: sb.append("/h"); break; 406 default: sb.append("/"); sb.append(hardKeyboardHidden); break; 407 } 408 switch (navigation) { 409 case NAVIGATION_UNDEFINED: sb.append(" ?nav"); break; 410 case NAVIGATION_NONAV: sb.append(" -nav"); break; 411 case NAVIGATION_DPAD: sb.append(" dpad"); break; 412 case NAVIGATION_TRACKBALL: sb.append(" tball"); break; 413 case NAVIGATION_WHEEL: sb.append(" wheel"); break; 414 default: sb.append(" nav="); sb.append(navigation); break; 415 } 416 switch (navigationHidden) { 417 case NAVIGATIONHIDDEN_UNDEFINED: sb.append("/?"); break; 418 case NAVIGATIONHIDDEN_NO: sb.append("/v"); break; 419 case NAVIGATIONHIDDEN_YES: sb.append("/h"); break; 420 default: sb.append("/"); sb.append(navigationHidden); break; 421 } 422 if (seq != 0) { 423 sb.append(" s."); 424 sb.append(seq); 425 } 426 sb.append('}'); 427 return sb.toString(); 428 } 429 430 /** 431 * Set this object to the system defaults. 432 */ 433 public void setToDefaults() { 434 fontScale = 1; 435 mcc = mnc = 0; 436 locale = null; 437 userSetLocale = false; 438 touchscreen = TOUCHSCREEN_UNDEFINED; 439 keyboard = KEYBOARD_UNDEFINED; 440 keyboardHidden = KEYBOARDHIDDEN_UNDEFINED; 441 hardKeyboardHidden = HARDKEYBOARDHIDDEN_UNDEFINED; 442 navigation = NAVIGATION_UNDEFINED; 443 navigationHidden = NAVIGATIONHIDDEN_UNDEFINED; 444 orientation = ORIENTATION_UNDEFINED; 445 screenLayout = SCREENLAYOUT_SIZE_UNDEFINED; 446 uiMode = UI_MODE_TYPE_UNDEFINED; 447 screenWidthDp = SCREEN_WIDTH_DP_UNDEFINED; 448 screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED; 449 smallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED; 450 seq = 0; 451 } 452 453 /** {@hide} */ 454 @Deprecated public void makeDefault() { 455 setToDefaults(); 456 } 457 458 /** 459 * Copy the fields from delta into this Configuration object, keeping 460 * track of which ones have changed. Any undefined fields in 461 * <var>delta</var> are ignored and not copied in to the current 462 * Configuration. 463 * @return Returns a bit mask of the changed fields, as per 464 * {@link #diff}. 465 */ 466 public int updateFrom(Configuration delta) { 467 int changed = 0; 468 if (delta.fontScale > 0 && fontScale != delta.fontScale) { 469 changed |= ActivityInfo.CONFIG_FONT_SCALE; 470 fontScale = delta.fontScale; 471 } 472 if (delta.mcc != 0 && mcc != delta.mcc) { 473 changed |= ActivityInfo.CONFIG_MCC; 474 mcc = delta.mcc; 475 } 476 if (delta.mnc != 0 && mnc != delta.mnc) { 477 changed |= ActivityInfo.CONFIG_MNC; 478 mnc = delta.mnc; 479 } 480 if (delta.locale != null 481 && (locale == null || !locale.equals(delta.locale))) { 482 changed |= ActivityInfo.CONFIG_LOCALE; 483 locale = delta.locale != null 484 ? (Locale) delta.locale.clone() : null; 485 } 486 if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0))) 487 { 488 userSetLocale = true; 489 changed |= ActivityInfo.CONFIG_LOCALE; 490 } 491 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED 492 && touchscreen != delta.touchscreen) { 493 changed |= ActivityInfo.CONFIG_TOUCHSCREEN; 494 touchscreen = delta.touchscreen; 495 } 496 if (delta.keyboard != KEYBOARD_UNDEFINED 497 && keyboard != delta.keyboard) { 498 changed |= ActivityInfo.CONFIG_KEYBOARD; 499 keyboard = delta.keyboard; 500 } 501 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED 502 && keyboardHidden != delta.keyboardHidden) { 503 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 504 keyboardHidden = delta.keyboardHidden; 505 } 506 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED 507 && hardKeyboardHidden != delta.hardKeyboardHidden) { 508 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 509 hardKeyboardHidden = delta.hardKeyboardHidden; 510 } 511 if (delta.navigation != NAVIGATION_UNDEFINED 512 && navigation != delta.navigation) { 513 changed |= ActivityInfo.CONFIG_NAVIGATION; 514 navigation = delta.navigation; 515 } 516 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED 517 && navigationHidden != delta.navigationHidden) { 518 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 519 navigationHidden = delta.navigationHidden; 520 } 521 if (delta.orientation != ORIENTATION_UNDEFINED 522 && orientation != delta.orientation) { 523 changed |= ActivityInfo.CONFIG_ORIENTATION; 524 orientation = delta.orientation; 525 } 526 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED 527 && screenLayout != delta.screenLayout) { 528 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT; 529 screenLayout = delta.screenLayout; 530 } 531 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED) 532 && uiMode != delta.uiMode) { 533 changed |= ActivityInfo.CONFIG_UI_MODE; 534 if ((delta.uiMode&UI_MODE_TYPE_MASK) != UI_MODE_TYPE_UNDEFINED) { 535 uiMode = (uiMode&~UI_MODE_TYPE_MASK) 536 | (delta.uiMode&UI_MODE_TYPE_MASK); 537 } 538 if ((delta.uiMode&UI_MODE_NIGHT_MASK) != UI_MODE_NIGHT_UNDEFINED) { 539 uiMode = (uiMode&~UI_MODE_NIGHT_MASK) 540 | (delta.uiMode&UI_MODE_NIGHT_MASK); 541 } 542 } 543 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED 544 && screenWidthDp != delta.screenWidthDp) { 545 changed |= ActivityInfo.CONFIG_SCREEN_SIZE; 546 screenWidthDp = delta.screenWidthDp; 547 } 548 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED 549 && screenHeightDp != delta.screenHeightDp) { 550 changed |= ActivityInfo.CONFIG_SCREEN_SIZE; 551 screenHeightDp = delta.screenHeightDp; 552 } 553 if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED 554 && smallestScreenWidthDp != delta.smallestScreenWidthDp) { 555 changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE; 556 smallestScreenWidthDp = delta.smallestScreenWidthDp; 557 } 558 559 if (delta.seq != 0) { 560 seq = delta.seq; 561 } 562 563 return changed; 564 } 565 566 /** 567 * Return a bit mask of the differences between this Configuration 568 * object and the given one. Does not change the values of either. Any 569 * undefined fields in <var>delta</var> are ignored. 570 * @return Returns a bit mask indicating which configuration 571 * values has changed, containing any combination of 572 * {@link android.content.pm.ActivityInfo#CONFIG_FONT_SCALE 573 * PackageManager.ActivityInfo.CONFIG_FONT_SCALE}, 574 * {@link android.content.pm.ActivityInfo#CONFIG_MCC 575 * PackageManager.ActivityInfo.CONFIG_MCC}, 576 * {@link android.content.pm.ActivityInfo#CONFIG_MNC 577 * PackageManager.ActivityInfo.CONFIG_MNC}, 578 * {@link android.content.pm.ActivityInfo#CONFIG_LOCALE 579 * PackageManager.ActivityInfo.CONFIG_LOCALE}, 580 * {@link android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN 581 * PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN}, 582 * {@link android.content.pm.ActivityInfo#CONFIG_KEYBOARD 583 * PackageManager.ActivityInfo.CONFIG_KEYBOARD}, 584 * {@link android.content.pm.ActivityInfo#CONFIG_NAVIGATION 585 * PackageManager.ActivityInfo.CONFIG_NAVIGATION}, 586 * {@link android.content.pm.ActivityInfo#CONFIG_ORIENTATION 587 * PackageManager.ActivityInfo.CONFIG_ORIENTATION}, 588 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT 589 * PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT}, or 590 * {@link android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE 591 * PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE}, or 592 * {@link android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE 593 * PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE}. 594 */ 595 public int diff(Configuration delta) { 596 int changed = 0; 597 if (delta.fontScale > 0 && fontScale != delta.fontScale) { 598 changed |= ActivityInfo.CONFIG_FONT_SCALE; 599 } 600 if (delta.mcc != 0 && mcc != delta.mcc) { 601 changed |= ActivityInfo.CONFIG_MCC; 602 } 603 if (delta.mnc != 0 && mnc != delta.mnc) { 604 changed |= ActivityInfo.CONFIG_MNC; 605 } 606 if (delta.locale != null 607 && (locale == null || !locale.equals(delta.locale))) { 608 changed |= ActivityInfo.CONFIG_LOCALE; 609 } 610 if (delta.touchscreen != TOUCHSCREEN_UNDEFINED 611 && touchscreen != delta.touchscreen) { 612 changed |= ActivityInfo.CONFIG_TOUCHSCREEN; 613 } 614 if (delta.keyboard != KEYBOARD_UNDEFINED 615 && keyboard != delta.keyboard) { 616 changed |= ActivityInfo.CONFIG_KEYBOARD; 617 } 618 if (delta.keyboardHidden != KEYBOARDHIDDEN_UNDEFINED 619 && keyboardHidden != delta.keyboardHidden) { 620 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 621 } 622 if (delta.hardKeyboardHidden != HARDKEYBOARDHIDDEN_UNDEFINED 623 && hardKeyboardHidden != delta.hardKeyboardHidden) { 624 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 625 } 626 if (delta.navigation != NAVIGATION_UNDEFINED 627 && navigation != delta.navigation) { 628 changed |= ActivityInfo.CONFIG_NAVIGATION; 629 } 630 if (delta.navigationHidden != NAVIGATIONHIDDEN_UNDEFINED 631 && navigationHidden != delta.navigationHidden) { 632 changed |= ActivityInfo.CONFIG_KEYBOARD_HIDDEN; 633 } 634 if (delta.orientation != ORIENTATION_UNDEFINED 635 && orientation != delta.orientation) { 636 changed |= ActivityInfo.CONFIG_ORIENTATION; 637 } 638 if (delta.screenLayout != SCREENLAYOUT_SIZE_UNDEFINED 639 && screenLayout != delta.screenLayout) { 640 changed |= ActivityInfo.CONFIG_SCREEN_LAYOUT; 641 } 642 if (delta.uiMode != (UI_MODE_TYPE_UNDEFINED|UI_MODE_NIGHT_UNDEFINED) 643 && uiMode != delta.uiMode) { 644 changed |= ActivityInfo.CONFIG_UI_MODE; 645 } 646 if (delta.screenWidthDp != SCREEN_WIDTH_DP_UNDEFINED 647 && screenWidthDp != delta.screenWidthDp) { 648 changed |= ActivityInfo.CONFIG_SCREEN_SIZE; 649 } 650 if (delta.screenHeightDp != SCREEN_HEIGHT_DP_UNDEFINED 651 && screenHeightDp != delta.screenHeightDp) { 652 changed |= ActivityInfo.CONFIG_SCREEN_SIZE; 653 } 654 if (delta.smallestScreenWidthDp != SMALLEST_SCREEN_WIDTH_DP_UNDEFINED 655 && smallestScreenWidthDp != delta.smallestScreenWidthDp) { 656 changed |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE; 657 } 658 659 return changed; 660 } 661 662 /** 663 * Determine if a new resource needs to be loaded from the bit set of 664 * configuration changes returned by {@link #updateFrom(Configuration)}. 665 * 666 * @param configChanges The mask of changes configurations as returned by 667 * {@link #updateFrom(Configuration)}. 668 * @param interestingChanges The configuration changes that the resource 669 * can handled, as given in {@link android.util.TypedValue#changingConfigurations}. 670 * 671 * @return Return true if the resource needs to be loaded, else false. 672 */ 673 public static boolean needNewResources(int configChanges, int interestingChanges) { 674 return (configChanges & (interestingChanges|ActivityInfo.CONFIG_FONT_SCALE)) != 0; 675 } 676 677 /** 678 * @hide Return true if the sequence of 'other' is better than this. Assumes 679 * that 'this' is your current sequence and 'other' is a new one you have 680 * received some how and want to compare with what you have. 681 */ 682 public boolean isOtherSeqNewer(Configuration other) { 683 if (other == null) { 684 // Sanity check. 685 return false; 686 } 687 if (other.seq == 0) { 688 // If the other sequence is not specified, then we must assume 689 // it is newer since we don't know any better. 690 return true; 691 } 692 if (seq == 0) { 693 // If this sequence is not specified, then we also consider the 694 // other is better. Yes we have a preference for other. Sue us. 695 return true; 696 } 697 int diff = other.seq - seq; 698 if (diff > 0x10000) { 699 // If there has been a sufficiently large jump, assume the 700 // sequence has wrapped around. 701 return false; 702 } 703 return diff > 0; 704 } 705 706 /** 707 * Parcelable methods 708 */ 709 public int describeContents() { 710 return 0; 711 } 712 713 public void writeToParcel(Parcel dest, int flags) { 714 dest.writeFloat(fontScale); 715 dest.writeInt(mcc); 716 dest.writeInt(mnc); 717 if (locale == null) { 718 dest.writeInt(0); 719 } else { 720 dest.writeInt(1); 721 dest.writeString(locale.getLanguage()); 722 dest.writeString(locale.getCountry()); 723 dest.writeString(locale.getVariant()); 724 } 725 if(userSetLocale) { 726 dest.writeInt(1); 727 } else { 728 dest.writeInt(0); 729 } 730 dest.writeInt(touchscreen); 731 dest.writeInt(keyboard); 732 dest.writeInt(keyboardHidden); 733 dest.writeInt(hardKeyboardHidden); 734 dest.writeInt(navigation); 735 dest.writeInt(navigationHidden); 736 dest.writeInt(orientation); 737 dest.writeInt(screenLayout); 738 dest.writeInt(uiMode); 739 dest.writeInt(screenWidthDp); 740 dest.writeInt(screenHeightDp); 741 dest.writeInt(smallestScreenWidthDp); 742 dest.writeInt(seq); 743 } 744 745 public void readFromParcel(Parcel source) { 746 fontScale = source.readFloat(); 747 mcc = source.readInt(); 748 mnc = source.readInt(); 749 if (source.readInt() != 0) { 750 locale = new Locale(source.readString(), source.readString(), 751 source.readString()); 752 } 753 userSetLocale = (source.readInt()==1); 754 touchscreen = source.readInt(); 755 keyboard = source.readInt(); 756 keyboardHidden = source.readInt(); 757 hardKeyboardHidden = source.readInt(); 758 navigation = source.readInt(); 759 navigationHidden = source.readInt(); 760 orientation = source.readInt(); 761 screenLayout = source.readInt(); 762 uiMode = source.readInt(); 763 screenWidthDp = source.readInt(); 764 screenHeightDp = source.readInt(); 765 smallestScreenWidthDp = source.readInt(); 766 seq = source.readInt(); 767 } 768 769 public static final Parcelable.Creator<Configuration> CREATOR 770 = new Parcelable.Creator<Configuration>() { 771 public Configuration createFromParcel(Parcel source) { 772 return new Configuration(source); 773 } 774 775 public Configuration[] newArray(int size) { 776 return new Configuration[size]; 777 } 778 }; 779 780 /** 781 * Construct this Configuration object, reading from the Parcel. 782 */ 783 private Configuration(Parcel source) { 784 readFromParcel(source); 785 } 786 787 public int compareTo(Configuration that) { 788 int n; 789 float a = this.fontScale; 790 float b = that.fontScale; 791 if (a < b) return -1; 792 if (a > b) return 1; 793 n = this.mcc - that.mcc; 794 if (n != 0) return n; 795 n = this.mnc - that.mnc; 796 if (n != 0) return n; 797 if (this.locale == null) { 798 if (that.locale != null) return 1; 799 } else if (that.locale == null) { 800 return -1; 801 } else { 802 n = this.locale.getLanguage().compareTo(that.locale.getLanguage()); 803 if (n != 0) return n; 804 n = this.locale.getCountry().compareTo(that.locale.getCountry()); 805 if (n != 0) return n; 806 n = this.locale.getVariant().compareTo(that.locale.getVariant()); 807 if (n != 0) return n; 808 } 809 n = this.touchscreen - that.touchscreen; 810 if (n != 0) return n; 811 n = this.keyboard - that.keyboard; 812 if (n != 0) return n; 813 n = this.keyboardHidden - that.keyboardHidden; 814 if (n != 0) return n; 815 n = this.hardKeyboardHidden - that.hardKeyboardHidden; 816 if (n != 0) return n; 817 n = this.navigation - that.navigation; 818 if (n != 0) return n; 819 n = this.navigationHidden - that.navigationHidden; 820 if (n != 0) return n; 821 n = this.orientation - that.orientation; 822 if (n != 0) return n; 823 n = this.screenLayout - that.screenLayout; 824 if (n != 0) return n; 825 n = this.uiMode - that.uiMode; 826 if (n != 0) return n; 827 n = this.screenWidthDp - that.screenWidthDp; 828 if (n != 0) return n; 829 n = this.screenHeightDp - that.screenHeightDp; 830 if (n != 0) return n; 831 n = this.smallestScreenWidthDp - that.smallestScreenWidthDp; 832 //if (n != 0) return n; 833 return n; 834 } 835 836 public boolean equals(Configuration that) { 837 if (that == null) return false; 838 if (that == this) return true; 839 return this.compareTo(that) == 0; 840 } 841 842 public boolean equals(Object that) { 843 try { 844 return equals((Configuration)that); 845 } catch (ClassCastException e) { 846 } 847 return false; 848 } 849 850 public int hashCode() { 851 int result = 17; 852 result = 31 * result + Float.floatToIntBits(fontScale); 853 result = 31 * result + mcc; 854 result = 31 * result + mnc; 855 result = 31 * result + (locale != null ? locale.hashCode() : 0); 856 result = 31 * result + touchscreen; 857 result = 31 * result + keyboard; 858 result = 31 * result + keyboardHidden; 859 result = 31 * result + hardKeyboardHidden; 860 result = 31 * result + navigation; 861 result = 31 * result + navigationHidden; 862 result = 31 * result + orientation; 863 result = 31 * result + screenLayout; 864 result = 31 * result + uiMode; 865 result = 31 * result + screenWidthDp; 866 result = 31 * result + screenHeightDp; 867 result = 31 * result + smallestScreenWidthDp; 868 return result; 869 } 870} 871