Skip to content

Commit

Permalink
Add locale API to device service (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
José Pereda authored Feb 14, 2024
1 parent 29fc154 commit b8a23f3
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Gluon
* Copyright (c) 2016, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -91,4 +91,11 @@ static Optional<DeviceService> create() {
* @since 3.3.0
*/
boolean isWearable();

/**
* Returns the string representation of the current locale of the device
* @return the device locale
* @since 4.0.20
*/
String getLocale();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Gluon
* Copyright (c) 2020, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -23,7 +23,8 @@
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/package com.gluonhq.attach.device.impl;
*/
package com.gluonhq.attach.device.impl;

import com.gluonhq.attach.device.DeviceService;

Expand All @@ -36,6 +37,7 @@ public class AndroidDeviceService implements DeviceService {
static {
System.loadLibrary("device");
}

private static final DeviceInfo deviceInfo = getDeviceInfo();

public AndroidDeviceService() {
Expand Down Expand Up @@ -66,5 +68,10 @@ public boolean isWearable() {
return deviceInfo.isWearable();
}

@Override
public String getLocale() {
return deviceInfo.getLocale();
}

private static native DeviceInfo getDeviceInfo();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Gluon
* Copyright (c) 2020, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -26,20 +26,24 @@
*/
package com.gluonhq.attach.device.impl;

import java.util.Locale;

public class DeviceInfo {

private String model;
private String uuid;
private String platform;
private String version;
private boolean wearable;
private final String model;
private final String uuid;
private final String platform;
private final String version;
private final boolean wearable;
private final String locale;

public DeviceInfo(String model, String uuid, String platform, String version, boolean wearable) {
public DeviceInfo(String model, String uuid, String platform, String version, boolean wearable, String locale) {
this.model = model;
this.uuid = uuid;
this.platform = platform;
this.version = version;
this.wearable = wearable;
this.locale = locale;
}

public String getModel() {
Expand All @@ -61,4 +65,8 @@ public String getVersion() {
public boolean isWearable() {
return wearable;
}

public String getLocale() {
return locale;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Gluon
* Copyright (c) 2016, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -35,11 +35,12 @@ public class IOSDeviceService implements DeviceService {
System.loadLibrary("Device");
initDevice();
}

private static String model;
private static String uuid;
private static String platform;
private static String version;
private static String locale;

@Override
public String getModel() {
Expand All @@ -66,16 +67,22 @@ public boolean isWearable() {
// TODO: Find out if iOS device is wearable
return false;
}


@Override
public String getLocale() {
return locale;
}

// native
private native static void initDevice();

// callback
private static void sendDeviceData(String model, String uuid, String platform, String version) {
private static void sendDeviceData(String model, String uuid, String platform, String version, String locale) {
IOSDeviceService.model = model;
IOSDeviceService.uuid = uuid;
IOSDeviceService.platform = platform;
IOSDeviceService.version = version;
IOSDeviceService.locale = locale;
}

}
15 changes: 10 additions & 5 deletions modules/device/src/main/native/android/c/device.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Gluon
* Copyright (c) 2020, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -38,10 +38,11 @@ static jmethodID jDeviceServiceGetUuid;
static jmethodID jDeviceServiceGetPlatform;
static jmethodID jDeviceServiceGetVersion;
static jmethodID jDeviceServiceIsWearable;
static jmethodID jDeviceServiceGetLocale;

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

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

jobject jActivity = substrateGetActivity();
jobject jtmpobj = (*dalvikEnv)->NewObject(dalvikEnv, jDeviceServiceClass, jDeviceServiceInitMethod, jActivity);
Expand Down Expand Up @@ -101,20 +103,23 @@ JNIEXPORT jobject JNICALL Java_com_gluonhq_attach_device_impl_AndroidDeviceServi
const char *responsePlatformChars = (*dalvikEnv)->GetStringUTFChars(dalvikEnv, platform, 0);
jstring version = (*dalvikEnv)->CallObjectMethod(dalvikEnv, jDalvikDeviceService, jDeviceServiceGetVersion);
const char *responseVersionChars = (*dalvikEnv)->GetStringUTFChars(dalvikEnv, version, 0);
jstring locale = (*dalvikEnv)->CallObjectMethod(dalvikEnv, jDalvikDeviceService, jDeviceServiceGetLocale);
const char *responseLocaleChars = (*dalvikEnv)->GetStringUTFChars(dalvikEnv, locale, 0);
jboolean wearable = (*dalvikEnv)->CallBooleanMethod(dalvikEnv, jDalvikDeviceService, jDeviceServiceIsWearable);
DETACH_DALVIK();

if (isDebugAttach()) {
ATTACH_LOG_FINE("Retrieved DeviceInfo: model=%s, uuid=%s, platform=%s, version=%s, wearable=%d\n",
responseModelChars, responseUuidChars, responsePlatformChars, responseVersionChars, wearable);
ATTACH_LOG_FINE("Retrieved DeviceInfo: model=%s, uuid=%s, platform=%s, version=%s, wearable=%d, locale=%s\n",
responseModelChars, responseUuidChars, responsePlatformChars, responseVersionChars, wearable, responseLocaleChars);
}

jstring responseModelString = (*env)->NewStringUTF(env, responseModelChars);
jstring responseUuidString = (*env)->NewStringUTF(env, responseUuidChars);
jstring responsePlatformString = (*env)->NewStringUTF(env, responsePlatformChars);
jstring responseVersionString = (*env)->NewStringUTF(env, responseVersionChars);
jstring responseLocaleString = (*env)->NewStringUTF(env, responseLocaleChars);
jobject jtmpobj = (*env)->NewObject(env, jGraalDeviceInfoClass, jGraalDeviceInfoInitMethod,
responseModelString, responseUuidString, responsePlatformString, responseVersionString,
wearable);
wearable, responseLocaleString);
return (*env)->NewGlobalRef(env, jtmpobj);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Gluon
* Copyright (c) 2020, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -33,6 +33,7 @@
import android.provider.Settings.Secure;
import android.util.Log;

import java.util.Locale;
import java.util.UUID;

public class DalvikDeviceService {
Expand Down Expand Up @@ -83,4 +84,17 @@ public String getVersion() {
public boolean isWearable() {
return activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
}

public String getLocale() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = activity.getResources().getConfiguration().getLocales().get(0);
} else {
locale = activity.getResources().getConfiguration().locale;
}
if (debug) {
Log.v(TAG, String.format("Current locale: %s", locale.toString()));
}
return locale.toString();
}
}
12 changes: 9 additions & 3 deletions modules/device/src/main/native/ios/Device.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Gluon
* Copyright (c) 2016, 2024, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -61,7 +61,7 @@
deviceInited = 1;

mat_jDeviceServiceClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/attach/device/impl/IOSDeviceService"));
mat_jDeviceService_sendDevice = (*env)->GetStaticMethodID(env, mat_jDeviceServiceClass, "sendDeviceData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
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");

UIDevice* currentDevice = [UIDevice currentDevice];

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

(*env)->CallStaticVoidMethod(env, mat_jDeviceServiceClass, mat_jDeviceService_sendDevice, argModel, argId, argPlatform, argVersion);
NSLocale *locale = [NSLocale currentLocale];
NSString *localeFormat = [NSString stringWithFormat:@"%@_%@", [locale objectForKey:NSLocaleLanguageCode], [locale objectForKey:NSLocaleCountryCode]];
const char *localeChars = [localeFormat UTF8String];
jstring argLocale = (*env)->NewStringUTF(env, localeChars);

(*env)->CallStaticVoidMethod(env, mat_jDeviceServiceClass, mat_jDeviceService_sendDevice, argModel, argId, argPlatform, argVersion, argLocale);
(*env)->DeleteLocalRef(env, argModel);
(*env)->DeleteLocalRef(env, argId);
(*env)->DeleteLocalRef(env, argPlatform);
(*env)->DeleteLocalRef(env, argVersion);
(*env)->DeleteLocalRef(env, argLocale);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"name" : "com.gluonhq.attach.device.impl.DeviceInfo",
"methods":[
{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean"] }
{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean","java.lang.String"] }
]
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name" : "com.gluonhq.attach.device.impl.IOSDeviceService",
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
}
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name" : "com.gluonhq.attach.device.impl.IOSDeviceService",
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
"methods":[{"name":"sendDeviceData","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","java.lang.String"] }]
}
]

0 comments on commit b8a23f3

Please sign in to comment.