javauis/mmapi_akn/volumekeys/src/cmmaglobalvolume.cpp
branchRCL_3
changeset 14 04becd199f91
child 24 6c158198356e
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2006 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 global volume
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // CLASS HEADER
       
    20 #include "cmmaglobalvolume.h"
       
    21 
       
    22 // INTERNAL INCLUDES
       
    23 #include "cmmaplayer.h"
       
    24 #include "cmmaforeground.h"
       
    25 #include "cmmavolumekeyslistener.h"
       
    26 #include "mmaprivatecrkeys.h"
       
    27 
       
    28 // EXTERNAL INCLUDES
       
    29 #include <centralrepository.h>
       
    30 #include <jdebug.h>
       
    31 
       
    32 // UNNAMED LOCAL NAMESPACE
       
    33 namespace
       
    34 {
       
    35 // Volume level step size
       
    36 const TInt KMMAVolumeLevelStep = 10;
       
    37 // Error when setting new volume level for a control
       
    38 _LIT(KMMAGlobalVolumeSetError, "Can't set volume level");
       
    39 }
       
    40 
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS =============================
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CMMAGlobalVolume::NewL
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CMMAGlobalVolume* CMMAGlobalVolume::NewL(CMMAForeground* aForeground)
       
    49 {
       
    50     CMMAGlobalVolume* self = new(ELeave) CMMAGlobalVolume(aForeground);
       
    51     CleanupStack::PushL(self);
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop(self);
       
    54     return self;
       
    55 }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CMMAGlobalVolume::~CMMAGlobalVolume
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CMMAGlobalVolume::~CMMAGlobalVolume()
       
    62 {
       
    63     delete iVolumeKeysListener;
       
    64     delete iSettingsStore;
       
    65     delete iForeground;
       
    66     iControlList.Close();
       
    67 }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CMMAGlobalVolume::AddPlayerL
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 void CMMAGlobalVolume::AddPlayerL(CMMAPlayer* aPlayer)
       
    74 {
       
    75     // Find if the player has a volume control
       
    76     CMMAVolumeControl* control = FindVolumeControl(aPlayer);
       
    77 
       
    78     // Ignore adding new player if it does not support volume control
       
    79     if (control)
       
    80     {
       
    81         TMMAPlayerHolder playerHolder;
       
    82         // Create new volume control and level index pair
       
    83         playerHolder.iVolumeControl = control;
       
    84         playerHolder.iVolumeIndex = control->AddLevelL();
       
    85         playerHolder.iPlayer = aPlayer;
       
    86         // Set current volume level for the control
       
    87         control->SetVolumeLevelL(playerHolder.iVolumeIndex, iLevel);
       
    88         // Add created pair to the control list
       
    89         iControlList.AppendL(playerHolder);
       
    90     }
       
    91 }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // CMMAGlobalVolume::RemovePlayer
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void CMMAGlobalVolume::RemovePlayer(CMMAPlayer* aPlayer)
       
    98 {
       
    99     // Find if the player has a volume control
       
   100     CMMAVolumeControl* control = FindVolumeControl(aPlayer);
       
   101 
       
   102     // Ignore adding new player if it does not support volume control
       
   103     if (control)
       
   104     {
       
   105         // Check that if this type of volume control can be found from
       
   106         // the control list and remove it
       
   107         TInt count(iControlList.Count());
       
   108         for (TInt i(0); i < count; i++)
       
   109         {
       
   110             const TMMAPlayerHolder& holder = iControlList[ i ];
       
   111             if (control == holder.iVolumeControl)
       
   112             {
       
   113                 iControlList.Remove(i);
       
   114                 break;
       
   115             }
       
   116         }
       
   117     }
       
   118 }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CMMAGlobalVolume::VolumeUp
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 void CMMAGlobalVolume::VolumeUp()
       
   125 {
       
   126     DEBUG_INT("THREADID = %d : CMMAGlobalVolume: VolumeUp: +", RThread().Id());
       
   127     // Adjust volume if midlet is in foreground and the volume level value
       
   128     // is not too high, in this case it cannot be set over KMMAVolumeMaxLevel
       
   129     if (iForeground->IsForeground() && (iLevel < KMMAVolumeMaxLevel))
       
   130     {
       
   131         DEBUG("CMMAGlobalVolume: VolumeUp: Volume up");
       
   132         // Check that the current volume level is not increased too much
       
   133         TInt level =
       
   134             iLevel > (KMMAVolumeMaxLevel - KMMAVolumeLevelStep) ?
       
   135             KMMAVolumeMaxLevel - iLevel : iLevel + KMMAVolumeLevelStep;
       
   136         // Increase level by new value
       
   137         SetControlVolumeLevels(level);
       
   138     }
       
   139     DEBUG("CMMAGlobalVolume: VolumeUp: -");
       
   140 }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CMMAGlobalVolume::VolumeDown
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CMMAGlobalVolume::VolumeDown()
       
   147 {
       
   148     DEBUG_INT("THREADID = %d : CMMAGlobalVolume: VolumeDown: +", RThread().Id());
       
   149     // Adjust volume if midlet is in foreground and the volume value
       
   150     // is not too low, in this case it cannot be set under zero
       
   151     if (iForeground->IsForeground() && (iLevel > 0))
       
   152     {
       
   153         DEBUG("CMMAGlobalVolume: VolumeDown: Volume down");
       
   154         // Check that the currnet volume level is not decreased too much
       
   155         TInt level =
       
   156             iLevel < KMMAVolumeLevelStep ?
       
   157             0 : iLevel - KMMAVolumeLevelStep;
       
   158         // Decrease level by new value
       
   159         SetControlVolumeLevels(level);
       
   160     }
       
   161     DEBUG("CMMAGlobalVolume: VolumeDown: -");
       
   162 }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CMMAGlobalVolume::FindVolumeControl
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 CMMAVolumeControl* CMMAGlobalVolume::FindVolumeControl(CMMAPlayer* aPlayer)
       
   169 {
       
   170     TInt count(aPlayer->ControlCount());
       
   171     for (TInt i(0); i < count; i++)
       
   172     {
       
   173         CMMAControl* control = aPlayer->Control(i);
       
   174         // Check that if this control supports volume control
       
   175         if (control->ClassName() == KMMAVolumeControlName)
       
   176         {
       
   177             return static_cast< CMMAVolumeControl* >(control);
       
   178         }
       
   179     }
       
   180     // Does not support volume control
       
   181     return NULL;
       
   182 }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // CMMAGlobalVolume::SetControlVolumeLevels
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CMMAGlobalVolume::SetControlVolumeLevels(TInt aLevel)
       
   189 {
       
   190     TInt count(iControlList.Count());
       
   191     // Adjust volume for all current volume controls associated
       
   192     for (TInt i(0); i < count; i++)
       
   193     {
       
   194         const TMMAPlayerHolder& hdr = iControlList[ i ];
       
   195         // Set new volume level for this control
       
   196         TRAPD(error,
       
   197               hdr.iVolumeControl->SetVolumeLevelL(hdr.iVolumeIndex, aLevel));
       
   198         if (error != KErrNone)
       
   199         {
       
   200             hdr.iPlayer->PostStringEvent(
       
   201                 CMMAPlayerEvent::EError,
       
   202                 KMMAGlobalVolumeSetError());
       
   203         }
       
   204     }
       
   205 
       
   206     iLevel = aLevel;
       
   207     DEBUG_INT(
       
   208         "CMMAGlobalVolume::SetControlVolumeLevels - iLevel = %d", iLevel);
       
   209 
       
   210     // Store new volume to MMA global settings. Error cannot be reported
       
   211     // in any sophisticated way so we just have to ignore it. Debug builds
       
   212     // may panic in this case
       
   213     TInt error = iSettingsStore->Set(KMobileMediaVolumeLevel, aLevel);
       
   214     __ASSERT_DEBUG(error == KErrNone, User::Invariant());
       
   215 }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CMMAGlobalVolume::CMMAGlobalVolume
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 CMMAGlobalVolume::CMMAGlobalVolume(CMMAForeground* aForeground) :
       
   222         iLevel(KMMAVolumeMaxLevel),
       
   223         iForeground(aForeground)
       
   224 {
       
   225 }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // CMMAGlobalVolume::ConstructL
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void CMMAGlobalVolume::ConstructL()
       
   232 {
       
   233     iVolumeKeysListener = CMMAVolumeKeysListener::NewL(this);
       
   234     iSettingsStore = CRepository::NewL(KCRUidMobileMedia);
       
   235 
       
   236     // Get level from the settings store
       
   237     User::LeaveIfError(iSettingsStore->Get(KMobileMediaVolumeLevel, iLevel));
       
   238 }
       
   239 
       
   240 // End of file