javauis/lcdui_akn/javalcdui/src.nokialcdui/CMIDDeviceControl.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Controls device backlight
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "CMIDDeviceControl.h"
       
    22 
       
    23 #include <jdebug.h>
       
    24 
       
    25 // CONSTANTS
       
    26 namespace
       
    27 {
       
    28 // Light devices supported by this API.
       
    29 const TInt KSupportedLightCount = 4;
       
    30 
       
    31 const TInt KSupportedLights[] = { CHWRMLight::EPrimaryDisplay,
       
    32                                   CHWRMLight::EPrimaryKeyboard, CHWRMLight::ESecondaryDisplay,
       
    33                                   CHWRMLight::ESecondaryKeyboard
       
    34                                 };
       
    35 
       
    36 const TInt KMIDMaxLightLevel = 100;
       
    37 }
       
    38 
       
    39 
       
    40 // ============================ MEMBER FUNCTIONS ===============================
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CMIDDeviceControl::CMIDDeviceControl
       
    44 // C++ default constructor can NOT contain any code, that
       
    45 // might leave.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CMIDDeviceControl::CMIDDeviceControl(MMIDEnv& aEnv)
       
    49         : iEnv(aEnv),
       
    50         iLightLevel(KErrNotFound),
       
    51         iSupportedLights(CHWRMLight::ENoTarget)
       
    52 {
       
    53 }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CMIDDeviceControl::ConstructL
       
    57 // Symbian 2nd phase constructor can leave.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 void CMIDDeviceControl::ConstructL()
       
    61 {
       
    62     DEBUG("nokialcdui::CMIDDeviceControl::ConstructL +");
       
    63 
       
    64     iLight = CHWRMLight::NewL(this);
       
    65 
       
    66     iEnv.AddObserverL(*this);
       
    67 
       
    68     // Find out light devices on the phone that are supported
       
    69     // by our API.
       
    70 
       
    71     TInt supportedLights = iLight->SupportedTargets();
       
    72 
       
    73     TInt mask = (CHWRMLight::EPrimaryDisplayAndKeyboard |
       
    74                  CHWRMLight::ESecondaryDisplayAndKeyboard);
       
    75 
       
    76     iSupportedLights = (supportedLights & mask);
       
    77 
       
    78     DEBUG("nokialcdui::CMIDDeviceControl::ConstructL -");
       
    79 }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CMIDDeviceControl::NewLC
       
    83 // Two-phased constructor. Leaves object in cleanup stack.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CMIDDeviceControl* CMIDDeviceControl::NewLC(MMIDEnv& aEnv)
       
    87 {
       
    88     CMIDDeviceControl* self = new(ELeave) CMIDDeviceControl(aEnv);
       
    89 
       
    90     CleanupStack::PushL(self);
       
    91     self->ConstructL();
       
    92 
       
    93     return self;
       
    94 }
       
    95 
       
    96 
       
    97 // Destructor
       
    98 CMIDDeviceControl::~CMIDDeviceControl()
       
    99 {
       
   100     Release();
       
   101     delete iLight;
       
   102 }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CMIDDeviceControl::resetUserInactivityTime
       
   106 // Reset the user inactivity time
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CMIDDeviceControl::GetUserInactivityTimeL(TInt* aRetVal)
       
   110 {
       
   111     DEBUG("nokialcdui::CMIDDeviceControl::GetUserInactivityTimeL +");
       
   112     TTimeIntervalSeconds ValInterval = User::InactivityTime() ;
       
   113     *aRetVal = ValInterval.Int();
       
   114     DEBUG("nokialcdui::CMIDDeviceControl::GetUserInactivityTimeL -");
       
   115 }
       
   116 
       
   117 void CMIDDeviceControl::StaticGetUserInactivityTimeL(CMIDDeviceControl* aDC,
       
   118         TInt* aRetVal)
       
   119 {
       
   120     aDC->GetUserInactivityTimeL(aRetVal);
       
   121 }
       
   122 
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CMIDDeviceControl::resetUserInactivityTime
       
   126 // Reset the user inactivity time
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CMIDDeviceControl::ResetUserInactivityTimeL()
       
   130 {
       
   131     DEBUG("nokialcdui::CMIDDeviceControl::ResetUserInactivityTime +");
       
   132     User::ResetInactivityTime();
       
   133     DEBUG("nokialcdui::CMIDDeviceControl::ResetUserInactivityTime -");
       
   134 }
       
   135 
       
   136 void CMIDDeviceControl::StaticResetUserInactivityTimeL(CMIDDeviceControl* aDC)
       
   137 {
       
   138     aDC->ResetUserInactivityTimeL();
       
   139 }
       
   140 
       
   141 /**
       
   142  * Requests a flashing effect for the device's backlight
       
   143  * Returns false as the emulator does not support this feature.
       
   144  */
       
   145 #if defined(__WINSCW__)
       
   146 
       
   147 TBool CMIDDeviceControl::FlashBacklightL(TInt /*aDuration*/)
       
   148 {
       
   149     DEBUG("nokialcdui::CMIDDeviceControl::FlashBacklightL");
       
   150 
       
   151     return EFalse;
       
   152 }
       
   153 
       
   154 #else // __WINSCW__
       
   155 
       
   156 /**
       
   157  * Requests a flashing effect for the device's backlight
       
   158  * Must return true if the backlight can be controlled by the application
       
   159  */
       
   160 TBool CMIDDeviceControl::FlashBacklightL(TInt aDuration)
       
   161 {
       
   162     DEBUG_INT("nokialcdui::CMIDDeviceControl::FlashBacklightL( %d ) +",
       
   163               aDuration);
       
   164 
       
   165     // Return EFalse if backlights cannot be controlled.
       
   166     if (iSupportedLights == CHWRMLight::ENoTarget)
       
   167     {
       
   168         return EFalse;
       
   169     }
       
   170 
       
   171     // Delay the screen saver starting.
       
   172     User::ResetInactivityTime();
       
   173 
       
   174     // Reserve the light(s) if not yet reserved.
       
   175     if (iLightLevel == KErrNotFound)
       
   176     {
       
   177         iLight->ReserveLightL(iSupportedLights);
       
   178     }
       
   179 
       
   180     if (!aDuration)
       
   181     {
       
   182         // 0 would be infinite
       
   183         aDuration = 1;
       
   184     }
       
   185     iDuration = aDuration;
       
   186     iStartTime = new TTime();
       
   187     iStartTime->HomeTime();
       
   188     iLight->LightBlinkL(iSupportedLights, aDuration);
       
   189 
       
   190     DEBUG_INT("nokialcdui::CMIDDeviceControl::FlashBacklightL( %d ) -",
       
   191               aDuration);
       
   192 
       
   193     return ETrue;
       
   194 }
       
   195 #endif // __WINSCW__
       
   196 
       
   197 
       
   198 void CMIDDeviceControl::StaticFlashBacklightL(
       
   199     CMIDDeviceControl* aDC,
       
   200     TInt aDuration,
       
   201     TBool* aRetVal)
       
   202 {
       
   203     *aRetVal = aDC->FlashBacklightL(aDuration);
       
   204 }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CMIDDeviceControl::SetLightL
       
   208 // sets the backlight level
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 void CMIDDeviceControl::SetLightL(TInt aLevel)
       
   212 {
       
   213     DEBUG_INT("nokialcdui::CMIDDeviceControl::SetLightL( %d ) +", aLevel);
       
   214 
       
   215     if ((aLevel < 0) || (aLevel > KMIDMaxLightLevel))
       
   216     {
       
   217         User::Leave(KErrArgument);
       
   218     }
       
   219 
       
   220     // Delay the screen saver starting.
       
   221     User::ResetInactivityTime();
       
   222 
       
   223     // Do nothing if backlights cannot be controlled.
       
   224     if (iSupportedLights != CHWRMLight::ENoTarget)
       
   225     {
       
   226         iLightLevel = aLevel;
       
   227 
       
   228         // Do nothing at the moment if the blinking is ongoing.
       
   229         // Adjust the new light level when the blinking has finished
       
   230         // (LightStatusChanged() is called).
       
   231         TBool isBlinking = LightsBlinking();
       
   232 
       
   233         if (!isBlinking)
       
   234         {
       
   235             DoSetLightsL(aLevel);
       
   236         }
       
   237     }
       
   238 
       
   239     DEBUG_INT("nokialcdui::CMIDDeviceControl::SetLightL( %d ) -", aLevel);
       
   240 }
       
   241 
       
   242 
       
   243 
       
   244 void CMIDDeviceControl::HandleSwitchOnL(TBool /*aSwitchOn*/)
       
   245 {
       
   246 }
       
   247 
       
   248 void CMIDDeviceControl::HandleForegroundL(TBool aForeground)
       
   249 {
       
   250     DEBUG_INT2("nokialcdui::CMIDDeviceControl::HandleForegroundL; aForeground=%d; iDuration=%d", aForeground, iDuration);
       
   251 
       
   252     if (aForeground && iDuration > 0)
       
   253     {
       
   254         TTime* now = new TTime();
       
   255         now->HomeTime();
       
   256         TInt nowDuration = now->MicroSecondsFrom(*iStartTime).Int64()/1000;
       
   257         if (nowDuration >= iDuration)
       
   258         {
       
   259             iDuration = 0;
       
   260         }
       
   261         else
       
   262         {
       
   263             DEBUG_INT("nokialcdui::CMIDDeviceControl::HandleForegroundL - continue blinking for ( %d ) -", (iDuration - nowDuration));
       
   264             FlashBacklightL(iDuration - nowDuration);
       
   265         }
       
   266     }
       
   267 }
       
   268 
       
   269 void CMIDDeviceControl::HandleResourceChangeL(TInt /*aType*/)
       
   270 {
       
   271 }
       
   272 
       
   273 void CMIDDeviceControl::StaticSetLightL(CMIDDeviceControl* aDC, TInt aLevel)
       
   274 {
       
   275     aDC->SetLightL(aLevel);
       
   276 }
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // CMIDDeviceControl::Release
       
   280 // Returns the backlight mode to sensor and releases resources
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 void CMIDDeviceControl::Release()
       
   284 {
       
   285     DEBUG("nokialcdui::CMIDDeviceControl::Release +");
       
   286 
       
   287     // Do nothing if backlights cannot be controlled.
       
   288     if (iSupportedLights != CHWRMLight::ENoTarget)
       
   289     {
       
   290         TRAP_IGNORE(iLight->LightOnL(iSupportedLights,
       
   291                                      KHWRMInfiniteDuration,
       
   292                                      KHWRMDefaultIntensity, EFalse));
       
   293 
       
   294         iLight->ReleaseLight(iSupportedLights);
       
   295     }
       
   296 
       
   297     DEBUG("nokialcdui::CMIDDeviceControl::Release -");
       
   298 }
       
   299 
       
   300 void CMIDDeviceControl::StaticRelease(CMIDDeviceControl* aDC)
       
   301 {
       
   302     aDC->Release();
       
   303 }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CMIDDeviceControl::Dispose
       
   307 // Deletes this object
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CMIDDeviceControl::Dispose()
       
   311 {
       
   312     delete this;
       
   313 }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CMIDDeviceControl::LightStatusChanged
       
   317 // If there is pending setLights call, do it now
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void CMIDDeviceControl::LightStatusChanged(
       
   321     TInt aTarget,
       
   322     CHWRMLight::TLightStatus aStatus)
       
   323 {
       
   324     DEBUG_INT2("nokialcdui::CMIDDeviceControl::LightStatusChanged( %d, %d ) +",
       
   325                aTarget, aStatus);
       
   326     (void)aTarget; // Just to suppress warning in release build
       
   327 
       
   328     // Do nothing if the backlights cannot be controlled.
       
   329     // Adjust the lights only if blinking has finished.
       
   330     // If the status informs about some error, the light devices have to be
       
   331     // released if blinking is not ongoing.
       
   332     if ((aStatus != CHWRMLight::ELightBlink) &&
       
   333             (iSupportedLights != CHWRMLight::ENoTarget))
       
   334     {
       
   335         // Check whether the blinking has stopped. Check that none of the light
       
   336         // devices is blinking.
       
   337         TBool isBlinking = LightsBlinking();
       
   338 
       
   339         // Adjust the lights only if blinking has finished.
       
   340         if (!isBlinking)
       
   341         {
       
   342             if (KErrNotFound == iLightLevel)
       
   343             {
       
   344                 // lights haven't been set yet, so lights must be released after
       
   345                 // blinking
       
   346 
       
   347                 iLight->ReleaseLight(iSupportedLights);
       
   348             }
       
   349             // Set the stored level to the lights only if the lights are in a
       
   350             // valid state (if the given status informed about some error, the
       
   351             // level is not tried to be set to the lights).
       
   352             else if ((aStatus == CHWRMLight::ELightOn) ||
       
   353                      (aStatus == CHWRMLight::ELightOff))
       
   354             {
       
   355                 TInt err;
       
   356                 TRAP(err, DoSetLightsL(iLightLevel));
       
   357 
       
   358                 DEBUG_INT(
       
   359                     "nokialcdui::CMIDDeviceControl::DoSetLightsL, err=%d", err);
       
   360             }
       
   361         }
       
   362     }
       
   363 
       
   364     DEBUG_INT2("nokialcdui::CMIDDeviceControl::LightStatusChanged( %d, %d ) -",
       
   365                aTarget, aStatus);
       
   366 }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CMIDDeviceControl::DoSetLightsL
       
   370 // Actually sets the lights
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CMIDDeviceControl::DoSetLightsL(TInt aLevel)
       
   374 {
       
   375     DEBUG_INT("nokialcdui::CMIDDeviceControl::DoSetLightsL( %d ) +", aLevel);
       
   376 
       
   377     iLight->ReserveLightL(iSupportedLights);
       
   378 
       
   379     // 0 means off in our API, but default in HWRMLight API.
       
   380     if (aLevel == 0)
       
   381     {
       
   382         iLight->LightOffL(iSupportedLights,
       
   383                           KHWRMInfiniteDuration,
       
   384                           EFalse);
       
   385     }
       
   386     else
       
   387     {
       
   388         iLight->LightOnL(iSupportedLights,
       
   389                          KHWRMInfiniteDuration,
       
   390                          aLevel, EFalse);
       
   391     }
       
   392 
       
   393     DEBUG_INT("nokialcdui::CMIDDeviceControl::DoSetLightsL( %d ) -", aLevel);
       
   394 }
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // CMIDDeviceControl::LightsBlinking
       
   398 // Checks whether lights are blinking.
       
   399 // -----------------------------------------------------------------------------
       
   400 //
       
   401 TBool CMIDDeviceControl::LightsBlinking()
       
   402 {
       
   403     DEBUG("nokialcdui::CMIDDeviceControl::LightsBlinking +");
       
   404 
       
   405     // Check all the supported lights.
       
   406 
       
   407     TBool isBlinking = EFalse;
       
   408 
       
   409     for (TInt i = 0; i < KSupportedLightCount; i++)
       
   410     {
       
   411         TInt light = KSupportedLights[ i ];
       
   412 
       
   413         if ((iSupportedLights & light) == light)
       
   414         {
       
   415             CHWRMLight::TLightStatus lightStatus = iLight->LightStatus(light);
       
   416             TBool blinking = (lightStatus == CHWRMLight::ELightBlink);
       
   417 
       
   418             isBlinking |= blinking;
       
   419         }
       
   420     }
       
   421 
       
   422 
       
   423     DEBUG_INT("nokialcdui::CMIDDeviceControl::LightsBlinking - %d",
       
   424               isBlinking);
       
   425 
       
   426     return isBlinking;
       
   427 }
       
   428 
       
   429 
       
   430 //  End of File