javauis/nokiauiapi_qt/src/CSoftNotification.cpp
changeset 79 2f468c1958d0
child 78 71ad690e91f5
equal deleted inserted replaced
76:4ad59aaee882 79:2f468c1958d0
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2009, 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     Nokia Corporation - initial implementation
       
    10  *******************************************************************************/
       
    11 
       
    12 
       
    13 #include <w32std.h>
       
    14 #include <apgtask.h>
       
    15 #include <hbdevicenotificationdialogsymbian.h>
       
    16 #include <hbsymbianvariant.h>
       
    17 #include "CSoftNotification.h"
       
    18 
       
    19 _LIT(KMidletId, "MidletId");
       
    20 _LIT(KNotificationId, "NotificationId");
       
    21 _LIT(KIndicatorTypeJavaSoftNote,"com.nokia.javasoftnotification.indicatorplugin/1.0_%u_%i");
       
    22 const TInt KIndicatorTypeStringLength = 250;
       
    23 const TInt KMidletUidLength = 50;
       
    24 
       
    25 // Event id when user accepted notification
       
    26 const TInt KEventNoteAccepted = 1;
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 // -----------------------------------------------------------------------------
       
    30 // CSoftNotification::NewLC
       
    31 // Static constructor
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CSoftNotification* CSoftNotification::NewL(TInt aAppId, TInt aNotificationId)
       
    35     {
       
    36     CSoftNotification* self = new (ELeave) CSoftNotification(aAppId,
       
    37             aNotificationId);
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop(self);
       
    41     return self;
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CSoftNotification::CSoftNotification
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CSoftNotification::CSoftNotification(TInt aAppId, TInt aNotificationId) :
       
    51     iNotificationId(aNotificationId)
       
    52     {
       
    53     iMidletId = TUid::Uid(aAppId);
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSoftNotification::ConstructL
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CSoftNotification::ConstructL()
       
    62     {
       
    63     iIndicator = CHbIndicatorSymbian::NewL();
       
    64     iIndicator->SetObserver(this);
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // Destructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CSoftNotification::~CSoftNotification()
       
    72     {
       
    73     if (iPrimaryText)
       
    74         {
       
    75         delete iPrimaryText;
       
    76         }
       
    77     
       
    78     if (iSecondaryText)
       
    79         {
       
    80         delete iSecondaryText;
       
    81         }
       
    82     
       
    83     if (iImagePath)
       
    84         {
       
    85         delete iImagePath;
       
    86         }
       
    87     
       
    88     if (iIndicator)
       
    89         {
       
    90         delete iIndicator;
       
    91         }
       
    92     
       
    93     if (iPeer)
       
    94         {
       
    95         iJniEnv->DeleteWeakGlobalRef((jweak)iPeer);
       
    96         }
       
    97     
       
    98     iPrimaryText = NULL;
       
    99     iSecondaryText = NULL;
       
   100     iImagePath = NULL;
       
   101     iIndicator = NULL;
       
   102     iPeer = NULL;
       
   103     iMethodId = NULL;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CSoftNotification::ShowSoftNotificationL
       
   108 // Displays a soft notification
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 bool CSoftNotification::ShowSoftNotificationL()
       
   112     {
       
   113     CHbDeviceNotificationDialogSymbian* dialog =
       
   114             CHbDeviceNotificationDialogSymbian::NewL();
       
   115     CleanupStack::PushL(dialog);
       
   116     
       
   117     if(iPrimaryText != NULL)
       
   118         {
       
   119         dialog->SetTitleL(iPrimaryText->Des());
       
   120         }
       
   121 
       
   122     if(iSecondaryText != NULL)
       
   123         {
       
   124         dialog->SetTextL(iSecondaryText->Des());
       
   125         }
       
   126     
       
   127     if (iImagePath != NULL)
       
   128         {
       
   129         dialog->SetIconNameL(iImagePath->Des());
       
   130         }
       
   131 
       
   132     dialog->ShowL();
       
   133   
       
   134     CHbSymbianVariant* varValues = NotificationParamsL();
       
   135     CleanupStack::PushL(varValues);
       
   136     
       
   137     TBuf<KIndicatorTypeStringLength> indicatorType;
       
   138     indicatorType.Format(KIndicatorTypeJavaSoftNote, iMidletId, iNotificationId);
       
   139     bool indStatus = iIndicator->Activate(indicatorType, varValues);
       
   140     
       
   141     CleanupStack::PopAndDestroy(varValues);
       
   142     CleanupStack::PopAndDestroy(dialog);
       
   143     return indStatus;
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CSoftNotification::RemoveSoftNotificationL
       
   148 // Cancels and removes the soft notification
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CSoftNotification::RemoveSoftNotificationL()
       
   152     {
       
   153     TBuf<KIndicatorTypeStringLength> type;
       
   154     type.Format(KIndicatorTypeJavaSoftNote, iMidletId, iNotificationId);
       
   155     iIndicator->Deactivate(type);
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CSoftNotification::SetTextL
       
   160 // Sets a text for a soft notification
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CSoftNotification::SetTextL(const TDesC& aPrimaryText, const TDesC& aSecondaryText)
       
   164     {
       
   165     HBufC* txt = aPrimaryText.AllocL();
       
   166     delete iPrimaryText;
       
   167     iPrimaryText = txt;
       
   168 
       
   169     txt = aSecondaryText.AllocL();
       
   170     delete iSecondaryText;
       
   171     iSecondaryText = txt;
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CSoftNotification::SetImagePathL
       
   176 // Sets a text for a soft notification
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CSoftNotification::SetImagePathL(const TDesC& aImagePath)
       
   180     {
       
   181     HBufC* txt = aImagePath.AllocL();
       
   182     delete iImagePath;
       
   183     iImagePath = txt;
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CSoftNotification::Id
       
   188 // Notification Id
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 TInt CSoftNotification::Id()
       
   192     {
       
   193     return iNotificationId;
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CSoftNotification::SetPeerObserverL
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CSoftNotification::SetPeerObserver(jobject aPeer, jmethodID aMethodId,
       
   201         JNIEnv* aJniEnv)
       
   202     {
       
   203     iJniEnv = aJniEnv;
       
   204     iPeer = aPeer;
       
   205     iMethodId = aMethodId;
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CSoftNotification::IndicatorUserActivated
       
   210 // Dynamic soft notification was accepted by user.
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CSoftNotification::IndicatorUserActivated(const TDesC& /*aType*/,
       
   214         CHbSymbianVariantMap &aData)
       
   215     {
       
   216     TInt notificationId = *reinterpret_cast<const TInt*>(aData.Get(KNotificationId)->Data());
       
   217     TUid midletId(TUid::Uid(*reinterpret_cast<const TUint*>(aData.Get(KMidletId)->Data())));
       
   218     if (iPeer && iMidletId == midletId &&
       
   219         iNotificationId == notificationId )
       
   220         {
       
   221         // Can't attempt callback if JVM is processing an exception. Event is
       
   222         // possibly lost by Java listener.
       
   223         if (iJniEnv->ExceptionCheck() == JNI_TRUE)
       
   224             {
       
   225             return;
       
   226             }
       
   227         if (iMethodId)
       
   228             {
       
   229             iJniEnv->CallVoidMethod(iPeer, iMethodId, KEventNoteAccepted);
       
   230             }
       
   231         }
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CSoftNotification::FillNotificationParams
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 CHbSymbianVariant* CSoftNotification::NotificationParamsL()
       
   239     {
       
   240     //only five parameter, so creat the array 
       
   241     //for five parameters to fit and append them
       
   242     CDesC16ArrayFlat* array = new CDesC16ArrayFlat(5);
       
   243     CleanupStack::PushL(array);
       
   244     TBuf<KMidletUidLength> midletId;
       
   245     _LIT(MidletId,"%u");
       
   246     midletId.Format(MidletId,iMidletId);
       
   247     array->AppendL(midletId);
       
   248     array->AppendL(reinterpret_cast<TDesC16&>(iNotificationId));
       
   249     if(iPrimaryText != NULL)
       
   250         {
       
   251         array->AppendL(iPrimaryText->Des());
       
   252         }
       
   253     else
       
   254         {
       
   255         array->AppendL(KNullDesC);
       
   256         }
       
   257     
       
   258     if(iSecondaryText != NULL)
       
   259         {
       
   260         array->AppendL(iSecondaryText->Des());
       
   261         }
       
   262     else
       
   263         {
       
   264         array->AppendL(KNullDesC);
       
   265         }
       
   266     
       
   267     if (iImagePath != NULL)
       
   268         {
       
   269         array->AppendL(iImagePath->Des());
       
   270         }
       
   271     else
       
   272         {
       
   273         array->AppendL(KNullDesC);
       
   274         }
       
   275     MDesCArray* marray = static_cast<MDesCArray*>(array);
       
   276     CHbSymbianVariant* varValues = CHbSymbianVariant::NewL( marray, CHbSymbianVariant::EDesArray);
       
   277     CleanupStack::PopAndDestroy(array);
       
   278     return varValues;
       
   279     }