utilities/mpapplicationmonitor/src/mpapplicationmonitor_p.cpp
changeset 58 ed94e1e8390e
equal deleted inserted replaced
54:c5b304f4d89b 58:ed94e1e8390e
       
     1 /*
       
     2 * Copyright (c) 2009 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 application monitor utility - private implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <apgtask.h>
       
    19 #include <AknTaskList.h>
       
    20 
       
    21 #include "mpapplicationmonitor_p.h"
       
    22 #include "mpapplicationmonitor.h"
       
    23 #include "mptrace.h"
       
    24 
       
    25 // -1 means the window will never be visible.
       
    26 const TInt KWindowGroupPriority = -1;
       
    27 
       
    28 /*!
       
    29     \class MpApplicationMonitorPrivate
       
    30     \brief Music Player application monitor utility - private implementation.
       
    31 
       
    32 
       
    33 */
       
    34 
       
    35 /*!
       
    36  Constructor
       
    37  */
       
    38 MpApplicationMonitorPrivate::MpApplicationMonitorPrivate( MpApplicationMonitor *qq )
       
    39     : CActive( CActive::EPriorityStandard ),
       
    40       q_ptr( qq )
       
    41 {
       
    42     TX_ENTRY_ARGS( "Q pointer=" << ( void * )qq )
       
    43     CActiveScheduler::Add( this );
       
    44     TX_EXIT
       
    45 }
       
    46 
       
    47 /*!
       
    48  Destructor
       
    49  */
       
    50 MpApplicationMonitorPrivate::~MpApplicationMonitorPrivate()
       
    51 {
       
    52     TX_LOG
       
    53     // Cancel any outstanding request
       
    54     Cancel();
       
    55 
       
    56     iWindowGroup.Close();
       
    57     iWsSession.Close();
       
    58     TX_EXIT
       
    59 }
       
    60 
       
    61 /*!
       
    62  \internal
       
    63  */
       
    64 void MpApplicationMonitorPrivate::init( quint32 clientSecureId )
       
    65 {
       
    66     TX_ENTRY
       
    67     iHostUid = TUid::Uid( clientSecureId );
       
    68     TRAPD( err, DoInitL() );
       
    69     if ( err != KErrNone ) {
       
    70         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
    71     }
       
    72     TX_EXIT
       
    73 }
       
    74 
       
    75 /*!
       
    76  Request if music player application is running.
       
    77  */
       
    78 bool MpApplicationMonitorPrivate::isApplicationRunning()
       
    79 {
       
    80     TX_ENTRY
       
    81     bool running = false;
       
    82     TRAPD( err, running = DoIsApplicationRunningL() );
       
    83     if ( err != KErrNone ) {
       
    84         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
    85     }
       
    86     TX_EXIT
       
    87     return running;
       
    88 }
       
    89 
       
    90 /*!
       
    91  \internal
       
    92  Checks if the chosen app is launched
       
    93  */
       
    94 void MpApplicationMonitorPrivate::RunL()
       
    95     {
       
    96     // Get the status of this object.
       
    97     const TInt errCode( iStatus.Int() );
       
    98     TX_ENTRY_ARGS("Status=" << errCode);
       
    99 
       
   100     if ( KErrNone == errCode ) {
       
   101         TWsEvent event;
       
   102         iWsSession.GetEvent( event );
       
   103         if ( event.Type() == EEventWindowGroupsChanged ) {
       
   104             emit q_ptr->applicationStatusChanged( isApplicationRunning() );
       
   105         }
       
   106         TX_LOG_ARGS("OK, renewing request");
       
   107         StartMonitor();
       
   108     }
       
   109     else if ( KErrCancel != errCode ) {
       
   110         TX_LOG_ARGS("Renewing request after error: " << errCode);
       
   111         StartMonitor();
       
   112     }
       
   113     else {
       
   114         TX_LOG_ARGS("cancelled");
       
   115     }
       
   116     TX_EXIT
       
   117 }
       
   118 
       
   119 /*!
       
   120  \internal
       
   121  Cancel the outstanding request.
       
   122  */
       
   123 void MpApplicationMonitorPrivate::DoCancel()
       
   124 {
       
   125     TX_ENTRY
       
   126     iWsSession.EventReadyCancel();
       
   127     TX_EXIT
       
   128 }
       
   129 
       
   130 /*!
       
   131  \internal
       
   132  */
       
   133 void MpApplicationMonitorPrivate::DoInitL()
       
   134 {
       
   135     TX_ENTRY
       
   136     CreateWindowGroupL();
       
   137     StartMonitor();
       
   138     TX_EXIT
       
   139 }
       
   140 
       
   141 /*!
       
   142  \internal
       
   143  */
       
   144 bool MpApplicationMonitorPrivate::DoIsApplicationRunningL()
       
   145 {
       
   146     TX_ENTRY
       
   147     bool taskExists = false;
       
   148     CAknTaskList* taskList = CAknTaskList::NewL( iWsSession );
       
   149     TApaTask task = taskList->FindRootApp( iHostUid );
       
   150     delete taskList;
       
   151     taskExists = task.Exists();
       
   152     TX_LOG_ARGS("taskExists=" << taskExists);
       
   153     return taskExists;
       
   154 }
       
   155 
       
   156 /*!
       
   157  \internal
       
   158  Creates a window group and hides it from the UI.
       
   159  */
       
   160 void MpApplicationMonitorPrivate::CreateWindowGroupL()
       
   161 {
       
   162     TX_ENTRY
       
   163     User::LeaveIfError( iWsSession.Connect() );
       
   164     RWindowGroup wg( iWsSession );
       
   165     // Make a handle from the address of window group object
       
   166     User::LeaveIfError( wg.Construct( reinterpret_cast<TUint32>( &wg ) ) );
       
   167     User::LeaveIfError( wg.EnableGroupChangeEvents() );
       
   168 
       
   169     // Get the ordinal position of this window group
       
   170     const TInt ordPos( wg.OrdinalPosition() );
       
   171     // Set priority so it's hidden in the UI
       
   172     wg.SetOrdinalPosition( ordPos, KWindowGroupPriority );
       
   173 
       
   174     // Needs to be a member variable. Must be closed in destructor.
       
   175     // Otherwise can't monitor what has been opened.
       
   176     iWindowGroup = wg;
       
   177 
       
   178     iWsSession.Flush();
       
   179     TX_EXIT
       
   180 }
       
   181 
       
   182 /*!
       
   183  \internal
       
   184  Starts event monitoring.
       
   185  */
       
   186 void MpApplicationMonitorPrivate::StartMonitor()
       
   187 {
       
   188     TX_ENTRY
       
   189     Cancel();
       
   190 
       
   191     iWsSession.EventReady( &iStatus );
       
   192     SetActive();
       
   193     TX_EXIT
       
   194 }
       
   195