lafagnosticuifoundation/cone/src/coevwman.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "COEVWMAN.H"
       
    17 #include "coepriv.h"	// CCoeView
       
    18 #include <coeaui.h>
       
    19 #include "coepanic.h"
       
    20 #include <coecntss.h>
       
    21 
       
    22 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    23 #include <viewclipartner.h>
       
    24 #endif
       
    25 
       
    26 const TInt KViewArrayGranularity=5;
       
    27 
       
    28 //
       
    29 // Class CCoeViewObserver
       
    30 //
       
    31 
       
    32 class CCoeViewObserver : public CBase, public MCoeViewObserver
       
    33 	{
       
    34 public:
       
    35 	CCoeViewObserver(CCoeViewManager& aViewManager);
       
    36 	~CCoeViewObserver();
       
    37 public: // from MCoeViewObserver
       
    38 	void HandleViewEventL(const TVwsViewEvent& aEvent);
       
    39 private:
       
    40 	CCoeViewManager& iViewManager;
       
    41 	};
       
    42 
       
    43 CCoeViewObserver::~CCoeViewObserver()
       
    44 	{}
       
    45 
       
    46 CCoeViewObserver::CCoeViewObserver(CCoeViewManager& aViewManager)
       
    47 	: iViewManager(aViewManager)
       
    48 	{}
       
    49 
       
    50 void CCoeViewObserver::HandleViewEventL(const TVwsViewEvent& aEvent)
       
    51 	{
       
    52 	switch (aEvent.iEventType)
       
    53 		{
       
    54 		case TVwsViewEvent::EVwsDeactivateView:
       
    55 			{
       
    56 			TVwsViewId viewId;
       
    57 			if (iViewManager.GetActiveViewId(viewId) != KErrNotFound)
       
    58 				iViewManager.DoDeactivation(viewId, aEvent.iViewTwoId, TVwsViewEvent::EVwsDeactivateView);
       
    59 			}
       
    60 			break;
       
    61 		default:
       
    62 			break;
       
    63 		}
       
    64 	}
       
    65 
       
    66 //
       
    67 // Class CCoeViewManager
       
    68 //
       
    69 
       
    70 CCoeViewManager::CCoeViewManager(CCoeEnv& aCoeEnv,CCoeAppUi& aAppUi,CCoeAppUi* aPreviousAppUi)
       
    71 	: iAppUi(aAppUi), iCoeEnv(aCoeEnv), iPreviousAppUi(aPreviousAppUi), iRegisteredApplicationView(EFalse),
       
    72 	  iViewArray(KViewArrayGranularity), iViewDeactivationObserverArray(KViewArrayGranularity),
       
    73 	  iViewActivationObserverArray(KViewArrayGranularity), iViewObserverArray(KViewArrayGranularity),
       
    74 	  iActiveViewIndex(KErrNotFound), iDefaultViewId(KNullViewId),
       
    75 	  iLastActiveViewId(KNullViewId), iActiveViewId(KNullViewId), iApplicationViewId(KNullViewId)
       
    76 	{
       
    77 	}
       
    78 
       
    79 void CCoeViewManager::ConstructL()
       
    80 	{
       
    81 	iVwsSession=iCoeEnv.InitViewServerSessionL(*this);
       
    82 	if (iPreviousAppUi)
       
    83 		{
       
    84 		iViewObserver=new(ELeave) CCoeViewObserver(*this);
       
    85 		AddViewObserverL(iViewObserver);
       
    86 		}
       
    87 	}
       
    88 
       
    89 CCoeViewManager* CCoeViewManager::NewL(CCoeEnv& aCoeEnv,CCoeAppUi& aAppUi,CCoeAppUi* aPreviousAppUi)
       
    90 	{
       
    91 	CCoeViewManager* self=new(ELeave) CCoeViewManager(aCoeEnv,aAppUi,aPreviousAppUi);
       
    92 	CleanupStack::PushL(self);
       
    93 	self->ConstructL();
       
    94 	CleanupStack::Pop();	// self
       
    95 	return self;
       
    96 	}
       
    97 
       
    98 CCoeViewManager::~CCoeViewManager()
       
    99 	{
       
   100 	if (iRegisteredApplicationView)
       
   101 		{
       
   102 		DeregisterApplicationView();
       
   103 		}
       
   104 
       
   105 	if (iPreviousAppUi)
       
   106 		{
       
   107 		RemoveViewObserver(iViewObserver);
       
   108 		delete iViewObserver;
       
   109 		}
       
   110 
       
   111 	iViewArray.ResetAndDestroy();
       
   112 	iViewDeactivationObserverArray.Reset();
       
   113 	iViewActivationObserverArray.Reset();
       
   114 	iViewObserverArray.Reset();
       
   115 	}
       
   116 
       
   117 void CCoeViewManager::HandleViewEventL(const TVwsViewEvent& aEvent)
       
   118 	{
       
   119 	switch (aEvent.iEventType)
       
   120 		{
       
   121 		case TVwsViewEvent::EVwsActivateView:
       
   122 			DoActivationL(aEvent.iViewOneId,aEvent.iViewTwoId,aEvent.iCustomMessageId,aEvent.iCustomMessageLength);
       
   123 			break;
       
   124 		case TVwsViewEvent::EVwsDeactivateView:
       
   125 		case TVwsViewEvent::EVwsDeactivateViewDifferentInstance:
       
   126 			DoDeactivation(aEvent.iViewOneId, aEvent.iViewTwoId, aEvent.iEventType);
       
   127 			break;
       
   128 		case TVwsViewEvent::EVwsScreenDeviceChanged:
       
   129 			{
       
   130 			TVwsViewIdAndMessage event;
       
   131 			TRAP_IGNORE(event=HandleScreenDeviceChangedL(aEvent.iViewOneId));
       
   132 			iVwsSession->ActivateViewViaViewEvent(event);
       
   133 			}
       
   134 			break;
       
   135 		case TVwsViewEvent::EVwsDeactivationNotification:
       
   136 			HandleViewDeactivationNotification(aEvent.iViewOneId,aEvent.iViewTwoId);
       
   137 			break;
       
   138 		case TVwsViewEvent::EVwsActivationNotification:
       
   139 			HandleViewActivationNotification(aEvent.iViewOneId,aEvent.iViewTwoId);
       
   140 			break;
       
   141 		default:
       
   142 			Panic(ECoePanicViewEventNotHandled);
       
   143 			break;
       
   144 		}
       
   145 
       
   146 	const TInt numObservers=iViewObserverArray.Count();
       
   147 	for (TInt ii=0;ii<numObservers;ii++)
       
   148 		{
       
   149 		iViewObserverArray[ii]->HandleViewEventL(aEvent);
       
   150 		}
       
   151 	}
       
   152 
       
   153 void CCoeViewManager::ActivateViewL(const TVwsViewId& aViewId, TBool aActivatingTopView)
       
   154  	{
       
   155 	ActivateViewL(aViewId,TUid::Null(),KNullDesC8,aActivatingTopView);
       
   156  	}
       
   157 
       
   158 void ClearActivatingTopViewFlag(TAny* aFlagPtr)
       
   159  	{
       
   160 	*(TBool*)aFlagPtr = EFalse;
       
   161  	}
       
   162 
       
   163 void CCoeViewManager::ActivateViewL(const TVwsViewId& aViewId,const TUid& aCustomMessageId,const TDesC8& aCustomMessage,TBool aActivatingTopView)
       
   164  	{
       
   165  	// Ignore duplicate ActivatingTopView request when there is already one outstanding.
       
   166  	if (iActivatingTopView && aActivatingTopView)
       
   167  	    {
       
   168  	    return;
       
   169  	    }
       
   170 	iActivatingTopView = aActivatingTopView;
       
   171 	CleanupStack::PushL(TCleanupItem(ClearActivatingTopViewFlag, &iActivatingTopView));
       
   172 	if (iPreviousAppUi)
       
   173 		{
       
   174 		if (ViewIndex(aViewId)!=KErrNotFound || aViewId==iApplicationViewId)
       
   175 			{
       
   176 			iCustomMessage.Set(aCustomMessage);
       
   177 			TVwsViewId viewIdToDeactivate = (aViewId==iLastActiveViewId) ? KNullViewId : iLastActiveViewId;
       
   178 			DoActivationL(aViewId,viewIdToDeactivate,aCustomMessageId,iCustomMessage.Length());
       
   179 			if (viewIdToDeactivate!=KNullViewId && viewIdToDeactivate!=aViewId)
       
   180 				{
       
   181 				DoDeactivation(viewIdToDeactivate, aViewId, TVwsViewEvent::EVwsDeactivateView);
       
   182 				}
       
   183 			}
       
   184 		else
       
   185 			{
       
   186 			iPreviousAppUi->ActivateViewL(aViewId,aCustomMessageId,aCustomMessage);
       
   187 			}
       
   188 		}
       
   189 	else
       
   190 		{
       
   191 		if (aViewId == iDefaultViewId && aViewId.iAppUid == aViewId.iViewUid)
       
   192 			{
       
   193 			// If were activating the default view of a non-view based app, don't wait for viewsrv to respond.
       
   194 			CreateActivateViewEventL(aViewId, aCustomMessageId, aCustomMessage);
       
   195 			// record this view as activated to prevent it getting activated twice.
       
   196 			iActiveViewId = aViewId;
       
   197 			}
       
   198 		else
       
   199 			{
       
   200 			iCustomMessage.Set(KNullDesC8);
       
   201 			TInt err=iVwsSession->ActivateView(aViewId,aCustomMessageId,aCustomMessage, iCustomControl);
       
   202 			if (err == KErrOverflow)
       
   203 				{
       
   204 				// avkon hides overflow messages on view activation
       
   205 				err = KLeaveWithoutAlert;
       
   206 				}
       
   207 			User::LeaveIfError(err);
       
   208 			}
       
   209 		}
       
   210 	CleanupStack::Pop(); // flag cleanup item
       
   211 	}
       
   212 
       
   213 void CCoeViewManager::DeactivateActiveViewL()
       
   214 	{
       
   215 	if (iPreviousAppUi)
       
   216 		{
       
   217 		iPreviousAppUi->DeactivateActiveViewL();
       
   218 		}
       
   219 	else
       
   220 		{
       
   221 		User::LeaveIfError(iVwsSession->DeactivateActiveView());
       
   222 		}
       
   223 	}
       
   224 void CCoeViewManager::DeactivateActiveViewIfOwnerMatchL()
       
   225 	{
       
   226  	if (iPreviousAppUi)
       
   227  		{
       
   228  		iPreviousAppUi->DeactivateActiveViewIfOwnerMatchL();
       
   229  		}
       
   230  	else
       
   231  		{
       
   232  		User::LeaveIfError(iVwsSession->DeactivateActiveViewIfOwnerMatch());
       
   233  		}
       
   234  	}
       
   235  
       
   236 
       
   237 void CCoeViewManager::ActivateTopViewL()
       
   238 	{
       
   239 	if (iPreviousAppUi)
       
   240 		{
       
   241 		iPreviousAppUi->ActivateTopViewL();
       
   242 		}
       
   243 	
       
   244 	if (iActiveViewId!=KNullViewId)
       
   245 		{
       
   246 		TVwsViewId activeViewId;
       
   247 		TInt err = iVwsSession->GetCurrentActiveViewInSystem(activeViewId);
       
   248 		if ((err == KErrNone) && (iActiveViewId == activeViewId))
       
   249 			{
       
   250 			return;
       
   251 			}
       
   252 		}
       
   253 
       
   254 	if (iLastActiveViewId!=KNullViewId)
       
   255 		{
       
   256 		ActivateViewL(iLastActiveViewId, ETrue);
       
   257 		}
       
   258 	else if (iDefaultViewId!=KNullViewId)
       
   259 		{
       
   260 		ActivateViewL(iDefaultViewId, ETrue);
       
   261 		}
       
   262 	else if (iViewArray.Count())
       
   263 		{
       
   264 		ActivateViewL(iViewArray[0]->iView.ViewId(), ETrue);
       
   265 		}
       
   266 	else if (iApplicationViewId!=KNullViewId)
       
   267 		{
       
   268 		ActivateViewL(iApplicationViewId, ETrue);
       
   269 		}
       
   270 	}
       
   271 
       
   272 TInt CCoeViewManager::GetActiveViewId(TVwsViewId& aViewId) const
       
   273 	{
       
   274 	if (iActiveViewId!=KNullViewId)
       
   275 		{
       
   276 		aViewId=iActiveViewId;
       
   277 		return KErrNone;
       
   278 		}
       
   279 
       
   280 	return KErrNotFound;
       
   281 	}
       
   282 
       
   283 void CCoeViewManager::RegisterViewL(MCoeView& aView)
       
   284 	{
       
   285 	CCoeView* newView=CCoeView::NewLC(aView);
       
   286 	iViewArray.AppendL(newView);
       
   287 	CleanupStack::Pop(); // newView
       
   288 	if (!iPreviousAppUi)
       
   289 		{
       
   290 		TInt err=iVwsSession->AddView(aView.ViewId());
       
   291 		if (err)
       
   292 			{
       
   293 			iViewArray.Delete(ViewIndex(aView));
       
   294 			User::Leave(err);
       
   295 			}
       
   296 		}
       
   297 	}
       
   298 
       
   299 void CCoeViewManager::RegisterApplicationViewL(const TUid& aAppUid)
       
   300 	{
       
   301 	TVwsViewId viewId(aAppUid,aAppUid);
       
   302 	if (!iPreviousAppUi)
       
   303 		{
       
   304 		TInt err=iVwsSession->AddView(viewId);
       
   305 		if (err!=KErrCouldNotConnect && err!=KErrNone)
       
   306 			{
       
   307 			User::Leave(err);
       
   308 			}
       
   309 		}
       
   310 	iApplicationViewId=viewId;
       
   311 	iDefaultViewId=viewId;
       
   312 	}
       
   313 
       
   314 void CCoeViewManager::DeregisterApplicationView()
       
   315 	{
       
   316 	if (!iPreviousAppUi && iApplicationViewId!=KNullViewId)
       
   317 		{
       
   318 		iVwsSession->RemoveView(iApplicationViewId);	// ignore error
       
   319 		}
       
   320 	iApplicationViewId=KNullViewId;
       
   321 	}
       
   322 
       
   323 void CCoeViewManager::SetDefaultViewL(const MCoeView& aView)
       
   324 	{
       
   325 	iDefaultViewId=aView.ViewId();
       
   326 	}
       
   327 
       
   328 void CCoeViewManager::SetApplicationViewAsDefaultL()
       
   329 	{
       
   330 	__ASSERT_DEBUG(iApplicationViewId!=KNullViewId,Panic(ECoePanicViewNotFound));
       
   331 	iDefaultViewId=iApplicationViewId;
       
   332 	}
       
   333 
       
   334 void CCoeViewManager::CheckInitializeViewsL(const TUid& aAppUid)
       
   335 	{
       
   336 	const TInt count=iViewArray.Count();
       
   337 	
       
   338 	// Register application view if there are no views
       
   339 	if (count==0 && iApplicationViewId==KNullViewId)
       
   340 		{
       
   341 		RegisterApplicationViewL(aAppUid);
       
   342 		iRegisteredApplicationView=ETrue;
       
   343 		}
       
   344 	
       
   345 	// Set default view if none set
       
   346 	if (iDefaultViewId==KNullViewId)
       
   347 		{
       
   348 		if (count==0)
       
   349 			{
       
   350 			SetApplicationViewAsDefaultL();
       
   351 			}
       
   352 		else
       
   353 			{
       
   354 			iDefaultViewId=iViewArray[0]->iView.ViewId();
       
   355 			}
       
   356 		}
       
   357 	}
       
   358 
       
   359 TInt CCoeViewManager::GetDefaultViewId(TVwsViewId& aViewId) const
       
   360 	{
       
   361 	if (iDefaultViewId!=KNullViewId)
       
   362 		{
       
   363 		aViewId=iDefaultViewId;
       
   364 		return KErrNone;
       
   365 		}
       
   366 
       
   367 	return KErrNotFound;
       
   368 	}
       
   369 
       
   370 void CCoeViewManager::DeregisterView(const MCoeView& aView)
       
   371 	{
       
   372 	const TInt viewIndex=ViewIndex(aView);
       
   373 	if (viewIndex == KErrNotFound)
       
   374 		return;
       
   375 
       
   376 	if (!iPreviousAppUi)
       
   377 		{
       
   378 		__ASSERT_ALWAYS(iVwsSession,Panic(ECoePanicNoViewServerConnection));
       
   379 		iVwsSession->RemoveView(aView.ViewId());	// Ignore error.
       
   380 		if(aView.ViewId()==iLastActiveViewId)
       
   381 			{
       
   382 			iLastActiveViewId=KNullViewId;
       
   383 			}
       
   384 		}
       
   385 	
       
   386 	if (iActiveViewIndex>viewIndex)
       
   387 		{
       
   388 		iActiveViewIndex--;
       
   389 		if (iPreviousAppUi)
       
   390 			{
       
   391 			if (iActiveViewIndex>=0)
       
   392 				{
       
   393 				iActiveViewId=iViewArray[iActiveViewIndex]->iView.ViewId();
       
   394 				}
       
   395 			else
       
   396 				{
       
   397 				iActiveViewId=KNullViewId;
       
   398 				}
       
   399 			iAppUi.SetCurrentControlStackGroupId(iActiveViewId.iViewUid.iUid);
       
   400 			}
       
   401 		}
       
   402 
       
   403 	delete iViewArray[viewIndex];
       
   404 	iViewArray.Delete(viewIndex);
       
   405 	}
       
   406 
       
   407 void CCoeViewManager::AddViewDeactivationObserverL(MCoeViewDeactivationObserver* aViewDeactivationObserver)
       
   408 	{
       
   409 	iViewDeactivationObserverArray.AppendL(aViewDeactivationObserver);
       
   410 	}
       
   411 
       
   412 void CCoeViewManager::RemoveViewDeactivationObserver(MCoeViewDeactivationObserver* aViewDeactivationObserver)
       
   413 	{
       
   414 	TInt pos;
       
   415 	TKeyArrayFix key(0,ECmpTInt);
       
   416 	if (iViewDeactivationObserverArray.Find(aViewDeactivationObserver,key,pos)==KErrNone)
       
   417 		{
       
   418 		iViewDeactivationObserverArray.Delete(pos);
       
   419 		}
       
   420 	}
       
   421 
       
   422 void CCoeViewManager::NotifyNextDeactivation(const TVwsViewId& aViewId, MCoeViewDeactivationObserver& aViewDeactivationObserver)
       
   423 	{
       
   424 	__ASSERT_DEBUG(iNextViewDeactivationObserver==NULL,Panic(ECoePanicViewNotificationObserverAlreadyExists));
       
   425 	__ASSERT_ALWAYS(iVwsSession,Panic(ECoePanicNoViewServerConnection));
       
   426 
       
   427 	iNextViewDeactivationObserver=&aViewDeactivationObserver;
       
   428 	iVwsSession->NotifyNextDeactivation(aViewId);
       
   429 	}
       
   430 
       
   431 void CCoeViewManager::NotifyNextDeactivation(MCoeViewDeactivationObserver& aViewDeactivationObserver)
       
   432 	{
       
   433 	__ASSERT_DEBUG(iNextViewDeactivationObserver==NULL,Panic(ECoePanicViewNotificationObserverAlreadyExists));
       
   434 	__ASSERT_ALWAYS(iVwsSession,Panic(ECoePanicNoViewServerConnection));
       
   435 
       
   436 	iNextViewDeactivationObserver=&aViewDeactivationObserver;
       
   437 	iVwsSession->NotifyNextDeactivation();
       
   438 	}
       
   439 
       
   440 void CCoeViewManager::AddViewActivationObserverL(MCoeViewActivationObserver* aViewActivationObserver)
       
   441 	{
       
   442 	iViewActivationObserverArray.AppendL(aViewActivationObserver);
       
   443 	}
       
   444 
       
   445 void CCoeViewManager::RemoveViewActivationObserver(MCoeViewActivationObserver* aViewActivationObserver)
       
   446 	{
       
   447 	TInt pos;
       
   448 	TKeyArrayFix key(0,ECmpTInt);
       
   449 	if (iViewActivationObserverArray.Find(aViewActivationObserver,key,pos)==KErrNone)
       
   450 		{
       
   451 		iViewActivationObserverArray.Delete(pos);
       
   452 		}
       
   453 #ifdef _DEBUG
       
   454 	else
       
   455 		{
       
   456 		RDebug::Printf("CCoeViewManager::RemoveViewActivationObserver. Coe view observer not found.");		
       
   457 		}
       
   458 #endif
       
   459 	}
       
   460 
       
   461 void CCoeViewManager::NotifyNextActivation(const TVwsViewId& aViewId, MCoeViewActivationObserver& aViewActivationObserver)
       
   462 	{
       
   463 	__ASSERT_DEBUG(iNextViewActivationObserver==NULL,Panic(ECoePanicViewNotificationObserverAlreadyExists));
       
   464 	__ASSERT_ALWAYS(iVwsSession,Panic(ECoePanicNoViewServerConnection));
       
   465 
       
   466 	iNextViewActivationObserver=&aViewActivationObserver;
       
   467 	iVwsSession->NotifyNextActivation(aViewId);
       
   468 	}
       
   469 
       
   470 void CCoeViewManager::NotifyNextActivation(MCoeViewActivationObserver& aViewActivationObserver)
       
   471 	{
       
   472 	__ASSERT_DEBUG(iNextViewActivationObserver==NULL,Panic(ECoePanicViewNotificationObserverAlreadyExists));
       
   473 	__ASSERT_ALWAYS(iVwsSession,Panic(ECoePanicNoViewServerConnection));
       
   474 
       
   475 	iNextViewActivationObserver=&aViewActivationObserver;
       
   476 	iVwsSession->NotifyNextActivation();
       
   477 	}
       
   478 
       
   479 void CCoeViewManager::AddViewObserverL(MCoeViewObserver* aViewObserver)
       
   480 	{
       
   481 	if (iPreviousAppUi)
       
   482 		{
       
   483 		iPreviousAppUi->AddViewObserverL(aViewObserver);
       
   484 		}
       
   485 	else
       
   486 		{
       
   487 		iViewObserverArray.AppendL(aViewObserver);
       
   488 		}
       
   489 	}
       
   490 
       
   491 void CCoeViewManager::RemoveViewObserver(MCoeViewObserver* aViewObserver)
       
   492 	{
       
   493 	if (iPreviousAppUi)
       
   494 		{
       
   495 		iPreviousAppUi->RemoveViewObserver(aViewObserver);
       
   496 		}
       
   497 	else
       
   498 		{
       
   499 		TInt pos;
       
   500 		TKeyArrayFix key(0,ECmpTInt);
       
   501 		if (iViewObserverArray.Find(aViewObserver,key,pos)==KErrNone)
       
   502 			{
       
   503 			iViewObserverArray.Delete(pos);
       
   504 			}
       
   505 		}
       
   506 	}
       
   507 
       
   508 TInt CCoeViewManager::ViewIndex(const MCoeView& aView) const
       
   509 	{
       
   510 	const TInt numViews=iViewArray.Count();
       
   511 	
       
   512 	for (TInt ii=0;ii<numViews;ii++)
       
   513 		{
       
   514 		const MCoeView& view=iViewArray[ii]->iView;
       
   515 		if (&view==&aView)
       
   516 			{
       
   517 			return ii;
       
   518 			}
       
   519 		}
       
   520 
       
   521 	return KErrNotFound;
       
   522 	}
       
   523 
       
   524 TInt CCoeViewManager::ViewIndex(const TVwsViewId& aViewId) const
       
   525 	{
       
   526 	const TInt numViews=iViewArray.Count();
       
   527 	
       
   528 	for (TInt ii=0;ii<numViews;ii++)
       
   529 		{
       
   530 		if (iViewArray[ii]->iView.ViewId()==aViewId)
       
   531 			{
       
   532 			return ii;
       
   533 			}
       
   534 		}
       
   535 
       
   536 	return KErrNotFound;
       
   537 	}
       
   538 
       
   539 void CCoeViewManager::DoActivationL(const TVwsViewId& aViewId,const TVwsViewId& aPreviousViewId,const TUid& aCustomMessageId,TInt aCustomMessageLength)
       
   540 	{
       
   541 	// Retrieve custom message from server if necessary.
       
   542 	HBufC8* customMessage=NULL;
       
   543 	if (aCustomMessageLength)
       
   544 		{
       
   545 		if (!iPreviousAppUi)
       
   546 			{
       
   547 			customMessage=HBufC8::NewLC(aCustomMessageLength);
       
   548 			TPtr8 msgPtr=customMessage->Des();
       
   549 			User::LeaveIfError(iVwsSession->RequestCustomMessage(msgPtr));
       
   550 			}
       
   551 		else
       
   552 			{
       
   553 			customMessage=iCustomMessage.AllocLC();
       
   554 			}
       
   555 		}
       
   556 
       
   557 	if (aViewId.iViewUid.iUid==aViewId.iAppUid.iUid)
       
   558 		{
       
   559 		if (customMessage)
       
   560 			{
       
   561 			CleanupStack::PopAndDestroy(customMessage); 
       
   562 			}
       
   563 		CompleteActivation(-1,aViewId,aPreviousViewId);
       
   564 		return;
       
   565 		}
       
   566 
       
   567 	// Find the index to activate
       
   568 	TInt indexToActivate=ViewIndex(aViewId);
       
   569 	__ASSERT_ALWAYS(indexToActivate!=KErrNotFound, Panic(ECoePanicViewNotFound));
       
   570 	CCoeView& viewToActivate=*(iViewArray[indexToActivate]);
       
   571 
       
   572 	// If first activation, construct.
       
   573 	if (!(viewToActivate.iConstructed))
       
   574 		{
       
   575 		viewToActivate.iView.ViewConstructL();
       
   576 		viewToActivate.iConstructed=ETrue;
       
   577 		}
       
   578 		
       
   579 	// check if view has a preferred screen mode
       
   580 	viewToActivate.iView.PrepareForViewActivation();
       
   581 	
       
   582 	// check view can cope with screenmode
       
   583 	const TInt screenMode=iCoeEnv.ScreenDevice()->CurrentScreenMode();
       
   584 	if (!viewToActivate.iView.ViewScreenModeCompatible(screenMode))
       
   585 		{
       
   586 		User::Leave(KErrViewWrongMode);
       
   587 		}
       
   588 	iActivatingViewId = aViewId;
       
   589 	// Do the activation.
       
   590 	if (customMessage)
       
   591 		{
       
   592 		viewToActivate.iView.ViewActivatedL(aPreviousViewId,aCustomMessageId,*customMessage);
       
   593 		CleanupStack::PopAndDestroy(customMessage);  
       
   594 		}
       
   595 	else
       
   596 		{
       
   597 		viewToActivate.iView.ViewActivatedL(aPreviousViewId,aCustomMessageId,KNullDesC8);
       
   598 		}
       
   599 	CompleteActivation(indexToActivate,aViewId,aPreviousViewId);
       
   600 	}
       
   601 
       
   602 TBool CCoeViewManager::CheckSourceOfViewSwitchL(const TSecurityPolicy& aSecurityPolicy,const char* aDiagnostic) const
       
   603 	{
       
   604 	if (iPreviousAppUi!=NULL)
       
   605 		{
       
   606 		return iPreviousAppUi->CheckSourceOfViewSwitchL(aSecurityPolicy,aDiagnostic);
       
   607 		}
       
   608 	TBool result=EFalse;
       
   609 	User::LeaveIfError(iVwsSession->CheckSourceOfViewSwitch(result,aSecurityPolicy,aDiagnostic));
       
   610 	return result;
       
   611 	}
       
   612 
       
   613 void CCoeViewManager::CompleteActivation(TInt aActiveViewIndex,const TVwsViewId& aViewId,const TVwsViewId& aPreviousViewId)
       
   614 	{
       
   615 	const TInt numObservers=iViewActivationObserverArray.Count();
       
   616 	for (TInt ii=0;ii<numObservers;ii++)
       
   617 		{
       
   618 		iViewActivationObserverArray[ii]->HandleViewActivation(aViewId,aPreviousViewId);
       
   619 		}
       
   620 	
       
   621 	// 	Should the ordinal only be set as long as the top view is not active?
       
   622 	if(CCoeControlStaticSettings::OrdinalForAllViews() == EFalse) // default behaviour
       
   623 		{
       
   624 		if (!iActivatingTopView)
       
   625 			{
       
   626 			if(!iCustomControl)
       
   627 				{
       
   628 				iCoeEnv.WsSession().SetWindowGroupOrdinalPosition(iCoeEnv.RootWin().Identifier(),iWindowGroupOrdinal);	
       
   629 				}
       
   630 			}
       
   631 			// else do nothing
       
   632 		}
       
   633 	else // Set for all views
       
   634 		{
       
   635 		if(!iCustomControl)
       
   636 			{
       
   637 			iCoeEnv.WsSession().SetWindowGroupOrdinalPosition(iCoeEnv.RootWin().Identifier(),iWindowGroupOrdinal);
       
   638 			}
       
   639 		}
       
   640 		
       
   641 	iActivatingTopView = EFalse;
       
   642 	iActiveViewIndex=aActiveViewIndex;
       
   643 	iActiveViewId=aViewId;
       
   644 	iAppUi.SetCurrentControlStackGroupId(iActiveViewId.iViewUid.iUid);
       
   645 	iLastActiveViewId=aViewId;
       
   646 	}
       
   647 
       
   648 void CCoeViewManager::DoDeactivation(const TVwsViewId& aViewId, const TVwsViewId& aActiveViewId, const TVwsViewEvent::TVwsViewEventType aType)
       
   649 	{
       
   650 	const TInt numObservers=iViewDeactivationObserverArray.Count();
       
   651 	for (TInt ii=0;ii<numObservers;ii++)
       
   652 		{
       
   653 		iViewDeactivationObserverArray[ii]->HandleViewDeactivation(aViewId,aActiveViewId);
       
   654 		}
       
   655 
       
   656 	if (aViewId.iViewUid.iUid != aViewId.iAppUid.iUid)
       
   657 		{	// Only deactive if not a pseudo-application view.
       
   658 		TInt indexToDeactivate=ViewIndex(aViewId);
       
   659 		__ASSERT_ALWAYS(indexToDeactivate!=KErrNotFound, Panic(ECoePanicViewNotFound));
       
   660 		iViewArray[indexToDeactivate]->iView.ViewDeactivated();
       
   661 		}
       
   662 		
       
   663 	/**	Reset active view record if:
       
   664 	1. Newly activated view is not in this app
       
   665 	2. The view to activate and view to deactivate are from different instances of the same application
       
   666 	*/
       
   667 	if ((aViewId.iAppUid!=aActiveViewId.iAppUid) || (aType == TVwsViewEvent::EVwsDeactivateViewDifferentInstance))
       
   668 		{
       
   669 		iActiveViewIndex=-1;
       
   670 		iActiveViewId=KNullViewId;
       
   671 		}
       
   672 	}
       
   673 
       
   674 TVwsViewIdAndMessage CCoeViewManager::HandleScreenDeviceChangedL(const TVwsViewId& aViewId)
       
   675 	{
       
   676 	CCoeScreenDeviceChangeDefaultHandler* screenChangeHandler=CCoeScreenDeviceChangeDefaultHandler::Self();
       
   677 	if (screenChangeHandler)
       
   678 		screenChangeHandler->HandleScreenDeviceChangedL();
       
   679 	if (aViewId.iViewUid.iUid == aViewId.iAppUid.iUid)
       
   680 		{
       
   681 		if (screenChangeHandler)
       
   682 			{
       
   683 			return screenChangeHandler->DefaultViewOnScreenDeviceChanged();
       
   684 			}
       
   685 		return TVwsViewIdAndMessage(aViewId);
       
   686 		}
       
   687 	else
       
   688 		{
       
   689 		TInt index=ViewIndex(aViewId);
       
   690 		__ASSERT_ALWAYS(index!=KErrNotFound, Panic(ECoePanicViewNotFound));
       
   691 		return iViewArray[index]->iView.ViewScreenDeviceChangedL();
       
   692 		}
       
   693 	}
       
   694 
       
   695 void CCoeViewManager::HandleViewDeactivationNotification(const TVwsViewId& aViewId,const TVwsViewId& aActiveViewId)
       
   696 	{
       
   697 	ASSERT(iNextViewDeactivationObserver);
       
   698 	MCoeViewDeactivationObserver* observer=iNextViewDeactivationObserver;
       
   699 	iNextViewDeactivationObserver=NULL;
       
   700 	observer->HandleViewDeactivation(aViewId,aActiveViewId);
       
   701 	}
       
   702 
       
   703 void CCoeViewManager::HandleViewActivationNotification(const TVwsViewId& aActiveViewId,const TVwsViewId& aViewToDeactivatedId)
       
   704 	{
       
   705 	ASSERT(iNextViewActivationObserver);
       
   706 	MCoeViewActivationObserver* observer=iNextViewActivationObserver;
       
   707 	iNextViewActivationObserver=NULL;
       
   708 	observer->HandleViewActivation(aActiveViewId,aViewToDeactivatedId);
       
   709 	}
       
   710 
       
   711 void CCoeViewManager::CreateActivateViewEventL(const TVwsViewId& aViewId,const TUid& aCustomMessageId,const TDesC8& aCustomMessage)
       
   712 	{
       
   713 	TInt err = iVwsSession->CreateActivateViewEvent(aViewId,aCustomMessageId,aCustomMessage, iCustomControl);
       
   714 	if (err == KErrOverflow)
       
   715 		{
       
   716 		err = KLeaveWithoutAlert;
       
   717 		}
       
   718 	User::LeaveIfError(err);
       
   719 	}
       
   720 
       
   721 void CCoeViewManager::SetSystemDefaultViewL(const TVwsViewId& aViewId,TInt aMode)
       
   722 	{
       
   723 	if(iPreviousAppUi)
       
   724 		{
       
   725 		iPreviousAppUi->SetSystemDefaultViewL(aViewId, aMode);
       
   726 		}
       
   727 	else
       
   728 		{
       
   729 		User::LeaveIfError(iVwsSession->SetSystemDefaultView(aViewId, aMode));
       
   730 		}
       
   731 	}
       
   732 
       
   733 void CCoeViewManager::SetSystemDefaultViewL(const TVwsViewId& aViewId)
       
   734 	{
       
   735 	if(iPreviousAppUi)
       
   736 		{
       
   737 		iPreviousAppUi->SetSystemDefaultViewL(aViewId);
       
   738 		}
       
   739 	else
       
   740 		{
       
   741 		User::LeaveIfError(iVwsSession->SetSystemDefaultView(aViewId));
       
   742 		}
       
   743 	}
       
   744 
       
   745 void CCoeViewManager::GetSystemDefaultViewL(TVwsViewId& aViewId)
       
   746 	{
       
   747 	User::LeaveIfError(iVwsSession->GetSystemDefaultView(aViewId));
       
   748 	}
       
   749 	
       
   750 TBool CCoeViewManager::IsViewConstructed(const TVwsViewId& aViewId) const
       
   751 	{
       
   752 	const TInt viewCount = iViewArray.Count();
       
   753 	for (TInt viewIndex = 0; viewIndex < viewCount; viewIndex++)
       
   754 		{
       
   755 		if (iViewArray[viewIndex]->iView.ViewId() == aViewId && iViewArray[viewIndex]->iConstructed)
       
   756 			{
       
   757 			return ETrue;
       
   758 			}
       
   759 		}
       
   760 	return EFalse;
       
   761 	}
       
   762 
       
   763 TInt CCoeViewManager::EnableExternalViewSwitches(TBool aEnable)
       
   764 	{
       
   765 	return iVwsSession->EnableExternalViewSwitches(aEnable);
       
   766 	}
       
   767 
       
   768 TInt CCoeViewManager::GetTopViewId(TVwsViewId& aViewId) const
       
   769 	{
       
   770 	if (iActivatingViewId!=KNullViewId)
       
   771 		{
       
   772 		aViewId = iActivatingViewId;
       
   773 		return KErrNone;
       
   774 		}
       
   775 	if (iLastActiveViewId!=KNullViewId)
       
   776 		{
       
   777 		aViewId = iLastActiveViewId;
       
   778 		return KErrNone;
       
   779 		}
       
   780 	if (iDefaultViewId!=KNullViewId)
       
   781 		{
       
   782 		aViewId = iDefaultViewId;
       
   783 		return KErrNone;
       
   784 		}
       
   785 	if (iViewArray.Count())
       
   786 		{
       
   787 		aViewId = iViewArray[0]->iView.ViewId();
       
   788 		return KErrNone;
       
   789 		}
       
   790 	if (iApplicationViewId!=KNullViewId)
       
   791 		{
       
   792 		aViewId = iApplicationViewId;
       
   793 		return KErrNone;
       
   794 		}
       
   795 	if (iPreviousAppUi)
       
   796 		{
       
   797 		return iPreviousAppUi->GetTopViewId(aViewId);
       
   798 		}
       
   799 	return KErrNotFound;
       
   800 	}
       
   801 
       
   802 void CCoeViewManager::SetCustomControl(const TInt aCustomControl)
       
   803 	{
       
   804 	iCustomControl=aCustomControl;
       
   805 	}
       
   806 
       
   807 void CCoeViewManager::SetWindowGroupOrdinal(const TInt aWindowGroupOrdinal)
       
   808 	{
       
   809 	iWindowGroupOrdinal=aWindowGroupOrdinal;
       
   810 	}
       
   811 
       
   812 void CCoeViewManager::UpdateViewServerBackgroundColor(const TRgb& aBgColor)
       
   813 	{
       
   814 	iVwsSession->SetWindowBackgroundColor(aBgColor);
       
   815 	}
       
   816 
       
   817 	
       
   818