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