javauis/mmapi_qt/baseline/src/cmmavolumecontrol.cpp
changeset 23 98ccebc37403
child 26 dc7c549001d5
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2002-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:  This class is used for volume setting
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 
       
    21 #include "cmmavolumecontrol.h"
       
    22 #include "cmmaplayer.h"
       
    23 #include <logger.h>
       
    24 
       
    25 _LIT(KMMAVolumeErrorMsg, "Can't set volume level");
       
    26 
       
    27 const TInt KMMAJavaSoundIndex = 0;
       
    28 const TInt KMMAGlobalVolumeSoundIndex = 2;
       
    29 
       
    30 void CMMAVolumeControl::StaticSetLevelL(CMMAVolumeControl* aVolumeControl,
       
    31                                         TInt aLevel)
       
    32 {
       
    33     // Java level is the first
       
    34     aVolumeControl->SetVolumeLevelL(KMMAJavaSoundIndex, aLevel);
       
    35 }
       
    36 
       
    37 void CMMAVolumeControl::StaticGetLevelL(CMMAVolumeControl* aVolumeControl,
       
    38                                         TInt* aLevel)
       
    39 {
       
    40     // Java level is the first
       
    41     aVolumeControl->GetVolumeLevelL(KMMAJavaSoundIndex, aLevel);
       
    42 }
       
    43 
       
    44 
       
    45 CMMAVolumeControl::CMMAVolumeControl(CMMAPlayer* aPlayer)
       
    46         : iPlayer(aPlayer), iLevel(KMMAVolumeMaxLevel)
       
    47 {
       
    48 }
       
    49 
       
    50 
       
    51 CMMAVolumeControl::~CMMAVolumeControl()
       
    52 {
       
    53     iLevels.Close();
       
    54 }
       
    55 
       
    56 
       
    57 void CMMAVolumeControl::ConstructBaseL()
       
    58 {
       
    59     iPlayer->AddStateListenerL(this);
       
    60 
       
    61     // Add level for java, will set in StaticSetLevelL method.
       
    62     // In constructor iLevels array is empty and Java level will be the first,
       
    63     // KMMAJavaSoundIndex.
       
    64     AddLevelL();
       
    65 
       
    66     iLevel = CalculateLevel();
       
    67 
       
    68     // The default value is not yet known. Volume control may change the
       
    69     // volume of the controller before it has been retrieved so when the
       
    70     // state of the player is changed the Java volume is retrieved from
       
    71     // the controller.
       
    72     iLevels[ KMMAJavaSoundIndex ] = KErrNotFound;
       
    73 }
       
    74 
       
    75 const TDesC& CMMAVolumeControl::ClassName() const
       
    76 {
       
    77     return KMMAVolumeControlName;
       
    78 }
       
    79 
       
    80 void CMMAVolumeControl::SetLevelL(TInt aLevel)
       
    81 {
       
    82     // Level cannot be set to derived control if player is not prefetched.
       
    83     if (iPlayer->State() > CMMAPlayer::ERealized)
       
    84     {
       
    85         DoSetLevelL(aLevel);
       
    86     }
       
    87     iLevel = aLevel;
       
    88 }
       
    89 
       
    90 void CMMAVolumeControl::StateChanged(TInt aState)
       
    91 {
       
    92     LOG1( EJavaMMAPI, EInfo, "CMMAVolumeControl::StateChanged - state %d", aState);
       
    93     // Set the volume if the player is prefetched
       
    94     if (aState == CMMAPlayer::EPrefetched)
       
    95     {
       
    96         TRAPD(error,
       
    97         {
       
    98             // Get the default value for the Java sound level
       
    99             if (iLevels[ KMMAJavaSoundIndex ] == KErrNotFound)
       
   100             {
       
   101                 iLevels[ KMMAJavaSoundIndex ] = DoGetLevelL();
       
   102                 iLevel = CalculateLevel();
       
   103             }
       
   104             SetLevelL(iLevel);
       
   105 
       
   106         });
       
   107         if (error != KErrNone)
       
   108         {
       
   109             iPlayer->PostStringEvent(CMMAPlayerEvent::EError,
       
   110                                      KMMAVolumeErrorMsg);
       
   111         }
       
   112     }
       
   113 
       
   114     // Level is already set for Global Volume so no need to set it again
       
   115     // Notify the initial global volume value to java
       
   116     // Notify only if STATE == CMMAPlayer::ERealized
       
   117     // Error ID AKUR-7G69Q5 GLOBAL VOLUME EVENT CR
       
   118     if (aState == CMMAPlayer::ERealized)
       
   119     {
       
   120         if ((iLevels.Count() - 1) == KMMAGlobalVolumeSoundIndex)
       
   121         {
       
   122             LOG( EJavaMMAPI, EInfo, "MMA::CMMAVolumeControl::StateChanged : Post GLOBAL VOL EVENT  ");
       
   123             if (iLevels[ KMMAGlobalVolumeSoundIndex ] != KErrNotFound)
       
   124             {
       
   125                 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAVolumeControl::StateChanged : Post complete Val = %d ",iLevels[ KMMAGlobalVolumeSoundIndex ]);
       
   126                 iPlayer->PostLongEvent(CMMAPlayerEvent::ENOKIA_EXTERNAL_VOLUME_EVENT,
       
   127                                        iLevels[ KMMAGlobalVolumeSoundIndex ]);
       
   128             }
       
   129         }
       
   130     }
       
   131 }
       
   132 
       
   133 void CMMAVolumeControl::RefreshVolume()
       
   134 {
       
   135     LOG( EJavaMMAPI, EInfo, "MMA::CMMAVolumeControl::RefreshVolume ++ ");
       
   136     TRAPD(error,
       
   137     {
       
   138         // Get the default value for the Java sound level
       
   139         if (iLevels[ KMMAJavaSoundIndex ] == KErrNotFound)
       
   140         {
       
   141             iLevels[ KMMAJavaSoundIndex ] = DoGetLevelL();
       
   142             iLevel = CalculateLevel();
       
   143         }
       
   144         SetLevelL(iLevel);
       
   145 
       
   146     });
       
   147 
       
   148     if (error != KErrNone)
       
   149     {
       
   150         iPlayer->PostStringEvent(CMMAPlayerEvent::EError,
       
   151                                  KMMAVolumeErrorMsg);
       
   152     }
       
   153     LOG( EJavaMMAPI, EInfo, "MMA::CMMAVolumeControl::RefreshVolume -- ");
       
   154 }
       
   155 
       
   156 void CMMAVolumeControl::RefreshControl()
       
   157 {
       
   158     RefreshVolume();
       
   159 }
       
   160 
       
   161 EXPORT_C TInt CMMAVolumeControl::AddLevelL()
       
   162 {
       
   163     User::LeaveIfError(iLevels.Append(KMMAVolumeMaxLevel));
       
   164 
       
   165     // New level is the last element
       
   166     return iLevels.Count() - 1;
       
   167 }
       
   168 
       
   169 EXPORT_C void CMMAVolumeControl::SetVolumeLevelL(TInt aLevelIndex,
       
   170         TInt aVolumeLevel)
       
   171 {
       
   172     LOG2( EJavaMMAPI, EInfo, "CMMAVolumeControl::SetVolumeLevelL - setting index %d, level %d",
       
   173                aLevelIndex, aVolumeLevel);
       
   174     if (0 >= iLevels.Count() ||  iLevels.Count() > 3)
       
   175     {
       
   176         return ;
       
   177     }
       
   178     TInt oldVolumeLevel = iLevels[ aLevelIndex ];
       
   179     iLevels[ aLevelIndex ] = aVolumeLevel;
       
   180     SetLevelL(CalculateLevel());
       
   181 
       
   182     // send event if level is really changed
       
   183     if ((aLevelIndex == KMMAJavaSoundIndex) &&
       
   184             (aVolumeLevel != oldVolumeLevel))
       
   185     {
       
   186         iPlayer->PostObjectEvent(CMMAPlayerEvent::EVolumeChanged,
       
   187                                  iControlObject);
       
   188     }
       
   189 
       
   190     // send event if global volume level is really changed
       
   191     // Error ID AKUR-7G69Q5 GLOBAL VOLUME EVENT CR
       
   192     if ((aLevelIndex == KMMAGlobalVolumeSoundIndex) &&
       
   193             (aVolumeLevel != oldVolumeLevel))
       
   194     {
       
   195 
       
   196         iPlayer->PostLongEvent(CMMAPlayerEvent::ENOKIA_EXTERNAL_VOLUME_EVENT,
       
   197                                iLevels[ aLevelIndex ]);
       
   198     }
       
   199 }
       
   200 
       
   201 void CMMAVolumeControl::GetVolumeLevelL(TInt aLevelIndex,
       
   202                                         TInt* aVolumeLevel)
       
   203 {
       
   204     LOG1( EJavaMMAPI, EInfo, "CMMAVolumeControl::GetVolumeLevelL - level index %d", aLevelIndex);
       
   205 
       
   206     // Return max volume if the default Java volume level is not yet known
       
   207     if (aLevelIndex == KMMAJavaSoundIndex &&
       
   208             iLevels[ KMMAJavaSoundIndex ] == KErrNotFound)
       
   209     {
       
   210         *aVolumeLevel = KMMAVolumeMaxLevel;
       
   211         return;
       
   212     }
       
   213 
       
   214     *aVolumeLevel = iLevels[ aLevelIndex ];
       
   215 
       
   216     LOG1( EJavaMMAPI, EInfo, "CMMAVolumeControl::GetVolumeLevelL - level %d", *aVolumeLevel);
       
   217 }
       
   218 
       
   219 TInt CMMAVolumeControl::CalculateLevel()
       
   220 {
       
   221     TInt levelCount = iLevels.Count();
       
   222 
       
   223     // 64 bit integer must be used to have more than 4 levels.
       
   224     TInt64 level = KMMAVolumeMaxLevel;
       
   225     TInt64 sum = 1;
       
   226 
       
   227     // All levels in the array are percentage of the max volume (0..100).
       
   228     // Actual sound level will be multiplying all levels.
       
   229     for (TInt i = 0; i < levelCount; i++)
       
   230     {
       
   231         // If the level is not known it is expected to be max volume
       
   232         level = (iLevels[ i ] == KErrNotFound ?
       
   233                  level * KMMAVolumeMaxLevel :
       
   234                  level * iLevels[ i ]);
       
   235         sum *= KMMAVolumeMaxLevel;
       
   236     }
       
   237     return level / sum;
       
   238 }
       
   239 //  END OF FILE