javaextensions/location/landmarks/src/landmark.cpp
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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 of Landmark class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INTERNAL INCLUDES
       
    20 #include "javax_microedition_location_Landmark.h"
       
    21 #include "jstringutils.h"
       
    22 #include "s60commonutils.h"
       
    23 #include "lapijnicommon.h"
       
    24 #include "logger.h"
       
    25 #include "clapilandmark.h"
       
    26 #include "clapiaddressinfo.h"
       
    27 #include "cleanupresetanddestroy.h"
       
    28 #include "locationfunctionserver.h"
       
    29 
       
    30 using namespace java::location;
       
    31 
       
    32 LOCAL_C void CreateLandmarkL(TInt* aHandle)
       
    33 {
       
    34     CLAPILandmark::TCtorParams params;
       
    35     params.iLandmark = NULL;
       
    36     params.iLandmarkStore = NULL;
       
    37 
       
    38     CLAPILandmark* landmark = CLAPILandmark::NewLC(params);
       
    39     // Add the created object to event source
       
    40     TInt handle = reinterpret_cast<TInt>(landmark);
       
    41     CleanupStack::Pop(landmark);
       
    42     *aHandle = handle;
       
    43 }
       
    44 
       
    45 /*
       
    46  * Class:     javax_microedition_location_Landmark
       
    47  * Method:    _createNativePeer
       
    48  * Signature: (I)I
       
    49  */
       
    50 JNIEXPORT jint
       
    51 JNICALL Java_javax_microedition_location_Landmark__1createNativePeer(
       
    52     JNIEnv* /*aJniEnv*/,
       
    53     jobject /*aPeer*/,
       
    54     jint aEventSourceHandle)
       
    55 {
       
    56     JELOG2(EJavaLocation);
       
    57 
       
    58     LocationFunctionServer* eventSource =
       
    59         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
    60 
       
    61     TInt handle = KErrGeneral;
       
    62 
       
    63     TInt error = eventSource->ExecuteTrap(
       
    64                      CreateLandmarkL,
       
    65                      &handle);
       
    66 
       
    67     // Return the handle if it was successfully obtained
       
    68 
       
    69     return error == KErrNone ? handle : error;
       
    70 
       
    71 }
       
    72 
       
    73 LOCAL_C void LandmarkNameL(JNIEnv* aJniEnv, CLAPILandmark* aLandmark,
       
    74                            jstring* aName)
       
    75 {
       
    76 
       
    77     const TDesC& name = aLandmark->NameL();
       
    78     *aName = java::util::S60CommonUtils::NativeToJavaString(*aJniEnv, name);
       
    79 }
       
    80 
       
    81 /*
       
    82  * Class:     javax_microedition_location_Landmark
       
    83  * Method:    _getName
       
    84  * Signature: (II)Ljava/lang/String;
       
    85  */
       
    86 JNIEXPORT jstring
       
    87 JNICALL Java_javax_microedition_location_Landmark__1getName(
       
    88     JNIEnv* , jobject /*aPeer*/, jint aEventSourceHandle, jint aLandmarkHandle)
       
    89 {
       
    90     JELOG2(EJavaLocation);
       
    91 
       
    92     LocationFunctionServer* eventSource =
       
    93         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
    94     CLAPILandmark* landmark =
       
    95         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
    96 
       
    97     jstring name(NULL);
       
    98 
       
    99     TInt error = eventSource->ExecuteTrap(
       
   100                      LandmarkNameL,
       
   101                      eventSource->getValidJniEnv(),
       
   102                      landmark,
       
   103                      &name);
       
   104 
       
   105     // Null indicates an error. Landmark should always have a name
       
   106     return name;
       
   107 }
       
   108 
       
   109 LOCAL_C void LandmarkDescriptionL(CLAPILandmark* aLandmark,
       
   110                                   const TDesC** aDescription)
       
   111 {
       
   112     *aDescription = aLandmark->DescriptionL();
       
   113 }
       
   114 
       
   115 /*
       
   116  * Class:     javax_microedition_location_Landmark
       
   117  * Method:    _getDescription
       
   118  * Signature: (II)Ljava/lang/String;
       
   119  */
       
   120 JNIEXPORT jstring
       
   121 JNICALL Java_javax_microedition_location_Landmark__1getDescription(
       
   122     JNIEnv* aJniEnv,
       
   123     jobject /*aPeer*/,
       
   124     jint aEventSourceHandle,
       
   125     jint aLandmarkHandle)
       
   126 {
       
   127     JELOG2(EJavaLocation);
       
   128 
       
   129     LocationFunctionServer* eventSource =
       
   130         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   131     CLAPILandmark* landmark =
       
   132         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   133 
       
   134     const TDesC* description(NULL);
       
   135 
       
   136     TInt error = eventSource->ExecuteTrap(
       
   137                      LandmarkDescriptionL,
       
   138                      landmark,
       
   139                      &description);
       
   140 
       
   141     jstring javaDescription(NULL);
       
   142     // Zero length descriptions are not allowed
       
   143     if (description && description->Length()> 0)
       
   144     {
       
   145         javaDescription = java::util::S60CommonUtils::NativeToJavaString(*aJniEnv, *description);
       
   146     }
       
   147 
       
   148     // Null indicates an error or that the landmark didn't have a description
       
   149     return javaDescription;
       
   150 }
       
   151 
       
   152 LOCAL_C void LandmarkCoordinatesL(CLAPILandmark* aLandmark,
       
   153                                   const TLocality** aPosition)
       
   154 {
       
   155     *aPosition = aLandmark->CoordinatesL();
       
   156 }
       
   157 
       
   158 /*
       
   159  * Class:     javax_microedition_location_Landmark
       
   160  * Method:    _getCoordinates
       
   161  * Signature: (II[D[F)I
       
   162  */
       
   163 JNIEXPORT jint
       
   164 JNICALL Java_javax_microedition_location_Landmark__1getCoordinates(
       
   165     JNIEnv* aJniEnv,
       
   166     jobject /*aPeer*/,
       
   167     jint aEventSourceHandle,
       
   168     jint aLandmarkHandle,
       
   169     jdoubleArray aCoordinates,
       
   170     jfloatArray aCoordinateInfo)
       
   171 {
       
   172     JELOG2(EJavaLocation);
       
   173 
       
   174     LocationFunctionServer* eventSource =
       
   175         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   176     CLAPILandmark* landmark =
       
   177         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   178 
       
   179     const TLocality* position(NULL);
       
   180 
       
   181     TInt error = eventSource->ExecuteTrap(
       
   182                      LandmarkCoordinatesL,
       
   183                      landmark,
       
   184                      &position);
       
   185 
       
   186     LOG1(EJavaLocation,EInfo, "Java_javax_microedition_location_Landmark__1getCoordinates - error %d", error);
       
   187 
       
   188     if (error == KErrNone)
       
   189     {
       
   190         if (position)
       
   191         {
       
   192             // The position was successfully received from the landmark. Create
       
   193             // position data arrays from the native landmark position
       
   194             CreateCoordinateInfos(*aJniEnv, *position, aCoordinates, aCoordinateInfo);
       
   195         }
       
   196         else
       
   197         {
       
   198             // Position has not been set to the landmark.
       
   199             error = KErrNotFound;
       
   200         }
       
   201     }
       
   202 
       
   203     return error;
       
   204 }
       
   205 
       
   206 LOCAL_C void AddressInfosL(JNIEnv* aJniEnv, CLAPILandmark* aLandmark,
       
   207                            jobjectArray* aJavaStringArray)
       
   208 {
       
   209 
       
   210     // Read all address information from the landmark
       
   211     const CLAPIAddressInfo* info = aLandmark->AddressInfoL();
       
   212     if (info && info->ValueCount() > 0)
       
   213     {
       
   214         RPointerArray<HBufC> values(ELAPINumAddressInfos);
       
   215         CleanupResetAndDestroyPushL(values);
       
   216 
       
   217         for (TInt i = 1; i <= ELAPINumAddressInfos; i++)
       
   218         {
       
   219             const TDesC* value = info->Value(i);
       
   220             // Null elements indicate an empty address info field
       
   221             HBufC* newValue = value ? value->AllocLC() : NULL;
       
   222             values.AppendL(newValue);
       
   223             // Pop the value if it was added to the cleanup stack
       
   224             if (newValue)
       
   225             {
       
   226                 CleanupStack::Pop(newValue);
       
   227             }
       
   228         }
       
   229 
       
   230         // Create a java string array from the address info array
       
   231         *aJavaStringArray = CreateJavaStringArray(*aJniEnv, values);
       
   232         CleanupStack::PopAndDestroy(&values);
       
   233     }
       
   234 }
       
   235 
       
   236 /*
       
   237  * Class:     javax_microedition_location_Landmark
       
   238  * Method:    _getAddressInfo
       
   239  * Signature: (II)[Ljava/lang/String;
       
   240  */
       
   241 JNIEXPORT jobjectArray
       
   242 JNICALL Java_javax_microedition_location_Landmark__1getAddressInfo(
       
   243     JNIEnv* ,
       
   244     jobject /*aPeer*/,
       
   245     jint aEventSourceHandle,
       
   246     jint aLandmarkHandle)
       
   247 {
       
   248     JELOG2(EJavaLocation);
       
   249 
       
   250     LocationFunctionServer* eventSource =
       
   251         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   252     CLAPILandmark* landmark =
       
   253         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   254 
       
   255     // Ownership of the name array is transferred to Java side
       
   256     jobjectArray javaStringArray(NULL);
       
   257 
       
   258     TInt error = eventSource->ExecuteTrap(
       
   259                      AddressInfosL,
       
   260                      eventSource->getValidJniEnv(),
       
   261                      landmark,
       
   262                      &javaStringArray);
       
   263 
       
   264     return javaStringArray; // NULL indicates an error
       
   265 }
       
   266 
       
   267 LOCAL_C void SetNameL(CLAPILandmark* aLandmark, const TDesC* aName)
       
   268 {
       
   269     aLandmark->SetNameL(*aName);
       
   270 }
       
   271 
       
   272 /*
       
   273  * Class:     javax_microedition_location_Landmark
       
   274  * Method:    _setName
       
   275  * Signature: (IILjava/lang/String;)I
       
   276  */
       
   277 JNIEXPORT jint
       
   278 JNICALL Java_javax_microedition_location_Landmark__1setName(
       
   279     JNIEnv* aJniEnv,
       
   280     jobject /*aPeer*/,
       
   281     jint aEventSourceHandle,
       
   282     jint aLandmarkHandle,
       
   283     jstring aName)
       
   284 {
       
   285     JELOG2(EJavaLocation);
       
   286     LocationFunctionServer* eventSource =
       
   287         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   288     CLAPILandmark* landmark =
       
   289         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   290 
       
   291     // Convert the description to a native descriptor. NULL is ok
       
   292     const JStringUtils name(*aJniEnv, aName);
       
   293     const TDesC* nativeName = (aName ? &name : NULL);
       
   294 
       
   295     TInt error = eventSource->ExecuteTrap(
       
   296                      SetNameL,
       
   297                      landmark,
       
   298                      nativeName);
       
   299     return error;
       
   300 }
       
   301 
       
   302 LOCAL_C void SetDescriptionL(CLAPILandmark* aLandmark,
       
   303                              const TDesC* aDescription)
       
   304 {
       
   305     aLandmark->SetDescriptionL(aDescription);
       
   306 }
       
   307 
       
   308 /*
       
   309  * Class:     javax_microedition_location_Landmark
       
   310  * Method:    _setDescription
       
   311  * Signature: (IILjava/lang/String;)I
       
   312  */
       
   313 JNIEXPORT jint
       
   314 JNICALL Java_javax_microedition_location_Landmark__1setDescription(
       
   315     JNIEnv* aJniEnv,
       
   316     jobject /*aPeer*/,
       
   317     jint aEventSourceHandle,
       
   318     jint aLandmarkHandle,
       
   319     jstring aDescription)
       
   320 {
       
   321     JELOG2(EJavaLocation);
       
   322 
       
   323     LocationFunctionServer* eventSource =
       
   324         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   325     CLAPILandmark* landmark =
       
   326         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   327 
       
   328     // Convert the description to a native descriptor. NULL is ok
       
   329     const JStringUtils description(*aJniEnv, aDescription);
       
   330     const TDesC* nativeDesc = (aDescription ? &description : NULL);
       
   331 
       
   332     TInt error = eventSource->ExecuteTrap(
       
   333                      SetDescriptionL,
       
   334                      landmark,
       
   335                      nativeDesc);
       
   336 
       
   337     LOG1(EJavaLocation,EInfo, "Java_javax_microedition_location_Landmark__1setDescription - error %d", error);
       
   338     return error;
       
   339 }
       
   340 
       
   341 LOCAL_C void SetCoordinatesL(CLAPILandmark* aLandmark, TLocality* aCoordinates)
       
   342 {
       
   343     aLandmark->SetCoordinatesL(aCoordinates);
       
   344 }
       
   345 
       
   346 /*
       
   347  * Class:     javax_microedition_location_Landmark
       
   348  * Method:    _setCoordinates
       
   349  * Signature: (II[D[F)I
       
   350  */
       
   351 JNIEXPORT jint
       
   352 JNICALL Java_javax_microedition_location_Landmark__1setCoordinates(
       
   353     JNIEnv* aJniEnv,
       
   354     jobject /*aPeer*/,
       
   355     jint aEventSourceHandle,
       
   356     jint aLandmarkHandle,
       
   357     jdoubleArray aCoordinates,
       
   358     jfloatArray aCoordinateInfo)
       
   359 {
       
   360     JELOG2(EJavaLocation);
       
   361 
       
   362     LocationFunctionServer* eventSource =
       
   363         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   364     CLAPILandmark* landmark =
       
   365         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   366 
       
   367     TLocality position;
       
   368     if (aCoordinates && aCoordinateInfo)
       
   369     {
       
   370         position = CreatePosition(*aJniEnv,
       
   371                                   aCoordinates,
       
   372                                   aCoordinateInfo);
       
   373     }
       
   374 
       
   375     TInt error = eventSource->ExecuteTrap(
       
   376                      SetCoordinatesL,
       
   377                      landmark,
       
   378                      aCoordinates && aCoordinateInfo ? &position : NULL);
       
   379     return error;
       
   380 }
       
   381 
       
   382 LOCAL_C void SetAddressInfoL(JNIEnv* aJniEnv, CLAPILandmark* aLandmark,
       
   383                              jobjectArray* aAddressInfo)
       
   384 {
       
   385     RPointerArray<HBufC> addressInfos = CreateNativeStringArrayL(*aJniEnv,
       
   386                                         *aAddressInfo);
       
   387     CleanupResetAndDestroyPushL(addressInfos);
       
   388     TInt count = addressInfos.Count();
       
   389     if (count > 0)
       
   390     {
       
   391         // Currently, the Java side address info object is handled so that
       
   392         // if the value is completely changed, all fields will be reset
       
   393         // So if the address info is set from the Java-side, it is expected
       
   394         // that it will completely replace the existing one. Works correctly if
       
   395         // the existing address info is modified or replaced with a new one
       
   396         CLAPIAddressInfo* info = CLAPIAddressInfo::NewLC();
       
   397         // Copy the address information from the array
       
   398         for (TInt i = 0; i < count; i++)
       
   399         {
       
   400             // The ownership of the value is NOT transferred to "info"
       
   401             info->SetValueL(i + 1, addressInfos[i]);
       
   402         }
       
   403         aLandmark->SetAddressInfoL(info);
       
   404         // The landmark takes the ownership of the address information object
       
   405         CleanupStack::Pop(info);
       
   406     }
       
   407     else
       
   408     {
       
   409         // Clear the address information from the landmark
       
   410         aLandmark->SetAddressInfoL(NULL);
       
   411     }
       
   412     // The array is not needed anymore
       
   413     CleanupStack::PopAndDestroy(&addressInfos);
       
   414 }
       
   415 
       
   416 /*
       
   417  * Class:     javax_microedition_location_Landmark
       
   418  * Method:    _setAddressInfo
       
   419  * Signature: (II[Ljava/lang/String;)I
       
   420  */
       
   421 JNIEXPORT jint
       
   422 JNICALL Java_javax_microedition_location_Landmark__1setAddressInfo(
       
   423     JNIEnv* aJniEnv,
       
   424     jobject /*aPeer*/,
       
   425     jint aEventSourceHandle,
       
   426     jint aLandmarkHandle,
       
   427     jobjectArray aAddressInfo)
       
   428 {
       
   429     JELOG2(EJavaLocation);
       
   430 
       
   431     LocationFunctionServer* eventSource =
       
   432         reinterpret_cast< LocationFunctionServer*>(aEventSourceHandle);
       
   433     CLAPILandmark* landmark =
       
   434         reinterpret_cast< CLAPILandmark*>(aLandmarkHandle);
       
   435 
       
   436     // Array must be created within a leave-safe content so it must be
       
   437     // passed to the Event Server's thread. A global reference is needed
       
   438     jobjectArray arrayRef(NULL);
       
   439     if (aAddressInfo)
       
   440     {
       
   441         // Create a new global reference.
       
   442         arrayRef = static_cast< jobjectArray>(
       
   443                        aJniEnv->NewGlobalRef(aAddressInfo));
       
   444     }
       
   445 
       
   446     TInt error = eventSource->ExecuteTrap(
       
   447                      SetAddressInfoL,
       
   448                      eventSource->getValidJniEnv(),
       
   449                      landmark,
       
   450                      &arrayRef);
       
   451 
       
   452     // Remove the created global reference
       
   453     if (arrayRef)
       
   454     {
       
   455         aJniEnv->DeleteGlobalRef(arrayRef);
       
   456     }
       
   457     return error;
       
   458 }
       
   459 
       
   460 /*
       
   461  * Class:     javax_microedition_location_Landmark
       
   462  * Method:    _dispose
       
   463  * Signature: (II)V
       
   464  */
       
   465 JNIEXPORT void
       
   466 JNICALL Java_javax_microedition_location_Landmark__1dispose(
       
   467     JNIEnv* /*aJniEnv*/,
       
   468     jobject /*aPeer*/,
       
   469     jint /*aEventSourceHandle*/,
       
   470     jint aLandmarkHandle)
       
   471 {
       
   472     JELOG2(EJavaLocation);
       
   473 
       
   474     CBase* object = reinterpret_cast< CBase*>(aLandmarkHandle);
       
   475 
       
   476     delete object;
       
   477 }
       
   478 
       
   479 // End of file