fmradio/fmradioengine/src/fmradioenginecentralrepositoryhandler.cpp
branchRCL_3
changeset 20 93c594350b9a
parent 0 f3d95d9c00ab
equal deleted inserted replaced
19:cce62ebc198e 20:93c594350b9a
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Contains implementation of the CCentralRepositoryHandler class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <centralrepository.h>
       
    21 #include <s32mem.h>
       
    22 #include <coemain.h> 
       
    23 #include <barsread.h>
       
    24 #include <ProfileEngineSDKCRKeys.h>
       
    25 #include <fmradioengine.rsg>
       
    26 
       
    27 #include "fmradioenginecentralrepositoryhandler.h"
       
    28 #include "fmradioengineradiosettings.h"
       
    29 #include "fmradioenginecrkeys.h"
       
    30 #include "debug.h"
       
    31 
       
    32 
       
    33 // CONSTANTS
       
    34 const TInt KRadioMaxStartupCountToTrack = 5;
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // ----------------------------------------------------
       
    38 // CCentralRepositoryHandler::CCentralRepositoryHandler
       
    39 // Default class constructor.
       
    40 // ----------------------------------------------------
       
    41 //
       
    42 CCentralRepositoryHandler::CCentralRepositoryHandler(
       
    43     TRadioSettings& aRadioSettings)  // a reference to aRadioSettings
       
    44     :iRadioSettings(&aRadioSettings)
       
    45     {
       
    46     FTRACE(FPrint(_L("calling CCentralRepositoryHandler::NewL()")));
       
    47     }
       
    48 
       
    49 // ----------------------------------------------------
       
    50 // CCentralRepositoryHandler::ConstructL
       
    51 // Second phase class constructor.
       
    52 // ----------------------------------------------------
       
    53 //
       
    54 void CCentralRepositoryHandler::ConstructL()
       
    55     {
       
    56     iCentralRepository = CRepository::NewL( KCRUidFMRadioEngine );
       
    57     
       
    58     RetrieveInitialSettings();                
       
    59     InitializeRegionsL();
       
    60         
       
    61     #ifdef __WINS__
       
    62     SetInitialSettings();
       
    63     #endif //__WINS__    
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------
       
    67 // CCentralRepositoryHandler::NewL
       
    68 // Two-phased class constructor.
       
    69 // ----------------------------------------------------
       
    70 //
       
    71 CCentralRepositoryHandler* CCentralRepositoryHandler::NewL(
       
    72     TRadioSettings& aRadioSettings)  // a reference to aRadioSettings
       
    73     {
       
    74     CCentralRepositoryHandler *self = new(ELeave) CCentralRepositoryHandler(aRadioSettings);
       
    75     CleanupStack::PushL(self);
       
    76     self->ConstructL();
       
    77     CleanupStack::Pop();
       
    78     return self;
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------
       
    82 // CCentralRepositoryHandler::~CCentralRepositoryHandler
       
    83 // Destructor of CCentralRepositoryHandler class.
       
    84 // ----------------------------------------------------
       
    85 //
       
    86 CCentralRepositoryHandler::~CCentralRepositoryHandler()
       
    87     {
       
    88     iRadioSettings = NULL;
       
    89     if ( iCentralRepository )
       
    90          {
       
    91          delete iCentralRepository;
       
    92          }
       
    93 	iRegions.ResetAndDestroy();
       
    94 	iRegions.Close();                
       
    95     }
       
    96 
       
    97 
       
    98 // ----------------------------------------------------
       
    99 // CCentralRepositoryHandler::RetrieveInitialSettings
       
   100 // Retrieves persistent settings from shared data.
       
   101 // Returns: None
       
   102 // ----------------------------------------------------
       
   103 //
       
   104 void CCentralRepositoryHandler::RetrieveInitialSettings()
       
   105     {
       
   106     FTRACE(FPrint(_L("calling CCentralRepositoryHandler::RetrieveInitialSettings() Start")));
       
   107     TInt tempData;
       
   108     
       
   109     if ( iCentralRepository->Get( KRadioHeadsetVolume, tempData ) == KErrNone )
       
   110           {
       
   111           iRadioSettings->SetHeadsetVolume( tempData );
       
   112           }
       
   113           
       
   114     if ( iCentralRepository->Get( KRadioSpeakerVolume, tempData ) == KErrNone ) 
       
   115             {
       
   116             iRadioSettings->SetSpeakerVolume( tempData );
       
   117             }
       
   118             
       
   119     if ( iCentralRepository->Get( KFmRadioFrequency, tempData ) == KErrNone )
       
   120         {
       
   121         iRadioSettings->SetFrequency( tempData );
       
   122         }
       
   123         
       
   124     if ( iCentralRepository->Get( KFmRadioChannel, tempData ) == KErrNone )
       
   125         {
       
   126         iRadioSettings->SetCurrentPreset( tempData );
       
   127         }
       
   128         
       
   129     if ( iCentralRepository->Get( KFmRadioMode, tempData ) == KErrNone)
       
   130         {
       
   131         iRadioSettings->SetRadioMode( static_cast<CRadioEngine::TRadioMode>( tempData ) );        
       
   132         }
       
   133         	
       
   134 	if ( iCentralRepository->Get( KFMCRCurrentRegion, tempData ) != KErrNone )	        
       
   135 		{
       
   136 		iCentralRepository->Set( KFMCRCurrentRegion, KErrNotFound );			
       
   137 		}
       
   138 			
       
   139 	if ( iCentralRepository->Get( KRadioCRStartupCount, tempData ) == KErrNone )
       
   140 		{
       
   141 		iRadioSettings->SetStartupCount( tempData );
       
   142 		}
       
   143 					
       
   144 	if ( iCentralRepository->Get( KFMCRRdsAfSearchEnabled, tempData ) == KErrNone )
       
   145         {
       
   146         if ( static_cast<TBool> ( tempData ) )
       
   147         	{
       
   148         	iRadioSettings->SetRdsAfSearchEnabled();
       
   149         	}
       
   150         else
       
   151         	{
       
   152             iRadioSettings->SetRdsAfSearchDisabled();
       
   153         	}
       
   154         }
       
   155 	
       
   156 	if ( iCentralRepository->Get( KFMCRJapanRegionAllowed, tempData ) == KErrNone )
       
   157 		{				
       
   158 		iRadioSettings->SetJapanRegionAllowed( static_cast<TBool>( tempData ) );
       
   159 		}			       	        
       
   160     }
       
   161 
       
   162 #ifdef __WINS__
       
   163 // ----------------------------------------------------
       
   164 // CCentralRepositoryHandler::SetInitialSettings
       
   165 // Set network info for emulator testing purposes
       
   166 // ----------------------------------------------------
       
   167 //
       
   168 void CCentralRepositoryHandler::SetInitialSettings()
       
   169 	{
       
   170 	_LIT(KNetworkInfoForEmulator, "358123");
       
   171 	iCentralRepository->Set( KFMRadioCREmulatorNetworkInfo, KNetworkInfoForEmulator );			
       
   172 	}
       
   173 #endif //__WINS__	
       
   174 
       
   175 // ----------------------------------------------------
       
   176 // CCentralRepositoryHandler::SaveEngineSettings
       
   177 // Save all engine related persistent info back to shared data
       
   178 // Returns: None
       
   179 // ----------------------------------------------------
       
   180 //
       
   181 void CCentralRepositoryHandler::SaveEngineSettings()
       
   182     {
       
   183     if ( IsFrequencyValid( iRadioSettings->Frequency() / KFMRadioFreqMultiplier ) )
       
   184     	{
       
   185     	iCentralRepository->Set( KFmRadioFrequency, iRadioSettings->Frequency() );
       
   186     	}
       
   187     else
       
   188     	{
       
   189     	TInt freq = MinFrequency() * KFMRadioFreqMultiplier;
       
   190     	iCentralRepository->Set( KFmRadioFrequency, freq  );
       
   191     	}
       
   192             
       
   193     iCentralRepository->Set( KFmRadioChannel, iRadioSettings->CurrentPreset() );
       
   194     iCentralRepository->Set( KFmRadioMode, iRadioSettings->RadioMode() );
       
   195     iCentralRepository->Set( KFMCRRdsAfSearchEnabled, iRadioSettings->IsRdsAfSearchEnabled() );
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 // CCentralRepositoryHandler::CountRegions
       
   200 // Return count of fmradio regions in regions array
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 TInt CCentralRepositoryHandler::CountRegions() const
       
   204     {
       
   205     return iRegions.Count();
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // CCentralRepositoryHandler::Region
       
   210 // Returns fmradio region at given index
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 CFMRadioRegion& CCentralRepositoryHandler::Region( TInt aIndex ) const
       
   214     {    
       
   215     if( aIndex == KErrNotFound )
       
   216         {
       
   217         aIndex = RegionIndexForId( static_cast<TInt>( EFMRadioRegionDefault ) );        
       
   218         }
       
   219 
       
   220     return *iRegions[aIndex];
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CCentralRepositoryHandler::RegionId
       
   225 // Return current region Id.
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 TInt CCentralRepositoryHandler::RegionId() const
       
   229     {
       
   230     TInt regionId;
       
   231     iCentralRepository->Get( KFMCRCurrentRegion, regionId );
       
   232     return regionId;
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // CCentralRepositoryHandler::SetRegionIdL
       
   237 // Change the the current region Id.
       
   238 // ---------------------------------------------------------------------------
       
   239 //
       
   240 void CCentralRepositoryHandler::SetRegionIdL( TInt aRegion )
       
   241     {
       
   242     iCentralRepository->Set( KFMCRCurrentRegion, aRegion );
       
   243     UpdateCurrentRegionIdx( aRegion );    
       
   244     
       
   245     iRadioSettings->SetFrequency( Region( iCurrentRegionIdx ).MinFrequency() * KFMRadioFreqMultiplier ); 
       
   246 	iRadioSettings->SetMinFrequency( MinFrequency() );
       
   247 	iRadioSettings->SetMaxFrequency( MaxFrequency() );
       
   248 	iRadioSettings->SetFrequencyStepSize( FrequencyStepSize() );
       
   249 	iRadioSettings->SetDecimalCount( DecimalCount() );
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // CCentralRepositoryHandler::FrequencyStepSize
       
   254 // Returns frequency step size used for current region
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 TUint32 CCentralRepositoryHandler::FrequencyStepSize() const
       
   258     {
       
   259     return Region( iCurrentRegionIdx ).StepSize();
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------------------------
       
   263 // CCentralRepositoryHandler::MaxFrequency
       
   264 // Returns highest allowed frequency for current region
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 TUint32 CCentralRepositoryHandler::MaxFrequency() const
       
   268     {
       
   269     return Region( iCurrentRegionIdx ).MaxFrequency();
       
   270     }
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // CCentralRepositoryHandler::MinFrequency
       
   274 // Returns lowest allowed frequency for current region
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 TUint32 CCentralRepositoryHandler::MinFrequency() const
       
   278     {
       
   279     return Region( iCurrentRegionIdx ).MinFrequency();
       
   280     }
       
   281 // ---------------------------------------------------------------------------
       
   282 // CCentralRepositoryHandler::DecimalCount
       
   283 // return decimal count for current region Mhz information
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 TInt CCentralRepositoryHandler::DecimalCount() const
       
   287     {
       
   288     return Region( iCurrentRegionIdx ).DecimalCount();
       
   289     }
       
   290 
       
   291 
       
   292 // ---------------------------------------------------------------------------
       
   293 // CCentralRepositoryHandler::InitializeRegionsL
       
   294 // Initializes the regions based on resources.
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 void CCentralRepositoryHandler::InitializeRegionsL()
       
   298     {
       
   299 	iRegions.ResetAndDestroy();
       
   300 
       
   301 	TResourceReader reader;
       
   302 	
       
   303 	CCoeEnv* coeEnv = CCoeEnv::Static();
       
   304 	
       
   305 	if ( iRadioSettings->IsJapanRegionAllowed() )
       
   306 		{
       
   307 		coeEnv->CreateResourceReaderLC( reader, R_RADIO_REGIONS_ALL );	
       
   308 		}
       
   309 	else // Japan region is not allowed so read region resources without Japan
       
   310 		{
       
   311 		coeEnv->CreateResourceReaderLC( reader, R_RADIO_REGIONS_WITHOUT_JAPAN );					
       
   312 		}
       
   313 			
       
   314 	TInt regionCount = reader.ReadInt16();
       
   315 
       
   316 	for( TInt i = 0 ; i < regionCount; i++ )
       
   317 		{
       
   318 		TInt resId = reader.ReadInt32(); // The next resource ID to read.
       
   319         TResourceReader regionReader;
       
   320         coeEnv->CreateResourceReaderLC( regionReader, resId );
       
   321 		User::LeaveIfError( iRegions.Append( CFMRadioRegion::NewL( regionReader ) ) );
       
   322 		CleanupStack::PopAndDestroy();
       
   323 		}
       
   324 
       
   325 	CleanupStack::PopAndDestroy();
       
   326 
       
   327     UpdateCurrentRegionIdx( RegionId() );
       
   328         	
       
   329 	iRadioSettings->SetMinFrequency( MinFrequency() * KFMRadioFreqMultiplier );
       
   330 	iRadioSettings->SetMaxFrequency( MaxFrequency() * KFMRadioFreqMultiplier );
       
   331 	iRadioSettings->SetFrequencyStepSize( FrequencyStepSize() );
       
   332 	iRadioSettings->SetDecimalCount( DecimalCount() );
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // CCentralRepositoryHandler::RegionIndexForId
       
   337 // Converts Region ID to index
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 TInt CCentralRepositoryHandler::RegionIndexForId( TInt aRegionId ) const
       
   341     {
       
   342     TInt idx = KErrNotFound;
       
   343     for( TInt i = 0 ; i < CountRegions(); i++ )
       
   344         {
       
   345         if( Region( i ).Id() == static_cast<TFMRadioRegionSetting>( aRegionId ) )
       
   346             {
       
   347             idx = i;
       
   348             /*lint -save -e960 (Note -- Violates MISRA Required Rule 58, non-switch break used)*/
       
   349             break;
       
   350             /*lint -restore*/
       
   351             }
       
   352         }
       
   353     return idx;
       
   354     }
       
   355 
       
   356 // ---------------------------------------------------------------------------
       
   357 // CCentralRepositoryHandler::UpdateCurrentRegionIdx
       
   358 // Updates the current region
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 void CCentralRepositoryHandler::UpdateCurrentRegionIdx( TInt aRegionId )
       
   362     {
       
   363     iCurrentRegionIdx = RegionIndexForId( aRegionId );
       
   364     }
       
   365 
       
   366 // ---------------------------------------------------------------------------
       
   367 // CCentralRepositoryHandler::IsOfflineProfileActiveL
       
   368 // Checks if offine profile is current active profile
       
   369 // Returns: true/false 
       
   370 // ---------------------------------------------------------------------------
       
   371 //    
       
   372 TBool CCentralRepositoryHandler::IsOfflineProfileActiveL()
       
   373 	{
       
   374 	CRepository* repository = CRepository::NewLC( KCRUidProfileEngine );
       
   375 	
       
   376 	TInt profileId(0);
       
   377 	
       
   378 	// Get ID of current profile
       
   379 	User::LeaveIfError( repository->Get( KProEngActiveProfile, profileId ) );
       
   380 	
       
   381 	CleanupStack::PopAndDestroy( repository );
       
   382 	
       
   383 	// Check value to determine the active profile
       
   384 	// 5 = Offline profile
       
   385 	if ( profileId == 5 )
       
   386 		{
       
   387 		// Current profile is the offline profile
       
   388 		return ETrue;
       
   389 		}
       
   390 	
       
   391 	return EFalse;	
       
   392 	}
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 // CCentralRepositoryHandler::IsFrequencyValid
       
   396 // Checks if frequency is in the current region range
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 TBool CCentralRepositoryHandler::IsFrequencyValid( TUint32 aFrequency ) const
       
   400     {
       
   401     TBool ret( EFalse );
       
   402     if( !aFrequency )
       
   403         {
       
   404         aFrequency = iRadioSettings->Frequency();
       
   405         }
       
   406     if ( aFrequency >= MinFrequency() && aFrequency <= MaxFrequency() )
       
   407         {
       
   408         ret = ETrue;
       
   409         }
       
   410     return ret;
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------------------------
       
   414 // CCentralRepositoryHandler::IncreaseStartupCount
       
   415 // Increases startup time by one and sets the value to the repository
       
   416 // ---------------------------------------------------------------------------
       
   417 //    
       
   418 void CCentralRepositoryHandler::IncreaseStartupCount() const
       
   419 	{				
       
   420 	TInt startCount = iRadioSettings->StartupCount();
       
   421 	// Dont update if maximum tracking count is reached
       
   422 	if ( startCount < KRadioMaxStartupCountToTrack )
       
   423 		{
       
   424 		startCount++;
       
   425 		iRadioSettings->SetStartupCount( startCount ); 
       
   426 		iCentralRepository->Set( KRadioCRStartupCount, startCount );	
       
   427 		}	
       
   428 	}
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // CCentralRepositoryHandler::SaveHeadsetVolumeSetting
       
   432 // Save headset volume level to the repository
       
   433 // ---------------------------------------------------------------------------
       
   434 //	
       
   435 void CCentralRepositoryHandler::SaveHeadsetVolumeSetting() const
       
   436 	{
       
   437 	FTRACE( FPrint(_L("CCentralRepositoryHandler::SaveHeadsetVolumeSetting()") ) );
       
   438 	iCentralRepository->Set( KRadioHeadsetVolume, iRadioSettings->HeadsetVolume() );	
       
   439 	}
       
   440 	
       
   441 // ---------------------------------------------------------------------------
       
   442 // CCentralRepositoryHandler::SaveSpeakerVolumeSetting
       
   443 // Save speaker volume level to the repository
       
   444 // ---------------------------------------------------------------------------
       
   445 //	
       
   446 void CCentralRepositoryHandler::SaveSpeakerVolumeSetting() const
       
   447 	{
       
   448 	FTRACE( FPrint(_L("CCentralRepositoryHandler::SaveSpeakerVolumeSetting()") ) );
       
   449 	iCentralRepository->Set( KRadioSpeakerVolume, iRadioSettings->SpeakerVolume() );		
       
   450 	}
       
   451 	
       
   452 // ---------------------------------------------------------------------------
       
   453 // CCentralRepositoryHandler::MusicStoreNoteDisplayed
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 TBool CCentralRepositoryHandler::MusicStoreNoteDisplayed()
       
   457     {
       
   458     TBool result;
       
   459     TInt err = iCentralRepository->Get( KRadioMusicStoreNoteDisplayed, result );
       
   460     if ( err != KErrNone )
       
   461     	{
       
   462     	result = EFalse;
       
   463     	}    
       
   464     return result;
       
   465     }
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // CCentralRepositoryHandler::SetMusicStoreNoteDisplayed
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CCentralRepositoryHandler::SetMusicStoreNoteDisplayed()
       
   472     {
       
   473     iCentralRepository->Set( KRadioMusicStoreNoteDisplayed, 1 );
       
   474     }
       
   475 
       
   476 // ---------------------------------------------------------------------------
       
   477 // CCentralRepositoryHandler::GetRTPlusSupportLevel
       
   478 // ---------------------------------------------------------------------------
       
   479 //
       
   480 TInt CCentralRepositoryHandler::GetRTPlusSupportLevel()
       
   481     {
       
   482     //See fmradioengine.hrh:TFMRadioRTPlusLevel for correct values
       
   483     TInt result;
       
   484     TInt err = iCentralRepository->Get( KRadioRTPlusSupportLevel, result );
       
   485     if ( err != KErrNone )
       
   486     	{
       
   487     	result = KErrNotFound;
       
   488     	}
       
   489     
       
   490     return result;
       
   491     }
       
   492 
       
   493 // end of file
       
   494