|
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: Tools for PIM JNI wrappers. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "pimjnitools.h" |
|
21 #include <e32base.h> |
|
22 #include "cpimitem.h" |
|
23 #include "pimpanics.h" |
|
24 #include "s60commonutils.h" |
|
25 #include "jstringutils.h" |
|
26 #include "logger.h" |
|
27 |
|
28 |
|
29 jintArray ConvertToJavaIntArray(JNIEnv& aJniEnv, |
|
30 const CArrayFix<TInt>& aSourceArray) |
|
31 { |
|
32 JELOG2(EPim); |
|
33 const TInt n = aSourceArray.Count(); |
|
34 jintArray targetArray = aJniEnv.NewIntArray(n); |
|
35 |
|
36 if (!targetArray) |
|
37 { |
|
38 return NULL; // Error |
|
39 } |
|
40 |
|
41 if (n > 0) |
|
42 { |
|
43 aJniEnv.SetIntArrayRegion(targetArray, 0, n, |
|
44 const_cast<int*>(aSourceArray.Back(0))); |
|
45 } |
|
46 |
|
47 return targetArray; |
|
48 } |
|
49 |
|
50 jintArray GetJavaItemHandles(JNIEnv& aJniEnv, |
|
51 const RPointerArray<CPIMItem>& aItems) |
|
52 { |
|
53 JELOG2(EPim); |
|
54 const TInt numItems = aItems.Count(); |
|
55 jintArray itemHandles = aJniEnv.NewIntArray(numItems); |
|
56 |
|
57 if (!itemHandles) |
|
58 { |
|
59 return NULL; // Error |
|
60 } |
|
61 |
|
62 if (numItems > 0) |
|
63 { |
|
64 jint* elems = aJniEnv.GetIntArrayElements(itemHandles, NULL); |
|
65 |
|
66 if (elems == NULL) |
|
67 { |
|
68 return NULL; // Error |
|
69 } |
|
70 |
|
71 for (TInt i = 0; i < numItems; i++) |
|
72 { |
|
73 pimbaseitem* item = aItems[i]; |
|
74 elems[i] = reinterpret_cast<int>(item); |
|
75 } |
|
76 |
|
77 aJniEnv.ReleaseIntArrayElements(itemHandles, elems, 0); |
|
78 } |
|
79 |
|
80 return itemHandles; |
|
81 } |
|
82 |
|
83 jintArray NativeDatesToJavaInts(JNIEnv& aJniEnv, |
|
84 const CArrayFix<TPIMDate>& aDates) |
|
85 { |
|
86 JELOG2(EPim); |
|
87 const TInt n = aDates.Count(); |
|
88 |
|
89 // Create an int array of double size to fit the dates in half |
|
90 // size elements. |
|
91 jintArray retArr = aJniEnv.NewIntArray(n * 2); |
|
92 |
|
93 if (!retArr) |
|
94 { |
|
95 return NULL; // Error |
|
96 } |
|
97 |
|
98 if (n == 0) |
|
99 { |
|
100 return retArr; // Nothing to do, return empty (non-NULL) array |
|
101 } |
|
102 |
|
103 jint* elems = aJniEnv.GetIntArrayElements(retArr, NULL); |
|
104 |
|
105 if (elems == NULL) |
|
106 { |
|
107 return NULL; // Error |
|
108 } |
|
109 |
|
110 for (TInt i = 0; i < n; i++) |
|
111 { |
|
112 jlong javaDate = java::util::S60CommonUtils::TTimeToJavaTime(aDates[i]); |
|
113 TInt64 int64Val = *reinterpret_cast<TInt64*>(&javaDate); |
|
114 elems[2 * i] = I64HIGH(int64Val); // high part to even element |
|
115 elems[(2 * i) + 1] = I64LOW(int64Val); // low part to odd element |
|
116 } |
|
117 |
|
118 aJniEnv.ReleaseIntArrayElements(retArr, elems, 0); |
|
119 return retArr; |
|
120 } |
|
121 |
|
122 CDesCArray* CreateNativeStringArrayL(JNIEnv* aJniEnv, jobjectArray aJavaArray) |
|
123 { |
|
124 JELOG2(EPim); |
|
125 const TInt numElems = aJniEnv->GetArrayLength(aJavaArray); |
|
126 |
|
127 CDesCArray* nativeArray = new(ELeave) CDesCArrayFlat(numElems + 1); // ensure granularity of at least 1 (panics if 0) |
|
128 |
|
129 CleanupStack::PushL(nativeArray); |
|
130 |
|
131 for (TInt elemIndex = 0; elemIndex < numElems; elemIndex++) |
|
132 { |
|
133 // Creates a new local reference |
|
134 jobject javaElemObject = aJniEnv->GetObjectArrayElement(aJavaArray, |
|
135 elemIndex); |
|
136 |
|
137 jstring javaElem = static_cast<jstring>(javaElemObject); |
|
138 |
|
139 if (!javaElem) |
|
140 { |
|
141 nativeArray->AppendL(KPIMNullArrayElement); |
|
142 } |
|
143 else |
|
144 { |
|
145 // The value returned by GetObjectArrayElement is not really |
|
146 // a pointer to the object, but it is a "local reference". |
|
147 // In practice, it is a pointer to a pointer which points to the |
|
148 // object. If the object is null, we get a pointer which points |
|
149 // to a pointer which points to null. |
|
150 void** objPointer = reinterpret_cast<void**>(javaElemObject); |
|
151 void* obj = *objPointer; |
|
152 if (!obj) |
|
153 { |
|
154 nativeArray->AppendL(KPIMNullArrayElement); |
|
155 } |
|
156 else |
|
157 { |
|
158 JStringUtils nativeElem(*aJniEnv, javaElem); |
|
159 nativeArray->AppendL(nativeElem); |
|
160 } |
|
161 } |
|
162 // The VM runs out of temporary local references, unless the |
|
163 // elements are released here |
|
164 aJniEnv->DeleteLocalRef(javaElemObject); |
|
165 } |
|
166 |
|
167 CleanupStack::Pop(nativeArray); |
|
168 return nativeArray; |
|
169 } |
|
170 |
|
171 jobjectArray CreateJavaStringArray(JNIEnv* aJniEnv, |
|
172 const CDesCArray& aNativeArray, TBool aConvertPIMNullArrayElements) |
|
173 { |
|
174 JELOG2(EPim); |
|
175 const TInt numElems = aNativeArray.Count(); |
|
176 |
|
177 // Initializes the array with NULLs |
|
178 jobjectArray javaStringArray = aJniEnv->NewObjectArray(numElems, |
|
179 aJniEnv->FindClass("java/lang/String"), NULL); |
|
180 |
|
181 if (!javaStringArray) |
|
182 { |
|
183 return NULL; |
|
184 } |
|
185 |
|
186 for (TInt i = 0; i < numElems; i++) |
|
187 { |
|
188 TPtrC elem = aNativeArray[i]; |
|
189 |
|
190 if (aConvertPIMNullArrayElements && elem == KPIMNullArrayElement) |
|
191 { |
|
192 // The element is marked "null", skip and leave the element NULL |
|
193 continue; |
|
194 } |
|
195 |
|
196 // Creates a new local reference |
|
197 jstring javaElem = java::util::S60CommonUtils::NativeToJavaString( |
|
198 *aJniEnv, aNativeArray[i]); |
|
199 |
|
200 if (!javaElem) |
|
201 { |
|
202 // javaStringArray is left for Garbage Collector |
|
203 return NULL; |
|
204 } |
|
205 |
|
206 aJniEnv->SetObjectArrayElement(javaStringArray, i, javaElem); |
|
207 |
|
208 // Avoid running out of local references |
|
209 aJniEnv->DeleteLocalRef(javaElem); |
|
210 } |
|
211 |
|
212 return javaStringArray; |
|
213 } |
|
214 |
|
215 // End of File |