|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: JNI implementation for SensorManagerImpl.java |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <jni.h> |
|
19 #include <string.h> |
|
20 #include "com_nokia_mj_impl_sensor_SensorManagerImpl.h" |
|
21 #include "sensorconnectionlist.h" |
|
22 #include "sensorjniutils.h" |
|
23 #include "logger.h" |
|
24 |
|
25 /* |
|
26 * Function for releasing the sensor info array before return |
|
27 */ |
|
28 void freeSensorArray(jchar** aSensors, int aCount) |
|
29 { |
|
30 JELOG2(ESensor); |
|
31 for (int i = 0; i < aCount; i++) |
|
32 { |
|
33 delete [] aSensors[i]; |
|
34 } |
|
35 delete aSensors; |
|
36 } |
|
37 |
|
38 /* |
|
39 * Class: com_nokia_mj_impl_sensor_SensorManagerImpl |
|
40 * Method: _initSensors |
|
41 * Signature: ([Ljava/lang/String;)I |
|
42 */ |
|
43 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_sensor_SensorManagerImpl__1initSensors |
|
44 (JNIEnv* aJniEnv, jobject, jobject aSensorIdVector) |
|
45 { |
|
46 JELOG2(ESensor); |
|
47 SensorConnectionList* sensorConnectionList = new SensorConnectionList(); |
|
48 if (!sensorConnectionList) |
|
49 { |
|
50 return ERROR_NOMEMORY; |
|
51 } |
|
52 |
|
53 // ownership of char** is received |
|
54 int sensorCount = 0; |
|
55 jchar** sensors = sensorConnectionList->InitSensors(&sensorCount); |
|
56 if (!sensors) |
|
57 { |
|
58 freeSensorArray(sensors, sensorCount); |
|
59 return ERROR_GENERAL; |
|
60 } |
|
61 |
|
62 jmethodID addElement = |
|
63 aJniEnv->GetMethodID( |
|
64 aJniEnv->GetObjectClass(aSensorIdVector), |
|
65 "addElement", |
|
66 "(Ljava/lang/Object;)V"); |
|
67 |
|
68 if (HandleException(*aJniEnv) < ERROR_NONE) |
|
69 { |
|
70 freeSensorArray(sensors, sensorCount); |
|
71 return ERROR_GENERAL; |
|
72 } |
|
73 |
|
74 for (int i = 0; i < sensorCount; i++) |
|
75 { |
|
76 int strLength = 0; |
|
77 while (sensors[ i ][ strLength++ ] != '\0') {}; |
|
78 jstring javaString = aJniEnv->NewString(sensors[ i ], strLength); |
|
79 |
|
80 // If NewStringUTF fails to allocate memory it returns NULL and throws OutOfMemoryError |
|
81 if (!javaString) |
|
82 { |
|
83 freeSensorArray(sensors, sensorCount); |
|
84 // OutOfMemoryError thrown on java side |
|
85 return ERROR_NOMEMORY; |
|
86 } |
|
87 |
|
88 aJniEnv->CallVoidMethod(aSensorIdVector, addElement, javaString); |
|
89 } |
|
90 |
|
91 freeSensorArray(sensors, sensorCount); |
|
92 return reinterpret_cast<int>(sensorConnectionList); |
|
93 } |
|
94 |
|
95 /* |
|
96 * Class: com_nokia_mj_impl_sensor_SensorManagerImpl |
|
97 * Method: _dispose |
|
98 * Signature: (I)V |
|
99 */ |
|
100 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_sensor_SensorManagerImpl__1dispose |
|
101 (JNIEnv *, jobject, jint aSensorList) |
|
102 { |
|
103 JELOG2(ESensor); |
|
104 SensorConnectionList* senList = reinterpret_cast< SensorConnectionList* >(aSensorList); |
|
105 delete senList; |
|
106 } |