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