taskswitcher/screenshotplugin/src/tsorientationmonitor.cpp
changeset 124 e36b2f4799c0
parent 121 0b3699f6c654
equal deleted inserted replaced
121:0b3699f6c654 124:e36b2f4799c0
     1 /*
       
     2 * Copyright (c) 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: 
       
    15 *
       
    16 */
       
    17 #include <centralrepository.h>
       
    18 #include <sensrvchannelfinder.h>
       
    19 #include <sensrvchannel.h>
       
    20 #include <sensrvorientationsensor.h>
       
    21 
       
    22 #include "tsorientationmonitor.h"
       
    23 const TUid KSensorCenrepUid  = {0x2002C384};
       
    24 const TUint32 KSensorCenrepKey  = 0x1;
       
    25 //------------------------------------------------------------------------------
       
    26 CTsOrientationMonitor* CTsOrientationMonitor::NewL( MTsOrientationObserver& aObserver )
       
    27     {
       
    28     CTsOrientationMonitor* self = new (ELeave) CTsOrientationMonitor(aObserver);
       
    29     CleanupStack::PushL( self );
       
    30     self->ConstructL();
       
    31     CleanupStack::Pop( self );
       
    32     return self;
       
    33     }
       
    34 
       
    35 //------------------------------------------------------------------------------
       
    36 CTsOrientationMonitor::CTsOrientationMonitor( MTsOrientationObserver& aObserver )
       
    37 :
       
    38     CActive(EPriorityStandard),
       
    39     iObserver(aObserver)
       
    40     {
       
    41     CActiveScheduler::Add(this);
       
    42     }
       
    43 
       
    44 //------------------------------------------------------------------------------
       
    45 void CTsOrientationMonitor::ConstructL()
       
    46     {
       
    47     iRepository = CRepository::NewL( KSensorCenrepUid );
       
    48     SetActive();
       
    49     TRequestStatus* status( &iStatus );
       
    50     User::RequestComplete( status, KErrNone );
       
    51     }
       
    52 
       
    53 //------------------------------------------------------------------------------
       
    54 CTsOrientationMonitor::~CTsOrientationMonitor()
       
    55     {
       
    56     Cancel();
       
    57     delete iChannel;
       
    58     delete iRepository;
       
    59     }
       
    60 
       
    61 //------------------------------------------------------------------------------
       
    62 void CTsOrientationMonitor::SubscribeL()
       
    63     {
       
    64     User::LeaveIfError( iRepository->NotifyRequest(KSensorCenrepKey, iStatus) );
       
    65     SetActive();
       
    66     }
       
    67 
       
    68 //------------------------------------------------------------------------------
       
    69 void CTsOrientationMonitor::StartSensorMonitoringL()
       
    70     {
       
    71     CSensrvChannelFinder *sensrvChannelFinder = CSensrvChannelFinder::NewLC();
       
    72 
       
    73     RSensrvChannelInfoList channelInfoList;
       
    74     CleanupClosePushL(channelInfoList);
       
    75 
       
    76     TSensrvChannelInfo mySearchConditions;
       
    77 
       
    78     //Search only Orientation events.
       
    79     mySearchConditions.iChannelType = KSensrvChannelTypeIdOrientationData;
       
    80 
       
    81     sensrvChannelFinder->FindChannelsL(channelInfoList, mySearchConditions);
       
    82     
       
    83     if( channelInfoList.Count() )
       
    84         {
       
    85         iChannel = CSensrvChannel::NewL(channelInfoList[0]);
       
    86         iChannel->OpenChannelL();
       
    87         iChannel->StartDataListeningL(this, 1, 1, 0);
       
    88         }
       
    89 
       
    90     CleanupStack::Pop(&channelInfoList);
       
    91     CleanupStack::PopAndDestroy(sensrvChannelFinder);
       
    92     }
       
    93 
       
    94 //------------------------------------------------------------------------------
       
    95 void CTsOrientationMonitor::StopSensorMonitoring()
       
    96     {
       
    97     if(0 != iChannel)
       
    98         {
       
    99         iChannel->StopDataListening();
       
   100         }
       
   101     delete iChannel;
       
   102     iChannel = 0;
       
   103     }
       
   104 
       
   105 //------------------------------------------------------------------------------
       
   106 void CTsOrientationMonitor::DoCancel()
       
   107     {
       
   108     iRepository->NotifyCancel(KSensorCenrepKey);
       
   109     }
       
   110 
       
   111 //------------------------------------------------------------------------------
       
   112 void CTsOrientationMonitor::RunL()
       
   113     {
       
   114     User::LeaveIfError( iStatus.Int() );
       
   115     TInt sensorStatus;
       
   116     User::LeaveIfError(iRepository->Get(KSensorCenrepKey, sensorStatus));
       
   117     (0 != sensorStatus ) ? StartSensorMonitoringL() : StopSensorMonitoring();
       
   118     SubscribeL();
       
   119     }
       
   120 
       
   121 //------------------------------------------------------------------------------
       
   122 TInt CTsOrientationMonitor::RunError(TInt aError)
       
   123     {
       
   124     if( KErrCancel != aError )
       
   125         {
       
   126         TRAP_IGNORE(SubscribeL());
       
   127         }
       
   128     return KErrNone;
       
   129     }
       
   130 
       
   131 //------------------------------------------------------------------------------
       
   132 void CTsOrientationMonitor::DataReceived( CSensrvChannel& aChannel, 
       
   133                                           TInt aCount, 
       
   134                                           TInt /*aDataLost*/ )
       
   135     {
       
   136     if (aChannel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdOrientationData)
       
   137         {
       
   138         TPckgBuf<TSensrvOrientationData> dataBuf;
       
   139         for(TInt i = 0; i < aCount; ++i) 
       
   140             {
       
   141             aChannel.GetData(dataBuf);
       
   142             iObserver.OrientationChanged( TSensrvOrientationData::EOrientationDisplayRightUp == dataBuf().iDeviceOrientation ? 270 : 0);
       
   143             }
       
   144         }
       
   145     }
       
   146 
       
   147 //------------------------------------------------------------------------------
       
   148 void CTsOrientationMonitor::DataError( CSensrvChannel& /*aChannel*/, 
       
   149                                        TSensrvErrorSeverity /*aError*/ )
       
   150     {
       
   151     //No implementation required
       
   152     }
       
   153 
       
   154 //------------------------------------------------------------------------------
       
   155 void CTsOrientationMonitor::GetDataListenerInterfaceL( TUid /*aInterfaceUid*/, 
       
   156                                                        TAny*& /*aInterface*/ )
       
   157     {
       
   158     //No implementation required
       
   159     }