uifw/AvKon/akncompamode/src/akncompautils.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Compatibility mode utility functions
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <data_caging_path_literals.hrh>
       
    21 #include <barsread.h>
       
    22 #include <bautils.h>
       
    23 #include <aknappui.h>
       
    24 #include <coemop.h>
       
    25 #include <AknScreenMode.h>
       
    26 #include <AknSgcc.h>
       
    27 #include <AknCapServerDefs.h>
       
    28 #include <AknNotifierWrapperDefs.h>
       
    29 #include <centralrepository.h>
       
    30 #include <AvkonInternalCRKeys.h>
       
    31 #include <pslninternalcrkeys.h>
       
    32 
       
    33 #include <akncompamode.rsg>
       
    34 
       
    35 #include "akncompautils.h"
       
    36 #include "akncompakb.h"
       
    37 #include "akncompabutton.h"
       
    38 #include "akncompaclearer.h"
       
    39 #include "akncompaflags.h"
       
    40 
       
    41 // Class to detect a console application via MopGetObject
       
    42 class MConsoleApp
       
    43     {
       
    44 public:
       
    45     DECLARE_TYPE_ID(0x2001b26a)
       
    46     };
       
    47 
       
    48 // --------------------------------------------------------------------------
       
    49 // Determine if app requires compa-mode and set to compa screen
       
    50 // mode if so
       
    51 // --------------------------------------------------------------------------
       
    52 TInt AknCompaUtils::SetCompaAppScreenModeL(TBool& aScrModeChanged,
       
    53     TBool& aIsConsoleApp, CAknCompaClearer*& aClearer, TInt aAppUiFlags,
       
    54     CAknAppUiBase& aAppUi, const CCoeEnv& aCoeEnv, CRepository& aRepository)
       
    55     {
       
    56     // Change application screen mode before a call to BaseConstructL().
       
    57     // This prevents HandleResourceChangeL() to be called before application
       
    58     // views have been constructed.
       
    59 
       
    60     aScrModeChanged = EFalse;
       
    61     aIsConsoleApp = IsConsoleApp(aAppUi);
       
    62     aClearer = NULL;
       
    63     TInt screenMode = KErrNotFound;
       
    64 
       
    65     TInt configFlags = 0;
       
    66     aRepository.Get(KAknCompaModeSettings, configFlags);
       
    67 
       
    68     // Before CAknAppUi::BaseConstructL(), current screen mode is the
       
    69     // same as the application that starts the current application.
       
    70     TCompaAppCheckResult result =
       
    71         IsCompaModeAppL(aIsConsoleApp, configFlags, aAppUiFlags,
       
    72         aCoeEnv);
       
    73     if (result == ECompaCheckResultYes)
       
    74         {
       
    75         screenMode = FindCompaScreenMode();
       
    76         if (screenMode != KErrNotFound)
       
    77             {
       
    78             // Clear whole screen with a skin backround before
       
    79             // screen mode changes
       
    80             CAknCompaClearer* clearer = CAknCompaClearer::NewLC();
       
    81 
       
    82             SetAppScreenModeL(aAppUi, screenMode);
       
    83             aScrModeChanged = ETrue;
       
    84             CleanupStack::Pop(); // clearer
       
    85             aClearer = clearer; // transfer ownership
       
    86             }
       
    87         }
       
    88     else if (InGlobalUiSrv(aAppUiFlags))
       
    89         {
       
    90         // Current process is a server that may display global
       
    91         // notes/notifications. Return compa screen mode but don't
       
    92         // set it as current mode.
       
    93         screenMode = FindCompaScreenMode();
       
    94         }
       
    95     return screenMode;
       
    96     }
       
    97 
       
    98 // --------------------------------------------------------------------------
       
    99 // Find compatibility screen mode
       
   100 // --------------------------------------------------------------------------
       
   101 TInt AknCompaUtils::FindCompaScreenMode()
       
   102     {
       
   103     // Look for screen mode style name QVGACOMPA
       
   104     const TInt KNameHash = 0xA786E9F3;
       
   105     CAknLayoutConfig::TScreenModeArray scrModeArray =
       
   106         CAknSgcClient::LayoutConfig().ScreenModes();
       
   107     TInt nModes = scrModeArray.Count();
       
   108     TInt mode = KErrNotFound;
       
   109     for(TInt i = 0; i < nModes && mode == KErrNotFound; i++)
       
   110         {
       
   111         if (scrModeArray.At(i).ScreenStyleHash() == KNameHash)
       
   112             {
       
   113             mode = i;
       
   114             }
       
   115         }
       
   116     return mode;
       
   117     }
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // Check if process is a server that displays global
       
   121 // notes/notifications (Eikon server, Avkon notify and cap servers)
       
   122 // --------------------------------------------------------------------------
       
   123 TBool AknCompaUtils::IsGlobalUiSrv(TSecureId aSecureId)
       
   124     {
       
   125     return IsAknCapSrv(aSecureId) ||
       
   126         aSecureId.iId == KCommonNotifierAppSrvUid.iUid ||
       
   127         IsEikSrv(aSecureId);
       
   128     }
       
   129 
       
   130 // --------------------------------------------------------------------------
       
   131 // Check if process is eikon server
       
   132 // --------------------------------------------------------------------------
       
   133 TBool AknCompaUtils::IsEikSrv(TSecureId aSecureId)
       
   134     {
       
   135     const TUid KEikSrvUid = {0x10003a4a};
       
   136     return aSecureId.iId == KEikSrvUid.iUid;
       
   137     }
       
   138 
       
   139 // --------------------------------------------------------------------------
       
   140 // Check if process is AknCap server
       
   141 // --------------------------------------------------------------------------
       
   142 TBool AknCompaUtils::IsAknCapSrv(TSecureId aSecureId)
       
   143     {
       
   144     return aSecureId.iId == KAknCapServerUid.iUid;
       
   145     }
       
   146 
       
   147 // --------------------------------------------------------------------------
       
   148 // Read and create buttons from a resource file
       
   149 // --------------------------------------------------------------------------
       
   150 void AknCompaUtils::ReadButtonsL(RPointerArray<CAknCompaButton>& aButtons,
       
   151     TInt& aPenButton, CCoeEnv& aCoeEnv)
       
   152     {
       
   153     RResourceFile resFile;
       
   154     AknCompaUtils::OpenResourceFileLC(resFile, aCoeEnv.FsSession());
       
   155 
       
   156     // Add localized text resource to CoeEnv
       
   157     TInt textResFileOffs = AddTextResourceFileL(aCoeEnv);
       
   158 
       
   159     // Read button resources
       
   160     HBufC8* buff = resFile.AllocReadLC(R_COMPAMODE_BUTTONS);
       
   161     TResourceReader reader;
       
   162     reader.SetBuffer(buff);   
       
   163 
       
   164     TInt buttonFlags(reader.ReadInt16());
       
   165     TInt numButtons(reader.ReadInt16());
       
   166 
       
   167     aPenButton = KErrNotFound;
       
   168     for(TInt i = 0; i < numButtons; i++)
       
   169         {
       
   170         // Create button
       
   171         CAknCompaButton* button = CAknCompaButton::NewLC(reader);
       
   172 
       
   173         if (button->ScanCode() != EStdKeyRightShift)
       
   174             {
       
   175             // Set flags for button
       
   176             button->SetButtonFlags(buttonFlags);
       
   177             }
       
   178         else
       
   179             {
       
   180             // Enable long press event for "pen" button
       
   181             button->SetButtonFlags(buttonFlags + KAknButtonReportOnLongPress);
       
   182             aPenButton = i;
       
   183             }
       
   184 
       
   185         aButtons.AppendL(button);
       
   186 
       
   187         CleanupStack::Pop(); // button
       
   188         }  
       
   189 
       
   190     aCoeEnv.DeleteResourceFile(textResFileOffs);
       
   191 
       
   192     CleanupStack::PopAndDestroy(2); // buff, resFile
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // Open compamode resource file
       
   197 // --------------------------------------------------------------------------
       
   198 void AknCompaUtils::OpenResourceFileLC(RResourceFile& aResourceFile,
       
   199     RFs& aFsSession)
       
   200     {
       
   201     _LIT(KResourceFileName, "z:akncompamode.rsc");
       
   202 
       
   203     TParse parse;
       
   204     parse.Set(KResourceFileName, &KDC_RESOURCE_FILES_DIR, NULL);
       
   205     TFileName fileName(parse.FullName());
       
   206     aResourceFile.OpenL(aFsSession, fileName);
       
   207 
       
   208     CleanupClosePushL(aResourceFile);
       
   209     aResourceFile.ConfirmSignatureL();
       
   210     }
       
   211 
       
   212 // --------------------------------------------------------------------------
       
   213 // Add compamode localized text resource file into CoeEnv
       
   214 // --------------------------------------------------------------------------
       
   215 TInt AknCompaUtils::AddTextResourceFileL(CCoeEnv& aCoeEnv)
       
   216     {
       
   217     _LIT(KResourceFileName, "z:akncomparesources.rsc");
       
   218 
       
   219     TParse parse;
       
   220     parse.Set(KResourceFileName, &KDC_RESOURCE_FILES_DIR, NULL);
       
   221     TFileName fileName(parse.FullName());
       
   222 
       
   223     BaflUtils::NearestLanguageFile(aCoeEnv.FsSession(), fileName);
       
   224     return aCoeEnv.AddResourceFileL(fileName);
       
   225     }
       
   226 
       
   227 // --------------------------------------------------------------------------
       
   228 // Wait for effects to go into disabled state
       
   229 // --------------------------------------------------------------------------
       
   230 void AknCompaUtils::WaitTransEffectsOff(CRepository& aThemesCenRep)
       
   231     {
       
   232     const TInt KTick = 10000; // 10 ms
       
   233     const TInt KMaxWait = 100;
       
   234 
       
   235     // There could be a timing issue between CenRep variable change and
       
   236     // when tfx server actually disables the effects. Tested following loop
       
   237     // with a busy wait and there were no problems. So timing doesn't seem
       
   238     // to matter. It would be better of course if tfx server
       
   239     // would provide a feedback when effects actually are disabled.
       
   240     TInt effects;
       
   241     for(TInt i = 0; i < KMaxWait; i++)
       
   242         {
       
   243         aThemesCenRep.Get(KThemesTransitionEffects, effects);
       
   244         if (effects == KAknCompaModeEffectsDisabled)
       
   245             {
       
   246             return;
       
   247             }
       
   248         User::After(KTick);
       
   249         }
       
   250     }
       
   251 
       
   252 // --------------------------------------------------------------------------
       
   253 // Panic application
       
   254 // --------------------------------------------------------------------------
       
   255 void AknCompaUtils::Panic(TInt aCode)
       
   256     {
       
   257     _LIT(KCategory, "Compamode");
       
   258     User::Panic(KCategory, aCode);
       
   259     }
       
   260 
       
   261 // --------------------------------------------------------------------------
       
   262 // Scale rectangle to correct size and move it if necessary.
       
   263 // --------------------------------------------------------------------------
       
   264 void AknCompaUtils::ScaleRect(TRect& aRect, TInt aMoveX, TInt aMoveY)
       
   265     {    
       
   266     aRect.iTl.iY = (aRect.iTl.iY * 2);
       
   267     aRect.iBr.iY = (aRect.iBr.iY * 2);
       
   268 
       
   269     TInt bX = aRect.iBr.iX;
       
   270     TInt tmp = bX / 2;
       
   271     aRect.iBr.iX = ((bX * 2) - tmp);
       
   272 
       
   273     TInt tX = aRect.iTl.iX;
       
   274     tmp = tX / 2;
       
   275     aRect.iTl.iX = ((tX * 2) - tmp);
       
   276     
       
   277     if(aMoveX != 0 || aMoveY != 0)
       
   278         {
       
   279         aRect.Move(aMoveX, aMoveY);
       
   280         }
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // Set application screen mode
       
   285 // --------------------------------------------------------------------------
       
   286 void AknCompaUtils::SetAppScreenModeL(CAknAppUiBase& aAppUi, TInt aMode)
       
   287     {
       
   288     TAknScreenModes scrModes = TAknScreenModes::GetModes();
       
   289     TAknScreenModes::SetAppUiScreenModeL(&aAppUi, scrModes[aMode]);
       
   290     }
       
   291 
       
   292 // --------------------------------------------------------------------------
       
   293 // Read a vector of integers from a resource file
       
   294 // --------------------------------------------------------------------------
       
   295 void AknCompaUtils::ReadIntegerResourceL(RArray<TInt>& aArray,
       
   296         const RResourceFile& aResourceFile, TInt aResId)
       
   297     {
       
   298     HBufC8* buff = aResourceFile.AllocReadLC(aResId);
       
   299 
       
   300     TResourceReader resReader;
       
   301     resReader.SetBuffer(buff);
       
   302 
       
   303     aArray.Reset();
       
   304     TInt cnt = resReader.ReadInt16();
       
   305     while(cnt--)
       
   306         {
       
   307         aArray.AppendL(resReader.ReadInt32());
       
   308         }
       
   309     CleanupStack::PopAndDestroy(); // buff
       
   310     }
       
   311 
       
   312 // --------------------------------------------------------------------------
       
   313 // Check if application requires a compatibility mode
       
   314 // --------------------------------------------------------------------------
       
   315 AknCompaUtils::TCompaAppCheckResult AknCompaUtils::IsCompaModeAppL(
       
   316     TBool aIsConsoleApp, TInt aConfigFlags, TInt aAppUiFlags,
       
   317     const CCoeEnv& aCoeEnv)
       
   318     {
       
   319     if ((aAppUiFlags & CAknAppUiBase::EAknTouchCompatible) ||
       
   320         (aAppUiFlags & CEikAppUi::ENoScreenFurniture && !aIsConsoleApp) ||
       
   321         (aConfigFlags & KAknCompaSettingEnaApps) == 0)
       
   322         {
       
   323         return ECompaCheckResultNo;
       
   324         }
       
   325 
       
   326     RResourceFile resFile;
       
   327     OpenResourceFileLC(resFile, aCoeEnv.FsSession());
       
   328 
       
   329     RProcess process;
       
   330     TSecureId secureId = process.SecureId();
       
   331 
       
   332     TCompaAppCheckResult appCheckResult = ECompaCheckResultUndefinite;
       
   333 
       
   334     RArray<TInt> array;
       
   335     CleanupClosePushL(array);
       
   336     // Application is not put into compa-mode if it is in non compa-mode list
       
   337     ReadIntegerResourceL(array, resFile, R_COMPAMODE_NON_COMPA_APPS);
       
   338     if (array.Find(secureId.iId) != KErrNotFound)
       
   339         {
       
   340         appCheckResult = ECompaCheckResultNo;
       
   341         }
       
   342     if (appCheckResult == ECompaCheckResultUndefinite)
       
   343         {
       
   344         // Application is put into compa-mode if it is in compa-mode list
       
   345         ReadIntegerResourceL(array, resFile, R_COMPAMODE_COMPA_APPS);
       
   346         if (array.Find(secureId.iId) != KErrNotFound)
       
   347             {
       
   348             appCheckResult = ECompaCheckResultYes;
       
   349             }
       
   350         }
       
   351 
       
   352     if (appCheckResult == ECompaCheckResultUndefinite)
       
   353         {
       
   354         // By default, all applications included in a rom image are
       
   355         // not put into compa-mode. Unless overriden by a flag or if
       
   356         // the app. is a console application.
       
   357         TFileName fileName = process.FileName();
       
   358         _LIT(KRomDrive,"z:");
       
   359         if (fileName.FindF(KRomDrive) == 0)
       
   360             {
       
   361             appCheckResult =
       
   362                 aConfigFlags & KAknCompaSettingEnaRomApps || aIsConsoleApp ?
       
   363                 ECompaCheckResultYes:ECompaCheckResultNo;
       
   364             }
       
   365         }
       
   366 
       
   367     if (appCheckResult == ECompaCheckResultUndefinite)
       
   368         {
       
   369         // Application is not executing from rom drive. Go to compa-mode.
       
   370         appCheckResult = ECompaCheckResultYes;
       
   371         }
       
   372 
       
   373     CleanupStack::PopAndDestroy(2); // resFile, array
       
   374     return appCheckResult;
       
   375     }
       
   376 
       
   377 // --------------------------------------------------------------------------
       
   378 // Check if current process is a server that displays global
       
   379 // notes/notifications (Eikon server, Avkon notify and cap servers)
       
   380 // --------------------------------------------------------------------------
       
   381 TBool AknCompaUtils::InGlobalUiSrv(TInt aAppUiFlags)
       
   382     {
       
   383     // Optimization: don't check secure id if ENoScreenFurniture
       
   384     // flag not set.
       
   385     TBool inGlobalUiSrv = EFalse;
       
   386     if (aAppUiFlags & CEikAppUi::ENoScreenFurniture)
       
   387         {
       
   388         RProcess process;
       
   389         inGlobalUiSrv = IsGlobalUiSrv(process.SecureId());
       
   390         }
       
   391     return inGlobalUiSrv;
       
   392     }
       
   393 
       
   394 // --------------------------------------------------------------------------
       
   395 // Check if application is console application
       
   396 // --------------------------------------------------------------------------
       
   397 TBool AknCompaUtils::IsConsoleApp(CAknAppUiBase& aAppUi)
       
   398     {
       
   399     MConsoleApp* consoleApp;
       
   400     aAppUi.MopGetObjectNoChaining(consoleApp);
       
   401     return consoleApp != NULL;
       
   402     }