javauis/nokiasound_akn/src/cmidsound.cpp
branchRCL_3
changeset 83 26b2b12093af
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Wrapper for CMIDClip and CMIDTone class initialisations.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <centralrepository.h>
       
    21 #include <ProfileEngineSDKCRKeys.h>
       
    22 
       
    23 #include "CMIDSound.h"
       
    24 #include "CMIDTone.h"
       
    25 #include "CMIDClip.h"
       
    26 #include "logger.h"
       
    27 #include "fs_methodcall.h"
       
    28 #include "com_nokia_mid_sound_Sound.h"
       
    29 
       
    30 
       
    31 CMIDSound* CMIDSound::NewL(JNIEnv& aJni, jobject aPeer)
       
    32 {
       
    33     JELOG2(EJavaUI);
       
    34 
       
    35     CMIDSound* self = new CMIDSound();
       
    36     if (self != NULL)
       
    37     {
       
    38         self->attachToVm(aJni, aPeer);
       
    39     }
       
    40     return self;
       
    41 }
       
    42 
       
    43 CMIDSound::~CMIDSound()
       
    44 {
       
    45     delete iMIDSound;
       
    46 
       
    47     // CENREP
       
    48     if (iProfileListener)
       
    49     {
       
    50         iProfileListener->StopListening();
       
    51         delete iProfileListener;
       
    52     }
       
    53     delete iRepository;
       
    54 }
       
    55 
       
    56 void CMIDSound::HandleNotifyInt(TUint32 aId, TInt aNewValue)
       
    57 {
       
    58     JELOG2(EJavaUI);
       
    59     if (aId == KProEngActiveWarningTones)
       
    60     {
       
    61         if (iMIDSound)
       
    62         {
       
    63             iMIDSound->SetVolume(aNewValue == 0 ? 0 : iRealVolume);
       
    64         }
       
    65     }
       
    66 }
       
    67 
       
    68 void CMIDSound::SendEvent(TInt aEventType)
       
    69 {
       
    70     /**
       
    71      *Callback to Java
       
    72      */
       
    73     JELOG2(EJavaUI);
       
    74 
       
    75     jclass javaPeerClass = mJniEnv->FindClass("com/nokia/mid/sound/Sound");
       
    76 
       
    77     jmethodID soundStateChangeCallback = mJniEnv->GetMethodID(
       
    78                                              javaPeerClass /*mJavaPeerClass */, "soundStateChanged", "(I)V");
       
    79 
       
    80 
       
    81     mJniEnv->CallVoidMethod(mJavaPeerObject, soundStateChangeCallback,
       
    82                             aEventType);
       
    83 }
       
    84 
       
    85 CMIDSound::CMIDSound()
       
    86         : java::util::FunctionServer("CMIDSoundFunctionServer"),
       
    87         iMIDSound(0)
       
    88 {
       
    89     JELOG2(EJavaUI);
       
    90     createServerToNewThread();
       
    91 }
       
    92 
       
    93 int CMIDSound::InitProfileListener()
       
    94 {
       
    95     TRAPD(err, CallMethodL(this, &CMIDSound::InitProfileListenerL, this));
       
    96     return err;
       
    97 }
       
    98 
       
    99 void CMIDSound::InitProfileListenerL()
       
   100 {
       
   101     JELOG2(EJavaUI);
       
   102     // repository for profile
       
   103     iRepository = CRepository::NewL(KCRUidProfileEngine);
       
   104 
       
   105     // get notifies about changes in KProEngActiveWarningTones
       
   106     iProfileListener = CCenRepNotifyHandler::NewL(*this,
       
   107                        *iRepository,
       
   108                        CCenRepNotifyHandler::EIntKey,
       
   109                        KProEngActiveWarningTones);
       
   110 
       
   111     // Changes will be informed to HandleNotifyInt
       
   112     iProfileListener->StartListeningL();
       
   113 }
       
   114 
       
   115 TBool CMIDSound::IsAudioEnabled(/*CMIDSound* aSelf*/)
       
   116 {
       
   117     JELOG2(EJavaUI);
       
   118     TInt isProfileOn = 0;
       
   119     TInt error = iRepository->Get(KProEngActiveWarningTones, isProfileOn);
       
   120     TBool retVal = EFalse;
       
   121 
       
   122     // If getting profile status fails do not play any tones.
       
   123     if ((isProfileOn == 1) &&
       
   124             (error == KErrNone))
       
   125     {
       
   126         retVal = ETrue;
       
   127     }
       
   128     return retVal;
       
   129 }
       
   130 
       
   131 void CMIDSound::SetBeep(TInt aFreq, TInt64 aDuration)
       
   132 {
       
   133     JELOG2(EJavaUI);
       
   134     iFreq = aFreq;
       
   135     iDur = aDuration;
       
   136 }
       
   137 
       
   138 TInt CMIDSound::Init(TInt aType, const TDesC8* aData)
       
   139 {
       
   140     JELOG2(EJavaUI);
       
   141     TRAPD(err, CallMethodL(this, &CMIDSound::InitL, aType, aData, this));
       
   142     return err;
       
   143 }
       
   144 
       
   145 void CMIDSound::InitL(TInt aType, const TDesC8* aData)
       
   146 {
       
   147     JELOG2(EJavaUI);
       
   148 
       
   149     if (iMIDSound)
       
   150     {
       
   151         if (iMIDSound->State() == CMIDSoundImpl::EInitialising)
       
   152         {
       
   153             return;
       
   154         }
       
   155         if (iMIDSound->State() == CMIDSoundImpl::EReadyToPlay ||
       
   156                 iMIDSound->State() == CMIDSoundImpl::EPlaying)
       
   157         {
       
   158             iMIDSound->Release();
       
   159         }
       
   160     }
       
   161 
       
   162     CMIDSoundImpl* soundImpl = NULL;
       
   163     switch (aType)
       
   164     {
       
   165     case com_nokia_mid_sound_Sound_FORMAT_TONE: //Tone
       
   166     {
       
   167         soundImpl = CMIDTone::NewL(*aData, this);
       
   168         break;
       
   169     }
       
   170     case com_nokia_mid_sound_Sound_FORMAT_BEEP: //Beep
       
   171     {
       
   172         soundImpl = CMIDTone::NewL(iFreq, iDur, this);
       
   173         break;
       
   174     }
       
   175     case com_nokia_mid_sound_Sound_FORMAT_WAV:  //Wav
       
   176     {
       
   177         soundImpl = CMIDClip::NewL(aData, this);
       
   178         break;
       
   179     }
       
   180     default:
       
   181     {
       
   182         break;
       
   183     }
       
   184     }
       
   185     if (soundImpl->State() != CMIDSoundImpl::EReadyToPlay)
       
   186     {
       
   187         TInt state = soundImpl->State();
       
   188         delete soundImpl;
       
   189         User::Leave(KErrNotReady);
       
   190     }
       
   191     iRealVolume = soundImpl->Volume();
       
   192 
       
   193     delete iMIDSound;
       
   194     iMIDSound = soundImpl;
       
   195 }
       
   196 
       
   197 TInt CMIDSound::Play(TInt aLoop)
       
   198 {
       
   199     JELOG2(EJavaUI);
       
   200     TInt err = 0;
       
   201     CallMethod(err, this, &CMIDSound::DoPlay, aLoop, this);
       
   202     return err;
       
   203 }
       
   204 
       
   205 TInt CMIDSound::DoPlay(TInt aLoop)
       
   206 {
       
   207     JELOG2(EJavaUI);
       
   208     
       
   209     if ( iMIDSound )
       
   210     {
       
   211         TInt state = iMIDSound->State();
       
   212         if (state != CMIDSoundImpl::EReadyToPlay)
       
   213         {
       
   214             return state;
       
   215     	}
       
   216     }
       
   217     else
       
   218     {
       
   219         return CMIDSoundImpl::ENotSupported;
       
   220     }
       
   221     if (!IsAudioEnabled())
       
   222     {
       
   223         iMIDSound->SetVolume(0);
       
   224     }
       
   225     return iMIDSound->Play(aLoop);
       
   226 }
       
   227 
       
   228 void CMIDSound::Resume()
       
   229 {
       
   230     JELOG2(EJavaUI);
       
   231     CallMethod(this, &CMIDSound::DoResume, this);
       
   232 }
       
   233 
       
   234 void CMIDSound::DoResume()
       
   235 {
       
   236     JELOG2(EJavaUI);
       
   237     iMIDSound->Resume();
       
   238 }
       
   239 
       
   240 void CMIDSound::Stop()
       
   241 {
       
   242     JELOG2(EJavaUI);
       
   243     CallMethod(this, &CMIDSound::DoStop, this);
       
   244 }
       
   245 
       
   246 void CMIDSound::DoStop()
       
   247 {
       
   248     JELOG2(EJavaUI);
       
   249     iMIDSound->Stop();
       
   250 }
       
   251 
       
   252 TInt CMIDSound::SoundVolume()
       
   253 {
       
   254     JELOG2(EJavaUI);
       
   255     TInt result = 0;
       
   256     CallMethod(result, this, &CMIDSound::Volume, this);
       
   257     return result;
       
   258 }
       
   259 
       
   260 TInt CMIDSound::Volume()
       
   261 {
       
   262     JELOG2(EJavaUI);
       
   263     return iMIDSound->Volume();
       
   264 }
       
   265 
       
   266 void CMIDSound::SetVolume(TInt aVolume)
       
   267 {
       
   268     JELOG2(EJavaUI);
       
   269     CallMethod(this, &CMIDSound::DoSetVolume, aVolume, this);
       
   270 }
       
   271 
       
   272 void CMIDSound::DoSetVolume(TInt aVolume)
       
   273 {
       
   274     JELOG2(EJavaUI);
       
   275     iRealVolume = aVolume;
       
   276     TInt currentState = iMIDSound->State();
       
   277     if ((currentState == CMIDSoundImpl::EReadyToPlay) ||
       
   278             (currentState == CMIDSoundImpl::EPlaying))
       
   279     {
       
   280         iMIDSound->SetVolume(IsAudioEnabled() ? aVolume : 0);
       
   281     }
       
   282 }
       
   283 
       
   284 
       
   285 TInt CMIDSound::PlayerState()
       
   286 {
       
   287     JELOG2(EJavaUI);
       
   288     TInt result = 0;
       
   289     CallMethod(result, this, &CMIDSound::State, this);
       
   290     return result;
       
   291 }
       
   292 
       
   293 TInt CMIDSound::State()
       
   294 {
       
   295     JELOG2(EJavaUI);
       
   296     return iMIDSound->State();
       
   297 }
       
   298 
       
   299 void CMIDSound::Release()
       
   300 {
       
   301     JELOG2(EJavaUI);
       
   302     CallMethod(this, &CMIDSound::DoRelease, this);
       
   303 }
       
   304 
       
   305 void CMIDSound::DoRelease()
       
   306 {
       
   307     JELOG2(EJavaUI);
       
   308     iMIDSound->Release();
       
   309 }