mpxmusicplayer/mediakeyhandler/src/mpxmediakeyhandlerimp.cpp
branchRCL_3
changeset 53 3de6c4cf6b67
equal deleted inserted replaced
52:14979e23cb5e 53:3de6c4cf6b67
       
     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:  Implementation of media key handler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <bldvariant.hrh>
       
    21 #include <remconcoreapitarget.h>
       
    22 #include <remconcoreapitargetobserver.h>
       
    23 #include <remconinterfaceselector.h>
       
    24 #include <aknconsts.h>
       
    25 #include <AknUtils.h>
       
    26 #include <eikenv.h>
       
    27 #include <bautils.h>
       
    28 #include <StringLoader.h>
       
    29 #include <apgwgnam.h>
       
    30 #include <utf.h>
       
    31 
       
    32 #ifdef UPNP_INCLUDED
       
    33 #include <upnpcopycommand.h>  
       
    34 #endif
       
    35 
       
    36 
       
    37 #include <aknnotewrappers.h>  // AknErrorNote
       
    38 #include <AknVolumePopup.h>
       
    39 
       
    40 #include <mpxplaybackutility.h>
       
    41 #include <mpxplaybackmessage.h>
       
    42 #include <data_caging_path_literals.hrh>
       
    43 #include <mpxmediakeyhandler.rsg>
       
    44 #include <mpxmedia.h>
       
    45 #include <mpxmessagegeneraldefs.h>
       
    46 #include <mpxplaybackmessagedefs.h>
       
    47 #include <mpxmediageneraldefs.h>
       
    48 #include <mpxuser.h>
       
    49 #include <mpxmediakeyhandler.h>
       
    50 #include <mpxlog.h>
       
    51 #include <layoutmetadata.cdl.h>
       
    52 #include <mpxconstants.h>
       
    53 
       
    54 #include "mpxmetadatahandler.h"
       
    55 #include <mpxplaybackframeworkdefs.h>
       
    56 #include <sounddevice.h>
       
    57 #include "mpxmediakeyhandlerimp.h"
       
    58 #include "mpxremconkeyresponse.h"
       
    59 #include "mpxnotifierdialog.h"
       
    60 
       
    61 
       
    62 // CONSTANTS
       
    63 const TInt KFirstTimerExpiryInterval( 1 ); // Expire immediately
       
    64 const TInt KTimerExpiryInterval( KAknStandardKeyboardRepeatRate );
       
    65 const TInt KMPXOneSecInMilliSecs( 1000 );
       
    66 const TInt KMPXMinVolume(0);
       
    67 const TInt KMPXMaxVolume(100);		// Max volume used in volume popup
       
    68 const TInt KMPXVolumeSteps(1);
       
    69 const TInt KTenStepsVolume = 10;   
       
    70 const TInt KTwentyStepsVolume = 20;
       
    71 const TInt KVolumePopupSynchInterval = 300000; // 300 ms
       
    72 const TInt KFilterEventInterval( 6000 ); //6ms
       
    73 const TRemConCoreApiOperationId KSupportedCoreFeatures[] = {
       
    74         ERemConCoreApiVolumeUp,
       
    75         ERemConCoreApiVolumeDown,
       
    76         ERemConCoreApiPlay,
       
    77         ERemConCoreApiStop,
       
    78         ERemConCoreApiPause,
       
    79         ERemConCoreApiRewind,
       
    80         ERemConCoreApiFastForward,
       
    81         ERemConCoreApiForward,
       
    82         ERemConCoreApiBackward,
       
    83         ERemConCoreApiPausePlayFunction
       
    84 };
       
    85 
       
    86 _LIT( KMPXMediaKeyHandlerRscPath, "mpxmediakeyhandler.rsc" );
       
    87 
       
    88 
       
    89 // ======== MEMBER FUNCTIONS ========
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // C++ default constructor can NOT contain any code, that
       
    93 // might leave.
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 CMPXMediaKeyHandlerImp::CMPXMediaKeyHandlerImp(
       
    97     MMPXMediaKeyHandlerObserver* aObserver ) :
       
    98     iObserver( aObserver ),
       
    99     iEnable( ETrue ),
       
   100     iCurrentVol( KErrNotFound ),
       
   101     iVolumeSteps(KMPXMaxVolume),
       
   102     iPreviousVol( KErrNotFound )
       
   103     {
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // Symbian 2nd phase constructor can leave.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CMPXMediaKeyHandlerImp::ConstructL(
       
   111 	TMPXMediaKeyPopupFlags aFlags )
       
   112     {
       
   113 	ASSERT( iObserver );
       
   114     CCoeEnv* coeEnv = CEikonEnv::Static();
       
   115     TParse parse;
       
   116     parse.Set( KMPXMediaKeyHandlerRscPath, &KDC_APP_RESOURCE_DIR, NULL );
       
   117     TFileName resourceFile( parse.FullName() );
       
   118     User::LeaveIfError( MPXUser::CompleteWithDllPath( resourceFile ) );
       
   119     BaflUtils::NearestLanguageFile( coeEnv->FsSession(), resourceFile );
       
   120     iResourceOffset = coeEnv->AddResourceFileL( resourceFile );
       
   121 
       
   122     // Register to remote control framework
       
   123     iInterfaceSelector = CRemConInterfaceSelector::NewL();
       
   124 
       
   125     // For AVRCP 1.4 support, pass the core features to the target API. 
       
   126     TInt entrySize = sizeof( KSupportedCoreFeatures[0] );
       
   127     TRemConCoreApiOperationId* entries = (TRemConCoreApiOperationId*) &KSupportedCoreFeatures[0];
       
   128     TInt count = sizeof( KSupportedCoreFeatures ) / entrySize;
       
   129     RArray<TRemConCoreApiOperationId> coreFeatures( entrySize, entries, count );
       
   130     iCoreTarget = CRemConCoreApiTarget::NewL( *iInterfaceSelector, *this, coreFeatures );
       
   131 
       
   132     // For handling AVRCP 1.3 metadata
       
   133     iMetaDataHandler = MMPXMetaDataHandler::NewL(*iInterfaceSelector);
       
   134     
       
   135     // For AVRCP 1.4 support, make the player named. 
       
   136     HBufC* avsrcname = StringLoader::LoadLC( R_MPX_AVSRC_NAME_TEXT );
       
   137     HBufC8* avsrcname8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L( avsrcname->Des() );
       
   138     CleanupStack::PushL( avsrcname8 );
       
   139     iInterfaceSelector->OpenTargetL(ERemConAudioPlayer, ERemConNoSubType, *avsrcname8);
       
   140     CleanupStack::PopAndDestroy( avsrcname8 );
       
   141     CleanupStack::PopAndDestroy( avsrcname );
       
   142     iResponseHandler = CMPXRemConKeyResponse::NewL( *iCoreTarget );
       
   143 
       
   144     // Timer for implementing repeat
       
   145     iTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   146 
       
   147     // Get the playback utility instance from engine.
       
   148     iPlaybackUtility = MMPXPlaybackUtility::UtilityL( KPbModeDefault );
       
   149     iPlaybackUtility->AddObserverL( *this );
       
   150     // get the current Volume and Max Volume
       
   151     iPlaybackUtility->PropertyL(*this, EPbPropertyVolume);
       
   152     iPlaybackUtility->PropertyL(*this, EPbPropertyMute);
       
   153     CMMFDevSound* devsound = CMMFDevSound::NewL();	
       
   154     MPX_DEBUG2( "CMPXMediaKeyHandlerImp::Constructdevsound->MaxVolume()%d",devsound->MaxVolume());
       
   155     iVolumeSteps = KTenStepsVolume; //Default 10 steps
       
   156     if ( devsound && devsound->MaxVolume() >= KTwentyStepsVolume ) 
       
   157         {
       
   158          // set 20-steps volume
       
   159          iVolumeSteps = KTwentyStepsVolume;
       
   160         }    
       
   161     delete devsound;
       
   162     if ( aFlags & EDisplayVolumePopup )
       
   163         {
       
   164         // Popup volume control
       
   165         iVolPopup = CAknVolumePopup::NewL(NULL, ETrue);
       
   166         iVolPopup->SetObserver(this);
       
   167         iVolPopup->SetRange(KMPXMinVolume, iVolumeSteps);
       
   168         iVolPopup->SetStepSize(KMPXVolumeSteps);
       
   169         // Ensure that initial value is set to popup. If not initialized, CAknVolumePopup::Value() returns 0 
       
   170         // but the control appears unmuted and allows volume to be decreased out of range (panics also with Avkon 6 in UDEB)
       
   171         iVolPopup->SetValue( 0 );
       
   172         
       
   173         HBufC* popupText = StringLoader::LoadLC( R_MPX_VOLUME_POPUP_TEXT );
       
   174         iVolPopup->SetTitleTextL( *popupText );
       
   175         CleanupStack::PopAndDestroy( popupText );
       
   176         }
       
   177 
       
   178     if ( aFlags & EDisplayMediaPopup )
       
   179         {
       
   180         // Playback popup
       
   181         iPlaybackPopup = CMPXNotifierDialog::NewL();
       
   182         }
       
   183 
       
   184     MMPXSource* s = iPlaybackUtility->Source();
       
   185     if ( s )
       
   186         {
       
   187         RArray<TMPXAttribute> attrs;
       
   188         CleanupClosePushL(attrs);
       
   189         attrs.Append( KMPXMediaGeneralTitle );
       
   190         s->MediaL( attrs.Array(), *this );
       
   191         CleanupStack::PopAndDestroy( &attrs );
       
   192         }
       
   193     else
       
   194         {
       
   195         // No current track.
       
   196         // This is constructed so that the first popup will display
       
   197         // it doesn't hold anything.
       
   198         iTrackTitle = HBufC::NewL( 1 );
       
   199         }
       
   200 #ifdef UPNP_INCLUDED
       
   201     if (!iUpnpCopyCommand )
       
   202         {
       
   203         MPX_TRAPD ( error, iUpnpCopyCommand = CUpnpCopyCommand::NewL() );   
       
   204 		    if ( error == KErrNone )
       
   205             {
       
   206             iUpnpFrameworkSupport = ETrue;
       
   207             }
       
   208         else
       
   209             {
       
   210             iUpnpFrameworkSupport = EFalse;
       
   211 			iUpnpCopyCommand = NULL;
       
   212             }
       
   213         }
       
   214 #endif
       
   215     iVolumePopupSynchTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   216     iFilterEventTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // Two-phased constructor.
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 MMPXMediaKeyHandler* CMPXMediaKeyHandlerImp::NewL(
       
   224     TMPXMediaKeyPopupFlags aFlags,
       
   225     MMPXMediaKeyHandlerObserver* aObserver )
       
   226     {
       
   227     CMPXMediaKeyHandlerImp* self =
       
   228         new(ELeave)CMPXMediaKeyHandlerImp( aObserver );
       
   229     CleanupStack::PushL( self );
       
   230     self->ConstructL( aFlags );
       
   231     CleanupStack::Pop();
       
   232 
       
   233     return self;
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // Destructor
       
   238 // ---------------------------------------------------------------------------
       
   239 //
       
   240 CMPXMediaKeyHandlerImp::~CMPXMediaKeyHandlerImp()
       
   241     {
       
   242 	if ( iFilterEventTimer )
       
   243 		{
       
   244 	    iFilterEventTimer->Cancel();
       
   245         delete iFilterEventTimer;
       
   246 		}
       
   247     if ( iVolumePopupSynchTimer )
       
   248         {
       
   249         iVolumePopupSynchTimer->Cancel();
       
   250         delete iVolumePopupSynchTimer;
       
   251         }
       
   252     if ( iPlaybackUtility )
       
   253         {
       
   254         TRAP_IGNORE( iPlaybackUtility->RemoveObserverL( *this ) );
       
   255         iPlaybackUtility->Close();
       
   256         }
       
   257 
       
   258     delete iMetaDataHandler;
       
   259 
       
   260     delete iResponseHandler;
       
   261     delete iInterfaceSelector;
       
   262 
       
   263     if( iTimer )
       
   264         {
       
   265         iTimer->Cancel();
       
   266         delete iTimer;
       
   267         }
       
   268 
       
   269     if ( iResourceOffset )
       
   270         {
       
   271         CEikonEnv::Static()->DeleteResourceFile( iResourceOffset );
       
   272         }
       
   273 
       
   274     delete iTrackTitle;
       
   275     delete iVolPopup;
       
   276     delete iPlaybackPopup;
       
   277 #ifdef UPNP_INCLUDED
       
   278     if ( iUpnpCopyCommand)
       
   279         {
       
   280         delete iUpnpCopyCommand;
       
   281         }
       
   282 #endif
       
   283     }
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // Filter and send command to playback utility.
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 void CMPXMediaKeyHandlerImp::FilterAndSendCommand(
       
   290     TMPXPlaybackCommand aCommandId )
       
   291     {
       
   292     MPX_FUNC( "CMPXMediaKeyHandlerImp::FilterAndSendCommand" );
       
   293     TRAP_IGNORE( DoFilterAndSendCommandL( aCommandId ) );
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // Help filter and send command to playback utility.
       
   298 // ---------------------------------------------------------------------------
       
   299 //
       
   300 void CMPXMediaKeyHandlerImp::DoFilterAndSendCommandL(
       
   301     TMPXPlaybackCommand aCommandId )
       
   302     {
       
   303     MPX_FUNC( "CMPXMediaKeyHandlerImp::DoFilterAndSendCommandL" );
       
   304     // Only send media key events if the media key component is enabled
       
   305     if( iEnable )
       
   306         {
       
   307         TBool forwardCommand = ETrue;
       
   308         if ( iUpnpFrameworkSupport )
       
   309             {
       
   310             switch ( aCommandId )
       
   311                 {
       
   312                 case EPbCmdStartSeekForward:
       
   313                 case EPbCmdStartSeekBackward:
       
   314                     {
       
   315                     if ( IsUpnpVisibleL() )
       
   316                         {
       
   317                         TFileName subPlayerName;
       
   318                         TMPXPlaybackPlayerType currentPlayerType = EPbLocal;
       
   319                         GetSubPlayerInfoL(subPlayerName, currentPlayerType);
       
   320 
       
   321                         if ( currentPlayerType != EPbLocal)
       
   322                             {
       
   323                             //show error note
       
   324                             HBufC* dialogText = StringLoader::LoadLC(R_MPX_MEDIA_KEY_NOT_SUPPORTED);
       
   325                             CAknErrorNote* errNote = new(ELeave) CAknErrorNote(ETrue);
       
   326                             errNote->SetTimeout(CAknNoteDialog::ELongTimeout);
       
   327                             errNote->ExecuteLD( *dialogText );
       
   328                             CleanupStack::PopAndDestroy( dialogText );
       
   329                             forwardCommand = EFalse;
       
   330                             }
       
   331                         }
       
   332                     break;
       
   333                     }
       
   334                 default:
       
   335                     {
       
   336                     //do nothing
       
   337                     break;
       
   338                     }
       
   339                 }
       
   340             }
       
   341 
       
   342         if ( forwardCommand )
       
   343             {
       
   344             TInt volume(0);
       
   345             TFileName subPlayerName;
       
   346             TMPXPlaybackPlayerType currentPlayerType = EPbLocal;
       
   347             GetSubPlayerInfoL(subPlayerName, currentPlayerType);
       
   348             
       
   349         	if ( currentPlayerType == EPbRemote )
       
   350                 {
       
   351         	    volume = iUpnpVolume % ( KPbPlaybackVolumeLevelMax / iVolumeSteps )
       
   352             	                    + iCurrentVol * KPbPlaybackVolumeLevelMax / iVolumeSteps;
       
   353         	    }
       
   354         	else
       
   355         	    {
       
   356         	    volume = iCurrentVol * KPbPlaybackVolumeLevelMax/iVolumeSteps;
       
   357                 iPreviousVol = iCurrentVol;
       
   358         	    }
       
   359             
       
   360             if ( volume < KMPXMinVolume )
       
   361                 {
       
   362                 volume = KMPXMinVolume;
       
   363                 }
       
   364             if ( volume > KMPXMaxVolume )
       
   365                 {
       
   366                 volume = KMPXMaxVolume;
       
   367                 }
       
   368             
       
   369             MPX_DEBUG3( "CMPXMediaKeyHandlerImp::DoFilterAndSendCommandL: aCommandId(%d), volume(%d)" , aCommandId, volume );
       
   370             iObserver->HandleMediaKeyCommand( aCommandId, volume );
       
   371             }
       
   372         }
       
   373     }
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // Handle playback message.
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 void CMPXMediaKeyHandlerImp::DoHandlePlaybackMessageL(
       
   380     const CMPXMessage& aMessage )
       
   381     {
       
   382     MPX_FUNC( "CMPXMediaKeyHandlerImp::DoHandlePlaybackMessageL(CMPXMessage)");
       
   383 
       
   384     TMPXMessageId id( aMessage.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
       
   385     if ( KMPXMessagePbMediaChanged == id )
       
   386         {
       
   387         if ( aMessage.IsSupported( KMPXMessagePbMedia ) )
       
   388             {
       
   389             CMPXMedia* media( aMessage.Value<CMPXMedia>( KMPXMessagePbMedia ) );
       
   390             User::LeaveIfNull( media );
       
   391             iPlaybackPosition = 0;
       
   392             DoHandleMediaL( *media, KErrNone );
       
   393             }
       
   394         }
       
   395     else if ( KMPXMessageGeneral == id )
       
   396         {
       
   397         TInt type( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralType ));
       
   398         TInt data( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralData ));
       
   399         switch ( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralEvent ) )
       
   400             {
       
   401             case TMPXPlaybackMessage::EPropertyChanged:
       
   402                 {
       
   403                 TMPXPlaybackProperty property(
       
   404                     static_cast<TMPXPlaybackProperty>( type ) );
       
   405                 TInt error( KErrNone );
       
   406 
       
   407                 DoHandlePropertyL( property, data, error );
       
   408                 break;
       
   409                 }
       
   410             case TMPXPlaybackMessage::EStateChanged:
       
   411                 {
       
   412                 MPX_DEBUG2( "CMPXMediaKeyHandlerImp::HandlePlaybackMessageL - EStateChanged(%d)", type );
       
   413 
       
   414                 TMPXPlaybackState state =
       
   415                     static_cast<TMPXPlaybackState>( type );
       
   416                 DoHandleStateChangedL( state );
       
   417                 break;
       
   418                 }
       
   419             case TMPXPlaybackMessage::EMediaChanged:
       
   420                 {
       
   421                 MMPXSource* s = iPlaybackUtility->Source();
       
   422                 if ( s )
       
   423                     {
       
   424                     RArray<TMPXAttribute> attrs;
       
   425                     CleanupClosePushL(attrs);
       
   426                     attrs.Append( KMPXMediaGeneralTitle );
       
   427                     MPX_DEBUG1( "CMPXMediaKeyHandlerImp::HandlePlaybackMessageL Media changed, calling MediaL to refresh" );
       
   428                     s->MediaL( attrs.Array(), *this );
       
   429                     CleanupStack::PopAndDestroy( &attrs );
       
   430                     }
       
   431                 break;
       
   432                 }
       
   433             case TMPXPlaybackMessage::ECommandReceived:
       
   434                 {
       
   435                 MPX_DEBUG2( "CMPXMediaKeyHandlerImp::HandlePlaybackMessageL - ECommandReceived(%d)", type );
       
   436                 break;
       
   437                 }
       
   438             case TMPXPlaybackMessage::ESkipping:
       
   439                 {
       
   440                 iSkipping = ETrue;
       
   441                 break;
       
   442                 }
       
   443             case TMPXPlaybackMessage::ESkipEnd:
       
   444                 {
       
   445                 iSkipping = EFalse;
       
   446                 break;
       
   447                 }
       
   448             default:
       
   449                 {
       
   450                 break;
       
   451                 }
       
   452             }
       
   453         }
       
   454     }
       
   455 
       
   456 // ---------------------------------------------------------------------------
       
   457 // Handle playback property.
       
   458 // ---------------------------------------------------------------------------
       
   459 //
       
   460 void CMPXMediaKeyHandlerImp::DoHandlePropertyL(
       
   461     TMPXPlaybackProperty aProperty,
       
   462     TInt aValue,
       
   463     TInt aError )
       
   464     {
       
   465     MPX_FUNC( "CMPXMediaKeyHandlerImp::DoHandlePropertyL" );
       
   466     MPX_DEBUG4( "CMPXMediaKeyHandlerImp::HandlePropertyL - Property(%d); Value(%d); Error(%d)", aProperty, aValue, aError );
       
   467 
       
   468     iUpnpVolume = aValue; 
       
   469     if ( KErrNone == aError )
       
   470         {
       
   471         switch ( aProperty	)
       
   472             {
       
   473             case EPbPropertyPosition:
       
   474                 {
       
   475                 iPlaybackPosition = aValue / KMPXOneSecInMilliSecs;
       
   476                 ShowPlaybackPopupL( EMPXPopupShowIfVisible );
       
   477                 break;
       
   478                 }
       
   479             case EPbPropertyMute:
       
   480             	{
       
   481             	iMuted = aValue;
       
   482             	StartVolumePopupSynchTimer();
       
   483 				break;
       
   484 				}
       
   485 			case EPbPropertyVolume:
       
   486                 {
       
   487                 // Avkon Volume has 10 steps, but the rest of the framework
       
   488                 // has 100 steps. Need to scale it to 10 steps.
       
   489                 if ( aValue > 0 )
       
   490                     {
       
   491                     aValue = aValue * iVolumeSteps;
       
   492                     aValue = aValue / KPbPlaybackVolumeLevelMax;
       
   493                     if ( aValue > iVolumeSteps )
       
   494                         {
       
   495                         aValue = iVolumeSteps;
       
   496                         }
       
   497                     }
       
   498                 
       
   499                 //if aValue equal to iPreviousVol, MediaKey should not set Volume again.
       
   500                 TBool OkToSetVolume = ( aValue != iPreviousVol );
       
   501                 iPreviousVol = KErrNotFound;
       
   502                 
       
   503                 if( iMuted && aValue > 0  ) // unmute
       
   504                     {
       
   505                     iMuted = EFalse;
       
   506                     iCurrentVol = aValue;
       
   507                     }
       
   508                 else if( aValue == 0 ) // mute
       
   509                     {
       
   510                     if( !iMuted ) 
       
   511                         {
       
   512                         iMuted = ETrue;
       
   513                         }
       
   514                     }
       
   515                 else if ( aValue != iCurrentVol )
       
   516                     {
       
   517                     if ( aValue != 0 && OkToSetVolume && ( iResponseHandler->iCountOfVolumeCommands == 0 ) )
       
   518                         {
       
   519                         // if we are processing remcon events we shouldn't change the current volume value                      
       
   520                         iCurrentVol = aValue;
       
   521                         }
       
   522                     }
       
   523                 
       
   524                 if ( iCurrentVol == KErrNotFound ) // muted by some other application before launching Music Player
       
   525                     {
       
   526                     iCurrentVol = aValue;
       
   527                     }
       
   528                 
       
   529                 if ( OkToSetVolume )
       
   530                 	{
       
   531                     StartVolumePopupSynchTimer();
       
   532                 	}
       
   533                 
       
   534                 // send a command to UI to display Volume bar on device when controlling volume via UPnP  
       
   535                 if ( IsUpnpVisibleL() && iPlayerState != EPbStateNotInitialised )
       
   536                     {
       
   537                     TFileName subPlayerName;
       
   538                     TMPXPlaybackPlayerType currentPlayerType = EPbLocal;
       
   539                     GetSubPlayerInfoL( subPlayerName, currentPlayerType );
       
   540 
       
   541                     if ( currentPlayerType != EPbLocal )
       
   542                         {
       
   543                         iObserver->HandleMediaKeyCommand( EPbCmdSetVolume, iUpnpVolume ); 
       
   544                         }            
       
   545                     }
       
   546                           
       
   547                 if ( iUpnpFrameworkSupport )
       
   548                     {
       
   549                     SetVolumePopupTitleL();
       
   550                     }
       
   551 					
       
   552 				break;
       
   553                 }
       
   554             default:
       
   555                 {
       
   556                 break;
       
   557                 }
       
   558             }
       
   559         }
       
   560     else
       
   561         {
       
   562         switch ( aProperty	)
       
   563             {
       
   564             case EPbPropertyVolume:
       
   565             case EPbPropertyMute:
       
   566                 {
       
   567                 if ( iVolPopup && iShowPopups & EDisplayVolumePopup )
       
   568                     {
       
   569                     //show error note
       
   570                     HBufC* dialogText = StringLoader::LoadLC(R_MPX_VOLUME_KEYS_NOT_SUPPORTED);
       
   571                     CAknErrorNote* errNote = new(ELeave) CAknErrorNote(ETrue);
       
   572                     errNote->SetTimeout(CAknNoteDialog::ELongTimeout);
       
   573                     errNote->ExecuteLD( *dialogText );
       
   574                     CleanupStack::PopAndDestroy( dialogText );
       
   575                     }
       
   576                 break;
       
   577                 }
       
   578             default:
       
   579                 {
       
   580                 break;
       
   581                 }
       
   582             }
       
   583         }
       
   584     }
       
   585 
       
   586 // ---------------------------------------------------------------------------
       
   587 // Handle media properties.
       
   588 // Notes: The client is responsible for delete the object of aProperties.
       
   589 // ---------------------------------------------------------------------------
       
   590 //
       
   591 void CMPXMediaKeyHandlerImp::DoHandleMediaL(
       
   592     const CMPXMedia& aMedia,
       
   593     TInt aError )
       
   594     {
       
   595     MPX_FUNC( "CMPXMediaKeyHandlerImp::DoHandleMediaL" );
       
   596 
       
   597     // Delete old title no matter there's error or not
       
   598     if ( iTrackTitle )
       
   599         {
       
   600         delete iTrackTitle;
       
   601         iTrackTitle = NULL;
       
   602         }
       
   603 
       
   604     if ( KErrNone == aError )
       
   605         {
       
   606         // Keep track of the current track's title
       
   607         iTrackTitle = aMedia.ValueText( KMPXMediaGeneralTitle ).AllocL();
       
   608 
       
   609         // Update popup if visible
       
   610         if ( iShowPopups & EDisplayMediaPopup )
       
   611             {
       
   612             MPX_DEBUG1( "CMPXMediaKeyHandlerImp::DoHandleMediaL EMPXPopupTimeout" );
       
   613             ShowPlaybackPopupL( EMPXPopupTimeout );
       
   614             }
       
   615         else
       
   616             {
       
   617             ShowPlaybackPopupL( EMPXPopupShowIfVisible );
       
   618             }
       
   619         }
       
   620     }
       
   621 
       
   622 // ---------------------------------------------------------------------------
       
   623 // Handle playback state changed.
       
   624 // ---------------------------------------------------------------------------
       
   625 //
       
   626 void CMPXMediaKeyHandlerImp::DoHandleStateChangedL(
       
   627     TMPXPlaybackState aState )
       
   628     {
       
   629     MPX_FUNC( "CMPXMediaKeyHandlerImp::DoHandleStateChangedL" );
       
   630     switch ( aState )
       
   631         {
       
   632         case EPbStateStopped:
       
   633             {
       
   634             iPlayerState = aState;
       
   635             ShowPlaybackPopupL( EMPXPopupShowIfVisible );
       
   636             break;
       
   637             }
       
   638         case EPbStatePlaying:
       
   639         case EPbStatePaused:
       
   640             {
       
   641             iPlayerState = aState;
       
   642             // Start the time if needed, otherwise, update content if visible
       
   643             if ( iShowPopups & EDisplayMediaPopup )
       
   644                 {
       
   645                 MPX_DEBUG1( "CMPXMediaKeyHandlerImp::DoHandleStateChangedL EMPXPopupTimeout" );
       
   646                 ShowPlaybackPopupL( EMPXPopupTimeout );
       
   647                 }
       
   648             else
       
   649                 {
       
   650                 ShowPlaybackPopupL( EMPXPopupShowIfVisibleRestartTimer );
       
   651                 }
       
   652             break;
       
   653             }
       
   654         default:
       
   655             {
       
   656             // do nothing
       
   657             break;
       
   658             }
       
   659         }
       
   660     }
       
   661 
       
   662 // ---------------------------------------------------------------------------
       
   663 // Callback for timer
       
   664 // ---------------------------------------------------------------------------
       
   665 //
       
   666 TInt CMPXMediaKeyHandlerImp::TimerCallback( TAny* aPtr )
       
   667     {
       
   668     static_cast<CMPXMediaKeyHandlerImp*>( aPtr )->HandleRepeatEvent();
       
   669     return KErrNone;
       
   670     }
       
   671 
       
   672 // ---------------------------------------------------------------------------
       
   673 // Handle repeat event
       
   674 // ---------------------------------------------------------------------------
       
   675 //
       
   676 void CMPXMediaKeyHandlerImp::HandleRepeatEvent()
       
   677     {
       
   678 	UpdateVolume();
       
   679     FilterAndSendCommand( iCommandId );
       
   680     }
       
   681 
       
   682 // -----------------------------------------------------------------------------
       
   683 // CMPXMediaKeyHandlerImp::GetSubPlayerInfoL
       
   684 // Retrieves the current player name and sets the volume popup title
       
   685 // -----------------------------------------------------------------------------
       
   686 //
       
   687 void CMPXMediaKeyHandlerImp::GetSubPlayerInfoL(
       
   688     TDes& aSubPlayerName,
       
   689     TMPXPlaybackPlayerType& aCurrentPlayerType )
       
   690     {
       
   691     aCurrentPlayerType = EPbLocal;
       
   692     aSubPlayerName = KNullDesC;
       
   693 
       
   694 //#ifdef __UPNP_FRAMEWORK_2_0_
       
   695     if ( iUpnpFrameworkSupport )
       
   696         {
       
   697         MMPXPlayerManager& manager = iPlaybackUtility->PlayerManager();
       
   698         TUid currentlyUsedPlayer;
       
   699         TInt currentlyUsedSubPlayer;
       
   700         HBufC* subPlayerName = NULL;
       
   701         TRAP_IGNORE( manager.GetSelectionL( aCurrentPlayerType,
       
   702                                             currentlyUsedPlayer,
       
   703                                             currentlyUsedSubPlayer,
       
   704                                             subPlayerName ) );
       
   705         if ( subPlayerName )
       
   706             {
       
   707             aSubPlayerName = (*subPlayerName);
       
   708             delete subPlayerName;
       
   709             }
       
   710         }
       
   711 //#endif
       
   712     }
       
   713 
       
   714 // -----------------------------------------------------------------------------
       
   715 // CMPXMediaKeyHandlerImp::SetVolumePopupTitleL
       
   716 // Retrieves the current player name and sets the volume popup title
       
   717 // -----------------------------------------------------------------------------
       
   718 //
       
   719 void CMPXMediaKeyHandlerImp::SetVolumePopupTitleL()
       
   720     {
       
   721     if ( iUpnpFrameworkSupport && IsUpnpVisibleL() )
       
   722         {
       
   723         TFileName subPlayerName;
       
   724         TMPXPlaybackPlayerType currentPlayerType = EPbLocal;
       
   725 
       
   726         GetSubPlayerInfoL(subPlayerName, currentPlayerType);
       
   727         if ( currentPlayerType != EPbLocal)
       
   728             {
       
   729             iVolPopup->SetTitleTextL( subPlayerName );
       
   730             }
       
   731         else  // need to restore original title
       
   732             {
       
   733             HBufC* popupText = StringLoader::LoadLC( R_MPX_VOLUME_POPUP_TEXT );
       
   734             iVolPopup->SetTitleTextL( *popupText );
       
   735             CleanupStack::PopAndDestroy( popupText );
       
   736             }
       
   737         }
       
   738     }
       
   739 
       
   740 // -----------------------------------------------------------------------------
       
   741 // CMPXMediaKeyHandlerImp::IsUpnpVisibleL
       
   742 // Taken from Gallery upnp support implementation
       
   743 // -----------------------------------------------------------------------------
       
   744 //
       
   745 TBool CMPXMediaKeyHandlerImp::IsUpnpVisibleL()
       
   746     {
       
   747     MPX_FUNC( "CMPXMediaKeyHandlerImp::IsUpnpVisible" );
       
   748     TBool returnValue = EFalse;
       
   749 #ifdef UPNP_INCLUDED
       
   750    	if ( iUpnpCopyCommand && iUpnpFrameworkSupport ) 	  
       
   751    	    {
       
   752         returnValue = iUpnpCopyCommand->IsAvailableL();   
       
   753         }
       
   754 #endif
       
   755     return returnValue; 
       
   756     }
       
   757 
       
   758 // ---------------------------------------------------------------------------
       
   759 // Updates the volume and the volume command
       
   760 // ---------------------------------------------------------------------------
       
   761 //
       
   762 void CMPXMediaKeyHandlerImp::UpdateVolume()
       
   763 	{
       
   764 	// Current volume level is not yet initialised
       
   765     if( iCurrentVol < 0 )
       
   766         {
       
   767         MPX_DEBUG1( "CMPXMediaKeyHandlerImp::UpdateVolume not initialised");
       
   768         return;
       
   769         }
       
   770 
       
   771 	if ( iIncreaseVol )
       
   772 		{
       
   773 		if ( iMuted )
       
   774 			{
       
   775 			iMuted = EFalse;
       
   776 			iCommandId = EPbCmdUnMuteVolume;
       
   777             if ( iCurrentVol == 0 ) // prevent muting again when HandleControlEvent is called next time
       
   778                 {
       
   779                 iCurrentVol = 1; // "1" is the first step of 20-step volume
       
   780                 }
       
   781 			iVolPopup->SetValue( iCurrentVol );
       
   782 			}
       
   783 		else
       
   784 			{
       
   785 			iCommandId = EPbCmdSetVolume;
       
   786 			iCurrentVol = iCurrentVol < iVolumeSteps ? (iCurrentVol + 1) : iCurrentVol;
       
   787 			iVolPopup->SetValue( iCurrentVol );
       
   788 			}
       
   789 		}
       
   790 	else
       
   791 		{
       
   792 		if ( iCurrentVol == 1 )
       
   793 			{
       
   794 			iMuted = ETrue;
       
   795 			iCommandId = EPbCmdMuteVolume;
       
   796 			iVolPopup->SetValue( 0 );
       
   797 			}
       
   798 		else if ( iMuted )	// Muted && volume > 1
       
   799 			{
       
   800 			iMuted = EFalse;
       
   801 			iCommandId = EPbCmdUnMuteVolume;
       
   802 			iVolPopup->SetValue( iCurrentVol );
       
   803 			}
       
   804 		else
       
   805 			{
       
   806 			iCommandId = EPbCmdSetVolume;
       
   807 			iCurrentVol = iCurrentVol - 1;
       
   808 			iVolPopup->SetValue( iCurrentVol );
       
   809 			}
       
   810 		}
       
   811 	}
       
   812 
       
   813 // ---------------------------------------------------------------------------
       
   814 // From MMPXMediaKeyHandler
       
   815 // Show playback popup.
       
   816 // ---------------------------------------------------------------------------
       
   817 //
       
   818 void CMPXMediaKeyHandlerImp::ShowPlaybackPopupL(
       
   819     TMPXPlaybackPopupModes aMode )
       
   820     {
       
   821     MPX_FUNC( "CMPXMediaKeyHandlerImp::ShowPlaybackPopupL" );
       
   822 
       
   823     // Check to see is it ok to display popup
       
   824     if ( !iPlaybackPopup || !iTrackTitle )
       
   825         {
       
   826         return;
       
   827         }
       
   828 
       
   829     switch ( aMode )
       
   830         {
       
   831         case EMPXPopupShowIfVisibleRestartTimer:
       
   832         case EMPXPopupTimeout:
       
   833             {
       
   834             iShowPopups &= ~EDisplayMediaPopup;
       
   835             break;
       
   836             }
       
   837         case EMPXPopupNoTimeout:
       
   838             {
       
   839             iShowPopups |= EDisplayMediaPopup;
       
   840             break;
       
   841             }
       
   842         case EMPXPopupShowIfVisible:
       
   843             {
       
   844             if ( iShowPopups & EDisplayMediaPopup )
       
   845                 {
       
   846                 aMode = EMPXPopupNoTimeout;
       
   847                 }
       
   848             break;
       
   849             }
       
   850         default:
       
   851             {
       
   852             break;
       
   853             }
       
   854         }
       
   855 
       
   856     CMPXNotifierDialog::TMPXPlaybackPopupIcons icon(
       
   857         CMPXNotifierDialog::EMPXPopupNoneIcon );
       
   858     TMPXPlaybackState playerState( EPbStateNotInitialised );
       
   859 
       
   860     // Get current playback state from playback utility
       
   861     playerState = iPlaybackUtility->StateL();
       
   862     switch ( playerState )
       
   863         {
       
   864         case EPbStatePlaying:
       
   865             {
       
   866             icon = CMPXNotifierDialog::EMPXPopupPlayIcon;
       
   867             break;
       
   868             }
       
   869         case EPbStatePaused:
       
   870             {
       
   871             icon = CMPXNotifierDialog::EMPXPopupPauseIcon;
       
   872             break;
       
   873             }
       
   874         case EPbStateSeekingForward:
       
   875             icon = CMPXNotifierDialog::EMPXPopupFFIcon;
       
   876             break;
       
   877         case EPbStateSeekingBackward:
       
   878             {
       
   879             icon = CMPXNotifierDialog::EMPXPopupFRIcon;
       
   880             break;
       
   881             }
       
   882         case EPbStateStopped:
       
   883             {
       
   884             // no icon if in stopped state
       
   885             break;
       
   886             }
       
   887         default:
       
   888             {
       
   889             // not to display popup in other states
       
   890             return;
       
   891             }
       
   892         }
       
   893 
       
   894     if ( iTrackTitle->Length() )
       
   895         {
       
   896         MPX_DEBUG2( "CMPXMediaKeyHandlerImp::ShowPlaybackPopupL aMode = %d", aMode );
       
   897         iPlaybackPopup->ShowInfoPopupL(
       
   898             icon, *iTrackTitle, iSkipping ? KErrNotFound : iPlaybackPosition, aMode );
       
   899         }
       
   900     }
       
   901 
       
   902 // ---------------------------------------------------------------------------
       
   903 // From MMPXMediaKeyHandler
       
   904 // Show Volume popup
       
   905 // ---------------------------------------------------------------------------
       
   906 //
       
   907 void CMPXMediaKeyHandlerImp::ShowVolumePopupL()
       
   908     {
       
   909     MPX_FUNC( "CMPXMediaKeyHandlerImp::ShowVolumePopupL" );
       
   910     if ( iVolPopup )
       
   911         {
       
   912         iShowPopups |= EDisplayVolumePopup;
       
   913         iVolPopup->ShowVolumePopupL();
       
   914         }
       
   915     }
       
   916 
       
   917 // ---------------------------------------------------------------------------
       
   918 // From MMPXMediaKeyHandler
       
   919 // Dismiss notifier popup.
       
   920 // ---------------------------------------------------------------------------
       
   921 //
       
   922 void CMPXMediaKeyHandlerImp::DismissNotifier(
       
   923     TMPXMediaKeyPopupFlags aFlags )
       
   924     {
       
   925     MPX_FUNC( "CMPXMediaKeyHandlerImp::DismissNotifier" );
       
   926     if ( aFlags & EDisplayMediaPopup && iPlaybackPopup )
       
   927         {
       
   928         iPlaybackPopup->CancelInfoPopup();
       
   929         iShowPopups &= ~EDisplayMediaPopup;
       
   930         }
       
   931 
       
   932     if ( aFlags & EDisplayVolumePopup && iVolPopup )
       
   933         {
       
   934         iVolPopup->CloseVolumePopup();
       
   935         iShowPopups &= ~EDisplayVolumePopup;
       
   936         }
       
   937     }
       
   938 
       
   939 // ---------------------------------------------------------------------------
       
   940 // From MMPXMediaKeyHandler
       
   941 // Enable or disable media keys
       
   942 // ---------------------------------------------------------------------------
       
   943 //
       
   944 void CMPXMediaKeyHandlerImp::SetEnableMediaKeys( TBool aEnable )
       
   945     {
       
   946     iEnable = aEnable;
       
   947     }
       
   948 
       
   949 // ---------------------------------------------------------------------------
       
   950 // From MMPXMediaKeyHandler
       
   951 // Gets called when orientation change begins
       
   952 // ---------------------------------------------------------------------------
       
   953 //
       
   954 void CMPXMediaKeyHandlerImp::NotifyOrientationChangeBegin()
       
   955 	{
       
   956 	iPlaybackPopup->NotifyOrientationChangeBegin();
       
   957 	}
       
   958 
       
   959 // From MMPXPlaybackObserver
       
   960 // Handle playback message.
       
   961 // ---------------------------------------------------------------------------
       
   962 //
       
   963 void CMPXMediaKeyHandlerImp::HandlePlaybackMessage(
       
   964     CMPXMessage* aMessage, TInt aError )
       
   965     {
       
   966     if ( aError == KErrNone && aMessage )
       
   967         {
       
   968         TRAP_IGNORE( DoHandlePlaybackMessageL( *aMessage ) );
       
   969         }
       
   970     }
       
   971 
       
   972 // ---------------------------------------------------------------------------
       
   973 // From MMPXPlaybackCallback
       
   974 // Handle playback property.
       
   975 // ---------------------------------------------------------------------------
       
   976 //
       
   977 void CMPXMediaKeyHandlerImp::HandlePropertyL(
       
   978     TMPXPlaybackProperty aProperty,
       
   979     TInt aValue,
       
   980     TInt aError )
       
   981     {
       
   982     TRAP_IGNORE( DoHandlePropertyL( aProperty, aValue, aError ) );
       
   983     }
       
   984 
       
   985 // ---------------------------------------------------------------------------
       
   986 // From MMPXPlaybackCallback
       
   987 // Method is called continously until aComplete=ETrue, signifying that
       
   988 // it is done and there will be no more callbacks
       
   989 // Only new items are passed each time
       
   990 // ---------------------------------------------------------------------------
       
   991 //
       
   992 void CMPXMediaKeyHandlerImp::HandleSubPlayerNamesL(
       
   993     TUid /* aPlayer */,
       
   994     const MDesCArray* /* aSubPlayers */,
       
   995     TBool /* aComplete */,
       
   996     TInt /* aError */ )
       
   997     {
       
   998     MPX_FUNC( "CMPXMediaKeyHandlerImp::HandleSubPlayerNamesL" );
       
   999     }
       
  1000 
       
  1001 // ---------------------------------------------------------------------------
       
  1002 // From MMPXPlaybackCallback
       
  1003 // Handle media properties.
       
  1004 // Notes: The client is responsible for delete the object of aMedia.
       
  1005 // ---------------------------------------------------------------------------
       
  1006 //
       
  1007 void CMPXMediaKeyHandlerImp::HandleMediaL(
       
  1008     const CMPXMedia& aMedia,
       
  1009     TInt aError )
       
  1010     {
       
  1011     MPX_FUNC( "CMPXMediaKeyHandlerImp::HandleMediaL" );
       
  1012     TRAP_IGNORE( DoHandleMediaL( aMedia, aError ) );
       
  1013     }
       
  1014 
       
  1015 // ---------------------------------------------------------------------------
       
  1016 // A 'play' command has been received.
       
  1017 // ---------------------------------------------------------------------------
       
  1018 //
       
  1019 void CMPXMediaKeyHandlerImp::MrccatoPlay(
       
  1020     TRemConCoreApiPlaybackSpeed /* aSpeed */,
       
  1021     TRemConCoreApiButtonAction aButtonAct )
       
  1022     {
       
  1023     MPX_DEBUG2( "-->CMPXMediaKeyHandlerImp::MrccatoPlay(aButtonAct=%d)", aButtonAct );
       
  1024 
       
  1025     if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
  1026         ( aButtonAct == ERemConCoreApiButtonPress ) )
       
  1027         {
       
  1028         FilterAndSendCommand( EPbCmdPlay );
       
  1029         }
       
  1030     iResponseHandler->CompleteAnyKey( ERemConCoreApiPlay );
       
  1031     MPX_DEBUG1( "<--CMPXMediaKeyHandlerImp::MrccatoPlay");
       
  1032     }
       
  1033 
       
  1034 // ---------------------------------------------------------------------------
       
  1035 // A command has been received.
       
  1036 // ---------------------------------------------------------------------------
       
  1037 //
       
  1038 void CMPXMediaKeyHandlerImp::MrccatoCommand(
       
  1039     TRemConCoreApiOperationId aOperationId,
       
  1040     TRemConCoreApiButtonAction aButtonAct )
       
  1041     {
       
  1042     MPX_DEBUG3( "CMPXMediaKeyHandlerImp::MrccatoCommand(aOperationId=%d, aButtonAct=%d)", aOperationId, aButtonAct );
       
  1043 
       
  1044     switch (aOperationId)
       
  1045         {
       
  1046         case ERemConCoreApiPausePlayFunction:
       
  1047             {
       
  1048             if ( aButtonAct == ERemConCoreApiButtonClick )
       
  1049                 {
       
  1050                 FilterAndSendCommand( EPbCmdPlayPause );
       
  1051                 }
       
  1052             break;
       
  1053             }
       
  1054         case ERemConCoreApiPlay:
       
  1055             {
       
  1056             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
  1057                 ( aButtonAct == ERemConCoreApiButtonPress ) )
       
  1058                 {
       
  1059                 FilterAndSendCommand( EPbCmdPlay );
       
  1060                 }
       
  1061             break;
       
  1062             }
       
  1063         case ERemConCoreApiStop:
       
  1064             {
       
  1065             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
  1066                 ( aButtonAct == ERemConCoreApiButtonPress ) )
       
  1067                 {
       
  1068                 FilterAndSendCommand( EPbCmdStop );
       
  1069                 }
       
  1070             break;
       
  1071             }
       
  1072         case ERemConCoreApiPause:
       
  1073             {
       
  1074             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
  1075                 ( aButtonAct == ERemConCoreApiButtonPress ) )
       
  1076                 {
       
  1077                 FilterAndSendCommand( EPbCmdPause );
       
  1078                 }
       
  1079             break;
       
  1080             }
       
  1081         case ERemConCoreApiRewind:
       
  1082             {
       
  1083             switch ( aButtonAct )
       
  1084                 {
       
  1085                 case ERemConCoreApiButtonPress:
       
  1086                     {
       
  1087                     FilterAndSendCommand( EPbCmdStartSeekBackward );
       
  1088                     break;
       
  1089                     }
       
  1090                 case ERemConCoreApiButtonRelease:
       
  1091                     {
       
  1092                     FilterAndSendCommand( EPbCmdStopSeeking );
       
  1093                     break;
       
  1094                     }
       
  1095                 default:
       
  1096                     {
       
  1097                     break;
       
  1098                     }
       
  1099                 }
       
  1100             break;
       
  1101             }
       
  1102         case ERemConCoreApiFastForward:
       
  1103             {
       
  1104             switch ( aButtonAct )
       
  1105                 {
       
  1106                 case ERemConCoreApiButtonPress:
       
  1107                     {
       
  1108                     FilterAndSendCommand( EPbCmdStartSeekForward );
       
  1109                     break;
       
  1110                     }
       
  1111                 case ERemConCoreApiButtonRelease:
       
  1112                     {
       
  1113                     FilterAndSendCommand( EPbCmdStopSeeking );
       
  1114                     break;
       
  1115                     }
       
  1116                 default:
       
  1117                     {
       
  1118                     break;
       
  1119                     }
       
  1120                 }
       
  1121             break;
       
  1122             }
       
  1123         case ERemConCoreApiBackward:
       
  1124             {
       
  1125             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
  1126                  ( aButtonAct == ERemConCoreApiButtonRelease ) )
       
  1127                 {
       
  1128                 FilterAndSendCommand( EPbCmdPrevious );
       
  1129                 }
       
  1130             break;
       
  1131             }
       
  1132         case ERemConCoreApiForward:
       
  1133             {
       
  1134             if ( ( aButtonAct == ERemConCoreApiButtonClick ) ||
       
  1135                  ( aButtonAct == ERemConCoreApiButtonRelease ) )
       
  1136                 {
       
  1137                 FilterAndSendCommand( EPbCmdNext );
       
  1138                 }
       
  1139             break;
       
  1140             }
       
  1141         case ERemConCoreApiVolumeUp:
       
  1142         case ERemConCoreApiVolumeDown:
       
  1143             {
       
  1144             iResponseHandler->iCountOfVolumeCommands++;
       
  1145             iTimer->Cancel();
       
  1146             MMPXPlaybackUtility* pbUtil( NULL );
       
  1147             MPX_TRAPD( err, pbUtil = MMPXPlaybackUtility::UtilityL( KPbModeActivePlayer ));
       
  1148             if( err != KErrNone )
       
  1149                 {
       
  1150                 MPX_DEBUG2( "CMPXMediaKeyHandlerImp::MrccatoCommand ERemConCoreApiVolumeDown leave err%d", err );
       
  1151                 break;
       
  1152                 }
       
  1153             TMPXPlaybackState playerState( EPbStateNotInitialised );
       
  1154             TRAP_IGNORE( playerState = pbUtil->StateL());
       
  1155             pbUtil->Close();
       
  1156             pbUtil = NULL;
       
  1157 
       
  1158             if( playerState == EPbStatePlaying || IsAppForeground() )
       
  1159                 {
       
  1160                 iIncreaseVol = (aOperationId == ERemConCoreApiVolumeUp ? ETrue: EFalse);
       
  1161                 switch (aButtonAct)
       
  1162                     {
       
  1163                     case ERemConCoreApiButtonPress:
       
  1164                         {
       
  1165                         //Start Timer
       
  1166                         iTimer->Start(
       
  1167                             KFirstTimerExpiryInterval,
       
  1168                             KTimerExpiryInterval,
       
  1169                             TCallBack( TimerCallback, this ) );
       
  1170                         break;
       
  1171                         }
       
  1172                     case ERemConCoreApiButtonClick:
       
  1173                         {
       
  1174                         UpdateVolume();
       
  1175                         FilterAndSendCommand( iCommandId );
       
  1176                         break;
       
  1177                         }
       
  1178                     case ERemConCoreApiButtonRelease:
       
  1179                     default:
       
  1180                         {
       
  1181                         break;
       
  1182                         }
       
  1183                     }
       
  1184                 }
       
  1185             break;
       
  1186             }
       
  1187         default:
       
  1188             {
       
  1189             break;
       
  1190             }
       
  1191         }
       
  1192     iResponseHandler->CompleteAnyKey( aOperationId );
       
  1193     MPX_DEBUG1( "<--CMPXMediaKeyHandlerImp::MrccatoCommand");
       
  1194     }
       
  1195 
       
  1196 // ---------------------------------------------------------------------------
       
  1197 // A 'tune function' command has been received.
       
  1198 // ---------------------------------------------------------------------------
       
  1199 //
       
  1200 void CMPXMediaKeyHandlerImp::MrccatoTuneFunction(
       
  1201     TBool /* aTwoPart */,
       
  1202     TUint /* aMajorChannel */,
       
  1203     TUint /* aMinorChannel */,
       
  1204     TRemConCoreApiButtonAction /* aButtonAct */ )
       
  1205     {
       
  1206     iResponseHandler->CompleteAnyKey(
       
  1207         ERemConCoreApiTuneFunction );
       
  1208     }
       
  1209 
       
  1210 // ---------------------------------------------------------------------------
       
  1211 // A 'select disk function' has been received.
       
  1212 // ---------------------------------------------------------------------------
       
  1213 //
       
  1214 void CMPXMediaKeyHandlerImp::MrccatoSelectDiskFunction(
       
  1215     TUint /* aDisk */,
       
  1216     TRemConCoreApiButtonAction /* aButtonAct */ )
       
  1217     {
       
  1218     iResponseHandler->CompleteAnyKey(
       
  1219         ERemConCoreApiSelectDiskFunction );
       
  1220     }
       
  1221 
       
  1222 // ---------------------------------------------------------------------------
       
  1223 // A 'select AV input function' has been received.
       
  1224 // ---------------------------------------------------------------------------
       
  1225 //
       
  1226 void CMPXMediaKeyHandlerImp::MrccatoSelectAvInputFunction(
       
  1227     TUint8 /* aAvInputSignalNumber */,
       
  1228     TRemConCoreApiButtonAction /* aButtonAct */ )
       
  1229     {
       
  1230     iResponseHandler->CompleteAnyKey(
       
  1231         ERemConCoreApiSelectAvInputFunction );
       
  1232     }
       
  1233 
       
  1234 // ---------------------------------------------------------------------------
       
  1235 // A 'select audio input function' has been received.
       
  1236 // ---------------------------------------------------------------------------
       
  1237 //
       
  1238 void CMPXMediaKeyHandlerImp::MrccatoSelectAudioInputFunction(
       
  1239     TUint8 /* aAudioInputSignalNumber */,
       
  1240     TRemConCoreApiButtonAction /* aButtonAct */ )
       
  1241     {
       
  1242     iResponseHandler->CompleteAnyKey(
       
  1243         ERemConCoreApiSelectAudioInputFunction );
       
  1244     }
       
  1245 
       
  1246 // ----------------------------------------------------------------------------
       
  1247 // void CMPlayerBaseView::HandleControlEventL
       
  1248 // ----------------------------------------------------------------------------
       
  1249 //
       
  1250 void CMPXMediaKeyHandlerImp::HandleControlEventL( CCoeControl* aControl, TCoeEvent aEventType )
       
  1251 	{
       
  1252     MPX_FUNC("CMPXMediaKeyHandlerImp::HandleControlEventL");
       
  1253 	if ( !AknLayoutUtils::PenEnabled() )
       
  1254 		{
       
  1255 		return;
       
  1256 		}
       
  1257 
       
  1258 	if( (aEventType == EEventStateChanged) && (aControl == iVolPopup) )
       
  1259 		{
       
  1260 		TInt vol = iVolPopup->Value();
       
  1261 		
       
  1262 		MPX_DEBUG4( "CMPXMediaKeyHandlerImp::HandleControlEventL: vol(%d), iCurrentVol(%d), iMuted(%d)", vol, iCurrentVol, iMuted );
       
  1263 		
       
  1264 		if ( vol == iCurrentVol && vol != 0 )
       
  1265 		    {
       
  1266             if ( iMuted )
       
  1267                 {
       
  1268                 iMuted = EFalse;
       
  1269                 FilterAndSendCommand( EPbCmdUnMuteVolume );
       
  1270                 }
       
  1271             else
       
  1272                 {
       
  1273                 return;
       
  1274                 }
       
  1275 		    }
       
  1276 
       
  1277 		if ( vol == 0 )
       
  1278 			{
       
  1279 			if ( iMuted )
       
  1280 			    {
       
  1281 			    return;
       
  1282 			    }
       
  1283 			else
       
  1284 			    {
       
  1285 			    iMuted = ETrue;
       
  1286 			    FilterAndSendCommand( EPbCmdMuteVolume );
       
  1287 			    }
       
  1288 			}
       
  1289 		else
       
  1290 			{
       
  1291 			if ( iMuted )
       
  1292 				{
       
  1293 				iMuted = EFalse;
       
  1294 				
       
  1295 				if ( iCurrentVol == 0 ) // setting volume to 0 would reapply mute, use slider value instead
       
  1296 				    {
       
  1297                     iCurrentVol = vol;
       
  1298 				    }
       
  1299 				
       
  1300 				iVolPopup->SetValue( iCurrentVol );
       
  1301 				FilterAndSendCommand( EPbCmdUnMuteVolume );
       
  1302 				}
       
  1303 			else
       
  1304 				{
       
  1305 				iCurrentVol = vol;
       
  1306 				//VolPopup CallBack functon is frequently called, 
       
  1307 				//But Music Palyer deals with the event slowly
       
  1308 				//Thus VolPopup will not display smoothly.
       
  1309 				//So Music Player needs timer to filter some events. 
       
  1310 				if ( !iFilterEventTimer->IsActive() )
       
  1311 					{
       
  1312 				    iFilterEventTimer->Start( KFilterEventInterval, 
       
  1313 				    		            KFilterEventInterval, 
       
  1314 				                        TCallBack( FilterEventTimerCallback, this ) );
       
  1315 					}
       
  1316 				}
       
  1317 			}
       
  1318 		}
       
  1319 	}
       
  1320 
       
  1321 // ---------------------------------------------------------------------------
       
  1322 // Check the mpx app is foreground.
       
  1323 // ---------------------------------------------------------------------------
       
  1324 //
       
  1325 TBool CMPXMediaKeyHandlerImp::IsAppForeground()
       
  1326     {
       
  1327     TBool isForeground( EFalse );
       
  1328     // Gets the window group id of the app in foreground
       
  1329     RWsSession& wsSession = CCoeEnv::Static()->WsSession();
       
  1330     TInt windowGroupId = wsSession.GetFocusWindowGroup();
       
  1331     if ( windowGroupId >= 0 )
       
  1332         {
       
  1333         TRAP_IGNORE(
       
  1334             {
       
  1335             CApaWindowGroupName* wgName = CApaWindowGroupName::NewL(
       
  1336                 wsSession, windowGroupId );
       
  1337 
       
  1338             isForeground = ( wgName->AppUid() == KAppUidMusicPlayerX );
       
  1339             delete wgName;
       
  1340             } );
       
  1341         }
       
  1342 
       
  1343     return isForeground;
       
  1344     }
       
  1345 
       
  1346 // ---------------------------------------------------------------------------
       
  1347 // CMPXMediaKeyHandlerImp::VolumePopupSynchTimerCallback
       
  1348 // ---------------------------------------------------------------------------
       
  1349 //
       
  1350 TInt CMPXMediaKeyHandlerImp::VolumePopupSynchTimerCallback( TAny* aPtr )
       
  1351     {
       
  1352     static_cast<CMPXMediaKeyHandlerImp*>( aPtr )->DoVolumePopupSynch();
       
  1353     return KErrNone;
       
  1354     }
       
  1355 
       
  1356 // ---------------------------------------------------------------------------
       
  1357 // CMPXMediaKeyHandlerImp::DoVolumePopupSynch
       
  1358 // ---------------------------------------------------------------------------
       
  1359 //
       
  1360 void CMPXMediaKeyHandlerImp::DoVolumePopupSynch()
       
  1361     {
       
  1362     MPX_FUNC("CMPXMediaKeyHandlerImp::DoVolumePopupSynch");
       
  1363     
       
  1364     iVolumePopupSynchTimer->Cancel();
       
  1365     
       
  1366     TInt popupValue = iVolPopup->Value();
       
  1367     
       
  1368     MPX_DEBUG4("CMPXMediaKeyHandlerImp::DoVolumePopupSynch: popupValue(%d), iMuted(%d), iCurrentVol(%d)",
       
  1369                popupValue, iMuted, iCurrentVol );
       
  1370     
       
  1371     if ( iMuted )
       
  1372         {
       
  1373         if ( popupValue != 0 )
       
  1374             {
       
  1375             MPX_DEBUG1("CMPXMediaKeyHandlerImp::DoVolumePopupSynch: popup out of synch (muted)" );
       
  1376             iVolPopup->SetValue( 0 );
       
  1377             }
       
  1378         }
       
  1379     else
       
  1380         {
       
  1381         if ( iCurrentVol != popupValue )
       
  1382             {
       
  1383             MPX_DEBUG1("CMPXMediaKeyHandlerImp::DoVolumePopupSynch: popup out of synch" );
       
  1384             iVolPopup->SetValue( iCurrentVol );
       
  1385             }
       
  1386         }
       
  1387     }
       
  1388 
       
  1389 // ---------------------------------------------------------------------------
       
  1390 // CMPXMediaKeyHandlerImp::StartVolumePopupSynchTimer
       
  1391 // ---------------------------------------------------------------------------
       
  1392 //
       
  1393 void CMPXMediaKeyHandlerImp::StartVolumePopupSynchTimer()
       
  1394     {
       
  1395     iVolumePopupSynchTimer->Cancel();
       
  1396     iVolumePopupSynchTimer->Start( KVolumePopupSynchInterval,
       
  1397                                    KVolumePopupSynchInterval,
       
  1398                                    TCallBack( VolumePopupSynchTimerCallback, this ) );
       
  1399 
       
  1400     }
       
  1401 
       
  1402 // ---------------------------------------------------------------------------
       
  1403 // CMPXMediaKeyHandlerImp::FilterEventTimerCallback
       
  1404 // ---------------------------------------------------------------------------
       
  1405 //
       
  1406 TInt CMPXMediaKeyHandlerImp::FilterEventTimerCallback( TAny* aPtr )
       
  1407 	{
       
  1408 	static_cast<CMPXMediaKeyHandlerImp*>( aPtr )->DoFilterEventTimer();
       
  1409 	return KErrNone;
       
  1410 	}
       
  1411 
       
  1412 // ---------------------------------------------------------------------------
       
  1413 // CMPXMediaKeyHandlerImp::FilterEventTimerCallback
       
  1414 // ---------------------------------------------------------------------------
       
  1415 //
       
  1416 void CMPXMediaKeyHandlerImp::DoFilterEventTimer()
       
  1417 	{
       
  1418 	MPX_FUNC("CMPXMediaKeyHandlerImp::DoFilterEventTimer");
       
  1419 	iFilterEventTimer->Cancel();
       
  1420 	FilterAndSendCommand( EPbCmdSetVolume );
       
  1421 	}
       
  1422 // End of File