Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2010 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 
     17 #define LOG_TAG "Configuration"
     18 #include <utils/Log.h>
     19 
     20 #include <androidfw/AssetManager2.h>
     21 
     22 #include <android_runtime/android_content_res_Configuration.h>
     23 #include <android_runtime/android_util_AssetManager.h>
     24 
     25 using namespace android;
     26 
     27 AConfiguration* AConfiguration_new() {
     28     AConfiguration* config = new AConfiguration;
     29     memset(config, 0, sizeof(AConfiguration));
     30     return config;
     31 }
     32 
     33 void AConfiguration_delete(AConfiguration* config) {
     34     delete config;
     35 }
     36 
     37 void AConfiguration_fromAssetManager(AConfiguration* out, AAssetManager* am) {
     38     ScopedLock<AssetManager2> locked_mgr(*AssetManagerForNdkAssetManager(am));
     39     ResTable_config config = locked_mgr->GetConfiguration();
     40 
     41     // AConfiguration is not a virtual subclass, so we can memcpy.
     42     memcpy(out, &config, sizeof(config));
     43 }
     44 
     45 void AConfiguration_copy(AConfiguration* dest, AConfiguration* src) {
     46     *dest = *src;
     47 }
     48 
     49 int32_t AConfiguration_getMcc(AConfiguration* config) {
     50     return config->mcc;
     51 }
     52 
     53 int32_t AConfiguration_getMnc(AConfiguration* config) {
     54     return config->mnc;
     55 }
     56 
     57 void AConfiguration_getLanguage(AConfiguration* config, char* outLanguage) {
     58     outLanguage[0] = config->language[0];
     59     outLanguage[1] = config->language[1];
     60 }
     61 
     62 void AConfiguration_getCountry(AConfiguration* config, char* outCountry) {
     63     outCountry[0] = config->country[0];
     64     outCountry[1] = config->country[1];
     65 }
     66 
     67 int32_t AConfiguration_getOrientation(AConfiguration* config) {
     68     return config->orientation;
     69 }
     70 
     71 int32_t AConfiguration_getTouchscreen(AConfiguration* config) {
     72     return config->touchscreen;
     73 }
     74 
     75 int32_t AConfiguration_getDensity(AConfiguration* config) {
     76     return config->density;
     77 }
     78 
     79 int32_t AConfiguration_getKeyboard(AConfiguration* config) {
     80     return config->keyboard;
     81 }
     82 
     83 int32_t AConfiguration_getNavigation(AConfiguration* config) {
     84     return config->navigation;
     85 }
     86 
     87 int32_t AConfiguration_getKeysHidden(AConfiguration* config) {
     88     return config->inputFlags&ResTable_config::MASK_KEYSHIDDEN;
     89 }
     90 
     91 int32_t AConfiguration_getNavHidden(AConfiguration* config) {
     92     return (config->inputFlags&ResTable_config::MASK_NAVHIDDEN)
     93             >> ResTable_config::SHIFT_NAVHIDDEN;
     94 }
     95 
     96 int32_t AConfiguration_getSdkVersion(AConfiguration* config) {
     97     return config->sdkVersion;
     98 }
     99 
    100 int32_t AConfiguration_getScreenSize(AConfiguration* config) {
    101     return config->screenLayout&ResTable_config::MASK_SCREENSIZE;
    102 }
    103 
    104 int32_t AConfiguration_getScreenLong(AConfiguration* config) {
    105     return (config->screenLayout&ResTable_config::MASK_SCREENLONG)
    106             >> ResTable_config::SHIFT_SCREENLONG;
    107 }
    108 
    109 int32_t AConfiguration_getScreenRound(AConfiguration* config) {
    110     return (config->screenLayout2&ResTable_config::MASK_SCREENROUND);
    111 }
    112 
    113 int32_t AConfiguration_getUiModeType(AConfiguration* config) {
    114     return config->uiMode&ResTable_config::MASK_UI_MODE_TYPE;
    115 }
    116 
    117 int32_t AConfiguration_getUiModeNight(AConfiguration* config) {
    118     return (config->uiMode&ResTable_config::MASK_UI_MODE_NIGHT)
    119             >> ResTable_config::SHIFT_UI_MODE_NIGHT;
    120 
    121 }
    122 
    123 int32_t AConfiguration_getScreenWidthDp(AConfiguration* config) {
    124     return config->screenWidthDp;
    125 }
    126 
    127 int32_t AConfiguration_getScreenHeightDp(AConfiguration* config) {
    128     return config->screenHeightDp;
    129 }
    130 
    131 int32_t AConfiguration_getSmallestScreenWidthDp(AConfiguration* config) {
    132     return config->smallestScreenWidthDp;
    133 }
    134 
    135 int32_t AConfiguration_getLayoutDirection(AConfiguration* config) {
    136     return (config->screenLayout&ResTable_config::MASK_LAYOUTDIR)
    137             >> ResTable_config::SHIFT_LAYOUTDIR;
    138 }
    139 
    140 // ----------------------------------------------------------------------
    141 
    142 void AConfiguration_setMcc(AConfiguration* config, int32_t mcc) {
    143     config->mcc = mcc;
    144 }
    145 
    146 void AConfiguration_setMnc(AConfiguration* config, int32_t mnc) {
    147     config->mnc = mnc;
    148 }
    149 
    150 void AConfiguration_setLanguage(AConfiguration* config, const char* language) {
    151     config->language[0] = language[0];
    152     config->language[1] = language[1];
    153 }
    154 
    155 void AConfiguration_setCountry(AConfiguration* config, const char* country) {
    156     config->country[0] = country[0];
    157     config->country[1] = country[1];
    158 }
    159 
    160 void AConfiguration_setOrientation(AConfiguration* config, int32_t orientation) {
    161     config->orientation = orientation;
    162 }
    163 
    164 void AConfiguration_setTouchscreen(AConfiguration* config, int32_t touchscreen) {
    165     config->touchscreen = touchscreen;
    166 }
    167 
    168 void AConfiguration_setDensity(AConfiguration* config, int32_t density) {
    169     config->density = density;
    170 }
    171 
    172 void AConfiguration_setKeyboard(AConfiguration* config, int32_t keyboard) {
    173     config->keyboard = keyboard;
    174 }
    175 
    176 void AConfiguration_setNavigation(AConfiguration* config, int32_t navigation) {
    177     config->navigation = navigation;
    178 }
    179 
    180 void AConfiguration_setKeysHidden(AConfiguration* config, int32_t keysHidden) {
    181     config->inputFlags = (config->inputFlags&~ResTable_config::MASK_KEYSHIDDEN)
    182             | (keysHidden&ResTable_config::MASK_KEYSHIDDEN);
    183 }
    184 
    185 void AConfiguration_setNavHidden(AConfiguration* config, int32_t navHidden) {
    186     config->inputFlags = (config->inputFlags&~ResTable_config::MASK_NAVHIDDEN)
    187             | ((navHidden<<ResTable_config::SHIFT_NAVHIDDEN)&ResTable_config::MASK_NAVHIDDEN);
    188 }
    189 
    190 void AConfiguration_setSdkVersion(AConfiguration* config, int32_t sdkVersion) {
    191     config->sdkVersion = sdkVersion;
    192 }
    193 
    194 void AConfiguration_setScreenSize(AConfiguration* config, int32_t screenSize) {
    195     config->screenLayout = (config->screenLayout&~ResTable_config::MASK_SCREENSIZE)
    196             | (screenSize&ResTable_config::MASK_SCREENSIZE);
    197 }
    198 
    199 void AConfiguration_setScreenLong(AConfiguration* config, int32_t screenLong) {
    200     config->screenLayout = (config->screenLayout&~ResTable_config::MASK_SCREENLONG)
    201             | ((screenLong<<ResTable_config::SHIFT_SCREENLONG)&ResTable_config::MASK_SCREENLONG);
    202 }
    203 
    204 void AConfiguration_setScreenRound(AConfiguration* config, int32_t screenRound) {
    205     config->screenLayout2 = (config->screenLayout2&~ResTable_config::MASK_SCREENROUND)
    206             | (screenRound&ResTable_config::MASK_SCREENROUND);
    207 }
    208 
    209 void AConfiguration_setUiModeType(AConfiguration* config, int32_t uiModeType) {
    210     config->uiMode = (config->uiMode&~ResTable_config::MASK_UI_MODE_TYPE)
    211             | (uiModeType&ResTable_config::MASK_UI_MODE_TYPE);
    212 }
    213 
    214 void AConfiguration_setUiModeNight(AConfiguration* config, int32_t uiModeNight) {
    215     config->uiMode = (config->uiMode&~ResTable_config::MASK_UI_MODE_NIGHT)
    216             | ((uiModeNight<<ResTable_config::SHIFT_UI_MODE_NIGHT)&ResTable_config::MASK_UI_MODE_NIGHT);
    217 
    218 }
    219 
    220 void AConfiguration_setScreenWidthDp(AConfiguration* config, int32_t value) {
    221     config->screenWidthDp = value;
    222 }
    223 
    224 void AConfiguration_setScreenHeightDp(AConfiguration* config, int32_t value) {
    225     config->screenHeightDp = value;
    226 }
    227 
    228 void AConfiguration_setSmallestScreenWidthDp(AConfiguration* config, int32_t value) {
    229     config->smallestScreenWidthDp = value;
    230 }
    231 
    232 void AConfiguration_setLayoutDirection(AConfiguration* config, int32_t value) {
    233     config->screenLayout = (config->screenLayout&~ResTable_config::MASK_LAYOUTDIR)
    234             | ((value<<ResTable_config::SHIFT_LAYOUTDIR)&ResTable_config::MASK_LAYOUTDIR);
    235 }
    236 
    237 // ----------------------------------------------------------------------
    238 
    239 int32_t AConfiguration_diff(AConfiguration* config1, AConfiguration* config2) {
    240     return (config1->diff(*config2));
    241 }
    242 
    243 int32_t AConfiguration_match(AConfiguration* base, AConfiguration* requested) {
    244     return base->match(*requested);
    245 }
    246 
    247 int32_t AConfiguration_isBetterThan(AConfiguration* base, AConfiguration* test,
    248         AConfiguration* requested) {
    249     return base->isBetterThan(*test, requested);
    250 }
    251