uifw/AvKon/src/AknSmileyImage.cpp
changeset 0 2f259fa3e83a
child 5 aabf2c525e0f
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002-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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <AknUtils.h>
       
    21 #include <AknsUtils.h>
       
    22 #include <AknIconUtils.h>
       
    23 #include <bautils.h>
       
    24 
       
    25 #include "AknSmileyImage.h"
       
    26 
       
    27 const TInt KFrameMaxInterval = 3*1000*1000; // 30s
       
    28 const TInt KFrameMonitorStep = 5;           // monitor once per 5 call, for increase performence 
       
    29 const TInt KMaxSameFrameRepeat = 6;         // 5 * 6, animation frame keep same for 6 times will be stopped
       
    30 
       
    31 #define DELZ(ptr) {delete (ptr); (ptr)=NULL;}
       
    32 
       
    33 
       
    34 ///////////////////////////////////////////////////////////////////////////////////////////////
       
    35 // BmpUtils
       
    36 ///////////////////////////////////////////////////////////////////////////////////////////////
       
    37 
       
    38 class BmpUtils
       
    39     {
       
    40 public:
       
    41     static void CopyBitmpL(CFbsBitmap* aDesBmp, const CFbsBitmap* aSrcBmp);
       
    42     static TBool BitmpIsSame(const CFbsBitmap* aDesBmp, const CFbsBitmap* aSrcBmp);
       
    43     };
       
    44 
       
    45 void BmpUtils::CopyBitmpL(CFbsBitmap* aDesBmp, const CFbsBitmap* aSrcBmp)
       
    46     {
       
    47     if(!aDesBmp || !aSrcBmp) return;
       
    48 
       
    49     TSize size(aSrcBmp->SizeInPixels());
       
    50     if(aDesBmp->SizeInPixels() != size)
       
    51         {
       
    52         aDesBmp->Reset();
       
    53         aDesBmp->Create(size, aSrcBmp->DisplayMode());
       
    54         }
       
    55 
       
    56     CFbsBitmapDevice* device = CFbsBitmapDevice::NewL(aDesBmp);
       
    57     CleanupStack::PushL(device);
       
    58     CFbsBitGc* gc = NULL;
       
    59     User::LeaveIfError(device->CreateContext(gc));
       
    60     CleanupStack::PushL(gc);
       
    61     gc->BitBlt(TPoint(), aSrcBmp);
       
    62     CleanupStack::PopAndDestroy(2);
       
    63     }
       
    64 
       
    65 TBool BmpUtils::BitmpIsSame(const CFbsBitmap* aDesBmp, const CFbsBitmap* aSrcBmp)
       
    66     {
       
    67     if(!aDesBmp || !aSrcBmp) return FALSE;
       
    68     
       
    69     if(aDesBmp->SizeInPixels() == aSrcBmp->SizeInPixels())
       
    70         {
       
    71         TSize size(aSrcBmp->SizeInPixels());
       
    72         TInt scanLineLength = CFbsBitmap::ScanLineLength(size.iWidth, aSrcBmp->DisplayMode());
       
    73         TInt length = scanLineLength*size.iHeight;
       
    74         
       
    75         aDesBmp->BeginDataAccess();
       
    76         aSrcBmp->BeginDataAccess();
       
    77         TInt result = Mem::Compare((TUint8*)aDesBmp->DataAddress(), length, (TUint8*)aSrcBmp->DataAddress(), length);
       
    78         aSrcBmp->EndDataAccess(TRUE);
       
    79         aDesBmp->EndDataAccess(TRUE);
       
    80         
       
    81         if(result == KErrNone) return TRUE;
       
    82         }
       
    83     
       
    84     return FALSE;
       
    85     }
       
    86 
       
    87 
       
    88 ///////////////////////////////////////////////////////////////////////////////////////////////
       
    89 // SmileyUtils
       
    90 ///////////////////////////////////////////////////////////////////////////////////////////////
       
    91 
       
    92 void SmileyUtils::GetCustomizableResPath(TDes& aResPath, const TDesC& aDefaultResPath)
       
    93     {
       
    94     aResPath = aDefaultResPath;
       
    95     
       
    96     aResPath[0] = 'c';
       
    97     if(!BaflUtils::FileExists(CCoeEnv::Static()->FsSession(), aResPath))
       
    98         {
       
    99         aResPath[0] = aDefaultResPath[0];
       
   100         }
       
   101     };
       
   102 
       
   103 
       
   104 ///////////////////////////////////////////////////////////////////////////////////////////////
       
   105 // CSmileyImage
       
   106 ///////////////////////////////////////////////////////////////////////////////////////////////
       
   107 
       
   108 CSmileyImage* CSmileyImage::NewL(const TAknsItemID& aSkinImage, TInt aPkgImage, TBool aIsAnimation, MSmileyImageObserver* aObserver)
       
   109     {
       
   110     CSmileyImage* self = new (ELeave) CSmileyImage(aSkinImage, aPkgImage, aIsAnimation, aObserver);
       
   111     CleanupStack::PushL(self);
       
   112     self->ConstructL();
       
   113     CleanupStack::Pop(); // self;
       
   114     return self;
       
   115     }
       
   116 
       
   117 void CSmileyImage::ConstructL()
       
   118     {
       
   119     iFrameSnap = new (ELeave) CFbsBitmap;
       
   120     iAsynchronousTaskTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   121     }
       
   122 
       
   123 CSmileyImage::CSmileyImage(const TAknsItemID& aSkinImage, TInt aPkgImage, TBool aIsAnimation, MSmileyImageObserver* aObserver) : 
       
   124 iImagePkgItem(aPkgImage), 
       
   125 iIsAnimation(aIsAnimation), 
       
   126 iImageObserver(aObserver)
       
   127     {
       
   128     iImageSkinItem.Set(aSkinImage);
       
   129     }
       
   130 
       
   131 CSmileyImage::~CSmileyImage()
       
   132     {
       
   133     DoRelease();
       
   134     DELZ(iFrameSnap);
       
   135     DELZ(iAsynchronousTaskTimer);
       
   136     }
       
   137 
       
   138 void CSmileyImage::LoadL(TInt aRepeat, TInt aDelay)
       
   139     {
       
   140     StartLoadAsynchronousL(aRepeat, aDelay);
       
   141     }
       
   142 
       
   143 void CSmileyImage::Release()
       
   144     {
       
   145     DoRelease();
       
   146     }
       
   147 
       
   148 void CSmileyImage::SetSize(const TSize& aSize)
       
   149     {
       
   150     if(iSize != aSize)
       
   151         {
       
   152         iSize = aSize;
       
   153         if(iFrame)
       
   154             {
       
   155             AknIconUtils::SetSize(iFrame, aSize);
       
   156             }
       
   157         }
       
   158     }
       
   159 
       
   160 const TSize& CSmileyImage::Size() const
       
   161     {
       
   162     return iSize;
       
   163     }
       
   164 
       
   165 TBool CSmileyImage::ReadyToDraw() const
       
   166     {
       
   167     return iReadyToDraw;
       
   168     }
       
   169 
       
   170 const CFbsBitmap* CSmileyImage::Image() const
       
   171     {
       
   172     return iFrame;
       
   173     }
       
   174 
       
   175 const CFbsBitmap* CSmileyImage::Mask() const
       
   176     {
       
   177     return iFrameMask;
       
   178     }
       
   179 
       
   180 void CSmileyImage::BitmapChanged(CFbsBitmap* aBitmap)
       
   181     {
       
   182     iReadyToDraw = TRUE; // animation is ready
       
   183 
       
   184     if(iImageObserver) iImageObserver->BitmapChanged(this, aBitmap);
       
   185 
       
   186     TRAP_IGNORE(MonitorAnimationEndedL());
       
   187     }
       
   188 
       
   189 void CSmileyImage::DoLoadL()
       
   190     {
       
   191     StopAnyAsynchronousTask();
       
   192     
       
   193     if(iFrame) return;
       
   194 
       
   195     TFileName smileyMifName;
       
   196     SmileyUtils::GetCustomizableResPath(smileyMifName, KSmileyMif);
       
   197     if(iImageSkinItem.iMinor > 0)
       
   198         {
       
   199         MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   200         TRAP_IGNORE( AknsUtils::CreateColorIconL(skin, iImageSkinItem, 
       
   201                                                KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG19, 
       
   202                                                iFrame, iFrameMask, 
       
   203                                                smileyMifName, iImagePkgItem, iImagePkgItem, 
       
   204                                                AKN_LAF_COLOR(215)));
       
   205         }
       
   206     else
       
   207         {
       
   208         TRAP_IGNORE( AknIconUtils::CreateIconL(iFrame, iFrameMask, smileyMifName, iImagePkgItem, iImagePkgItem));
       
   209         }
       
   210     
       
   211     if(iIsAnimation) // the first frame of animation svg is blank without correct content
       
   212         {
       
   213         iReadyToDraw = FALSE;
       
   214         AknIconUtils::SetObserver(iFrame, this);
       
   215         
       
   216         StopAnimationAsynchronousL(KFrameMaxInterval);
       
   217         }
       
   218     else // the first frame of static svg has correct content
       
   219         {
       
   220         iReadyToDraw = TRUE;
       
   221         if(iImageObserver) iImageObserver->BitmapChanged(this, iFrame);
       
   222         }
       
   223 
       
   224     AknIconUtils::SetSize(iFrame, iSize);
       
   225     }
       
   226 
       
   227 void CSmileyImage::DoRelease()
       
   228     {
       
   229     StopAnyAsynchronousTask();
       
   230     
       
   231     if(!iFrame) return;
       
   232 
       
   233     DELZ(iFrame);
       
   234     DELZ(iFrameMask);
       
   235     iFrameSnap->Reset();
       
   236     
       
   237     iReadyToDraw = FALSE;
       
   238     }
       
   239 
       
   240 void CSmileyImage::StopAnyAsynchronousTask()
       
   241     {
       
   242     iAsynchronousTaskTimer->Cancel();
       
   243     }
       
   244 
       
   245 void CSmileyImage::MonitorAnimationEndedL()
       
   246     {
       
   247     // for animation doesn't call back
       
   248     StopAnimationAsynchronousL(KFrameMaxInterval);
       
   249     
       
   250     // for reduce the times of animation monitor
       
   251     iFrameCounter++;
       
   252     if(iFrameCounter % KFrameMonitorStep) return;
       
   253     
       
   254     // monitor the end of animation clip, replay or stop animation if ended
       
   255     if(BmpUtils::BitmpIsSame(iFrameSnap, iFrame))
       
   256         {
       
   257         iSameFrameCounter++;
       
   258         if(iSameFrameCounter > KMaxSameFrameRepeat)
       
   259             {
       
   260             StopAnimationAsynchronousL();
       
   261             }
       
   262         }
       
   263     else
       
   264         {
       
   265         iSameFrameCounter = 0;
       
   266         BmpUtils::CopyBitmpL(iFrameSnap, iFrame);
       
   267         }
       
   268     }
       
   269 
       
   270 void CSmileyImage::HandleAnimationEndedL()
       
   271     {
       
   272     DoRelease();
       
   273     
       
   274     if(iRepeatCount != 0)
       
   275         {
       
   276         iRepeatCount--;
       
   277         DoLoadL();
       
   278         }
       
   279     }
       
   280 
       
   281 void CSmileyImage::StartLoadAsynchronousL(TInt aRepeat, TInt aDelayMicroSeconds)
       
   282     {
       
   283     iRepeatCount = aRepeat;
       
   284 
       
   285     iAsynchronousTaskTimer->Cancel();
       
   286     iAsynchronousTaskTimer->Start(aDelayMicroSeconds, 1, TCallBack(StartLoadAsynchronousCallBackL,this));
       
   287     }
       
   288 
       
   289 TInt CSmileyImage::StartLoadAsynchronousCallBackL(TAny* aPtr)
       
   290     {
       
   291     CSmileyImage* self = (CSmileyImage*)aPtr;
       
   292     self->StopAnyAsynchronousTask();
       
   293     self->DoLoadL();
       
   294     return KErrNone;
       
   295     }
       
   296 
       
   297 void CSmileyImage::StopAnimationAsynchronousL(TInt aDelayMicroSeconds)
       
   298     {
       
   299     iAsynchronousTaskTimer->Cancel();
       
   300     iAsynchronousTaskTimer->Start(aDelayMicroSeconds, 1, TCallBack(StopAnimationAsynchronousCallBackL,this));
       
   301     }
       
   302 
       
   303 TInt CSmileyImage::StopAnimationAsynchronousCallBackL(TAny* aPtr)
       
   304     {
       
   305     CSmileyImage* self = (CSmileyImage*)aPtr;
       
   306     self->StopAnyAsynchronousTask();
       
   307     self->HandleAnimationEndedL();
       
   308     return KErrNone;
       
   309     }
       
   310 
       
   311 
       
   312 // end of file
       
   313