javaextensions/location/common/src/lapijnicommon.cpp
branchRCL_3
changeset 14 04becd199f91
child 17 0fd27995241b
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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:  Common JNI implementations
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INTERNAL INCLUDES
       
    20 #include "lapijnicommon.h"
       
    21 #include "clapilandmark.h"
       
    22 #include "cleanupresetanddestroy.h"
       
    23 #include "lapipanics.h"
       
    24 #include "jstringutils.h"
       
    25 #include "s60commonutils.h"
       
    26 #include "logger.h"
       
    27 
       
    28 // EXTERNAL INCLUDES
       
    29 #include <lbsposition.h>
       
    30 
       
    31 // UNNAMED LOCAL NAMESPACE
       
    32 namespace
       
    33 {
       
    34 // Size of the coordinates array (latitude and longitude)
       
    35 const TInt KLAPINumCoordinates = 2;
       
    36 // Size of the coordinate information array (altitude, horizontal
       
    37 // accuracy and vertical accuracy
       
    38 const TInt KLAPINumCoordinateInfos = 3;
       
    39 }
       
    40 
       
    41 /**
       
    42  * Sets an error code to an error array
       
    43  * @param aJniEnv Pointer to the JNI environment
       
    44  * @param aTo The array to which the error is to be added
       
    45  * @param aFrom The value which will be added to the array
       
    46  */
       
    47 void SetJavaErrorCode(JNIEnv& aJniEnv, jintArray aTo, TInt aFrom)
       
    48 {
       
    49     JELOG2(EJavaLocation);
       
    50     jint javaError[1] =
       
    51         { aFrom };
       
    52     aJniEnv.SetIntArrayRegion(aTo, 0, 1, javaError);
       
    53 }
       
    54 
       
    55 /**
       
    56  * Creates a position from the passed coordinate information
       
    57  * @param aJniEnv A reference to the JNI environment
       
    58  * @param aCoordinates Latitude and longitude values
       
    59  * @param aCoordinateInfo Altitude, horizontal accuracy and vertical accurazy
       
    60  */
       
    61 TLocality CreatePosition(JNIEnv& aJniEnv, jdoubleArray aCoordinates,
       
    62                          jfloatArray aCoordinateInfo)
       
    63 {
       
    64     JELOG2(EJavaLocation);
       
    65     __ASSERT_DEBUG(aCoordinates && aCoordinateInfo, LAPIError::Panic(
       
    66                        ELAPIPanicNullArgument));
       
    67 
       
    68     jdouble dArray[KLAPINumCoordinates];
       
    69     jfloat fArray[KLAPINumCoordinateInfos];
       
    70 
       
    71     // Get latitude and longitude
       
    72     aJniEnv.GetDoubleArrayRegion(aCoordinates, 0, KLAPINumCoordinates, dArray);
       
    73     // Get altitude, horizontal accuracy and vertical accuracy
       
    74     aJniEnv.GetFloatArrayRegion(aCoordinateInfo, 0, KLAPINumCoordinateInfos,
       
    75                                 fArray);
       
    76 
       
    77     TCoordinate coordinates(dArray[0], dArray[1], fArray[0]);
       
    78     TLocality position(coordinates, fArray[1], fArray[2]);
       
    79     return position;
       
    80 }
       
    81 
       
    82 /**
       
    83  * Creates a position information from the passed position
       
    84  * @param aJniEnv A reference to the JNI environment
       
    85  * @param aCoordinates Latitude and longitude values
       
    86  * @param aCoordinateInfo Altitude, horizontal accuracy and vertical accurazy
       
    87  */
       
    88 void CreateCoordinateInfos(JNIEnv& aJniEnv, const TLocality& aPosition,
       
    89                            jdoubleArray aCoordinates, jfloatArray aCoordinateInfo)
       
    90 {
       
    91     JELOG2(EJavaLocation);
       
    92     __ASSERT_DEBUG(
       
    93         (aJniEnv.GetArrayLength(aCoordinates) >= KLAPINumCoordinates),
       
    94         LAPIError::Panic(ELAPIPanicJniIncorrectArrayLength));
       
    95     __ASSERT_DEBUG((aJniEnv.GetArrayLength(aCoordinateInfo)
       
    96                     >= KLAPINumCoordinateInfos), LAPIError::Panic(
       
    97                        ELAPIPanicJniIncorrectArrayLength));
       
    98 
       
    99     jdouble coordinates[KLAPINumCoordinates] =
       
   100         { aPosition.Latitude(), aPosition.Longitude() };
       
   101 
       
   102     jfloat coordinateInfo[KLAPINumCoordinateInfos] =
       
   103         { aPosition.Altitude(), aPosition.HorizontalAccuracy(),
       
   104           aPosition.VerticalAccuracy()
       
   105         };
       
   106 
       
   107     // Set coordinate values to the java array
       
   108     aJniEnv.SetDoubleArrayRegion(aCoordinates, 0, KLAPINumCoordinates,
       
   109                                  coordinates);
       
   110     // Set coordinate information to the java array
       
   111     aJniEnv.SetFloatArrayRegion(aCoordinateInfo, 0, KLAPINumCoordinateInfos,
       
   112                                 coordinateInfo);
       
   113 }
       
   114 
       
   115 /**
       
   116  * Creates a new native string array from java string array
       
   117  * @param aJniEnv A reference to the JNI environment
       
   118  * @param aJavaArray The Java string array from which the new array is created
       
   119  */
       
   120 RPointerArray<HBufC> CreateNativeStringArrayL(JNIEnv& aJniEnv,
       
   121         jobjectArray aJavaArray)
       
   122 {
       
   123     JELOG2(EJavaLocation);
       
   124     TInt count = 0;
       
   125     if (aJavaArray)
       
   126     {
       
   127         count = aJniEnv.GetArrayLength(aJavaArray);
       
   128     }
       
   129     // Create pointer array holding HBufCs so that null java elements
       
   130     // can be represented as NULL pointers in the array
       
   131     RPointerArray<HBufC> nativeArray(10);
       
   132     CleanupResetAndDestroyPushL(nativeArray);
       
   133 
       
   134     for (TInt i = 0; i < count; i++)
       
   135     {
       
   136         // Creates a new local reference
       
   137         jobject joe = aJniEnv.GetObjectArrayElement(aJavaArray, i);
       
   138         jstring javaString = static_cast<jstring>(joe);
       
   139         if (!javaString)
       
   140         {
       
   141             nativeArray.AppendL(NULL);
       
   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             TAny** objPointer = reinterpret_cast<TAny**>(joe);
       
   151             TAny* obj = *objPointer;
       
   152             if (!obj)
       
   153             {
       
   154                 nativeArray.AppendL(NULL);
       
   155             }
       
   156             else
       
   157             {
       
   158                 JStringUtils nativeElem(aJniEnv, javaString);
       
   159                 HBufC* element = nativeElem.AllocLC();
       
   160                 nativeArray.AppendL(element);
       
   161                 CleanupStack::Pop(element);
       
   162             }
       
   163         }
       
   164         // The VM runs out of temporary local references, unless the
       
   165         // elements are not released here
       
   166         aJniEnv.DeleteLocalRef(joe);
       
   167     }
       
   168 
       
   169     CleanupStack::Pop(&nativeArray);
       
   170     return nativeArray;
       
   171 }
       
   172 
       
   173 /**
       
   174  * Creates a new java string array from a native string array
       
   175  * @param aJniEnv A reference to the JNI environment
       
   176  * @param aNativeArray The native string array from which the new array is created
       
   177  */
       
   178 jobjectArray CreateJavaStringArray(JNIEnv& aJniEnv,
       
   179                                    RPointerArray<HBufC>& aNativeArray)
       
   180 {
       
   181     JELOG2(EJavaLocation);
       
   182     return java::util::S60CommonUtils::NativeToJavaStringArray(aJniEnv,
       
   183             aNativeArray);
       
   184 }
       
   185 
       
   186 jobjectArray CopyToNewJavaStringArrayL(JNIEnv& aJni,
       
   187                                        const CDesCArray& aNativeArray)
       
   188 {
       
   189     JELOG2(EJavaLocation);
       
   190     jclass stringClass = aJni.FindClass("java/lang/String");
       
   191     User::LeaveIfNull(stringClass);
       
   192     //
       
   193     jobjectArray result = aJni.NewObjectArray(aNativeArray.Count(),
       
   194                           stringClass, NULL);
       
   195     if (result != NULL)
       
   196     {
       
   197         TPtrC item;
       
   198         for (int i = 0; i < aNativeArray.Count(); ++i)
       
   199         {
       
   200             item.Set(aNativeArray[i]);
       
   201             jstring javaString =
       
   202                 java::util::S60CommonUtils::NativeToJavaString(aJni, item);
       
   203             User::LeaveIfNull(javaString);
       
   204             //
       
   205             aJni.SetObjectArrayElement(result, i, javaString);
       
   206             aJni.DeleteLocalRef(javaString);
       
   207         }
       
   208     }
       
   209     return result;
       
   210 }
       
   211 
       
   212 /**
       
   213  * Creates an item handle array from the paased landmarks
       
   214  * @param aJniEnv A reference to the JNI environment
       
   215  * @param aLandmarks The landmarks from which the handles are created
       
   216  * @return Java integer array. Ownership is transferred to the caller
       
   217  */
       
   218 jintArray CreateHandleArray(JNIEnv& aJniEnv,
       
   219                             const CArrayPtr<CLAPILandmark>& aLandmarks)
       
   220 {
       
   221     JELOG2(EJavaLocation);
       
   222     TInt count = aLandmarks.Count();
       
   223     jintArray handles = aJniEnv.NewIntArray(count);
       
   224 
       
   225     if (handles && count > 0)
       
   226     {
       
   227         jint* elems = aJniEnv.GetIntArrayElements(handles, NULL);
       
   228         if (!elems)
       
   229         {
       
   230             return NULL; // Error
       
   231         }
       
   232         // Make handles from the native side peer objects
       
   233         for (TInt i = 0; i < count; i++)
       
   234         {
       
   235             elems[i] = reinterpret_cast<jint>(aLandmarks[i]);
       
   236         }
       
   237         // Elemenet must be released since it is local
       
   238         aJniEnv.ReleaseIntArrayElements(handles, elems, 0);
       
   239     }
       
   240 
       
   241     return handles;
       
   242 }
       
   243 
       
   244 // End of file