fotaapplication/fotaserver/FotaServer/src/fotaupdate.cpp
branchRCL_3
changeset 26 19bba8228ff0
equal deleted inserted replaced
25:b183ec05bd8c 26:19bba8228ff0
       
     1 /*
       
     2  * Copyright (c) 2005-2006 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:   starts update sequence
       
    15  *
       
    16  */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <StringLoader.h>
       
    22 #include <fotaserver.rsg>
       
    23 #include <centralrepository.h>
       
    24 #include <AknUtils.h>
       
    25 #include <AknBidiTextUtils.h> 
       
    26 #include <biditext.h>
       
    27 #include <gdi.h>
       
    28 #include <fotaengine.h>
       
    29 #include <apgtask.h>
       
    30 #include <SyncMLNotifierParams.h>
       
    31 #include <aknradiobuttonsettingpage.h> 
       
    32 #include <akntitle.h>
       
    33 #include <schtime.h>
       
    34 #include <csch_cli.h>
       
    35 
       
    36 #include "fotaupdate.h"
       
    37 #include "fmsclient.h"
       
    38 #include "FotaReminderDlg.h"
       
    39 #include "FotasrvSession.h"
       
    40 #include "fotaserverPrivateCRKeys.h"
       
    41 #include "fotaserverPrivatePSKeys.h"
       
    42 
       
    43 
       
    44 
       
    45 
       
    46 
       
    47 // ============== LOCAL FUNCTIONS ============================================
       
    48 
       
    49 
       
    50 
       
    51 TInt WriteUpdateBitmapL( const TDesC& aText, const TDesC& aFile)
       
    52     {
       
    53     FLOG(_L("WriteUpdateBitmapL writing %S to %S w/ txtdir"),&aText,&aFile);
       
    54 
       
    55     TSize   screensize = CCoeEnv::Static()->ScreenDevice()->SizeInPixels();
       
    56     TInt                width  = screensize.iWidth - KBmpMargin*2;
       
    57     TInt                height =  screensize.iHeight;
       
    58 
       
    59     CArrayFixSeg<TPtrC>*   lines = new CArrayFixSeg<TPtrC>(5);
       
    60     CleanupStack::PushL(lines);
       
    61     CFbsBitmap*         bitmap = new ( ELeave ) CFbsBitmap;
       
    62     CleanupStack::PushL( bitmap );
       
    63     bitmap->Create(  TSize(width,height), EColor64K );
       
    64     CFbsBitmapDevice*   device = CFbsBitmapDevice::NewL( bitmap );
       
    65     CleanupStack::PushL( device );
       
    66     const CFont* font = AknLayoutUtils::FontFromId(EAknLogicalFontPrimaryFont);
       
    67     CFbsBitGc*          context;
       
    68     User::LeaveIfError( device->CreateContext( context ) );
       
    69     CleanupStack::PushL( context );
       
    70     TInt                ascent = font->AscentInPixels();
       
    71     TInt                descent = font->DescentInPixels();
       
    72     context->UseFont ( font );
       
    73     context->Clear(); // bg color
       
    74 
       
    75     // Visually ordered text
       
    76     HBufC* wrappedstring = AknBidiTextUtils::ConvertToVisualAndWrapToArrayL(
       
    77             aText, width,*font, *lines);
       
    78     CleanupStack::PushL ( wrappedstring );
       
    79     TBool dirfound (ETrue);
       
    80     // direction of text, affects alignemnt
       
    81     TBidiText::TDirectionality direction = TBidiText::TextDirectionality(
       
    82             *wrappedstring, &dirfound );
       
    83 
       
    84     // Print visual text to bitmap
       
    85     for ( TInt i=0; i<lines->Count(); ++i ) 
       
    86         {
       
    87         TPtrC l = (*lines)[i];
       
    88         TInt top = (ascent+descent)*(i);
       
    89         TInt bot = (ascent+descent)*(i+1);
       
    90         TRect rect (0, top ,width, bot );
       
    91         CGraphicsContext::TTextAlign alignment = 
       
    92         direction==TBidiText::ELeftToRight ? CGraphicsContext::ELeft 
       
    93             : CGraphicsContext::ERight;
       
    94         context->DrawText(l, rect, ascent, alignment);
       
    95         }
       
    96     height = (ascent+descent)*lines->Count() + descent;
       
    97     bitmap->Resize( TSize(width,height));
       
    98     bitmap->Save( aFile );
       
    99     context->DiscardFont();
       
   100     CleanupStack::PopAndDestroy( wrappedstring );
       
   101     CleanupStack::PopAndDestroy( context );
       
   102     CleanupStack::PopAndDestroy( device );
       
   103     CleanupStack::PopAndDestroy( bitmap );
       
   104     CleanupStack::PopAndDestroy( lines );
       
   105     return 1;
       
   106     }
       
   107 
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // PackageFileName  Creates pgk filename ,like 5.swupd
       
   111 // ---------------------------------------------------------------------------
       
   112 void PackageFileName( TInt aPkgid, TDes8& aFileName )
       
   113     {
       
   114     aFileName.AppendNum(aPkgid);
       
   115     aFileName.Append( KSwupdFileExt8 );
       
   116     }
       
   117 
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // PackageFilePath  Creates pk file path, like c:\\private\\102072C4\\5.swupd
       
   121 // ---------------------------------------------------------------------------
       
   122 void PackageFilePath( TInt aPkgid, TDes8& aPath )
       
   123     {
       
   124     TBuf8<20> fn;
       
   125     PackageFileName ( aPkgid,fn);
       
   126     aPath.Append(KSwupdPath8);
       
   127     aPath.Append(fn);
       
   128     }
       
   129 
       
   130 // ============================= MEMBER FUNCTIONS ============================
       
   131 
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CFotaUpdate::CFotaUpdate()
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 CFotaUpdate::CFotaUpdate() : CActive(EPriorityNormal) 
       
   138 ,iScheduledUpdate(NULL),iHandleUpdateAcceptLater(EFalse),iCentrep(NULL)
       
   139                 {
       
   140                 CActiveScheduler::Add( this ); 
       
   141                 iNotifParams.iNoteType  = ESyncMLFwUpdUnknown;  
       
   142                 iNotifParams.iIntParam  = 0;
       
   143                 iChargeToMonitor = 0;
       
   144                 iFinalizeLater = EFalse;
       
   145                 }
       
   146 
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CFotaUpdate::~CFotaUpdate()
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 CFotaUpdate::~CFotaUpdate()
       
   153     {
       
   154     if(iScheduledUpdate)
       
   155         {
       
   156         delete iScheduledUpdate;
       
   157         iScheduledUpdate = NULL;	
       
   158         }
       
   159     iIntervalType.Close();  
       
   160     iInterval.Close();  
       
   161    if(iCentrep)
       
   162        {
       
   163        delete iCentrep;
       
   164        iCentrep = NULL;
       
   165        }
       
   166     }
       
   167 
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CFotaUpdate::NewL 
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 CFotaUpdate* CFotaUpdate::NewL (CFotaServer* aServer)
       
   174     {
       
   175     CFotaUpdate* ao = new (ELeave) CFotaUpdate();
       
   176     ao->iFotaServer = aServer;
       
   177     return ao;
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // CFotaUpdate::CheckUpdateResults
       
   182 // Checks if there is update result file available (meaning that update just
       
   183 // took place)
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 TBool CFotaUpdate::CheckUpdateResults( RFs& aRfs )
       
   187     {
       
   188     RFile   f;
       
   189     TInt    err;
       
   190     err = f.Open ( aRfs, KUpdateResultFile, EFileShareAny );
       
   191     f.Close();
       
   192     if ( err!=KErrNone )
       
   193         {
       
   194         return EFalse;
       
   195         }
       
   196     FLOG(_L(" CFotaUpdate::CheckUpdateResults  update result file Found! "));
       
   197     return ETrue;
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CFotaUpdate::ExecuteUpdateResultFileL
       
   203 // Read result code from update result file and update state accordingly. 
       
   204 // Show notifier "Update succesful". Do some cleanup.
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 void CFotaUpdate::ExecuteUpdateResultFileL( RFs& aRfs )
       
   208     {
       
   209     FLOG(_L("CFotaUpdate::ExecuteUpdateResultFileL >>"));
       
   210     iFs = &aRfs;
       
   211     TInt                err;
       
   212     RFileReadStream     rstr;
       
   213     TInt                result;
       
   214     TInt                msglen;
       
   215     HBufC8*             message=NULL;
       
   216     HBufC16*            message16=NULL;
       
   217     TBool               deleteData ( EFalse );
       
   218 
       
   219     // Open update result file
       
   220     FLOG(_L("opening rstr 1/2  "));
       
   221     err = rstr.Open(*iFs,KUpdateResultFile ,EFileRead|EFileStream);
       
   222     FLOG(_L("opened  rstr 2/2  "));
       
   223     if(err) FLOG(_L("   update result file open err %d"), err);
       
   224     User::LeaveIfError (err );
       
   225     FLOG(_L("       0.1"));
       
   226     CleanupClosePushL( rstr );
       
   227     FLOG(_L("       0.2"));
       
   228 
       
   229     // Read resultcode 
       
   230     result = rstr.ReadUint32L();        FLOG(_L("       0.3"));
       
   231     msglen = rstr.ReadUint32L();        FLOG(_L("       0.4  result: %d  \
       
   232     msglen:%d "),  result, msglen);
       
   233     if(msglen != 0)
       
   234         {
       
   235         FLOG(_L("       0.5  reading msg"));
       
   236         message         = HBufC8::NewLC(msglen+1);
       
   237         TPtr8  ptrdesc  = message->Des();
       
   238         TRAPD ( err2, rstr.ReadL(ptrdesc) );
       
   239         if ( err2 != KErrNone && err2 != KErrEof)
       
   240             {
       
   241             FLOG(_L("  file read err %d"),err2); User::Leave( err2 ); 
       
   242             }
       
   243 
       
   244         message16 = HBufC16::NewLC (message->Des().Length());
       
   245         message16->Des().Copy( *message );
       
   246         FLOG(_L("   1 update result: %d"),  result) ;
       
   247         FLOG(_L("   2 dbg msg: %S"),message16);
       
   248         CleanupStack::PopAndDestroy( message16 );
       
   249         CleanupStack::PopAndDestroy( message );
       
   250         }
       
   251 
       
   252     FLOG(_L("       0.6 "));
       
   253     CleanupStack::PopAndDestroy( &rstr ); 
       
   254 
       
   255     // Map resultcode to FUMO result code
       
   256     RFotaEngineSession::TState  fstate  
       
   257     = RFotaEngineSession::EUpdateFailed;
       
   258     RFotaEngineSession::TResult fresult 
       
   259     = RFotaEngineSession::EResUpdateFailed;
       
   260 
       
   261     TDriveNumber drive;
       
   262     TBool toencrypt =   iFotaServer->NeedToEncryptL(drive);
       
   263 
       
   264     FLOG(_L("   3"));
       
   265     switch ( result )
       
   266         {
       
   267         case UPD_OK:
       
   268             {
       
   269             fstate = RFotaEngineSession::EUpdateSuccessfulNoData;
       
   270             fresult = RFotaEngineSession::EResSuccessful;
       
   271             deleteData = ETrue;
       
   272             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNone, toencrypt );
       
   273             }
       
   274             break;
       
   275         case UPD_INSTALL_REQUEST_IS_INVALID:
       
   276             {
       
   277             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   278             fresult = RFotaEngineSession::EResUpdateFailed;
       
   279             deleteData = ETrue;
       
   280             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNotFound, toencrypt );
       
   281             }
       
   282             break;
       
   283         case UPD_UPDATE_PACKAGE_IS_ABSENT:
       
   284             {
       
   285             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   286             fresult = RFotaEngineSession::EResUpdateFailed;
       
   287             }
       
   288             break;
       
   289         case UPD_UPDATE_PACKAGE_IS_CORRUPTED:
       
   290             {
       
   291             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   292             fresult = RFotaEngineSession::EResCorruptedFWUPD;
       
   293             deleteData = ETrue;
       
   294             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNotSupported, toencrypt );
       
   295             }
       
   296             break;
       
   297         case UPD_UPDATE_PACKAGE_CONTENTS_IS_INVALID:
       
   298             {
       
   299             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   300             fresult = RFotaEngineSession::EResCorruptedFWUPD;
       
   301             deleteData = ETrue;
       
   302             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNotSupported, toencrypt );
       
   303             }
       
   304             break;
       
   305         case UPD_UPDATE_PACKAGE_IS_NOT_COMPATIBLE_WITH_CURRENT_MOBILE_DEVICE:
       
   306             {
       
   307             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   308             fresult = RFotaEngineSession::EResPackageMismatch;
       
   309             deleteData = ETrue;
       
   310             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNotSupported, toencrypt );
       
   311             }
       
   312             break;
       
   313         case UPD_FATAL_ERROR:
       
   314             {
       
   315             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   316             fresult = RFotaEngineSession::EResUpdateFailed;
       
   317             deleteData = ETrue;
       
   318             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNotFound, toencrypt );
       
   319             }
       
   320             break;
       
   321 
       
   322         default:
       
   323             {
       
   324             FLOG(_L("   3.1 invalid result: %d"), result);
       
   325             LaunchNotifierL ( ESyncMLFwUpdResultNote, KErrNotFound, toencrypt );
       
   326             }
       
   327             break;      
       
   328         }
       
   329 
       
   330     // Find the state 60 (update  progressing) -> 100 (etc)
       
   331     RArray<TInt>    states;
       
   332     TPackageState   state;
       
   333     CleanupClosePushL (states);
       
   334     iFotaServer->iDatabase->OpenDBL();
       
   335     iFotaServer->iDatabase->GetAllL(states);
       
   336 
       
   337     FLOG(_L("   4.1 found %d states "),states.Count() );
       
   338     for(TInt i=0;i<states.Count(); ++i ) 
       
   339         {
       
   340         TPackageState   tmp;
       
   341         tmp = iFotaServer->iDatabase->GetStateL(  states[i]  );
       
   342         FLOG(_L("   5 got state "));
       
   343         if ( tmp.iState == RFotaEngineSession::EUpdateProgressing )
       
   344             {
       
   345             state = tmp;
       
   346             state.iState    = fstate;
       
   347             state.iResult   = fresult;
       
   348             FLOG(_L("   6 Updating state id %d  to %d ,result %d  ")
       
   349                     ,state.iPkgId, state.iState,state.iResult );
       
   350             iFotaServer->iDatabase->SetStateL(state,KNullDesC8
       
   351                     ,EFDBState|EFDBResult ) ;
       
   352             }
       
   353         }
       
   354     iFotaServer->iDatabase->CloseAndCommitDB();
       
   355     CleanupStack::PopAndDestroy(&states);
       
   356 
       
   357 
       
   358     // Delete request file
       
   359     err = BaflUtils::DeleteFile ( *iFs, KUpdateRequestFile );
       
   360     if ( err != KErrNone && err != KErrNotFound ) 
       
   361         {
       
   362         FLOG(_L("   6.1  req file deleted, err %d"), err);
       
   363         User::Leave(err); 
       
   364         } 
       
   365 
       
   366     // Write timestamp (shown to user in device management ui)
       
   367     if( fresult == RFotaEngineSession::EResSuccessful )
       
   368         {
       
   369         RFileWriteStream    wstr;
       
   370         TTime               time;
       
   371         User::LeaveIfError( wstr.Replace( *iFs, KUpdateTimeStampFileName
       
   372                 ,EFileWrite ) );
       
   373         CleanupClosePushL ( wstr );
       
   374         time.HomeTime();
       
   375         TInt                year   = time.DateTime().Year();
       
   376         TInt                month  = time.DateTime().Month();    
       
   377         TInt                day    = time.DateTime().Day();    
       
   378         TInt                hour   = time.DateTime().Hour();    
       
   379         TInt                minute = time.DateTime().Minute();    
       
   380         wstr.WriteInt32L(year) ;
       
   381         wstr.WriteInt32L(month) ;
       
   382         wstr.WriteInt32L(day) ;
       
   383         wstr.WriteInt32L(hour) ;
       
   384         wstr.WriteInt32L(minute) ;   
       
   385         CleanupStack::PopAndDestroy (1); // wstr
       
   386         }
       
   387     // Delete package content
       
   388     if ( deleteData  && state.iPkgId > KErrNotFound )
       
   389         {
       
   390         iFotaServer->StoragePluginL()->DeleteUpdatePackageL( state.iPkgId ) ;
       
   391         }
       
   392 
       
   393 
       
   394     // Report state back to syncml server
       
   395     if ( state.iPkgId >= 0 )
       
   396         {
       
   397         FLOG(_L("   6.1 creating device mgmt session for profile %d")
       
   398                 ,state.iProfileId);
       
   399         iFotaServer->CreateDeviceManagementSessionL( state );
       
   400         }
       
   401     else 
       
   402         {
       
   403         FLOG(_L(" No state found in 'update progress' mode! cannot report\
       
   404         status to DM server "));
       
   405         }
       
   406 
       
   407     FLOG(_L("CFotaUpdate::ExecuteUpdateResultFileL <<"));
       
   408     }
       
   409 
       
   410 // ---------------------------------------------------------------------------
       
   411 // CFotaUpdate::DeleteUpdateResultsL
       
   412 // ---------------------------------------------------------------------------
       
   413 //
       
   414 void CFotaUpdate::DeleteUpdateResultFileL( RFs& aRfs )
       
   415     {
       
   416     FLOG(_L("CFotaUpdate::DeleteUpdateResultsL "));
       
   417     // Delete result file
       
   418     BaflUtils::DeleteFile( aRfs, KUpdateResultFile );
       
   419     BaflUtils::DeleteFile( aRfs, KUpdateRequestFile );
       
   420     BaflUtils::DeleteFile( aRfs, KUpdateBitmap );
       
   421     BaflUtils::DeleteFile( aRfs, KRestartingBitmap );
       
   422     }
       
   423 
       
   424 
       
   425 // ---------------------------------------------------------------------------
       
   426 // CFotaUpdate::RunL
       
   427 //  Handles user selection (button press: accept/ cancel update)
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 void CFotaUpdate::RunL()
       
   431     {
       
   432     FLOG(_L( "[FotaServer] CFotaUpdate::RunL() >> %d"  ),iStatus.Int() );
       
   433     TBool       handled( EFalse );
       
   434     User::LeaveIfError(iNotifier.CancelNotifier(KSyncMLFwUpdNotifierUid));
       
   435     iNotifier.Close();
       
   436 
       
   437     if (iStatus.Int() == KErrNotFound )
       
   438         {
       
   439         FLOG(_L("Exiting...."));
       
   440         TApaTaskList taskList(iFotaServer->GetEikEnv()->WsSession());
       
   441         TApaTask task=taskList.FindApp(TUid::Uid(KOmaDMAppUid));
       
   442         if(task.Exists())
       
   443             {
       
   444             task.EndTask();
       
   445             }
       
   446         return;
       
   447 
       
   448         }
       
   449     // Handle update start query
       
   450     if ( iNotifParams.iNoteType == ESyncMLFwUpdStartQuery || iNotifParams.iNoteType == ESyncMLFwUpdForceQuery || iNotifParams.iNoteType == ESyncMLFwUpdPostponeLimitQuery)
       
   451         {
       
   452         FLOG(_L("   update start query"));
       
   453         // User pressed accept
       
   454         if ( iStatus.Int() == KErrNone )
       
   455             {
       
   456             handled = ETrue;
       
   457             HandleUpdateAcceptStartL();
       
   458             }
       
   459         // User pressed later
       
   460         else if ( iStatus.Int() == KErrCancel )
       
   461             {
       
   462 
       
   463             FLOG(_L("USER PRESSED LATER"));
       
   464             handled = ETrue;
       
   465             HandleUpdateAcceptLaterL();
       
   466             }
       
   467         }
       
   468     if ( iNotifParams.iNoteType == ESyncMLFwUpdStartQueryEnc )
       
   469         {
       
   470         FLOG(_L("   update start query"));
       
   471 
       
   472         if ( iStatus.Int() == KErrNone )
       
   473             {
       
   474             FLOG(_L(" user accepted query to decrypt memory"));
       
   475             handled = ETrue;
       
   476             TRAPD(err, iFotaServer->DoStartDecryptionL());
       
   477             
       
   478             if (err == KErrNotReady)
       
   479                 {
       
   480                 HandleUpdateErrorL(err);
       
   481                 }
       
   482             }
       
   483         else if (iStatus.Int() == KErrCancel )
       
   484             {
       
   485             FLOG(_L("User pressed cancel. Don't decrypt. Stop."));
       
   486             handled = ETrue;
       
   487             LaunchNotifierL( ESyncMLFwUpdNoReminder , KErrNone );
       
   488             }
       
   489 
       
   490         }
       
   491     // Handle battery error
       
   492     if ( iNotifParams.iNoteType == ESyncMLFwUpdErrorNote )
       
   493         {
       
   494 
       
   495         if (iNotifParams.iIntParam == KErrBadPower || iNotifParams.iIntParam == KErrBadUsbPower)
       
   496             {
       
   497             FLOG(_L("       battery note"));
       
   498             if ( iStatus.Int() == KErrNone )
       
   499                 {
       
   500                 handled = ETrue;
       
   501 
       
   502                 // Still check batt. (user might have connected charger)
       
   503 
       
   504                 HandleErrorBasedonChargingTypeL();
       
   505 
       
   506                 }
       
   507             else if (iNotifParams.iIntParam == KErrDeviceMemoryBusy)
       
   508                 {
       
   509                 FLOG(_L("Do nothing. Stop the update."));
       
   510                 iFotaServer->FinalizeUpdateL();
       
   511                 }
       
   512             else if (iNotifParams.iIntParam == KErrBadPowerForEnc)
       
   513                 {
       
   514                 if (iFinalizeLater)
       
   515                     {
       
   516                     FLOG(_L("Finalizing update now..."));
       
   517                     iFinalizeLater = EFalse;
       
   518                     iFotaServer->FinalizeUpdateL();
       
   519                     }
       
   520                 }
       
   521             }
       
   522         }
       
   523 
       
   524     //  Handle update complete notification
       
   525     if ( iNotifParams.iNoteType == ESyncMLFwUpdResultNote )
       
   526         {
       
   527         handled = ETrue;
       
   528         DeleteUpdateResultFileL( iFotaServer->iFs );
       
   529         if (!iNotifParams.iEncryptReq)
       
   530             {
       
   531             FLOG(_L("Going to finalize update..."));
       
   532             iFotaServer->FinalizeUpdateL();
       
   533             }
       
   534 
       
   535         }
       
   536     if ( iNotifParams.iNoteType == ESyncMLFwUpdNoReminder )
       
   537         {
       
   538         FLOG(_L(" No Reminder selected"));
       
   539         if ( iStatus.Int() == KErrNone )
       
   540             {
       
   541             handled = ETrue;
       
   542 
       
   543             FindScheduleL( ETrue );
       
   544             iUpdateState.iState = RFotaEngineSession::EStartingUpdate;
       
   545             iFotaServer->iDatabase->OpenDBL();
       
   546             iFotaServer->iDatabase->SetStateL(iUpdateState ,KNullDesC8,EFDBState);
       
   547             iFotaServer->iDatabase->CloseAndCommitDB();
       
   548 
       
   549             // Report state back to syncml server
       
   550             if ( iUpdateState.iPkgId >= 0 && iUpdateState.iSendAlert )
       
   551                 {
       
   552                 FLOG(_L("   creating device mgmt session for profile %d")
       
   553                         ,iUpdateState.iProfileId);
       
   554                 iFotaServer->CreateDeviceManagementSessionL( iUpdateState );
       
   555                 }
       
   556             iFotaServer->FinalizeUpdateL();
       
   557             }
       
   558         }
       
   559     if ( iNotifParams.iNoteType == ESyncMLFwUpdResultNote && iNotifParams.iEncryptReq)
       
   560         {
       
   561         if (iStatus.Int() == KErrNone)
       
   562             {
       
   563             FLOG(_L("User wanted to start encryption..."));
       
   564             iFotaServer->DoStartEncryptionL();
       
   565             }
       
   566         else 
       
   567             {
       
   568             FLOG(_L("User do not want to start encryption."));
       
   569             }
       
   570         if (!iFinalizeLater)
       
   571             iFotaServer->FinalizeUpdateL();
       
   572         }
       
   573     if ( !handled ) 
       
   574         {
       
   575         FLOG(_L("CANNOT INTERPRET NOTIFIER RESULT CODE: %d"), iStatus.Int() );
       
   576         }
       
   577 
       
   578     FLOG(_L( "[FotaServer] CFotaUpdate::RunL() <<" ) );
       
   579     }
       
   580 
       
   581 
       
   582 
       
   583 
       
   584 
       
   585 // ---------------------------------------------------------------------------
       
   586 //  CFotaUpdate::HandleUpdateAcceptStartL()
       
   587 //  Handles user selection of NOW
       
   588 // ---------------------------------------------------------------------------
       
   589 //
       
   590 void CFotaUpdate::HandleUpdateAcceptStartL(TBool aSkipBatteryChk)
       
   591     {
       
   592     FLOG(_L("USER PRESSED ACCEPT"));
       
   593 
       
   594     TBool	enoughPower (EFalse);
       
   595     TInt usbchargingsupport =0;
       
   596     if (aSkipBatteryChk)
       
   597         {
       
   598         enoughPower = ETrue;
       
   599         }
       
   600     else
       
   601         {
       
   602         CRepository* centrep = NULL;
       
   603         //centrep = CRepository::NewLC( TUid::Uid(KFotaServerUid));
       
   604         TRAPD(err, centrep = CRepository::NewL( TUid::Uid(KFotaServerUid)));
       
   605         if(err)
       
   606             {
       
   607             FLOG(_L("reading from cenrep failed"));
       
   608             }
       
   609 
       
   610         if(centrep)
       
   611             {centrep->Get( KFotaUSBChargerEnabled , usbchargingsupport );
       
   612             if(usbchargingsupport != 0 && usbchargingsupport != 1)
       
   613                 usbchargingsupport =0;
       
   614             delete centrep;
       
   615             } 
       
   616         centrep = NULL;
       
   617         enoughPower = CheckBatteryL(usbchargingsupport);
       
   618 #if defined(__WINS__)
       
   619         enoughPower = ETrue;
       
   620 #endif
       
   621         }
       
   622     // Failure due to low battery
       
   623     if ( !enoughPower )
       
   624         {
       
   625         FLOG(_L("Launching low battery notifier"));
       
   626         if(!usbchargingsupport)
       
   627             LaunchNotifierL( ESyncMLFwUpdErrorNote, KErrBadPower );
       
   628         else
       
   629             LaunchNotifierL( ESyncMLFwUpdErrorNote , KErrBadUsbPower );
       
   630         }            
       
   631     else
       
   632         {
       
   633         UpdateL();
       
   634         }   
       
   635     }
       
   636 
       
   637 
       
   638 void CFotaUpdate::HandleUpdateErrorL(TInt aError, TInt aValue)
       
   639     {
       
   640     FLOG(_L("CFotaUpdate::HandleUpdateErrorL, error = %d, value = %d >>"), aError, aValue);
       
   641 
       
   642     switch (aError)
       
   643         {
       
   644         case KErrNotReady: //Called from within StartUpdate
       
   645             {
       
   646             LaunchNotifierL(ESyncMLFwUpdErrorNote,KErrDeviceMemoryBusy);
       
   647             }
       
   648             break;
       
   649         case KErrBadPower: //Called from fotaserver when battery power is not enough
       
   650             {
       
   651             iChargeToMonitor = aValue;
       
   652             CRepository* centrep = NULL;
       
   653             TRAPD(err, centrep = CRepository::NewL( TUid::Uid(KFotaServerUid)));
       
   654             if(err)
       
   655                 {
       
   656                 FLOG(_L("reading from cenrep failed"));
       
   657                 }
       
   658             TInt usbchargingsupport =0;
       
   659             if(centrep)
       
   660                 {centrep->Get( KFotaUSBChargerEnabled , usbchargingsupport );
       
   661                 if(usbchargingsupport != 0 && usbchargingsupport != 1)
       
   662                     usbchargingsupport =0;
       
   663                 delete centrep;
       
   664                 } 
       
   665             centrep = NULL;
       
   666             FLOG(_L("Launching low battery notifier, usbsupport = %d, state = %d"), usbchargingsupport, iUpdateState.iState);
       
   667             if(!usbchargingsupport )
       
   668                 {
       
   669                 FLOG(_L("Here 1"));
       
   670                 if (iUpdateState.iState == RFotaEngineSession::EStartingUpdate)
       
   671                     {
       
   672                     FLOG(_L("Here 2"));
       
   673                     LaunchNotifierL( ESyncMLFwUpdErrorNote, KErrBadPower );
       
   674                     }
       
   675                 }
       
   676             else
       
   677                 {
       
   678                 LaunchNotifierL( ESyncMLFwUpdErrorNote , KErrBadUsbPower );
       
   679                 }
       
   680 
       
   681             }
       
   682             break;
       
   683         default:
       
   684             {
       
   685             //Do nothing
       
   686             }
       
   687         }
       
   688 
       
   689 
       
   690     FLOG(_L("CFotaUpdate::HandleUpdateErrorL <<"));
       
   691     }
       
   692 
       
   693 void CFotaUpdate::HandleEncryptionErrorL(TInt aError)
       
   694     {
       
   695     FLOG(_L("CFotaUpdate::HandleEncryptionErrorL, error = %d >>"), aError);
       
   696 
       
   697     if (aError == KErrBadPower)
       
   698         {
       
   699         iFinalizeLater = ETrue;
       
   700         LaunchNotifierL(ESyncMLFwUpdErrorNote, KErrBadPowerForEnc);
       
   701         }
       
   702     else 
       
   703         {
       
   704         FLOG(_L("Unknown error. Ignored"));
       
   705         }
       
   706 
       
   707     FLOG(_L("CFotaUpdate::HandleEncryptionErrorL <<"));
       
   708     }
       
   709 
       
   710 
       
   711 // ---------------------------------------------------------------------------
       
   712 //  CFotaUpdate::HandleUpdateAcceptLaterL
       
   713 //  Handles user selection of Later
       
   714 // ---------------------------------------------------------------------------
       
   715 void CFotaUpdate::HandleUpdateAcceptLaterL()
       
   716     {
       
   717     TInt fotaUpdateAppName (EFotaUpdateDM);
       
   718     TInt err = RProperty::Get( KPSUidNSmlDMSyncApp, KNSmlCurrentFotaUpdateAppName, fotaUpdateAppName );
       
   719     FLOG(_L("Setting iHandleUpdateAcceptLater to True"));
       
   720 
       
   721     if (!err && ((fotaUpdateAppName == EFotaUpdateNSC) || (fotaUpdateAppName == EFotaUpdateNSCBg )))
       
   722         {
       
   723         iFotaServer->FinalizeUpdateL();		//make sure it is not called 2 times				
       
   724         }
       
   725     else
       
   726         {
       
   727         iHandleUpdateAcceptLater = ETrue;
       
   728         ShowReminderDialogL();				
       
   729         }
       
   730     }
       
   731 
       
   732 
       
   733 // ---------------------------------------------------------------------------
       
   734 //  CFotaUpdate::HandleErrorBasedonChargingTypeL
       
   735 //  Handles the error based on charger type supported by the device
       
   736 // ---------------------------------------------------------------------------
       
   737 
       
   738 void CFotaUpdate::HandleErrorBasedonChargingTypeL()
       
   739     {
       
   740     FLOG(_L("CFotaUpdate::HandleErrorBasedonChargingTypeL >>"));
       
   741 
       
   742     if (iUpdateState.iState == RFotaEngineSession::EStartingUpdate)
       
   743         {
       
   744         if (iNotifParams.iIntParam == KErrBadUsbPower)
       
   745             {  
       
   746             FLOG(_L("handling usb charger error "));
       
   747             SetUpdateFailedStatesL();
       
   748             FLOG(_L("calling for monitoring  the battery"));
       
   749             iFotaServer->MonitorBattery(iChargeToMonitor);
       
   750             iFotaServer->FinalizeUpdateL();
       
   751             }
       
   752         else
       
   753             {
       
   754             TInt usbcharger = KErrNone; 
       
   755             TBool enoughPower = CheckBatteryL(usbcharger);
       
   756 
       
   757 #if defined(__WINS__)
       
   758             //It will not come here, just used for testing.
       
   759             enoughPower = ETrue;
       
   760 #endif
       
   761 
       
   762             if (enoughPower)
       
   763                 {
       
   764                 //User has connected the charger
       
   765                 FLOG(_L("User has connected the charger"));
       
   766                 UpdateL();
       
   767                 }
       
   768             else
       
   769                 {
       
   770                 FLOG(_L("User hasn't connected the charger"));
       
   771                 //User hasn't connected the charger, hence notify user to update later.
       
   772                 SetUpdateFailedStatesL();
       
   773                 //Call reminder here
       
   774                 if(!iHandleUpdateAcceptLater)
       
   775                     {
       
   776                     FLOG(_L("calling HandleUpdateAcceptLaterL"));
       
   777                     HandleUpdateAcceptLaterL();
       
   778                     }
       
   779                 iHandleUpdateAcceptLater = EFalse;
       
   780                 }
       
   781             }
       
   782         }
       
   783     else //must be during encryption after update
       
   784         {
       
   785         if (iNotifParams.iIntParam == KErrBadUsbPower)
       
   786             {
       
   787             iFotaServer->MonitorBattery(iChargeToMonitor);
       
   788             }
       
   789         else
       
   790             {
       
   791             //nothing to do for dynamo charging
       
   792             }
       
   793         iFotaServer->FinalizeUpdateL();
       
   794         }
       
   795     FLOG(_L("CFotaUpdate::HandleErrorBasedonChargingTypeL <<"));
       
   796     }
       
   797 
       
   798 // ---------------------------------------------------------------------------
       
   799 //  CFotaUpdate::SetUpdateFailedStatesL
       
   800 //  Set the state to update failed
       
   801 // ---------------------------------------------------------------------------
       
   802 
       
   803 void CFotaUpdate::SetUpdateFailedStatesL()
       
   804     {
       
   805     FLOG(_L("CFotaUpdate::SetUpdateFailedStatesL >>"));
       
   806 
       
   807     iUpdateState.iState = RFotaEngineSession::EStartingUpdate;
       
   808     iUpdateState.iResult= RFotaEngineSession::EResUpdateFailed;
       
   809     iFotaServer->iDatabase->OpenDBL();
       
   810     iFotaServer->iDatabase->SetStateL( iUpdateState ,KNullDesC8
       
   811             ,EFDBState|EFDBResult );
       
   812     iFotaServer->iDatabase->CloseAndCommitDB();
       
   813 
       
   814     FLOG(_L("CFotaUpdate::SetUpdateFailedStatesL <<"));
       
   815 
       
   816     }
       
   817 
       
   818 
       
   819 // ---------------------------------------------------------------------------
       
   820 // CFotaUpdate::DoCancel()
       
   821 // ---------------------------------------------------------------------------
       
   822 //
       
   823 void CFotaUpdate::DoCancel()
       
   824     {
       
   825     FLOG(_L("CFotaUpdate::DoCancel() >>"));
       
   826     iNotifier.CancelNotifier(KSyncMLFwUpdNotifierUid);
       
   827     iNotifier.Close();
       
   828     FLOG(_L("CFotaUpdate::DoCancel() <<"));
       
   829     }
       
   830 
       
   831 // ---------------------------------------------------------------------------
       
   832 // CFotaUpdate::RunError(TInt aError)
       
   833 // ---------------------------------------------------------------------------
       
   834 //
       
   835 TInt CFotaUpdate::RunError(TInt aError)
       
   836     {
       
   837     FLOG(_L("CFotaUpdate::RunError >> err %d"),aError);
       
   838     FLOG(_L("CFotaUpdate::RunError << err %d"),aError);
       
   839     return KErrNone;
       
   840     }
       
   841 
       
   842 // ---------------------------------------------------------------------------
       
   843 // CSyncMLNTestAppAO::LaunchNotifierL
       
   844 // Shows one of the following queries: "update now?", "update success",
       
   845 //  "update fail", "too low battery"
       
   846 // ---------------------------------------------------------------------------
       
   847 void CFotaUpdate::LaunchNotifierL( const TSyncMLFwUpdNoteTypes aNotetype
       
   848         ,const TInt aIntParam, const TBool aEnc )
       
   849     {
       
   850     FLOG(_L("CFotaUpdate::LaunchNotifierL() >>  prof.Id: %d")
       
   851             ,iUpdateState.iProfileId);
       
   852     if (!IsActive())
       
   853         {
       
   854         TSyncMLFwUpdNotifParams         params;
       
   855         params.iNoteType        = aNotetype;
       
   856         params.iIntParam        = aIntParam;
       
   857         params.iEncryptReq = aEnc;
       
   858         TSyncMLFwUpdNotifParamsPckg     pckg(params);
       
   859         iNotifParams.iNoteType  = params.iNoteType;
       
   860         iNotifParams.iIntParam  = params.iIntParam;
       
   861         iNotifParams.iEncryptReq = params.iEncryptReq;
       
   862         iDummyResponsePckg = TSyncMLFwUpdNotifRetValPckg();
       
   863         User::LeaveIfError( iNotifier.Connect() );
       
   864         iNotifier.StartNotifierAndGetResponse( iStatus,KSyncMLFwUpdNotifierUid
       
   865                 , pckg, iDummyResponsePckg );
       
   866         SetActive();
       
   867         }
       
   868     FLOG(_L("CFotaUpdate::LaunchNotifierL() <<"));
       
   869     }
       
   870 
       
   871 
       
   872 // ---------------------------------------------------------------------------
       
   873 // CFotaUpdate::LaunchNotifierL
       
   874 // Shows the postpone information note when postpone limit feature is enabled.
       
   875 // ---------------------------------------------------------------------------
       
   876 void CFotaUpdate::LaunchNotifierL( const TSyncMLFwUpdNoteTypes aNotetype
       
   877                                             ,const TInt aIntUserPostpone,const TInt aIntMaxPostpone,TIntervalType aIntervalType,const TInt aInterval)
       
   878     {
       
   879     FLOG(_L("CFotaUpdate::LaunchNotifierL() >>  prof.Id: %d aIntUserPostpone: %d aIntMaxPostpone %d")
       
   880                                             ,iUpdateState.iProfileId,aIntUserPostpone,aIntMaxPostpone);
       
   881     if (!IsActive())
       
   882         {
       
   883         TSyncMLFwUpdNotifParams         params;
       
   884         
       
   885         // Parameters to display the information note is displayed
       
   886     params.iNoteType        = aNotetype;
       
   887     params.iFotaUserPostponeCount = aIntUserPostpone;
       
   888     params.iFotaMaxPostponeCount = aIntMaxPostpone;
       
   889     params.iIntervalType = aIntervalType;
       
   890     params.iInterval = aInterval;
       
   891     
       
   892     /*
       
   893     if(iReminderString.Length() > 0)
       
   894         params.iSelectedReminder.Copy(iReminderString);
       
   895     
       
   896     TBuf<5> aTime;
       
   897     aTime.Zero();
       
   898     
       
   899     for(TInt i = 0; i<iReminderString.Length();i++)
       
   900         {
       
   901         TChar ch = iReminderString[i];
       
   902         if ( ch.IsDigit())            //separates digits from the string
       
   903             {  
       
   904             aTime.Append(ch); 
       
   905             }
       
   906         else
       
   907             {
       
   908             break;
       
   909             }
       
   910         }
       
   911     
       
   912     TInt val =1 ; 
       
   913     TLex  lex(aTime);
       
   914     TInt err = lex.Val(val); */
       
   915 
       
   916     TSyncMLFwUpdNotifParamsPckg     pckg(params);
       
   917     iNotifParams.iNoteType  = params.iNoteType;
       
   918     iNotifParams.iIntParam  = params.iIntParam;
       
   919     iDummyResponsePckg = TSyncMLFwUpdNotifRetValPckg();
       
   920     User::LeaveIfError( iNotifier.Connect() );
       
   921     iNotifier.StartNotifierAndGetResponse( iStatus,KSyncMLFwUpdNotifierUid
       
   922                                             , pckg, iDummyResponsePckg );
       
   923     SetActive();
       
   924         }
       
   925     FLOG(_L("CFotaUpdate::LaunchNotifierL() <<"));
       
   926     }
       
   927 
       
   928 
       
   929 
       
   930 
       
   931 // ---------------------------------------------------------------------------
       
   932 // CFotaUpdate::UpdateL
       
   933 // Updates the fw: Creates input files for update agent and boots device to 
       
   934 // update mode.
       
   935 // ---------------------------------------------------------------------------
       
   936 void CFotaUpdate::UpdateL()
       
   937     {
       
   938     FLOG(_L("CFotaUpdate::UpdateL() >>"));
       
   939     // Set state ........................................
       
   940     iUpdateState.iState  = RFotaEngineSession::EUpdateProgressing;
       
   941     iFotaServer->iDatabase->OpenDBL();
       
   942     iFotaServer->iDatabase->SetStateL( iUpdateState ,KNullDesC8, EFDBState );
       
   943     iFotaServer->iDatabase->CloseAndCommitDB();
       
   944 
       
   945     // Write update request for update agent..............
       
   946     FLOG(_L("CFotaUpdate::UpdateL  1 writing update.req "));
       
   947     TBuf8<150>           dp2filepath;
       
   948     HBufC16*             dp2;
       
   949     RFileWriteStream    wstr;
       
   950     CleanupClosePushL ( wstr );
       
   951     FLOG(_L("CFotaUpdate::UpdateL  2 getting pkg location"));
       
   952     iFotaServer->StoragePluginL()->GetUpdatePackageLocationL( 
       
   953             iUpdateState.iPkgId,  dp2filepath  );
       
   954     FLOG(_L("CFotaUpdate::UpdateL  3 craeting update.req") );
       
   955     User::LeaveIfError( wstr.Replace( iFotaServer->iFs
       
   956             ,KUpdateRequestFile, EFileWrite ) );
       
   957     wstr.WriteInt16L( 1 ); // version number is  1
       
   958     wstr.WriteInt32L( 1 ); // count   of cmds is 1
       
   959     wstr.WriteInt16L( 0 ); // requestid is 0
       
   960 
       
   961     dp2 = HBufC16::NewLC ( dp2filepath.Length( ) );
       
   962     dp2->Des().Copy( dp2filepath );
       
   963     wstr.WriteInt32L( dp2->Des().Length() + 1 ); // length of filename + null
       
   964     wstr.WriteL( dp2->Des() );
       
   965     wstr.WriteInt16L( 0 ); // null character
       
   966     CleanupStack::PopAndDestroy( dp2 );
       
   967     CleanupStack::PopAndDestroy( &wstr ); // wstr
       
   968 
       
   969     FLOG(_L("CFotaUpdate::UpdateL  4 craeting update.bmp") );
       
   970 
       
   971     // Write update graphic for update agent ...............
       
   972     HBufC*              updatetxt;
       
   973     HBufC*              restarttxt;
       
   974     updatetxt  = StringLoader::LoadLC( R_APPS_FOTA_UPDATING );
       
   975     restarttxt = StringLoader::LoadLC( R_APPS_FOTA_RESTARTING );
       
   976     WriteUpdateBitmapL( updatetxt->Des(), KUpdateBitmap );
       
   977     WriteUpdateBitmapL( restarttxt->Des(), KRestartingBitmap ); 
       
   978     CleanupStack::PopAndDestroy( restarttxt );
       
   979     CleanupStack::PopAndDestroy( updatetxt );
       
   980     TRAPD(error,FindScheduleL( ETrue)); //deleting the schedule (fix for failed update) 
       
   981     if(error)
       
   982         {
       
   983         FLOG(_L("CFotaUpdate::UpdateL  error in deleting previous schedule") );	
       
   984 
       
   985         }  
       
   986     // Simulate update agent by writing result file.
       
   987     CRepository* centrep( NULL);
       
   988     TInt err = KErrNone;
       
   989     TRAP(err, centrep = CRepository::NewL( KCRUidFotaServer ) );
       
   990     TInt simulate(KErrNotFound);
       
   991     if ( centrep ) 
       
   992         {
       
   993         err = centrep->Get( KSimulateUpdateAgent, simulate );
       
   994         }
       
   995     delete centrep;
       
   996     if ( simulate>0 )
       
   997         {   
       
   998         FLOG(_L("CFotaUpdate::UpdateL  5  - writing update.resp"));
       
   999         RFileWriteStream    respstr;
       
  1000         CleanupClosePushL ( respstr );
       
  1001         User::LeaveIfError( respstr.Replace( iFotaServer->iFs, KUpdateResultFile
       
  1002                 , EFileWrite ) );
       
  1003 
       
  1004         respstr.WriteUint32L(UPD_OK);
       
  1005         respstr.WriteUint32L(15);
       
  1006         respstr.WriteL(_L("UPDATE DONE!"));
       
  1007         CleanupStack::PopAndDestroy( &respstr );
       
  1008         }
       
  1009 
       
  1010     CRepository* centrep2 = NULL;
       
  1011     TRAP( err, centrep2 = CRepository::NewL( KCRUidFotaServer ) )
       
  1012     if (err==KErrNone ) 
       
  1013         {
       
  1014         centrep2->Set(  KFotaUpdateState, EFotaPendingGenAlert );
       
  1015         if(iFOTAUICustomization)
       
  1016             centrep2->Set(  KFOTAUserPostponeCount, 0 );
       
  1017        }
       
  1018      delete centrep2;
       
  1019      
       
  1020     // Boot to update mode 
       
  1021     LaunchNotifierL( ESyncMLFwUpdRebootNote, KErrNone);
       
  1022 
       
  1023     FLOG(_L("   waiting 2 sec to keep 'restarting' visible"));
       
  1024     User::After(TTimeIntervalMicroSeconds32(2000000));
       
  1025 
       
  1026     RStarterSession     starter;
       
  1027     FLOG(_L("           starter->Connect"));
       
  1028     User::LeaveIfError(starter.Connect());
       
  1029     starter.Reset(RStarterSession::EFirmwareUpdate);
       
  1030     starter.Close();
       
  1031     FLOG(_L("CFotaUpdate::UpdateL() <<"));
       
  1032     }   
       
  1033 
       
  1034 
       
  1035 // ---------------------------------------------------------------------------
       
  1036 // CFotaUpdate::CheckBatteryL()
       
  1037 // Cheks if there's enough battery power to update
       
  1038 // ---------------------------------------------------------------------------
       
  1039 //
       
  1040 TBool CFotaUpdate::CheckBatteryL(TBool aUSBChargingsupported)
       
  1041     {
       
  1042     TInt        chargingstatus( EChargingStatusError );
       
  1043     TInt        batterylevel( 1 );
       
  1044     TBool       enoughPower( EFalse );
       
  1045     // Read battery
       
  1046     FLOG(_L("   0.9 checking batt level"));
       
  1047     RProperty   pw;
       
  1048     User::LeaveIfError(pw.Attach(KPSUidHWRMPowerState,KHWRMBatteryLevel));
       
  1049     User::LeaveIfError(pw.Get( batterylevel ));
       
  1050     pw.Close();
       
  1051     if(aUSBChargingsupported)
       
  1052         {
       
  1053         if ( batterylevel >= EBatteryLevelLevel2 ) 
       
  1054             enoughPower = ETrue;
       
  1055 
       
  1056         //return enoughPower; 
       
  1057         }
       
  1058     else
       
  1059         {
       
  1060         User::LeaveIfError(pw.Attach(KPSUidHWRMPowerState,KHWRMChargingStatus));
       
  1061         User::LeaveIfError(pw.Get( chargingstatus ));
       
  1062         pw.Close();
       
  1063 
       
  1064         // Too low battery, power insufficient
       
  1065         if ( batterylevel >= EBatteryLevelLevel2 ) 
       
  1066             {
       
  1067             enoughPower = ETrue;
       
  1068             }
       
  1069         // But charger is connected, power sufficient
       
  1070         if ( chargingstatus != EChargingStatusError
       
  1071                 && chargingstatus != EChargingStatusNotConnected )
       
  1072             {
       
  1073             enoughPower = ETrue;
       
  1074             }
       
  1075         }
       
  1076 
       
  1077     if (!enoughPower)
       
  1078         iChargeToMonitor = EBatteryLevelLevel2;
       
  1079     FLOG(_L("   1.0 batt level: %d  (0..7), chargingstatus %d")
       
  1080             ,batterylevel,chargingstatus);
       
  1081     FLOG(_L("CFotaUpdate::checkBattery %d"), enoughPower?1:0 );
       
  1082     return enoughPower;
       
  1083     }
       
  1084 
       
  1085 
       
  1086 // ---------------------------------------------------------------------------
       
  1087 // CFotaUpdate::StartUpdateL
       
  1088 // Starts fw updating (shows a notifier to user). 
       
  1089 // ---------------------------------------------------------------------------
       
  1090 //
       
  1091 void CFotaUpdate::StartUpdateL( const TDownloadIPCParams &aParams )
       
  1092     {
       
  1093     FLOG(_L("CFotaUpdate::StartUpdateL(TDownloadIPCParams aParams) >> \
       
  1094     pkig:%d"), aParams.iPkgId );
       
  1095     TSmlProfileId profile(KErrNotFound);
       
  1096 
       
  1097     // Get update state from db
       
  1098     iFotaServer->iDatabase->OpenDBL( );
       
  1099     iUpdateState = iFotaServer->iDatabase->GetStateL( aParams.iPkgId );
       
  1100     iFotaServer->iDatabase->CloseAndCommitDB( );
       
  1101 
       
  1102     FLOG(_L("   0 "));
       
  1103     if ( iUpdateState.iPkgId == KErrNotFound )
       
  1104         {
       
  1105         FLOG(_L("  STATE NOT FOUND pkgid %d"), aParams.iPkgId);
       
  1106         User::Leave(KErrNotFound);
       
  1107         }
       
  1108 
       
  1109     // Resolve profile id
       
  1110     RSyncMLSession sml;
       
  1111     TInt                    tmp1;
       
  1112     TSmlServerAlertedAction tmp2;
       
  1113     TBool	                tmp3;
       
  1114     TInt					tmp4;
       
  1115     CleanupClosePushL( sml );
       
  1116     sml.OpenL();
       
  1117     // Use stored profile
       
  1118     if ( aParams.iProfileId == KErrNotFound )
       
  1119         {
       
  1120         iFotaServer->GetProfileDataL( &sml,iUpdateState.iProfileId
       
  1121                 ,tmp1,tmp2,tmp3,tmp4);
       
  1122         profile = iUpdateState.iProfileId;
       
  1123         }
       
  1124     // Use passed profile. If error, then try to use stored profile
       
  1125     else
       
  1126         {
       
  1127         TRAPD(err, iFotaServer->GetProfileDataL( &sml,aParams.iProfileId
       
  1128                 ,tmp1,tmp2, tmp3, tmp4 ) );
       
  1129         if ( err != KErrNone )
       
  1130             {
       
  1131             iFotaServer->GetProfileDataL( &sml,iUpdateState.iProfileId
       
  1132                     ,tmp1,tmp2,tmp3,tmp4);
       
  1133             profile = iUpdateState.iProfileId;
       
  1134             }
       
  1135         else
       
  1136             {
       
  1137             profile = aParams.iProfileId;
       
  1138             }
       
  1139         }
       
  1140     CleanupStack::PopAndDestroy( &sml );
       
  1141 
       
  1142     // Must update package state with fresh params (it could be empty)
       
  1143     TUint fields = EFDBState | EFDBPkgName|EFDBVersion;
       
  1144     if ( profile != iUpdateState.iProfileId )
       
  1145         {  
       
  1146         fields |= EFDBProfileId;
       
  1147         }
       
  1148 
       
  1149     iUpdateState.iProfileId     = profile;
       
  1150     iUpdateState.iPkgName       = aParams.iPkgName;
       
  1151     iUpdateState.iPkgVersion    = aParams.iPkgVersion;
       
  1152     iUpdateState.iState         = RFotaEngineSession::EStartingUpdate;
       
  1153     iUpdateState.iResult		= KErrNotFound;
       
  1154     iUpdateState.iSendAlert     = EFalse; // HJHA-6MJCFE . This operation will
       
  1155     // not reach any final state.
       
  1156     iFotaServer->iDatabase->OpenDBL( );
       
  1157     iFotaServer->iDatabase->SetStateL( iUpdateState, KNullDesC8, fields );
       
  1158     iFotaServer->iDatabase->CloseAndCommitDB( );
       
  1159 
       
  1160     CRepository* centrep2 = NULL;
       
  1161     TRAPD( err, centrep2 = CRepository::NewL( KCRUidFotaServer ) )
       
  1162     if (err==KErrNone ) 
       
  1163         {  TInt val;
       
  1164         //
       
  1165         centrep2->Get(  KFotaUpdateState, val );
       
  1166 
       
  1167         if(val == EFotaUpdateInterrupted)
       
  1168             centrep2->Set(  KFotaUpdateState, EFotaDefault );
       
  1169         }
       
  1170     delete centrep2;
       
  1171 
       
  1172     FLOG(_L("CFotaUpdate::StartUpdateL  2"));
       
  1173     //new code
       
  1174     	 TInt client = iFotaServer->GetInstallUpdateClientL();
       
  1175        if( CheckUpdateVariations() && 
       
  1176    			client!= CFotaSrvSession::EOMADMAppUi &&  
       
  1177          client != CFotaSrvSession::EFMSServer ) 
       
  1178        {
       
  1179        TInt callactive(EFalse);
       
  1180        RFMSClient fmsclient;    
       
  1181        TRAPD(err,fmsclient.OpenL());
       
  1182        if(err == KErrNone)
       
  1183            {
       
  1184            CleanupClosePushL(fmsclient);
       
  1185            FLOG(_L("CFotaUpdate::startupdate FMS for active call >>"));                
       
  1186            TInt err1 = fmsclient.IsPhoneCallActive(callactive);
       
  1187            FLOG(_L("IsPhoneCallActive returns %d  >>"),err1);       
       
  1188            }
       
  1189        if(callactive)
       
  1190            {
       
  1191            FLOG(_L(" active phone call found"));           
       
  1192            
       
  1193            FLOG(_L("putting call end Ao start pkgid=%d, profileid=%d"),iUpdateState.iPkgId,iUpdateState.iProfileId);
       
  1194            TInt err1 = fmsclient.MonitorActivePhoneCallEnd(iUpdateState.iPkgId,iUpdateState.iProfileId);
       
  1195            FLOG(_L("MonitorActivePhoneCallEnd returns %d  >>"),err1);    
       
  1196            }
       
  1197        else
       
  1198            {
       
  1199            FLOG(_L("CFotaUpdate::StartUpdateL  2"));
       
  1200            
       
  1201            TDriveNumber drive;
       
  1202 
       
  1203     TBool ret (EFalse);
       
  1204     TRAPD(err2, ret = iFotaServer->NeedToDecryptL(iUpdateState.iPkgId, drive));
       
  1205     if ( err2 == KErrNone )
       
  1206         {
       
  1207         if (!ret)
       
  1208             {
       
  1209             //Drive is not encrypted
       
  1210 			DisplayInstallationNoteTypeL();
       
  1211             //LaunchNotifierL( ESyncMLFwUpdStartQuery, iUpdateState.iProfileId );
       
  1212             }
       
  1213         else
       
  1214             {
       
  1215             //Drive is encrypted.
       
  1216             LaunchNotifierL( ESyncMLFwUpdStartQueryEnc, iUpdateState.iProfileId );
       
  1217             }
       
  1218         }
       
  1219     else
       
  1220         {
       
  1221         HandleUpdateErrorL(err2);
       
  1222         }
       
  1223        
       
  1224            }
       
  1225        if(err == KErrNone) //fms opening error
       
  1226            {
       
  1227            CleanupStack::PopAndDestroy(&fmsclient);
       
  1228            }
       
  1229        }
       
  1230    else
       
  1231        {
       
  1232        FLOG(_L("CFotaUpdate::StartUpdateL  2 DM UI Install update"));
       
  1233        //LaunchNotifierL( ESyncMLFwUpdStartQuery, iUpdateState.iProfileId );
       
  1234        
       
  1235            TDriveNumber drive;
       
  1236 
       
  1237     TBool ret (EFalse);
       
  1238     TRAP(err, ret = iFotaServer->NeedToDecryptL(iUpdateState.iPkgId, drive));
       
  1239     if ( err == KErrNone )
       
  1240         {
       
  1241         if (!ret)
       
  1242             {
       
  1243             //Drive is not encrypted
       
  1244 			DisplayInstallationNoteTypeL();
       
  1245             //LaunchNotifierL( ESyncMLFwUpdStartQuery, iUpdateState.iProfileId );
       
  1246             }
       
  1247         else
       
  1248             {
       
  1249             //Drive is encrypted.
       
  1250             LaunchNotifierL( ESyncMLFwUpdStartQueryEnc, iUpdateState.iProfileId );
       
  1251             }
       
  1252         }
       
  1253     else
       
  1254         {
       
  1255         HandleUpdateErrorL(err);
       
  1256         }
       
  1257        
       
  1258        }                   
       
  1259 
       
  1260     //new code
       
  1261     
       
  1262 
       
  1263     FLOG(_L("CFotaUpdate::StartUpdateL(TDownloadIPCParams aParams) << \
       
  1264     pkig:%d"),aParams.iPkgId);
       
  1265     }
       
  1266 	
       
  1267 	
       
  1268 void CFotaUpdate::DisplayInstallationNoteTypeL()
       
  1269     {
       
  1270     FLOG(_L("CFotaUpdate::DisplayInstallationNoteTypeL: >>"));
       
  1271     
       
  1272     //CRepository* centrep = NULL;
       
  1273     TInt err = KErrNone;
       
  1274     TInt CustomNotes = 0;
       
  1275 	if(iCentrep == NULL)
       
  1276 	{
       
  1277     TRAP(err, iCentrep = CRepository::NewL(TUid::Uid(KFotaServerUid)));
       
  1278     if(err)
       
  1279         {
       
  1280         FLOG(_L("DisplayInstallationNoteTypeL: reading from cenrep failed, returning ENormal"));
       
  1281         LaunchNotifierL( ESyncMLFwUpdStartQuery, iUpdateState.iProfileId );
       
  1282         return;
       
  1283         }
       
  1284 	}
       
  1285     
       
  1286     err= iCentrep->Get( KFOTAUINotesCustomization, CustomNotes);    
       
  1287     if (err)
       
  1288         CustomNotes = 0;
       
  1289     iFOTAUICustomization = CustomNotes;
       
  1290     
       
  1291     if(iFOTAUICustomization)
       
  1292         {
       
  1293         //TRAP(err, centrep = CRepository::NewL( TUid::Uid(KFotaServerUid)));
       
  1294         //if(err)
       
  1295         //    {
       
  1296         //    FLOG(_L("DetermineNoteType: reading from cenrep failed, returning ETrue"));
       
  1297         //    return ETrue;
       
  1298         //    }
       
  1299         
       
  1300         TInt fotaUserPostpone = 0,fotaMaxPostpone = 3;
       
  1301         if(iCentrep)
       
  1302             {
       
  1303             iCentrep->Get( KFOTAUserPostponeCount , fotaUserPostpone );
       
  1304             iCentrep->Get( KFOTAMaxPostponeCount , fotaMaxPostpone );
       
  1305             }
       
  1306         iFOTAUserPostponeCount = fotaUserPostpone;
       
  1307         iFOTAMaxPostponeCount = fotaMaxPostpone;
       
  1308 
       
  1309         if(iFOTAUserPostponeCount < iFOTAMaxPostponeCount)
       
  1310             {
       
  1311             FLOG(_L("DisplayInstallationNoteTypeL: returning EPostponeLimit"));
       
  1312             LaunchNotifierL( ESyncMLFwUpdPostponeLimitQuery, iUpdateState.iProfileId );
       
  1313             //return EPostponeLimit;
       
  1314             }
       
  1315         else
       
  1316             {
       
  1317             FLOG(_L("DisplayInstallationNoteTypeL: returning EForceStart"));
       
  1318             LaunchNotifierL( ESyncMLFwUpdForceQuery, iUpdateState.iProfileId );
       
  1319             //return EForceStart;
       
  1320             }
       
  1321         }
       
  1322     else
       
  1323         {
       
  1324         FLOG(_L("DisplayInstallationNoteTypeL: returning ENormal"));
       
  1325         LaunchNotifierL( ESyncMLFwUpdStartQuery, iUpdateState.iProfileId );
       
  1326         //return ENormal;
       
  1327         }
       
  1328     
       
  1329     }
       
  1330 
       
  1331 
       
  1332 
       
  1333 // ---------------------------------------------------------------------------
       
  1334 // CFotaUpdate::CreateRadionButtonArrayLC
       
  1335 // Creates the array of radio buttons to display 
       
  1336 // on radio button setting page
       
  1337 // ---------------------------------------------------------------------------
       
  1338 //
       
  1339 
       
  1340 CDesCArray* CFotaUpdate::CreateRadionButtonArrayLC( const TScheduleEntryInfo2 aScEntry,
       
  1341         TInt& aSelectionIndex)
       
  1342     {
       
  1343     // selection defaults to 0
       
  1344     FLOG(_L("CFotaUpdate::CreateRadionButtonArrayLC >>"));  
       
  1345     aSelectionIndex = 0;
       
  1346     CDesCArray* array = new (ELeave) CDesC16ArrayFlat(4);
       
  1347     CleanupStack::PushL(array);
       
  1348     for (TInt i=0;i<iInterval.Count();i++)   
       
  1349 
       
  1350         {    
       
  1351         switch (iIntervalType[i])
       
  1352             {
       
  1353             case EHourly :
       
  1354                 {
       
  1355 
       
  1356                 HBufC* string1 ;
       
  1357                 if(iInterval[i] == 1)
       
  1358                     string1 = StringLoader::LoadLC(R_FOTA_RADIO_DIALOG_1HOUR_REMAINDER);
       
  1359                 else
       
  1360                     string1 = StringLoader::LoadLC(R_QTN_FOTA_REMINDER_HOURS , iInterval[i] );
       
  1361 
       
  1362                 array->AppendL( string1->Des() );
       
  1363                 CleanupStack::PopAndDestroy( string1 );
       
  1364                 if(aScEntry.Interval() == iInterval[i] && aScEntry.IntervalType()== EHourly ) 
       
  1365                     aSelectionIndex = i; 
       
  1366                 }
       
  1367                 break; 
       
  1368 
       
  1369             case EDaily:
       
  1370                 {
       
  1371 
       
  1372                 HBufC* string1; //= StringLoader::LoadLC(R_QTN_FOTA_REMINDER_DAYS , iInterval[i] );
       
  1373                 if(iInterval[i] == 1)
       
  1374                     string1 = StringLoader::LoadLC(R_FOTA_RADIO_DIALOG_1DAY_REMAINDER);
       
  1375                 else
       
  1376                     string1 = StringLoader::LoadLC(R_QTN_FOTA_REMINDER_DAYS , iInterval[i] );
       
  1377 
       
  1378                 array->AppendL( string1->Des() );
       
  1379                 CleanupStack::PopAndDestroy( string1 );
       
  1380                 if(aScEntry.Interval() == iInterval[i] && aScEntry.IntervalType()== EDaily ) 
       
  1381                     aSelectionIndex = i;
       
  1382                 }
       
  1383                 break;
       
  1384             case EMonthly:
       
  1385                 {
       
  1386 
       
  1387                 HBufC* string1 ;//== StringLoader::LoadLC(R_QTN_FOTA_REMINDER_MINUTES , iInterval[i] );
       
  1388                 if(iInterval[i] == 1)
       
  1389                     string1 = StringLoader::LoadLC(R_QTN_FOTA_REMINDER_1_MINUTE);
       
  1390                 else
       
  1391                     string1 = StringLoader::LoadLC(R_QTN_FOTA_REMINDER_MINUTES , iInterval[i] );
       
  1392 
       
  1393                 array->AppendL( string1->Des() );
       
  1394                 CleanupStack::PopAndDestroy( string1 );
       
  1395                 if(aScEntry.Interval() == iInterval[i] && aScEntry.IntervalType()== EMonthly) 
       
  1396                     aSelectionIndex = i;
       
  1397                 }
       
  1398                 break; 
       
  1399 
       
  1400                 /*case EYearly:
       
  1401     		     {
       
  1402     		     tempChar.Num(iInterval[i]);
       
  1403     		     HBufC* string1 = StringLoader::LoadLC(R_FOTA_RADIO_DIALOG_YEAR_REMAINDER , iInterval[i] );
       
  1404                	 array->AppendL( string1->Des() );
       
  1405                	 CleanupStack::PopAndDestroy( string1 );
       
  1406                	 if(aScEntry.Interval() == iInterval[i] ) 
       
  1407                	          aSelectionIndex = i;
       
  1408     		     }
       
  1409     		     break;*/
       
  1410             }
       
  1411         }
       
  1412 
       
  1413     if(iIntervalType[iIntervalType.Count()-1] == ENoReminderOn )
       
  1414         {
       
  1415         HBufC* string1 = StringLoader::LoadLC( R_FOTA_RADIO_DIALOG_NO_REMAINDER);
       
  1416         array->AppendL( string1->Des() );
       
  1417         CleanupStack::PopAndDestroy( string1 );
       
  1418         }
       
  1419     FLOG(_L("CFotaUpdate::CreateRadionButtonArrayLC <<"));  
       
  1420     return array;
       
  1421     }
       
  1422 // ---------------------------------------------------------------------------
       
  1423 // CFotaUpdate::ParseScheduleString()
       
  1424 // Parse the schedule string from cenrep
       
  1425 // ---------------------------------------------------------------------------
       
  1426 //
       
  1427 //
       
  1428 void CFotaUpdate::ParseScheduleStringL()
       
  1429     {    
       
  1430     FLOG(_L("CFotaUpdate::ParseScheduleString() >>")); 
       
  1431 
       
  1432     GetSchedulesInfoL();
       
  1433 
       
  1434 
       
  1435 
       
  1436     iInterval.Reset();
       
  1437     iIntervalType.Reset();
       
  1438     TBuf<5> aTime;
       
  1439     TBuf<9> aSched,tempChar;
       
  1440     aTime.Zero();
       
  1441     aSched.Zero();
       
  1442 
       
  1443 
       
  1444     //TText*sep =   (TText *)ksep.Ptr();
       
  1445     TBufC<1>ksep(Ksep);
       
  1446     TText *t = (TText *)ksep.Ptr();      //just for Initialization
       
  1447     for(TInt i = 0; i<iscInfo.Length();i++)
       
  1448         {
       
  1449 
       
  1450         TChar ch = iscInfo[i];
       
  1451         if ( ch.IsDigit())            //separates digits from the string
       
  1452             {  aTime.Append(ch); 
       
  1453 
       
  1454             }
       
  1455         else
       
  1456             {  
       
  1457             aSched.Append(ch);             
       
  1458             *t = ch;
       
  1459             if (*t ==     'h' || *t =='H')      //hour
       
  1460                 { iIntervalType.Append(EHourly) ;
       
  1461                 TInt val =1 ; 
       
  1462                 TLex  lex(aTime);
       
  1463                 TInt err = lex.Val(val);
       
  1464                 if (err)
       
  1465                     {
       
  1466                     iInterval.Append(1);
       
  1467                     }
       
  1468                 else
       
  1469                     {
       
  1470                     iInterval.Append(val); 	
       
  1471                     }
       
  1472 
       
  1473                 }
       
  1474             else if (*t ==     'd'|| *t =='D')      //daily
       
  1475                 {iIntervalType.Append(EDaily);
       
  1476                 TInt val =1 ; 
       
  1477                 TLex  lex(aTime);
       
  1478                 TInt err = lex.Val(val);
       
  1479                 if (err)
       
  1480                     {
       
  1481                     iInterval.Append(1);
       
  1482                     }
       
  1483                 else
       
  1484                     {
       
  1485                     iInterval.Append(val);  	
       
  1486                     }
       
  1487 
       
  1488                 }
       
  1489             else if (*t ==     'm'||  *t =='M')   //minute
       
  1490                 {iIntervalType.Append( EMonthly);
       
  1491                 TInt val =1 ; 
       
  1492                 TLex  lex(aTime);
       
  1493                 TInt err = lex.Val(val);
       
  1494                 if (err)
       
  1495                     {
       
  1496 
       
  1497                     iInterval.Append(1);
       
  1498                     }
       
  1499                 else
       
  1500                     {
       
  1501                     iInterval.Append(val);  	
       
  1502                     }
       
  1503 
       
  1504                 }
       
  1505             /* else if (*t ==     'y')   //year
       
  1506                   {iIntervalType.Append( EYearly);
       
  1507                     TInt val =1 ; 
       
  1508                	    TLex  lex(aTime);
       
  1509                	    TInt err = lex.Val(val);
       
  1510                	    if (err)
       
  1511                	    {
       
  1512 
       
  1513                         iInterval.Append(1);    
       
  1514                	    }
       
  1515                	    else
       
  1516                	    {
       
  1517                	     iInterval.Append(val); 	
       
  1518                	    }
       
  1519 
       
  1520                   }*/
       
  1521             else if (*t == ':')   //colon
       
  1522                 {
       
  1523                 aTime.Zero();
       
  1524                 aSched.Zero();
       
  1525                 }
       
  1526             }
       
  1527         }
       
  1528     TBool  checkSchInfo =EFalse;
       
  1529 
       
  1530     if(iInterval.Count() == iIntervalType.Count() )
       
  1531         {  
       
  1532         for (TInt j =0 ;j< iInterval.Count();j++ )
       
  1533             {                                                 //setting to default 1h:4h:1d:3d
       
  1534             if(iInterval[j] < 0 || iIntervalType[j] > 3 ) 
       
  1535                 {   
       
  1536                 iInterval.Reset();    iIntervalType.Reset();
       
  1537                 iInterval.Append(1);  iIntervalType.Append(EHourly); 
       
  1538                 iInterval.Append(4);  iIntervalType.Append(EHourly); 
       
  1539                 iInterval.Append(1);  iIntervalType.Append(EDaily); 
       
  1540                 iInterval.Append(3);  iIntervalType.Append(EDaily);     
       
  1541                 checkSchInfo =ETrue;
       
  1542                 break;
       
  1543                 }
       
  1544             }
       
  1545         }
       
  1546     else
       
  1547         {        
       
  1548         iInterval.Reset();    iIntervalType.Reset();
       
  1549         iInterval.Append(1);  iIntervalType.Append(EHourly); 
       
  1550         iInterval.Append(4);  iIntervalType.Append(EHourly);  //setting to default 1h:4h:1d:3d
       
  1551         iInterval.Append(1);  iIntervalType.Append(EDaily); 
       
  1552         iInterval.Append(3);  iIntervalType.Append(EDaily);     
       
  1553         checkSchInfo =ETrue;
       
  1554         }
       
  1555 
       
  1556     TPtrC16 lastFive =  iscInfo.Right(5);
       
  1557     _LIT( KNoRem , "NROFF");
       
  1558     TPtrC noRem( KNoRem );   //check for No Remainder
       
  1559     if (lastFive != noRem || checkSchInfo)
       
  1560         {
       
  1561         iIntervalType.Append(ENoReminderOn);
       
  1562         }
       
  1563     else
       
  1564         { 
       
  1565         iIntervalType.Append(ENoReminderOff);
       
  1566 
       
  1567         }
       
  1568     FLOG(_L("CFotaUpdate::ParseScheduleString() <<"));
       
  1569 
       
  1570     } 
       
  1571 
       
  1572 
       
  1573 // ---------------------------------------------------------------------------
       
  1574 // CFotaUpdate::GetSchedulesInfoL
       
  1575 // Get the schedule info from the cenrep
       
  1576 // ---------------------------------------------------------------------------
       
  1577 //
       
  1578 //
       
  1579 void CFotaUpdate::GetSchedulesInfoL()
       
  1580     {
       
  1581 
       
  1582     CRepository* centrep = NULL;
       
  1583     //TBuf<255> scInfo; 
       
  1584     centrep = CRepository::NewLC( TUid::Uid(KFotaServerUid));
       
  1585     if ( centrep )
       
  1586         {TBuf<255> temp;
       
  1587         if (centrep->Get( KFotaOptionsForReminderDuration , temp )==KErrNone && temp.Length() )
       
  1588             {   FLOG(_L("CFotaUpdate::ParseScheduleString taking from cenrep <<"));
       
  1589             temp.Trim();    //read from cenrep
       
  1590             iscInfo.Copy(temp);
       
  1591             }
       
  1592         else
       
  1593             {                               //else set to default
       
  1594             iscInfo.Copy(KDefaultSched);  
       
  1595 
       
  1596             }
       
  1597         CleanupStack::PopAndDestroy(centrep);
       
  1598         }
       
  1599     else
       
  1600         {
       
  1601         iscInfo.Copy(KDefaultSched);    //else set to default
       
  1602         }	
       
  1603 
       
  1604     }
       
  1605 
       
  1606 // ---------------------------------------------------------------------------
       
  1607 // CFotaUpdate::ShowReminderDialogL
       
  1608 // Prepare and display Reminder Dialog using RFotaReminderDlg
       
  1609 // on radio button setting page
       
  1610 // ---------------------------------------------------------------------------
       
  1611 //
       
  1612 void CFotaUpdate::ShowReminderDialogL()
       
  1613     {
       
  1614     // Push default content to navigation pane and change title
       
  1615 	if(iFOTAUICustomization)
       
  1616 	    {
       
  1617 	    //if(iFOTAUICustomization)
       
  1618 	    //    {
       
  1619 	        if(iCentrep)
       
  1620 	            {
       
  1621 	            // When User has pressed Later, Postpone limit has to be Incremented
       
  1622 	            iFOTAUserPostponeCount = iFOTAUserPostponeCount + 1;
       
  1623 	            iCentrep->Set( KFOTAUserPostponeCount , iFOTAUserPostponeCount );
       
  1624 	            }
       
  1625 	    //    }
       
  1626 	    
       
  1627 	    
       
  1628 	    if(iFOTAUserPostponeCount > iFOTAMaxPostponeCount)
       
  1629 	        {
       
  1630 	        // When User presses the end key during a Force Update Query or a Update Warning note, Force Update Query should be shown again.
       
  1631 	        TIntervalType   tIntervalType;    
       
  1632 	        TInt tInterval;
       
  1633 	        tIntervalType = (TIntervalType)EEndKeyReminder;
       
  1634 	        tInterval = 1;
       
  1635 	        CreateScheduleL ( iUpdateState.iPkgId, tIntervalType ,tInterval );
       
  1636 	        iFotaServer->FinalizeUpdateL();
       
  1637 	        return;
       
  1638 	        }
       
  1639 	    }
       
  1640     CEikStatusPane* statusPane = iAvkonAppUi->StatusPane();
       
  1641     CAknNavigationControlContainer* naviPane = NULL;
       
  1642     HBufC* originalTitle;
       
  1643     CAknTitlePane* titlePane = NULL;
       
  1644     originalTitle = HBufC::NewLC( 50 ); //KNsmlMaxTitleSize
       
  1645 
       
  1646     if ( statusPane && statusPane->PaneCapabilities( 
       
  1647             TUid::Uid(EEikStatusPaneUidTitle) ).IsPresent() )
       
  1648         {
       
  1649         naviPane = ( CAknNavigationControlContainer* )
       
  1650         statusPane->ControlL( TUid::Uid(EEikStatusPaneUidNavi) );
       
  1651         naviPane->PushDefaultL();
       
  1652         titlePane = static_cast<CAknTitlePane*>
       
  1653         ( statusPane->ControlL( TUid::Uid(
       
  1654                 EEikStatusPaneUidTitle) ) );
       
  1655         if ( titlePane->Text() ) 
       
  1656             {
       
  1657             originalTitle->Des().Copy( *titlePane->Text() );
       
  1658             HBufC* titleText = StringLoader::LoadLC(
       
  1659                     R_QTN_DM_TITLE_SETTINGS_DIALOG );
       
  1660             titlePane->SetTextL( *titleText );
       
  1661             CleanupStack::PopAndDestroy( titleText );
       
  1662             titleText = NULL;
       
  1663             }
       
  1664         }
       
  1665 
       
  1666     TApaTaskList taskList(iFotaServer->GetEikEnv()->WsSession());
       
  1667     TApaTask task=taskList.FindApp(TUid::Uid(KFotaServerUid));
       
  1668 
       
  1669     if(task.Exists())
       
  1670         task.BringToForeground();
       
  1671 
       
  1672     // Construct radio btn list and launch radio button dlg
       
  1673     TScheduleEntryInfo2 en=FindScheduleL( EFalse );
       
  1674     TInt radioSelectionIndex( KErrNotFound );
       
  1675     TRAPD(error,ParseScheduleStringL());  
       
  1676     if (error)
       
  1677         {
       
  1678         iInterval.Reset();    iIntervalType.Reset();
       
  1679         iInterval.Append(1);  iIntervalType.Append(EHourly); 
       
  1680         iInterval.Append(4);  iIntervalType.Append(EHourly); 
       
  1681         iInterval.Append(1);  iIntervalType.Append(EDaily); 
       
  1682         iInterval.Append(3);  iIntervalType.Append(EDaily); 
       
  1683         iIntervalType.Append(ENoReminderOn);
       
  1684         }
       
  1685     if(iInterval.Count() > 4)
       
  1686         {
       
  1687         for(TInt i= 4;i <iInterval.Count();i++)
       
  1688             iInterval.Remove(i);
       
  1689         }  
       
  1690     CDesCArray* itemArray = CreateRadionButtonArrayLC( en,radioSelectionIndex );
       
  1691 
       
  1692     TInt aEvent(KErrNone);
       
  1693     CFotaReminderDlg* aFotaReminderDlg = CFotaReminderDlg::NewL(radioSelectionIndex,itemArray,aEvent );
       
  1694     FLOG(_L("after creating fota reminder dialog"));   
       
  1695     CleanupStack::PushL(aFotaReminderDlg);
       
  1696     TInt presentPriotiry = CEikonEnv::Static()->RootWin().OrdinalPriority();
       
  1697     TInt presentPosition = CEikonEnv::Static()->RootWin().OrdinalPosition();
       
  1698     CEikonEnv::Static()->RootWin().SetOrdinalPosition(presentPosition, ECoeWinPriorityMedium);  //setting the priority to medium for activation on top of global notes
       
  1699 
       
  1700     TInt ret = aFotaReminderDlg->ExecuteLD(CAknSettingPage::EUpdateWhenChanged);
       
  1701     CleanupStack::Pop(aFotaReminderDlg);	     							
       
  1702     CleanupStack::PopAndDestroy( itemArray );
       
  1703 
       
  1704     CEikonEnv::Static()->RootWin().SetOrdinalPosition(presentPosition, presentPriotiry); //resetting to previous priority
       
  1705     FLOG(_L("****Event key = %d"),aEvent);
       
  1706 
       
  1707     if (aEvent == EEventKey)
       
  1708         {
       
  1709         FLOG(_L("Exiting...."));
       
  1710         TApaTaskList taskList(iFotaServer->GetEikEnv()->WsSession());
       
  1711         TApaTask task=taskList.FindApp(TUid::Uid(KOmaDMAppUid));
       
  1712         if(task.Exists())
       
  1713             {
       
  1714             task.EndTask();
       
  1715             task.KillTask();
       
  1716             }
       
  1717 
       
  1718         return;
       
  1719         }
       
  1720 		if(naviPane)
       
  1721     	naviPane->Pop();
       
  1722     if(	titlePane )
       
  1723     {	
       
  1724     	if ( titlePane->Text() && originalTitle ) 
       
  1725         {
       
  1726         TRAP_IGNORE( titlePane->SetTextL( *originalTitle ) );
       
  1727         }
       
  1728     }
       
  1729     CleanupStack::PopAndDestroy( originalTitle );
       
  1730     task.SendToBackground();	
       
  1731 
       
  1732     CreateNewScheduleL(ret,radioSelectionIndex);	
       
  1733 
       
  1734 
       
  1735     }
       
  1736 
       
  1737 // ---------------------------------------------------------------------------
       
  1738 // CFotaUpdate::CreateNewScheduleL
       
  1739 // Creates the new schedule
       
  1740 // ---------------------------------------------------------------------------
       
  1741 //
       
  1742 void CFotaUpdate::CreateNewScheduleL(TInt aRet, TInt aRadioSelectionIndex)
       
  1743     {
       
  1744 
       
  1745     TScheduleEntryInfo2 en=FindScheduleL( EFalse );
       
  1746     TIntervalType   tIntervalType;    
       
  1747     TInt tInterval; 
       
  1748     if ( aRet && aRadioSelectionIndex== 0 ) 
       
  1749         {
       
  1750         FLOG(_L("Creating reminder: for 1st option"));
       
  1751         tIntervalType = (TIntervalType)iIntervalType[aRadioSelectionIndex];				
       
  1752         tInterval     = iInterval[aRadioSelectionIndex];
       
  1753         if(iFOTAUICustomization)
       
  1754                     LaunchNotifierL( ESyncMLFwUpdPostponeNote , iFOTAUserPostponeCount, iFOTAMaxPostponeCount, tIntervalType,tInterval);
       
  1755         en=FindScheduleL( ETrue );
       
  1756         CreateScheduleL ( iUpdateState.iPkgId, tIntervalType ,tInterval );
       
  1757         iFotaServer->FinalizeUpdateL();
       
  1758         }
       
  1759     else if(aRet && aRadioSelectionIndex == 1 )
       
  1760         {
       
  1761         FLOG(_L("Creating reminder: 4 hours"));
       
  1762         tIntervalType = (TIntervalType)iIntervalType[aRadioSelectionIndex];				
       
  1763         tInterval     = iInterval[aRadioSelectionIndex];
       
  1764         if(iFOTAUICustomization)
       
  1765                     LaunchNotifierL( ESyncMLFwUpdPostponeNote , iFOTAUserPostponeCount, iFOTAMaxPostponeCount, tIntervalType,tInterval);
       
  1766         en=FindScheduleL( ETrue );
       
  1767         CreateScheduleL ( iUpdateState.iPkgId, tIntervalType ,tInterval );
       
  1768         iFotaServer->FinalizeUpdateL();
       
  1769         }
       
  1770     else if(aRet && aRadioSelectionIndex == 2 )
       
  1771         {
       
  1772         FLOG(_L("Creating reminder: for 2nd option"));
       
  1773         tIntervalType = (TIntervalType)iIntervalType[aRadioSelectionIndex];				
       
  1774         tInterval     = iInterval[aRadioSelectionIndex];
       
  1775         if(iFOTAUICustomization)
       
  1776                     LaunchNotifierL( ESyncMLFwUpdPostponeNote , iFOTAUserPostponeCount, iFOTAMaxPostponeCount, tIntervalType,tInterval);
       
  1777         en=FindScheduleL( ETrue );
       
  1778         CreateScheduleL ( iUpdateState.iPkgId, tIntervalType ,tInterval );
       
  1779         iFotaServer->FinalizeUpdateL();
       
  1780         }
       
  1781     else if(aRet && aRadioSelectionIndex == 3 )
       
  1782         {
       
  1783         FLOG(_L("Creating reminder: for 3rd option"));
       
  1784         tIntervalType = (TIntervalType)iIntervalType[aRadioSelectionIndex];				
       
  1785         tInterval     = iInterval[aRadioSelectionIndex];
       
  1786         if(iFOTAUICustomization)
       
  1787                     LaunchNotifierL( ESyncMLFwUpdPostponeNote , iFOTAUserPostponeCount, iFOTAMaxPostponeCount, tIntervalType,tInterval);
       
  1788         en=FindScheduleL( ETrue );
       
  1789         CreateScheduleL ( iUpdateState.iPkgId, tIntervalType ,tInterval );  
       
  1790         iFotaServer->FinalizeUpdateL();
       
  1791         }
       
  1792     else if (!aRet || aRadioSelectionIndex == 4)
       
  1793         {
       
  1794         FLOG(_L("Deleting reminder, btn press %d"),aRet);
       
  1795         TScheduleEntryInfo2 en=FindScheduleL( ETrue );
       
  1796 
       
  1797         if ( !aRet ) // cancel pressed
       
  1798             {
       
  1799 			if(iFOTAUICustomization)
       
  1800 			    {
       
  1801 			    if(iCentrep)
       
  1802 			        {
       
  1803 			        	// When User has pressed Later and then does a cancel for the warning query, Postpone limit has to be decremented
       
  1804 			        iFOTAUserPostponeCount = iFOTAUserPostponeCount - 1;
       
  1805 			        iCentrep->Set( KFOTAUserPostponeCount , iFOTAUserPostponeCount );
       
  1806 			        }
       
  1807 			    }
       
  1808 			DisplayInstallationNoteTypeL();			
       
  1809 			}
       
  1810         else			  // ok pressed
       
  1811             {
       
  1812             LaunchNotifierL( ESyncMLFwUpdNoReminder , KErrNone );
       
  1813             //iFotaServer->FinalizeUpdateL();
       
  1814             }
       
  1815         }
       
  1816 
       
  1817     }
       
  1818 
       
  1819 // ---------------------------------------------------------------------------
       
  1820 // CFotaUpdate::FindScheduleL
       
  1821 // Finds fotaupdate schedule (named [102072c4])
       
  1822 // ---------------------------------------------------------------------------
       
  1823 //
       
  1824 TScheduleEntryInfo2 CFotaUpdate::FindScheduleL( const TBool aDelete )
       
  1825     {
       
  1826     TScheduleEntryInfo2                     ret;
       
  1827     TInt                                    err;    
       
  1828     RScheduler                              sc;
       
  1829     TTime                                   t; 
       
  1830     TTsTime                                 time;
       
  1831     TSchedulerItemRef                       scitem; 
       
  1832     CArrayFixFlat<TSchedulerItemRef>*     	aSchRefArray = new CArrayFixFlat <TSchedulerItemRef>(5);
       
  1833     TScheduleFilter                      	aFilter(EAllSchedules);
       
  1834     User::LeaveIfError( sc.Connect() );                             // xx
       
  1835     CleanupClosePushL( sc );
       
  1836     CleanupStack::PushL(aSchRefArray);
       
  1837 
       
  1838     User::LeaveIfError( sc.GetScheduleRefsL( *aSchRefArray,aFilter) );  // xx
       
  1839     FLOG(_L("Schedule items: "));
       
  1840     for ( TInt i=0; i<aSchRefArray->Count(); ++i  )
       
  1841         {
       
  1842         TSchedulerItemRef it = (*aSchRefArray)[i];
       
  1843         if ( it.iName == TUid::Uid(KFotaServerUid).Name()  )
       
  1844             {
       
  1845             TScheduleState2 sc_state;
       
  1846             CArrayFixFlat<TScheduleEntryInfo2>*  sc_entries = new CArrayFixFlat <TScheduleEntryInfo2>(5);
       
  1847             CArrayFixFlat<TTaskInfo>*            sc_tasks  = new CArrayFixFlat <TTaskInfo>(5);
       
  1848             TTsTime                              sc_duetime;
       
  1849             CleanupStack::PushL( sc_entries );
       
  1850             CleanupStack::PushL( sc_tasks );
       
  1851             FLOG (_L("%d. schedule handle: %d name:'%S'"),i,it.iHandle, &(it.iName) );
       
  1852 
       
  1853             err = sc.GetScheduleL ( it.iHandle , sc_state, *sc_entries,*sc_tasks,sc_duetime ); // xx
       
  1854 
       
  1855             TDateTime  dtm = sc_duetime.GetLocalTime().DateTime();
       
  1856             FLOG(_L("   schedule duetime:%d:%d"), dtm.Hour(), dtm.Minute());
       
  1857 
       
  1858             if ( err ) FLOG(_L("     schedule  sc get err %d"),err);
       
  1859             else 
       
  1860                 {
       
  1861                 for ( TInt k=0; k<sc_entries->Count();++k)
       
  1862                     {
       
  1863                     TScheduleEntryInfo2 sc_entry = (*sc_entries)[k];
       
  1864                     ret = sc_entry;
       
  1865                     TTime sctime = sc_entry.StartTime().GetLocalTime();
       
  1866                     FLOG(_L("         schedule entry %d int-type:%d int:%d start: %d:%d"),k,sc_entry.IntervalType(),sc_entry.Interval(),sctime.DateTime().Hour(),sctime.DateTime().Minute());
       
  1867                     }
       
  1868 
       
  1869                 for ( TInt j=0; j<sc_tasks->Count();++j)
       
  1870                     {
       
  1871                     TTaskInfo sc_task = (*sc_tasks)[j];
       
  1872                     FLOG(_L("         schedule task  %d  '%S'"),sc_task.iTaskId,&(sc_task.iName) );
       
  1873                     if ( aDelete && sc_task.iName==TUid::Uid(KFotaServerUid).Name() )
       
  1874                         {
       
  1875                         FLOG(_L("          schedule DeleteTask %d"),sc_task.iTaskId);
       
  1876                         User::LeaveIfError( sc.DeleteTask(sc_task.iTaskId) );       // xx
       
  1877                         }
       
  1878                     }
       
  1879                 }
       
  1880             if ( aDelete )
       
  1881                 {
       
  1882                 FLOG(_L("     DeleteSchedule %d"),it.iHandle);
       
  1883                 User::LeaveIfError(sc.DeleteSchedule(it.iHandle ));                 // xx
       
  1884                 }
       
  1885             CleanupStack::PopAndDestroy( sc_tasks );
       
  1886             CleanupStack::PopAndDestroy( sc_entries );
       
  1887             }
       
  1888         }
       
  1889     CleanupStack::PopAndDestroy( aSchRefArray );
       
  1890     CleanupStack::PopAndDestroy(&sc);
       
  1891     return ret;
       
  1892     }
       
  1893 
       
  1894 
       
  1895 
       
  1896 // ---------------------------------------------------------------------------
       
  1897 // CFotaUpdate::CreateScheduleL
       
  1898 // Creates fotaupdate schedule (named [102072c4])
       
  1899 // ---------------------------------------------------------------------------
       
  1900 //
       
  1901 TInt CFotaUpdate::CreateScheduleL ( const TInt aPackageId 
       
  1902                                    ,TIntervalType  aIntervalType
       
  1903                                    ,const TInt aInterval)
       
  1904     {
       
  1905     FLOG(_L("CFotaUpdate::CreateScheduleL ()") );
       
  1906     const TInt KRepeatForever = 0;
       
  1907     const TInt KTaskPriority = 1;
       
  1908     _LIT(KFotaScheduleExe, "Z:\\sys\\bin\\fotaschedulehandler.exe");
       
  1909     RScheduler                              sc;
       
  1910     TTime                                   t; 
       
  1911     TTsTime                                 time;
       
  1912     TSchedulerItemRef                       scitem; 
       
  1913     CArrayFixFlat<TScheduleEntryInfo2>*     scentries = new CArrayFixFlat <TScheduleEntryInfo2>(5);
       
  1914     User::LeaveIfError( sc.Connect() );                                       // xx
       
  1915     CleanupClosePushL( sc );
       
  1916     CleanupStack::PushL(scentries );
       
  1917     t.HomeTime(); 
       
  1918     switch (aIntervalType)  //create schedule based on the interval type 
       
  1919         {
       
  1920         case EHourly :
       
  1921             t = t + ((TTimeIntervalHours)aInterval);
       
  1922             break;
       
  1923         case EDaily  :
       
  1924             t = t + ((TTimeIntervalDays) aInterval);
       
  1925             break;
       
  1926         case EMonthly :
       
  1927             t = t + ((TTimeIntervalMinutes ) aInterval);
       
  1928             break; 
       
  1929         case EYearly  :
       
  1930             t = t + ((TTimeIntervalYears ) aInterval);
       
  1931             break;
       
  1932 		case EEndKeyReminder:
       
  1933                         aIntervalType = (TIntervalType)EHourly;  
       
  1934                         t = t + ((TTimeIntervalMinutes)aInterval);
       
  1935                         break;
       
  1936         default :
       
  1937             User::Panic(KFotaPanic, KErrArgument);
       
  1938 
       
  1939         }  
       
  1940 
       
  1941     time.SetLocalTime( t );
       
  1942     TScheduleEntryInfo2 scentry1(time, aIntervalType, aInterval, (TTimeIntervalMinutes)1440 ) ; // xx
       
  1943     scentries->AppendL( scentry1 );
       
  1944 
       
  1945     scitem.iName = TUid::Uid(KFotaServerUid).Name();
       
  1946     User::LeaveIfError( sc.Register(TFileName( KFotaScheduleExe ), 0 ) );    // xxx
       
  1947     User::LeaveIfError( sc.CreatePersistentSchedule(scitem, *scentries) );   // xxx
       
  1948     FLOG(_L("created schedule %d  %d:%d"),scitem.iHandle, t.DateTime().Hour(),t.DateTime().Minute() );
       
  1949     TTaskInfo taskInfo;
       
  1950     taskInfo.iName      = TUid::Uid(KFotaServerUid).Name();
       
  1951     taskInfo.iRepeat    = KRepeatForever;
       
  1952     taskInfo.iPriority  = KTaskPriority;
       
  1953 
       
  1954     TFotaScheduledUpdate fotareminder( aPackageId, scitem.iHandle );
       
  1955     TPckg<TFotaScheduledUpdate>   fotareminderpkg(fotareminder);
       
  1956 
       
  1957     HBufC* data = HBufC::NewLC(fotareminderpkg.Length());
       
  1958     data->Des().Copy(fotareminderpkg);
       
  1959 
       
  1960     User::LeaveIfError( sc.ScheduleTask(taskInfo, *data, scitem.iHandle) );  // xxx
       
  1961 
       
  1962     CleanupStack::PopAndDestroy( data );
       
  1963     CleanupStack::PopAndDestroy( scentries );
       
  1964     CleanupStack::PopAndDestroy(&sc);                                         // xx
       
  1965     return scitem.iHandle;
       
  1966 	}
       
  1967 // --------------------------------------------------------------------------
       
  1968 // CFotaUpdate::CheckUpdateVariations
       
  1969 // Checks the Install update note display,when a phone call is active
       
  1970 // --------------------------------------------------------------------------
       
  1971 //
       
  1972 TBool CFotaUpdate::CheckUpdateVariations()
       
  1973     {
       
  1974     FLOG(_L("CFotaUpdate::CheckUpdateVariations >>"));
       
  1975     CRepository* centrep( NULL);
       
  1976     TRAPD(err, centrep = CRepository::NewL( KCRUidFotaServer ) );
       
  1977     if (err) FLOG(_L("Error reading FotaServer cenrep... %d"),err);
       
  1978     TInt supported(KErrNone);
       
  1979     if (centrep ) 
       
  1980         {
       
  1981         err = centrep->Get( KFotaUpdateNoteDisplayAlways, supported );
       
  1982         if (err) FLOG(_L("Error reading cenrep key... %d"),err);                
       
  1983         delete centrep;
       
  1984         centrep = NULL;
       
  1985         }
       
  1986     if(supported) //means note to be displayed when a call is active
       
  1987         {
       
  1988         return EFalse;
       
  1989         }
       
  1990     FLOG(_L("CFotaUpdate::CheckUpdateVariations <<"));
       
  1991     return ETrue; //means note should not be displayed when a call is there
       
  1992     }
       
  1993