From 88c0d696cc25defc021ae885811d1d52c482ebcf Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Thu, 12 May 2011 18:07:47 -0700 Subject: [PATCH] DO NOT MERGE. Integrate add new screen width/height in "dp" configs. You can now specify resource configuration variants "wNNNdp" and "hNNNdp". These are the minimum screen width/height in "dp" units. This allows you to do things like have your app adjust its layout based only on the about of horizontal space available. This introduces a new configuration change flag for screen size. Note that this configuration change happens each time the orientation changes. Applications often say they handle the orientation change to avoid being restarted at a screen rotation, and this will now cause them to be restarted. To address this, we assume the app can handle this new config change if its target SDK version is < ICS. Change-Id: I4acb73d82677b74092c1da9e4046a4951921f9f4 --- include/utils/ResourceTypes.h | 67 +++++++++++++++++++++++++++++++++-- libs/utils/ResourceTypes.cpp | 31 ++++++++++++---- 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/include/utils/ResourceTypes.h b/include/utils/ResourceTypes.h index 6227f3e92..35792dc0c 100644 --- a/include/utils/ResourceTypes.h +++ b/include/utils/ResourceTypes.h @@ -972,6 +972,14 @@ struct ResTable_config uint32_t screenConfig; }; + union { + struct { + uint16_t screenWidthDp; + uint16_t screenHeightDp; + }; + uint32_t screenSizeDp; + }; + inline void copyFromDeviceNoSwap(const ResTable_config& o) { const size_t size = dtohl(o.size); if (size >= sizeof(ResTable_config)) { @@ -992,6 +1000,8 @@ struct ResTable_config screenHeight = dtohs(screenHeight); sdkVersion = dtohs(sdkVersion); minorVersion = dtohs(minorVersion); + screenWidthDp = dtohs(screenWidthDp); + screenHeightDp = dtohs(screenHeightDp); } inline void swapHtoD() { @@ -1003,6 +1013,8 @@ struct ResTable_config screenHeight = htods(screenHeight); sdkVersion = htods(sdkVersion); minorVersion = htods(minorVersion); + screenWidthDp = htods(screenWidthDp); + screenHeightDp = htods(screenHeightDp); } inline int compare(const ResTable_config& o) const { @@ -1021,6 +1033,8 @@ struct ResTable_config diff = (int32_t)(screenLayout - o.screenLayout); if (diff != 0) return diff; diff = (int32_t)(uiMode - o.uiMode); + if (diff != 0) return diff; + diff = (int32_t)(screenSizeDp - o.screenSizeDp); return (int)diff; } @@ -1061,6 +1075,7 @@ struct ResTable_config if (version != o.version) diffs |= CONFIG_VERSION; if (screenLayout != o.screenLayout) diffs |= CONFIG_SCREEN_LAYOUT; if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE; + if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE; return diffs; } @@ -1105,6 +1120,18 @@ struct ResTable_config } } + if (screenSizeDp || o.screenSizeDp) { + if (screenWidthDp != o.screenWidthDp) { + if (!screenWidthDp) return false; + if (!o.screenWidthDp) return true; + } + + if (screenHeightDp != o.screenHeightDp) { + if (!screenHeightDp) return false; + if (!o.screenHeightDp) return true; + } + } + if (orientation != o.orientation) { if (!orientation) return false; if (!o.orientation) return true; @@ -1243,6 +1270,30 @@ struct ResTable_config } } + if (screenSizeDp || o.screenSizeDp) { + // Better is based on the sum of the difference between both + // width and height from the requested dimensions. We are + // assuming the invalid configs (with smaller dimens) have + // already been filtered. Note that if a particular dimension + // is unspecified, we will end up with a large value (the + // difference between 0 and the requested dimension), which is + // good since we will prefer a config that has specified a + // dimension value. + int myDelta = 0, otherDelta = 0; + if (requested->screenWidthDp) { + myDelta += requested->screenWidthDp - screenWidthDp; + otherDelta += requested->screenWidthDp - o.screenWidthDp; + } + if (requested->screenHeightDp) { + myDelta += requested->screenHeightDp - screenHeightDp; + otherDelta += requested->screenHeightDp - o.screenHeightDp; + } + //LOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d", + // screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp, + // requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta); + return (myDelta <= otherDelta); + } + if ((orientation != o.orientation) && requested->orientation) { return (orientation); } @@ -1426,6 +1477,18 @@ struct ResTable_config return false; } } + if (screenSizeDp != 0) { + if (settings.screenWidthDp != 0 && screenWidthDp != 0 + && screenWidthDp > settings.screenWidthDp) { + //LOGI("Filtering out width %d in requested %d", screenWidthDp, settings.screenWidthDp); + return false; + } + if (settings.screenHeightDp != 0 && screenHeightDp != 0 + && screenHeightDp > settings.screenHeightDp) { + //LOGI("Filtering out height %d in requested %d", screenHeightDp, settings.screenHeightDp); + return false; + } + } if (screenType != 0) { if (settings.orientation != 0 && orientation != 0 && orientation != settings.orientation) { @@ -1505,13 +1568,13 @@ struct ResTable_config String8 toString() const { char buf[200]; sprintf(buf, "imsi=%d/%d lang=%c%c reg=%c%c orient=%d touch=%d dens=%d " - "kbd=%d nav=%d input=%d scrnW=%d scrnH=%d sz=%d long=%d " + "kbd=%d nav=%d input=%d ssz=%dx%d %ddp x %ddp sz=%d long=%d " "ui=%d night=%d vers=%d.%d", mcc, mnc, language[0] ? language[0] : '-', language[1] ? language[1] : '-', country[0] ? country[0] : '-', country[1] ? country[1] : '-', orientation, touchscreen, density, keyboard, navigation, inputFlags, - screenWidth, screenHeight, + screenWidth, screenHeight, screenWidthDp, screenHeightDp, screenLayout&MASK_SCREENSIZE, screenLayout&MASK_SCREENLONG, uiMode&MASK_UI_MODE_TYPE, uiMode&MASK_UI_MODE_NIGHT, sdkVersion, minorVersion); diff --git a/libs/utils/ResourceTypes.cpp b/libs/utils/ResourceTypes.cpp index 7197ad7a2..ac9cdf9f9 100644 --- a/libs/utils/ResourceTypes.cpp +++ b/libs/utils/ResourceTypes.cpp @@ -2424,7 +2424,7 @@ void ResTable::setParameters(const ResTable_config* params) { mLock.lock(); TABLE_GETENTRY(LOGI("Setting parameters: imsi:%d/%d lang:%c%c cnt:%c%c " - "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", + "orien:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d %ddp x %ddp\n", params->mcc, params->mnc, params->language[0] ? params->language[0] : '-', params->language[1] ? params->language[1] : '-', @@ -2437,7 +2437,9 @@ void ResTable::setParameters(const ResTable_config* params) params->inputFlags, params->navigation, params->screenWidth, - params->screenHeight)); + params->screenHeight, + params->screenWidthDp, + params->screenHeightDp)); mParams = *params; for (size_t i=0; iconfig); - TABLE_GETENTRY(LOGI("Match entry 0x%x in type 0x%x (sz 0x%x): imsi:%d/%d=%d/%d lang:%c%c=%c%c cnt:%c%c=%c%c " - "orien:%d=%d touch:%d=%d density:%d=%d key:%d=%d inp:%d=%d nav:%d=%d w:%d=%d h:%d=%d\n", + TABLE_GETENTRY(LOGI("Match entry 0x%x in type 0x%x (sz 0x%x): imsi:%d/%d=%d/%d " + "lang:%c%c=%c%c cnt:%c%c=%c%c orien:%d=%d touch:%d=%d " + "density:%d=%d key:%d=%d inp:%d=%d nav:%d=%d w:%d=%d h:%d=%d " + "wdp:%d=%d hdp:%d=%d\n", entryIndex, typeIndex+1, dtohl(thisType->config.size), thisConfig.mcc, thisConfig.mnc, config ? config->mcc : 0, config ? config->mnc : 0, @@ -3786,7 +3790,11 @@ ssize_t ResTable::getEntry( thisConfig.screenWidth, config ? config->screenWidth : 0, thisConfig.screenHeight, - config ? config->screenHeight : 0)); + config ? config->screenHeight : 0, + thisConfig.screenWidthDp, + config ? config->screenWidthDp : 0, + thisConfig.screenHeightDp, + config ? config->screenHeightDp : 0)); // Check to make sure this one is valid for the current parameters. if (config && !thisConfig.match(*config)) { @@ -4067,7 +4075,8 @@ status_t ResTable::parsePackage(const ResTable_package* const pkg, ResTable_config thisConfig; thisConfig.copyFromDtoH(type->config); LOGI("Adding config to type %d: imsi:%d/%d lang:%c%c cnt:%c%c " - "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d\n", + "orien:%d touch:%d density:%d key:%d inp:%d nav:%d w:%d h:%d " + "wdp:%d hdp:%d\n", type->id, thisConfig.mcc, thisConfig.mnc, thisConfig.language[0] ? thisConfig.language[0] : '-', @@ -4081,7 +4090,9 @@ status_t ResTable::parsePackage(const ResTable_package* const pkg, thisConfig.inputFlags, thisConfig.navigation, thisConfig.screenWidth, - thisConfig.screenHeight)); + thisConfig.screenHeight, + thisConfig.screenWidthDp, + thisConfig.screenHeightDp)); t->configs.add(type); } else { status_t err = validate_chunk(chunk, sizeof(ResChunk_header), @@ -4444,6 +4455,12 @@ void ResTable::print(bool inclValues) const if (type->config.screenHeight != 0) { printf(" h=%d", dtohs(type->config.screenHeight)); } + if (type->config.screenWidthDp != 0) { + printf(" wdp=%d", dtohs(type->config.screenWidthDp)); + } + if (type->config.screenHeightDp != 0) { + printf(" hdp=%d", dtohs(type->config.screenHeightDp)); + } if (type->config.sdkVersion != 0) { printf(" sdk=%d", dtohs(type->config.sdkVersion)); }