javauis/softnotification_akn/src/SoftNotification.cpp
branchRCL_3
changeset 14 04becd199f91
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:  JNI context for SoftNotificationImpl Java class.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <jutils.h>
       
    21 #include <JniEnvWrapper.h>
       
    22 #include <jdebug.h>
       
    23 #include "com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl.h"
       
    24 #include "CSoftNotification.h"
       
    25 #include "CSoftNotificationEventSource.h"
       
    26 
       
    27 /*
       
    28  * Class: com_nokia_mid_ui_SoftNotification
       
    29  * Method: _createEventSource
       
    30  */
       
    31 JNIEXPORT jint JNICALL
       
    32 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1createEventSource(
       
    33     JNIEnv* aJniEnv,
       
    34     jobject aPeer,
       
    35     jint aEventServerHandle)
       
    36 {
       
    37     DEBUG("SoftNotification::_createEventSource +");
       
    38 
       
    39     TInt eventSourceHandle = CSoftNotificationEventSource::New(
       
    40                                  *aJniEnv,
       
    41                                  aPeer,
       
    42                                  aEventServerHandle);
       
    43 
       
    44     DEBUG_INT("SoftNotification::_createEventSource -, handle=%d",
       
    45               eventSourceHandle);
       
    46 
       
    47     return eventSourceHandle;
       
    48 }
       
    49 
       
    50 /*
       
    51  * Wrapper for CSoftNotification::NewL().
       
    52  */
       
    53 LOCAL_C void CreateSoftNotificationL(
       
    54     TInt* aSoftNotificationHandle,
       
    55     CSoftNotificationEventSource* aEventSource,
       
    56     jobject aPeer,
       
    57     TInt aMidletId,
       
    58     TInt aNotificationId)
       
    59 {
       
    60     DEBUG("SoftNotification::CreateSoftNotificationL +");
       
    61 
       
    62     JNIEnv* jniEnv = JniEnvWrapper::GetValidJniRef();
       
    63 
       
    64     // Get method id for the notify function.
       
    65     jclass classRef = jniEnv->GetObjectClass(aPeer);
       
    66     jmethodID methodId = jniEnv->GetMethodID(
       
    67                              classRef, "notificationCallback", "(I)V");
       
    68 
       
    69     // Handle possible exception occurred during the previous operation.
       
    70     if (jniEnv->ExceptionCheck())
       
    71     {
       
    72         // Print the stack trace and clear the pending exception from
       
    73         // the Java VM.
       
    74         jniEnv->ExceptionDescribe();
       
    75         jniEnv->ExceptionClear();
       
    76         User::Leave(KErrGeneral);
       
    77     }
       
    78 
       
    79     // create a soft notification instance
       
    80     CSoftNotification* softNote = CSoftNotification::NewL(
       
    81                                       aMidletId,
       
    82                                       aNotificationId,
       
    83                                       *aEventSource);
       
    84 
       
    85     // The weak reference is stored in the created soft notification object.
       
    86     // The reference will be deleted when the object is destroyed.
       
    87     softNote->SetPeerObserver(aPeer, methodId);
       
    88 
       
    89     // Get handle to the created soft notification.
       
    90     *aSoftNotificationHandle = JavaMakeHandle(softNote);
       
    91 
       
    92     DEBUG("SoftNotification::CreateSoftNotificationL -");
       
    93 }
       
    94 
       
    95 /*
       
    96  * Class: com_nokia_mid_ui_SoftNotification
       
    97  * Method: _createNativePeer
       
    98  */
       
    99 JNIEXPORT jint JNICALL
       
   100 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1createNativePeer(
       
   101     JNIEnv* aJniEnv,
       
   102     jobject aPeer,
       
   103     jint aEventSourceHandle,
       
   104     jint aMIDletID,
       
   105     jint aNotificationId)
       
   106 {
       
   107     DEBUG("SoftNotification::_createNativePeer +");
       
   108 
       
   109     CSoftNotificationEventSource* eventSource =
       
   110         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   111 
       
   112     // Create a weak reference since the object is transferred between
       
   113     // two threads. The reference is deleted when the native class is destroyed.
       
   114     jobject peer = aJniEnv->NewWeakGlobalRef(aPeer);
       
   115 
       
   116     // Handle possible exception occurred during the previous operation.
       
   117     if (aJniEnv->ExceptionCheck())
       
   118     {
       
   119         // Print the stack trace and clear the pending exception from
       
   120         // the Java VM.
       
   121         aJniEnv->ExceptionDescribe();
       
   122         aJniEnv->ExceptionClear();
       
   123         return KErrNoMemory;
       
   124     }
       
   125 
       
   126     TInt softNotificationHandle = 0;
       
   127 
       
   128     TInt error = eventSource->ExecuteTrap(
       
   129                      &CreateSoftNotificationL,
       
   130                      &softNotificationHandle,
       
   131                      eventSource,
       
   132                      peer,
       
   133                      aMIDletID,
       
   134                      aNotificationId);
       
   135 
       
   136     // Delete the created global reference if object creation failed.
       
   137     if (error != KErrNone)
       
   138     {
       
   139         softNotificationHandle = error;
       
   140         aJniEnv->DeleteWeakGlobalRef((jweak)peer);
       
   141     }
       
   142 
       
   143     DEBUG_INT("SoftNotification::_createNativePeer -, handle=%d",
       
   144               softNotificationHandle);
       
   145 
       
   146     return softNotificationHandle;
       
   147 }
       
   148 
       
   149 /*
       
   150  * Destroys the given soft notification and global reference that it owns.
       
   151  */
       
   152 LOCAL_C void DestroyNotification(CSoftNotification* aNotification)
       
   153 {
       
   154     DEBUG("SoftNotification::DestroyNotification +");
       
   155 
       
   156     JNIEnv* jniEnv = JniEnvWrapper::GetValidJniRef();
       
   157 
       
   158     // Delete the weak reference stored in the class.
       
   159     aNotification->RemovePeerObserver(*jniEnv);
       
   160 
       
   161     delete aNotification;
       
   162 
       
   163     DEBUG("SoftNotification::DestroyNotification -");
       
   164 }
       
   165 
       
   166 /*
       
   167  * Class: com_nokia_mid_ui_SoftNotification
       
   168  * Method: _destroy
       
   169  */
       
   170 JNIEXPORT void JNICALL
       
   171 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1destroy(
       
   172     JNIEnv* aJniEnv,
       
   173     jobject /*aPeer*/,
       
   174     jint aEventSourceHandle,
       
   175     jint aSoftNotificationHandle)
       
   176 {
       
   177     DEBUG_INT("SoftNotification::_destroy +, handle=%d",
       
   178               aSoftNotificationHandle);
       
   179 
       
   180     // There are no native resources if event server is not yet created or
       
   181     // is already destroyed.
       
   182     if (aEventSourceHandle <= KErrNone)
       
   183     {
       
   184         DEBUG("SoftNotification::_destroy -, nothing to do");
       
   185         return;
       
   186     }
       
   187 
       
   188     CSoftNotificationEventSource* eventSource =
       
   189         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   190 
       
   191     // Destroy the soft notification. The destruction has to be done in the
       
   192     // event server thread because all used resources are not shared between
       
   193     // different threads.
       
   194     if (aSoftNotificationHandle > KErrNone)
       
   195     {
       
   196         CSoftNotification* notification =
       
   197             JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   198 
       
   199         eventSource->Execute(&DestroyNotification, notification);
       
   200     }
       
   201 
       
   202     // Destroy the event source.
       
   203     eventSource->Dispose(*aJniEnv);
       
   204 
       
   205     DEBUG("SoftNotification::_destroy -");
       
   206 }
       
   207 
       
   208 /*
       
   209  * wrapper for CSoftNotification::ShowSoftNotificationL()
       
   210  */
       
   211 LOCAL_C void ShowSoftNotificationL(
       
   212     CSoftNotification* aSoftNotification)
       
   213 {
       
   214     aSoftNotification->ShowSoftNotificationL();
       
   215 }
       
   216 
       
   217 /*
       
   218  * Class: com_nokia_mid_ui_SoftNotification
       
   219  * Method: _showSoftNotification
       
   220  */
       
   221 JNIEXPORT jint JNICALL
       
   222 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1showSoftNotification(
       
   223     JNIEnv* /*aJniEnv*/,
       
   224     jobject /*aPeer*/,
       
   225     jint aEventSourceHandle,
       
   226     jint aSoftNotificationHandle)
       
   227 {
       
   228     DEBUG("SoftNotification::_showSoftNotification +");
       
   229 
       
   230     CSoftNotificationEventSource* eventSource =
       
   231         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   232 
       
   233     CSoftNotification* softNotification =
       
   234         JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   235 
       
   236 
       
   237     TInt error = eventSource->ExecuteTrap(
       
   238                      &ShowSoftNotificationL,
       
   239                      softNotification);
       
   240 
       
   241     DEBUG("SoftNotification::_showSoftNotification -");
       
   242 
       
   243     return error;
       
   244 }
       
   245 
       
   246 /*
       
   247  * wrapper for CSoftNotification::RemoveSoftNotificationL()
       
   248  */
       
   249 LOCAL_C void RemoveSoftNotificationL(
       
   250     CSoftNotification* aSoftNotification)
       
   251 {
       
   252     aSoftNotification->RemoveSoftNotificationL();
       
   253 }
       
   254 
       
   255 /*
       
   256  * Class: com_nokia_mid_ui_SoftNotification
       
   257  * Method: _removeSoftNotification
       
   258  */
       
   259 JNIEXPORT jint JNICALL
       
   260 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1removeSoftNotification(
       
   261     JNIEnv* /*aJniEnv*/,
       
   262     jobject /*aPeer*/,
       
   263     jint aEventSourceHandle,
       
   264     jint aSoftNotificationHandle)
       
   265 {
       
   266     DEBUG("SoftNotification::_removeSoftNotification +");
       
   267 
       
   268     CSoftNotificationEventSource* eventSource =
       
   269         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   270 
       
   271     CSoftNotification* softNotification =
       
   272         JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   273 
       
   274 
       
   275     TInt error = eventSource->ExecuteTrap(
       
   276                      &RemoveSoftNotificationL,
       
   277                      softNotification);
       
   278 
       
   279     DEBUG("SoftNotification::_removeSoftNotification -");
       
   280 
       
   281     return error;
       
   282 }
       
   283 
       
   284 /*
       
   285  * wrapper for CSoftNotification::SetTextL()
       
   286  */
       
   287 LOCAL_C void SetTextL(
       
   288     CSoftNotification* aSoftNotification,
       
   289     TDesC* aText,
       
   290     TDesC* aGroupText)
       
   291 {
       
   292     aSoftNotification->SetTextL(*aText, *aGroupText);
       
   293 }
       
   294 
       
   295 /*
       
   296  * Class:     com_nokia_mid_ui_SoftNotificationImpl
       
   297  * Method:    _setText
       
   298  */
       
   299 JNIEXPORT int JNICALL
       
   300 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1setText(
       
   301     JNIEnv* aJni,
       
   302     jobject /*aPeer*/,
       
   303     jint aEventSourceHandle,
       
   304     jint aSoftNotificationHandle,
       
   305     jstring aText,
       
   306     jstring aGroupText)
       
   307 {
       
   308     DEBUG("SoftNotification::_setText +");
       
   309 
       
   310     CSoftNotificationEventSource* eventSource =
       
   311         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   312 
       
   313     CSoftNotification* softNotification =
       
   314         JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   315 
       
   316 
       
   317     RJString text(*aJni, aText);
       
   318     RJString grouptext(*aJni, aGroupText);
       
   319 
       
   320     TInt error = eventSource->ExecuteTrap(
       
   321                      &SetTextL,
       
   322                      softNotification,
       
   323                      (TDesC*)&text,
       
   324                      (TDesC*)&grouptext);
       
   325 
       
   326     DEBUG("SoftNotification::_setText -");
       
   327 
       
   328     return error;
       
   329 }
       
   330 
       
   331 /*
       
   332  * wrapper for CSoftNotification::SetSoftkeyLabels()
       
   333  */
       
   334 LOCAL_C void SetSoftkeyLabelsL(
       
   335     CSoftNotification* aSoftNotification,
       
   336     TDesC* aSoftkey1Label,
       
   337     TDesC* aSoftkey2Label)
       
   338 {
       
   339     aSoftNotification->SetSoftkeyLabelsL(*aSoftkey1Label, *aSoftkey2Label);
       
   340 }
       
   341 
       
   342 /*
       
   343  * Class:     com_nokia_mid_ui_SoftNotificationImpl
       
   344  * Method:    _setSoftkeyLabels
       
   345  */
       
   346 JNIEXPORT int JNICALL
       
   347 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1setSoftkeyLabels(
       
   348     JNIEnv* aJni,
       
   349     jobject /*aPeer*/,
       
   350     jint aEventSourceHandle,
       
   351     jint aSoftNotificationHandle,
       
   352     jstring aSoftkey1Label,
       
   353     jstring aSoftkey2Label)
       
   354 {
       
   355     DEBUG("SoftNotification::_setSoftkeyLabels +");
       
   356 
       
   357     CSoftNotificationEventSource* eventSource =
       
   358         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   359 
       
   360     CSoftNotification* softNotification =
       
   361         JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   362 
       
   363 
       
   364     RJString softkey1Label(*aJni, aSoftkey1Label);
       
   365     RJString softkey2Label(*aJni, aSoftkey2Label);
       
   366 
       
   367     TInt error = eventSource->ExecuteTrap(
       
   368                      &SetSoftkeyLabelsL,
       
   369                      softNotification,
       
   370                      (TDesC*)&softkey1Label,
       
   371                      (TDesC*)&softkey2Label);
       
   372 
       
   373     DEBUG("SoftNotification::_setSoftkeyLabels -");
       
   374 
       
   375     return error;
       
   376 }
       
   377 
       
   378 /*
       
   379  * wrapper for CSoftNotification::SetImage()
       
   380  */
       
   381 LOCAL_C void SetImageL(
       
   382     CSoftNotification* aSoftNotification,
       
   383     const TDesC8* aImageData)
       
   384 {
       
   385     aSoftNotification->SetImageL(*aImageData);
       
   386 }
       
   387 
       
   388 /*
       
   389  * Class:     com_nokia_mid_ui_SoftNotificationImpl
       
   390  * Method:    _setImage
       
   391  */
       
   392 JNIEXPORT int JNICALL
       
   393 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1setImage(
       
   394     JNIEnv* aJni,
       
   395     jobject /*aPeer*/,
       
   396     jint aEventSourceHandle,
       
   397     jint aSoftNotificationHandle,
       
   398     jbyteArray aImageData)
       
   399 {
       
   400     DEBUG("SoftNotification::_setImage +");
       
   401 
       
   402     CSoftNotificationEventSource* eventSource =
       
   403         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   404 
       
   405     CSoftNotification* softNotification =
       
   406         JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   407 
       
   408 
       
   409     // Returning just KErrNone if there is no image data
       
   410     TInt error = KErrNone;
       
   411 
       
   412     if (aImageData)
       
   413     {
       
   414         // Image data: Get pointer to Java header data
       
   415         jbyte* imageDataJava = aJni->GetByteArrayElements(aImageData, NULL);
       
   416 
       
   417         // Handle possible exception occurred during the previous operation.
       
   418         if (aJni->ExceptionCheck())
       
   419         {
       
   420             // Print the stack trace and clear the pending exception from
       
   421             // the Java VM.
       
   422             aJni->ExceptionDescribe();
       
   423             aJni->ExceptionClear();
       
   424             return KErrNoMemory;
       
   425         }
       
   426 
       
   427         TInt imageDataLength = aJni->GetArrayLength(aImageData);
       
   428         TPtrC8 imageData((TUint8*)imageDataJava, imageDataLength);
       
   429 
       
   430         error = eventSource->ExecuteTrap(
       
   431                     &SetImageL,
       
   432                     softNotification,
       
   433                     (const TDesC8*)&imageData);
       
   434 
       
   435         // Release image bytes got with GetByteArrayElements.
       
   436         // The function does not throw any exceptions.
       
   437         aJni->ReleaseByteArrayElements(aImageData,
       
   438                                        imageDataJava,
       
   439                                        0);
       
   440     }
       
   441     else
       
   442     {
       
   443         // No image data defined, reset previous image.
       
   444         TPtrC8 imageData(KNullDesC8);
       
   445         error = eventSource->ExecuteTrap(
       
   446                     &SetImageL,
       
   447                     softNotification,
       
   448                     (const TDesC8*)&imageData);
       
   449     }
       
   450 
       
   451     DEBUG("SoftNotification::_setImage -");
       
   452 
       
   453     return error;
       
   454 }
       
   455 
       
   456 /*
       
   457  * wrapper for CSoftNotification::GetId()
       
   458  */
       
   459 LOCAL_C TInt GetId(CSoftNotification* aSoftNotification)
       
   460 {
       
   461     return aSoftNotification->Id();
       
   462 }
       
   463 
       
   464 /*
       
   465  * Class:     com_nokia_mid_ui_SoftNotificationImpl
       
   466  * Method:    _getId
       
   467  */
       
   468 JNIEXPORT jint JNICALL
       
   469 Java_com_nokia_mj_impl_dynamicsoftnotification_SoftNotificationImpl__1getId(
       
   470     JNIEnv* /*aJniEnv*/,
       
   471     jobject /*aPeer*/,
       
   472     jint aEventSourceHandle,
       
   473     jint aSoftNotificationHandle)
       
   474 {
       
   475     DEBUG("SoftNotification::_getId +");
       
   476 
       
   477     CSoftNotificationEventSource* eventSource =
       
   478         JavaUnhand< CSoftNotificationEventSource >(aEventSourceHandle);
       
   479 
       
   480     CSoftNotification* softNotification =
       
   481         JavaUnhand< CSoftNotification >(aSoftNotificationHandle);
       
   482 
       
   483     TInt id = eventSource->Execute(&GetId, softNotification);
       
   484 
       
   485     DEBUG("SoftNotification::_getId -");
       
   486 
       
   487     return id;
       
   488 }
       
   489 
       
   490 //  End of File