musichomescreen/musicplayeractionhandlerplugin/src/musicplayeractionhandler.cpp
branchRCL_3
changeset 53 3de6c4cf6b67
equal deleted inserted replaced
52:14979e23cb5e 53:3de6c4cf6b67
       
     1 /*
       
     2 * Copyright (c) 2008-2008 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:  Music Player Action Handler Plugin
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <LiwVariant.h>                 // TLiwVariant
       
    20 #include <apgcli.h>                     // RApaLsSession, TApaAppInfo
       
    21 #include <apacmdln.h>                   // CApaCommandLine
       
    22 #include <apgtask.h>                    // TApaTaskList
       
    23 #include <mpxlog.h>                     // MPX_DEBUG
       
    24 #include <mpxparameter.h>               // CMPXParameter
       
    25 #include <mpxplaybackutility.h>         // MMPXPlaybackUtility
       
    26 #include <mpxconstants.h>               // KAppUidMusicPlayerX
       
    27 #include <mpxmusicplayerviewplugin.hrh> // KMPXPluginTypePlaybackUid
       
    28 #include <AknTaskList.h>                // CAknTaskList
       
    29 
       
    30 #include <mpxcommandgeneraldefs.h>
       
    31 #include <mpxcommonuihelper.h>
       
    32 #include <mpxcollectionhelperfactory.h>
       
    33 #include "musicplayeractionhandler.h"
       
    34 
       
    35 
       
    36 
       
    37 const TInt KPlayerMusicPlayerParameterGranularity = 50;
       
    38 const TUid  KMusicPlayerAppUid = { 0x102072C3 };
       
    39 
       
    40 const TInt KMPXStandAloneProcessIDTop32( 1 );
       
    41 const TInt KMPXStandAloneProcessIDBottom32( 2 );
       
    42 // RProperty key to identify the case when Music Player launching
       
    43 // in the background
       
    44 const TInt KMPXLaunchingOnBackground( 100 );
       
    45 
       
    46 //map values
       
    47 _LIT( KActionPlaybackCommand , "PlaybackCommand" );
       
    48 _LIT( KactionMessageToMusicPlayer, "MessageToMusicPlayer" );
       
    49 _LIT( KGoToNowPlaying, "GoToNowPlaying" );
       
    50 _LIT( KGoToLastPlayed, "GoToLastPlayed" );
       
    51 _LIT( KGoToLastPlayedMinimized, "GoToLastPlayedMinimized" );
       
    52 _LIT( KGoToMusicLibrary, "GoToMusicLibrary" );
       
    53 
       
    54 //map keys
       
    55 _LIT8( KType, "type" );
       
    56 _LIT8( KCommand, "command" );
       
    57 _LIT8( KMessage, "message" );
       
    58 
       
    59 // ======== MEMBER FUNCTIONS ========
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Symbian 2nd phase constructor can leave.
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 void CMusicPlayerActionHandler::ConstructL()
       
    66     {
       
    67     MPX_DEBUG1("-->CMusicPlayerActionHandler::ConstructL()");
       
    68     
       
    69     //iPlaybackUtility = MMPXPlaybackUtility::UtilityL( KPbModeActivePlayer );
       
    70     iPlaybackUtility = MMPXPlaybackUtility::NewL( KMusicPlayerAppUid);
       
    71 
       
    72     MPX_DEBUG1("<--CMusicPlayerActionHandler::ConstructL()");
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Two-phased constructor.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CMusicPlayerActionHandler* CMusicPlayerActionHandler::NewL()
       
    80     {
       
    81     CMusicPlayerActionHandler* self = CMusicPlayerActionHandler::NewLC();
       
    82     CleanupStack::Pop( self );
       
    83     return self;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Two-phased constructor.
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CMusicPlayerActionHandler* CMusicPlayerActionHandler::NewLC()
       
    91     {
       
    92     CMusicPlayerActionHandler* self = new( ELeave ) CMusicPlayerActionHandler;
       
    93     CleanupStack::PushL( self );
       
    94     self->ConstructL();
       
    95     return self;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // destructor
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 CMusicPlayerActionHandler::~CMusicPlayerActionHandler()
       
   103     {
       
   104     MPX_DEBUG1("-->CMusicPlayerActionHandler::~CMusicPlayerActionHandler()");
       
   105     if ( iPlaybackUtility )
       
   106         {
       
   107         iPlaybackUtility->Close();
       
   108         }
       
   109     MPX_DEBUG1("<--CMusicPlayerActionHandler::~CMusicPlayerActionHandler()");
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // Executes Playback command on current player
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 TInt CMusicPlayerActionHandler::ExecutePlaybackCommandL(const CLiwMap* aMap)
       
   117     {
       
   118     MPX_DEBUG1("-->CMusicPlayerActionHandler::ExecutePlaybackCommandL()");
       
   119     TInt errCode;
       
   120     TLiwVariant variant;
       
   121     variant.PushL();
       
   122     errCode = ExtractVariantL( aMap, variant, KCommand );
       
   123     if ( errCode == KErrNone )
       
   124         {
       
   125         TMPXPlaybackCommand Command;
       
   126         Command = static_cast<TMPXPlaybackCommand>( variant.AsTInt32() );
       
   127         SetPlaybackCommandL( Command );
       
   128         }
       
   129     CleanupStack::PopAndDestroy( &variant);
       
   130     MPX_DEBUG1("<--CMusicPlayerActionHandler::ExecutePlaybackCommandL()");
       
   131     return errCode;
       
   132     }
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // Sends a message to music player
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 TInt CMusicPlayerActionHandler::ExecuteMessageToMusicPlayerL(
       
   139         const CLiwMap* aMap )
       
   140     {
       
   141     MPX_DEBUG1("-->CMusicPlayerActionHandler::"
       
   142             "ExecuteMessageToMusicPlayerL()");
       
   143     TInt errCode;
       
   144     RBuf msg;
       
   145     CleanupClosePushL( msg );
       
   146     errCode = ExtractDesL( aMap, msg, KMessage );
       
   147     if ( errCode == KErrNone )
       
   148         {
       
   149         if ( ! msg.CompareF( KGoToNowPlaying ) )
       
   150             {
       
   151             errCode = GoToNowPlayingL();
       
   152             }
       
   153         else if ( ! msg.CompareF( KGoToLastPlayed ) )
       
   154             {
       
   155             errCode = GoToLastPlayedL( EFalse );
       
   156             }
       
   157         else if ( ! msg.CompareF( KGoToLastPlayedMinimized ) )
       
   158             {
       
   159             errCode = GoToLastPlayedL( ETrue );
       
   160             }
       
   161         else if ( ! msg.CompareF( KGoToMusicLibrary ) )
       
   162             {
       
   163             errCode = GoToMusicLibraryL();
       
   164             }        
       
   165         }
       
   166     CleanupStack::PopAndDestroy( &msg );
       
   167     MPX_DEBUG1("<--CMusicPlayerActionHandler::"
       
   168             "ExecuteMessageToMusicPlayerL()");
       
   169     return errCode;
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // Opens musicplayer in library view, even if it is already open and playing.
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 TInt CMusicPlayerActionHandler::GoToMusicLibraryL()
       
   177     {
       
   178     MPX_DEBUG1("-->CMusicPlayerActionHandler::GoToNowPlayingL()");
       
   179     //Launch player
       
   180     RWsSession wsSession;
       
   181     User::LeaveIfError( wsSession.Connect() );
       
   182     CAknTaskList *taskList = CAknTaskList::NewL( wsSession );
       
   183     TApaTask task = taskList->FindRootApp( KAppUidMusicPlayerX );
       
   184     delete taskList;
       
   185     CMPXParameter* param = new ( ELeave ) CMPXParameter();
       
   186     CleanupStack::PushL( param );
       
   187     param->iType.iUid = KMPXPluginTypeCollectionUid;
       
   188     param->iCmdForward = EMPXCmdFwdNone;
       
   189 
       
   190     MMPXCollectionUiHelper* collectionHelper = CMPXCollectionHelperFactory::NewCollectionUiHelperL();
       
   191     CMPXCollectionPath* path = collectionHelper->MusicMenuPathL();
       
   192     if (path)
       
   193         {
       
   194         param->iCollectionPath = path;
       
   195         param->iPathType = EMPXTypeCollectionPath;
       
   196         }
       
   197     collectionHelper->Close();
       
   198     
       
   199     CBufBase* buffer =
       
   200             CBufFlat::NewL( KPlayerMusicPlayerParameterGranularity );
       
   201     CleanupStack::PushL( buffer );
       
   202     RBufWriteStream writeStream( *buffer );
       
   203     CleanupClosePushL( writeStream );
       
   204     param->ExternalizeL( writeStream );
       
   205     writeStream.CommitL();
       
   206     buffer->Compress();
       
   207     CleanupStack::PopAndDestroy( &writeStream );
       
   208     if ( task.Exists() )
       
   209         {
       
   210 //        task.SendMessage( KAppUidMusicPlayerX, buffer->Ptr( 0 ));
       
   211         wsSession.SendMessageToWindowGroup( task.WgId(), KAppUidMusicPlayerX,
       
   212                     buffer->Ptr( 0 ) );
       
   213         }
       
   214     else        //Application needs to be launched. The first view is library view.
       
   215         {
       
   216         // Launch Music Player Application
       
   217         RApaLsSession   appArcSession;
       
   218         _LIT(KEmpty,"");
       
   219         User::LeaveIfError(appArcSession.Connect());    // connect to AppArc server
       
   220         TThreadId id;
       
   221         appArcSession.StartDocument( KEmpty, KAppUidMusicPlayerX, id );
       
   222         appArcSession.Close();                                
       
   223         }
       
   224     CleanupStack::PopAndDestroy( buffer );
       
   225     CleanupStack::PopAndDestroy( param );
       
   226     wsSession.Close();
       
   227     MPX_DEBUG1("<--CMusicPlayerActionHandler::GoToNowPlayingL()");
       
   228     return KErrNone;
       
   229     }
       
   230 
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // Opens musicplayer, if it is already open and playing it goes to nowplaying.
       
   234 // ---------------------------------------------------------------------------
       
   235 //
       
   236 TInt CMusicPlayerActionHandler::GoToNowPlayingL()
       
   237     {
       
   238     MPX_DEBUG1("-->CMusicPlayerActionHandler::GoToNowPlayingL()");
       
   239     //Launch player
       
   240     RWsSession wsSession;
       
   241     User::LeaveIfError( wsSession.Connect() );
       
   242     CAknTaskList *taskList = CAknTaskList::NewL( wsSession );
       
   243     TApaTask task = taskList->FindRootApp( KAppUidMusicPlayerX );
       
   244     delete taskList;
       
   245     CMPXParameter* param = new ( ELeave ) CMPXParameter();
       
   246     CleanupStack::PushL( param );
       
   247     param->iType.iUid = KMPXPluginTypePlaybackUid;
       
   248     param->iCmdForward = EMPXCmdFwdNone;
       
   249     CBufBase* buffer =
       
   250             CBufFlat::NewL( KPlayerMusicPlayerParameterGranularity );
       
   251     CleanupStack::PushL( buffer );
       
   252     RBufWriteStream writeStream( *buffer );
       
   253     CleanupClosePushL( writeStream );
       
   254     param->ExternalizeL( writeStream );
       
   255     writeStream.CommitL();
       
   256     buffer->Compress();
       
   257     CleanupStack::PopAndDestroy( &writeStream );
       
   258     if ( task.Exists() )
       
   259         {
       
   260         wsSession.SendMessageToWindowGroup( task.WgId(), KAppUidMusicPlayerX,
       
   261                     buffer->Ptr( 0 ) );
       
   262         
       
   263         
       
   264         }
       
   265     else
       
   266         {
       
   267         RApaLsSession ls;
       
   268         CleanupClosePushL( ls );
       
   269         User::LeaveIfError( ls.Connect() );
       
   270         TApaAppInfo appInfo;
       
   271         User::LeaveIfError( ls.GetAppInfo( appInfo, KAppUidMusicPlayerX ) );
       
   272         CApaCommandLine* apaCommandLine = CApaCommandLine::NewLC();
       
   273         apaCommandLine->SetExecutableNameL( appInfo.iFullName );
       
   274         apaCommandLine->SetTailEndL( buffer->Ptr( 0 ) );
       
   275         User::LeaveIfError( ls.StartApp( *apaCommandLine ) );
       
   276         CleanupStack::PopAndDestroy(); // apaCommandLine
       
   277         CleanupStack::PopAndDestroy(); // ls
       
   278         }
       
   279     CleanupStack::PopAndDestroy( buffer );
       
   280     CleanupStack::PopAndDestroy( param );
       
   281     wsSession.Close();
       
   282     MPX_DEBUG1("<--CMusicPlayerActionHandler::GoToNowPlayingL()");
       
   283     return KErrNone;
       
   284     }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // Opens Stand alone MP and goes to now playing, it will display last played
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 TInt CMusicPlayerActionHandler::GoToLastPlayedL( TBool aMinimized )
       
   292     {
       
   293     MPX_DEBUG1("-->CMusicPlayerActionHandler::GoToLastPlayed()");
       
   294     //Launch player
       
   295     RWsSession wsSession;
       
   296     User::LeaveIfError( wsSession.Connect() );
       
   297     CAknTaskList *taskList = CAknTaskList::NewL( wsSession );
       
   298     TApaTask task = taskList->FindRootApp( KAppUidMusicPlayerX );
       
   299     delete taskList;
       
   300     CMPXParameter* param = new ( ELeave ) CMPXParameter();
       
   301     CleanupStack::PushL( param );
       
   302     param->iType.iUid = KMPXPluginTypeLastPlayedUid;//KMPXPluginTypePlaybackUid;
       
   303     param->iCmdForward = EMPXCmdFwdNone;
       
   304     CBufBase* buffer =
       
   305             CBufFlat::NewL( KPlayerMusicPlayerParameterGranularity );
       
   306     CleanupStack::PushL( buffer );
       
   307     RBufWriteStream writeStream( *buffer );
       
   308     CleanupClosePushL( writeStream );
       
   309     param->ExternalizeL( writeStream );
       
   310     writeStream.CommitL();
       
   311     buffer->Compress();
       
   312     CleanupStack::PopAndDestroy( &writeStream );
       
   313     if ( task.Exists() )
       
   314         {
       
   315         wsSession.SendMessageToWindowGroup( task.WgId(), KAppUidMusicPlayerX,
       
   316                     buffer->Ptr( 0 ) );
       
   317         if (!aMinimized)
       
   318             {
       
   319             task.BringToForeground();
       
   320             }
       
   321         }
       
   322     else
       
   323         {
       
   324         RApaLsSession ls;
       
   325         CleanupClosePushL( ls );
       
   326         User::LeaveIfError( ls.Connect() );
       
   327         TApaAppInfo appInfo;
       
   328         User::LeaveIfError( ls.GetAppInfo( appInfo, KAppUidMusicPlayerX ) );
       
   329         CApaCommandLine* apaCommandLine = CApaCommandLine::NewLC();
       
   330         if (aMinimized)
       
   331             {
       
   332             apaCommandLine->SetCommandL(EApaCommandBackground);
       
   333             
       
   334             TInt err( RProperty::Define(
       
   335                 KAppUidMusicPlayerX, KMPXLaunchingOnBackground, RProperty::EInt ) );
       
   336             MPX_DEBUG2( "CMusicPlayerActionHandler::GoToLastPlayed() RProperty define err = %d", err );
       
   337             if ( err == KErrNone || err == KErrAlreadyExists )
       
   338                 {
       
   339                 err = RProperty::Set( KAppUidMusicPlayerX,
       
   340                             KMPXLaunchingOnBackground,
       
   341                             ETrue );
       
   342                 MPX_DEBUG2( "CMusicPlayerActionHandler::GoToLastPlayed() RProperty set to true err = %d", err );
       
   343                 }
       
   344             }
       
   345         apaCommandLine->SetExecutableNameL( appInfo.iFullName );
       
   346         apaCommandLine->SetTailEndL( buffer->Ptr( 0 ) );
       
   347         User::LeaveIfError( ls.StartApp( *apaCommandLine ) );
       
   348         CleanupStack::PopAndDestroy(); // apaCommandLine
       
   349         CleanupStack::PopAndDestroy(); // ls
       
   350         
       
   351         if ( aMinimized )
       
   352             {
       
   353             TInt err = RProperty::Set( KAppUidMusicPlayerX,
       
   354                         KMPXLaunchingOnBackground,
       
   355                         EFalse );
       
   356             MPX_DEBUG2( "CMusicPlayerActionHandler::GoToLastPlayed() RProperty set to false err = %d", err );
       
   357             }
       
   358         }
       
   359     CleanupStack::PopAndDestroy( buffer );
       
   360     CleanupStack::PopAndDestroy( param );
       
   361     wsSession.Close();
       
   362     MPX_DEBUG1("<--CMusicPlayerActionHandler::GoToLastPlayed()");
       
   363     return KErrNone;
       
   364     }
       
   365 // ---------------------------------------------------------------------------
       
   366 // Extracts a descriptor
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 TInt CMusicPlayerActionHandler::ExtractDesL( const CLiwMap* aMap,
       
   370         RBuf& aString, const TDesC8& aMapName )
       
   371     {
       
   372     MPX_DEBUG1("-->CMusicPlayerActionHandler::ExtractDesL()");
       
   373     TInt errCode( KErrArgument );
       
   374     TLiwVariant variant;
       
   375     variant.PushL();
       
   376     TPtrC tempString( KNullDesC );
       
   377     if ( aMap->FindL( aMapName, variant) )
       
   378         {
       
   379         variant.Get( tempString );
       
   380         aString.ReAllocL( tempString.Length() );
       
   381         aString.Append( tempString );
       
   382         errCode = KErrNone;
       
   383         }
       
   384     CleanupStack::PopAndDestroy( &variant );
       
   385     MPX_DEBUG1("<--CMusicPlayerActionHandler::ExtractDesL()");
       
   386     return errCode;
       
   387     }
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // Extract variant
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 TInt CMusicPlayerActionHandler::ExtractVariantL( const CLiwMap* aMap,
       
   394         TLiwVariant& aVariant, const TDesC8& aMapName )
       
   395     {
       
   396     MPX_DEBUG1("-->CMusicPlayerActionHandler::ExtractVariantL()");
       
   397     TInt errCode( KErrNone );
       
   398     if ( ! aMap->FindL( aMapName, aVariant ) )
       
   399         {
       
   400         errCode = KErrArgument;
       
   401         }
       
   402     MPX_DEBUG1("<--CMusicPlayerActionHandler::ExtractVariantL()");
       
   403     return errCode;
       
   404     }
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 // From CAHPlugin
       
   408 // Executes provided action
       
   409 // ---------------------------------------------------------------------------
       
   410 //
       
   411 TInt CMusicPlayerActionHandler::ExecuteActionL( const CLiwMap* aMap )
       
   412     {
       
   413     MPX_DEBUG1("-->CMusicPlayerActionHandler::ExecuteActionL()");
       
   414     TInt errCode;
       
   415     RBuf type;
       
   416     CleanupClosePushL( type );
       
   417     errCode = ExtractDesL( aMap, type, KType );
       
   418     if ( errCode == KErrNone )
       
   419         {
       
   420         if ( ! type.CompareF( KActionPlaybackCommand ) )
       
   421             {
       
   422             errCode = ExecutePlaybackCommandL( aMap );
       
   423             }
       
   424         else
       
   425             if ( ! type.CompareF( KactionMessageToMusicPlayer ) )
       
   426                 {
       
   427                 errCode = ExecuteMessageToMusicPlayerL( aMap );
       
   428                 }
       
   429         }
       
   430     CleanupStack::PopAndDestroy( &type );
       
   431     MPX_DEBUG1("<--CMusicPlayerActionHandler::ExecuteActionL()");
       
   432     return errCode;
       
   433     }
       
   434 
       
   435 // ---------------------------------------------------------------------------
       
   436 // Set the command to playbackUtility
       
   437 // ---------------------------------------------------------------------------
       
   438 //
       
   439 void CMusicPlayerActionHandler::SetPlaybackCommandL( TMPXPlaybackCommand aCommand )
       
   440     {
       
   441     MPX_DEBUG1("-->CMusicPlayerActionHandler::SetPlaybackCommandL()");
       
   442     //Try to get musicplay StandAloneMode process id
       
   443     TInt procId( 0 );
       
   444     TUint64 mpProcId( 0 );
       
   445     TInt err( RProperty::Get( KMusicPlayerAppUid, KMPXStandAloneProcessIDTop32, procId ) );
       
   446     if ( err == KErrNone )
       
   447         {
       
   448         mpProcId = TUint64( procId ) << 32;
       
   449         err = RProperty::Get( KMusicPlayerAppUid, KMPXStandAloneProcessIDBottom32, procId );
       
   450         if ( err == KErrNone )
       
   451             {
       
   452             mpProcId += procId;
       
   453             }
       
   454         else
       
   455             {
       
   456             mpProcId = 0;
       
   457             }
       
   458         }
       
   459     
       
   460     CMPXCommand* cmd( CMPXCommand::NewL() );
       
   461     CleanupStack::PushL( cmd );
       
   462     cmd->SetTObjectValueL<TInt>( KMPXCommandGeneralId, KMPXCommandIdPlaybackGeneral );
       
   463     cmd->SetTObjectValueL<TBool>( KMPXCommandGeneralDoSync, ETrue );
       
   464     cmd->SetTObjectValueL<TInt>( KMPXCommandPlaybackGeneralType, aCommand );
       
   465     cmd->SetTObjectValueL<TInt>( KMPXCommandPlaybackGeneralData, 0 );
       
   466     if ( mpProcId )
       
   467         {
       
   468         //This attribute will be used by playbckEngine->iLastActiveProcess 
       
   469         cmd->SetTObjectValueL<TProcessId> ( KMPXCommandPlaybackGeneralClientPid, mpProcId );
       
   470         }
       
   471     iPlaybackUtility->CommandL( *cmd );
       
   472     CleanupStack::PopAndDestroy( cmd );
       
   473     MPX_DEBUG1("<--CMusicPlayerActionHandler::SetPlaybackCommandL()");
       
   474     }
       
   475 //  End of File
       
   476