Skip to content

Commit b8a23f3

Browse files
author
José Pereda
authored
Add locale API to device service (#389)
1 parent 29fc154 commit b8a23f3

File tree

10 files changed

+80
-26
lines changed

10 files changed

+80
-26
lines changed

modules/device/src/main/java/com/gluonhq/attach/device/DeviceService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019 Gluon
2+
* Copyright (c) 2016, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -91,4 +91,11 @@ static Optional<DeviceService> create() {
9191
* @since 3.3.0
9292
*/
9393
boolean isWearable();
94+
95+
/**
96+
* Returns the string representation of the current locale of the device
97+
* @return the device locale
98+
* @since 4.0.20
99+
*/
100+
String getLocale();
94101
}

modules/device/src/main/java/com/gluonhq/attach/device/impl/AndroidDeviceService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Gluon
2+
* Copyright (c) 2020, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -23,7 +23,8 @@
2323
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2424
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2525
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26-
*/package com.gluonhq.attach.device.impl;
26+
*/
27+
package com.gluonhq.attach.device.impl;
2728

2829
import com.gluonhq.attach.device.DeviceService;
2930

@@ -36,6 +37,7 @@ public class AndroidDeviceService implements DeviceService {
3637
static {
3738
System.loadLibrary("device");
3839
}
40+
3941
private static final DeviceInfo deviceInfo = getDeviceInfo();
4042

4143
public AndroidDeviceService() {
@@ -66,5 +68,10 @@ public boolean isWearable() {
6668
return deviceInfo.isWearable();
6769
}
6870

71+
@Override
72+
public String getLocale() {
73+
return deviceInfo.getLocale();
74+
}
75+
6976
private static native DeviceInfo getDeviceInfo();
7077
}

modules/device/src/main/java/com/gluonhq/attach/device/impl/DeviceInfo.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Gluon
2+
* Copyright (c) 2020, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -26,20 +26,24 @@
2626
*/
2727
package com.gluonhq.attach.device.impl;
2828

29+
import java.util.Locale;
30+
2931
public class DeviceInfo {
3032

31-
private String model;
32-
private String uuid;
33-
private String platform;
34-
private String version;
35-
private boolean wearable;
33+
private final String model;
34+
private final String uuid;
35+
private final String platform;
36+
private final String version;
37+
private final boolean wearable;
38+
private final String locale;
3639

37-
public DeviceInfo(String model, String uuid, String platform, String version, boolean wearable) {
40+
public DeviceInfo(String model, String uuid, String platform, String version, boolean wearable, String locale) {
3841
this.model = model;
3942
this.uuid = uuid;
4043
this.platform = platform;
4144
this.version = version;
4245
this.wearable = wearable;
46+
this.locale = locale;
4347
}
4448

4549
public String getModel() {
@@ -61,4 +65,8 @@ public String getVersion() {
6165
public boolean isWearable() {
6266
return wearable;
6367
}
68+
69+
public String getLocale() {
70+
return locale;
71+
}
6472
}

modules/device/src/main/java/com/gluonhq/attach/device/impl/IOSDeviceService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019 Gluon
2+
* Copyright (c) 2016, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -35,11 +35,12 @@ public class IOSDeviceService implements DeviceService {
3535
System.loadLibrary("Device");
3636
initDevice();
3737
}
38-
38+
3939
private static String model;
4040
private static String uuid;
4141
private static String platform;
4242
private static String version;
43+
private static String locale;
4344

4445
@Override
4546
public String getModel() {
@@ -66,16 +67,22 @@ public boolean isWearable() {
6667
// TODO: Find out if iOS device is wearable
6768
return false;
6869
}
69-
70+
71+
@Override
72+
public String getLocale() {
73+
return locale;
74+
}
75+
7076
// native
7177
private native static void initDevice();
7278

7379
// callback
74-
private static void sendDeviceData(String model, String uuid, String platform, String version) {
80+
private static void sendDeviceData(String model, String uuid, String platform, String version, String locale) {
7581
IOSDeviceService.model = model;
7682
IOSDeviceService.uuid = uuid;
7783
IOSDeviceService.platform = platform;
7884
IOSDeviceService.version = version;
85+
IOSDeviceService.locale = locale;
7986
}
8087

8188
}

modules/device/src/main/native/android/c/device.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Gluon
2+
* Copyright (c) 2020, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -38,10 +38,11 @@ static jmethodID jDeviceServiceGetUuid;
3838
static jmethodID jDeviceServiceGetPlatform;
3939
static jmethodID jDeviceServiceGetVersion;
4040
static jmethodID jDeviceServiceIsWearable;
41+
static jmethodID jDeviceServiceGetLocale;
4142

4243
static void initializeGraalHandles(JNIEnv* env) {
4344
jGraalDeviceInfoClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/attach/device/impl/DeviceInfo"));
44-
jGraalDeviceInfoInitMethod = (*env)->GetMethodID(env, jGraalDeviceInfoClass, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V");
45+
jGraalDeviceInfoInitMethod = (*env)->GetMethodID(env, jGraalDeviceInfoClass, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V");
4546
}
4647

4748
static void initializeDeviceDalvikHandles() {
@@ -53,6 +54,7 @@ static void initializeDeviceDalvikHandles() {
5354
jDeviceServiceGetPlatform = (*dalvikEnv)->GetMethodID(dalvikEnv, jDeviceServiceClass, "getPlatform", "()Ljava/lang/String;");
5455
jDeviceServiceGetVersion = (*dalvikEnv)->GetMethodID(dalvikEnv, jDeviceServiceClass, "getVersion", "()Ljava/lang/String;");
5556
jDeviceServiceIsWearable = (*dalvikEnv)->GetMethodID(dalvikEnv, jDeviceServiceClass, "isWearable", "()Z");
57+
jDeviceServiceGetLocale = (*dalvikEnv)->GetMethodID(dalvikEnv, jDeviceServiceClass, "getLocale", "()Ljava/lang/String;");
5658

5759
jobject jActivity = substrateGetActivity();
5860
jobject jtmpobj = (*dalvikEnv)->NewObject(dalvikEnv, jDeviceServiceClass, jDeviceServiceInitMethod, jActivity);
@@ -101,20 +103,23 @@ JNIEXPORT jobject JNICALL Java_com_gluonhq_attach_device_impl_AndroidDeviceServi
101103
const char *responsePlatformChars = (*dalvikEnv)->GetStringUTFChars(dalvikEnv, platform, 0);
102104
jstring version = (*dalvikEnv)->CallObjectMethod(dalvikEnv, jDalvikDeviceService, jDeviceServiceGetVersion);
103105
const char *responseVersionChars = (*dalvikEnv)->GetStringUTFChars(dalvikEnv, version, 0);
106+
jstring locale = (*dalvikEnv)->CallObjectMethod(dalvikEnv, jDalvikDeviceService, jDeviceServiceGetLocale);
107+
const char *responseLocaleChars = (*dalvikEnv)->GetStringUTFChars(dalvikEnv, locale, 0);
104108
jboolean wearable = (*dalvikEnv)->CallBooleanMethod(dalvikEnv, jDalvikDeviceService, jDeviceServiceIsWearable);
105109
DETACH_DALVIK();
106110

107111
if (isDebugAttach()) {
108-
ATTACH_LOG_FINE("Retrieved DeviceInfo: model=%s, uuid=%s, platform=%s, version=%s, wearable=%d\n",
109-
responseModelChars, responseUuidChars, responsePlatformChars, responseVersionChars, wearable);
112+
ATTACH_LOG_FINE("Retrieved DeviceInfo: model=%s, uuid=%s, platform=%s, version=%s, wearable=%d, locale=%s\n",
113+
responseModelChars, responseUuidChars, responsePlatformChars, responseVersionChars, wearable, responseLocaleChars);
110114
}
111115

112116
jstring responseModelString = (*env)->NewStringUTF(env, responseModelChars);
113117
jstring responseUuidString = (*env)->NewStringUTF(env, responseUuidChars);
114118
jstring responsePlatformString = (*env)->NewStringUTF(env, responsePlatformChars);
115119
jstring responseVersionString = (*env)->NewStringUTF(env, responseVersionChars);
120+
jstring responseLocaleString = (*env)->NewStringUTF(env, responseLocaleChars);
116121
jobject jtmpobj = (*env)->NewObject(env, jGraalDeviceInfoClass, jGraalDeviceInfoInitMethod,
117122
responseModelString, responseUuidString, responsePlatformString, responseVersionString,
118-
wearable);
123+
wearable, responseLocaleString);
119124
return (*env)->NewGlobalRef(env, jtmpobj);
120125
}

modules/device/src/main/native/android/dalvik/DalvikDeviceService.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Gluon
2+
* Copyright (c) 2020, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -33,6 +33,7 @@
3333
import android.provider.Settings.Secure;
3434
import android.util.Log;
3535

36+
import java.util.Locale;
3637
import java.util.UUID;
3738

3839
public class DalvikDeviceService {
@@ -83,4 +84,17 @@ public String getVersion() {
8384
public boolean isWearable() {
8485
return activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
8586
}
87+
88+
public String getLocale() {
89+
Locale locale;
90+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
91+
locale = activity.getResources().getConfiguration().getLocales().get(0);
92+
} else {
93+
locale = activity.getResources().getConfiguration().locale;
94+
}
95+
if (debug) {
96+
Log.v(TAG, String.format("Current locale: %s", locale.toString()));
97+
}
98+
return locale.toString();
99+
}
86100
}

modules/device/src/main/native/ios/Device.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019 Gluon
2+
* Copyright (c) 2016, 2024, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -61,7 +61,7 @@
6161
deviceInited = 1;
6262

6363
mat_jDeviceServiceClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/attach/device/impl/IOSDeviceService"));
64-
mat_jDeviceService_sendDevice = (*env)->GetStaticMethodID(env, mat_jDeviceServiceClass, "sendDeviceData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
64+
mat_jDeviceService_sendDevice = (*env)->GetStaticMethodID(env, mat_jDeviceServiceClass, "sendDeviceData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
6565

6666
UIDevice* currentDevice = [UIDevice currentDevice];
6767

@@ -81,9 +81,15 @@
8181
const char *versionChars = [deviceVersion UTF8String];
8282
jstring argVersion = (*env)->NewStringUTF(env, versionChars);
8383

84-
(*env)->CallStaticVoidMethod(env, mat_jDeviceServiceClass, mat_jDeviceService_sendDevice, argModel, argId, argPlatform, argVersion);
84+
NSLocale *locale = [NSLocale currentLocale];
85+
NSString *localeFormat = [NSString stringWithFormat:@"%@_%@", [locale objectForKey:NSLocaleLanguageCode], [locale objectForKey:NSLocaleCountryCode]];
86+
const char *localeChars = [localeFormat UTF8String];
87+
jstring argLocale = (*env)->NewStringUTF(env, localeChars);
88+
89+
(*env)->CallStaticVoidMethod(env, mat_jDeviceServiceClass, mat_jDeviceService_sendDevice, argModel, argId, argPlatform, argVersion, argLocale);
8590
(*env)->DeleteLocalRef(env, argModel);
8691
(*env)->DeleteLocalRef(env, argId);
8792
(*env)->DeleteLocalRef(env, argPlatform);
8893
(*env)->DeleteLocalRef(env, argVersion);
94+
(*env)->DeleteLocalRef(env, argLocale);
8995
}

modules/device/src/main/resources/META-INF/substrate/config/jniconfig-aarch64-android.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name" : "com.gluonhq.attach.device.impl.DeviceInfo",
44
"methods":[
5-
{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean"] }
5+
{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean","java.lang.String"] }
66
]
77
}
88
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"name" : "com.gluonhq.attach.device.impl.IOSDeviceService",
4-
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
4+
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
55
}
66
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"name" : "com.gluonhq.attach.device.impl.IOSDeviceService",
4-
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
4+
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
55
}
66
]

0 commit comments

Comments
 (0)