Android release v6.7_preview1
[xcsoar.git] / src / Units / UnitsGlue.cpp
blob355aa9b0ed96b7db9c61a811bf4fac524f3910cb
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Units/UnitsGlue.hpp"
25 #include "Units/UnitsStore.hpp"
26 #include "LogFile.hpp"
28 #include <tchar.h>
29 #include <string.h>
31 #ifndef HAVE_POSIX
32 #include <windows.h>
33 #endif
35 #ifdef _WIN32_WCE
36 #include "OS/DynamicLibrary.hpp"
37 #endif
39 #ifdef ANDROID
40 #include "Java/Global.hpp"
41 #include "Java/Class.hpp"
42 #include "Java/Object.hpp"
43 #endif
45 struct language_unit_map {
46 unsigned region_id;
47 const TCHAR* region_code;
48 unsigned store_index;
51 #ifdef ANDROID
52 /**
53 * Several fake WIN32 constants. These are not used on Android, but
54 * we need them or we have to have a separate version of
55 * #language_table on Android.
57 enum {
58 LANG_ENGLISH,
60 enum {
61 SUBLANG_ENGLISH_UK,
62 SUBLANG_ENGLISH_US,
63 SUBLANG_ENGLISH_AUS,
66 #define MAKELANGID(x,y) 0
68 #endif
70 #if !defined(HAVE_POSIX) || defined(ANDROID)
72 const struct language_unit_map language_table[] = {
73 { MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK), _T("en_UK"), 1 },
74 { MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), _T("en_US"), 2 },
75 { MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_AUS), _T("en_AU"), 3 },
76 { 0, NULL, 0 }
79 #endif
81 #ifndef HAVE_POSIX
82 static unsigned
83 FindLanguage(LANGID lang)
85 // Search for supported languages matching the language code
86 for (unsigned i = 0; language_table[i].region_code != NULL; ++i)
87 if (language_table[i].region_id == lang)
88 return language_table[i].store_index;
90 return 0;
92 #elif defined(ANDROID)
93 static unsigned
94 FindLanguage(const TCHAR* lang)
96 // Search for supported languages matching the language code
97 for (unsigned i = 0; language_table[i].region_code != NULL; ++i)
98 if (_tcscmp(language_table[i].region_code, lang) == 0)
99 return language_table[i].store_index;
101 return 0;
103 #endif
105 static unsigned
106 AutoDetect()
108 #ifndef HAVE_POSIX
110 #if defined(_WIN32_WCE)
111 /* the GetUserDefaultUILanguage() prototype is missing on
112 mingw32ce, we have to look it up dynamically */
113 DynamicLibrary coreloc_dll(_T("coredll"));
114 if (!coreloc_dll.IsDefined()) {
115 LogFormat("Units: coredll.dll not found");
116 return 0;
119 typedef LANGID WINAPI (*GetUserDefaultUILanguage_t)();
120 GetUserDefaultUILanguage_t GetUserDefaultUILanguage =
121 (GetUserDefaultUILanguage_t)
122 coreloc_dll.Lookup(_T("GetUserDefaultUILanguage"));
123 if (GetUserDefaultUILanguage == NULL) {
124 LogFormat("Units: GetUserDefaultUILanguage() not available");
125 return 0;
127 #endif
129 // Retrieve the default user language identifier from the OS
130 LANGID lang_id = GetUserDefaultUILanguage();
131 LogFormat("Units: GetUserDefaultUILanguage() = 0x%x", (int)lang_id);
132 if (lang_id == 0)
133 return 0;
135 return FindLanguage(lang_id);
137 #elif defined(ANDROID)
138 JNIEnv *env = Java::GetEnv();
140 Java::Class cls(env, "java/util/Locale");
142 // Call static function Locale.getDefault() that
143 // returns the user's default Locale object
145 jmethodID cid = env->GetStaticMethodID(cls, "getDefault",
146 "()Ljava/util/Locale;");
147 assert(cid != NULL);
149 jobject _obj = env->CallStaticObjectMethod(cls, cid);
150 if (_obj == NULL)
151 return 0;
153 Java::LocalObject obj(env, _obj);
155 // Call function Locale.getLanguage() that
156 // returns a two-letter language string
158 cid = env->GetMethodID(cls, "toString", "()Ljava/lang/String;");
159 assert(cid != NULL);
161 jstring language = (jstring)env->CallObjectMethod(obj, cid);
162 if (language == NULL)
163 return 0;
165 // Convert the jstring to a char string
166 const char *language2 = env->GetStringUTFChars(language, NULL);
167 if (language2 == NULL) {
168 env->DeleteLocalRef(language);
169 return 0;
172 unsigned id = FindLanguage(language2);
174 // Clean up the memory
175 env->ReleaseStringUTFChars(language, language2);
176 env->DeleteLocalRef(language);
178 // Return e.g. "de.mo"
179 return id;
181 #else
182 // Metric default on Linux
183 return 0;
184 #endif
187 const UnitSetting &
188 Units::LoadFromOSLanguage()
190 unsigned index = AutoDetect();
191 return Units::Store::Read(index);