phoneapp/phoneuiutils/inc/ctelerecoverysystem.h
changeset 0 5f000ab63145
equal deleted inserted replaced
-1:000000000000 0:5f000ab63145
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Recovery system class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifndef CTeleRecoverySystem_H
       
    21 #define CTeleRecoverySystem_H
       
    22 
       
    23 //  INCLUDES
       
    24 #include <e32base.h>
       
    25 #include <e32property.h>
       
    26 
       
    27 // FORWARD DECLARATIONS
       
    28 class CPhoneTimer;
       
    29 class CTeleWaitingIdle;
       
    30 
       
    31 // DATA TYPES
       
    32 typedef TInt TPhoneRecoveryId;
       
    33 typedef TInt8 TPhoneRecoveryPriority;
       
    34 
       
    35 // Template class for late construct
       
    36 // User guide:
       
    37 // In header file:
       
    38 // class A
       
    39 //     {
       
    40 //     public:
       
    41 //         static A* NewL( CTeleRecoverySystem& aRecoverySystem );
       
    42 //         void ~A();
       
    43 //         void EarlyDestruct();
       
    44 //     private:
       
    45 //         A( CTeleRecoverySystem& aRecoverySystem );
       
    46 //         void ConstructL();
       
    47 //         void LateConstructL();
       
    48 //         static TInt LateConstructCallBackL( TAny* aAny );
       
    49 //     private:
       
    50 //         CTeleRecoverySystem& iRecoverySystem;
       
    51 //         TInt iLateConstructId;
       
    52 //     }
       
    53 // 
       
    54 // In cpp file
       
    55 // A* A::NewL( CPhoneRecoverySstem& aRecoverySystem )
       
    56 //     {
       
    57 //     A* self = new ( ELeave ) A( aRecoverySystem );
       
    58 //     CleanupStack::PushL( A );
       
    59 //     A->ConstructL();
       
    60 //     CleanupStack::Pop();
       
    61 //     }
       
    62 // void A::~A();
       
    63 //    {
       
    64 //    EarlyDestruct();
       
    65 //    iRecoverySystem.Remove( iLateConstructId );
       
    66 //    }
       
    67 // void A::EarlyDestruct()
       
    68 //    {
       
    69 //    //do all the construct
       
    70 //    }
       
    71 // A::A( CTeleRecoverySystem& aRecoverySystem ) 
       
    72 //    : iReoverySystem( aRecoverySystem )
       
    73 //    {
       
    74 //    }
       
    75 // void A::ConstructL()
       
    76 //    {
       
    77 //    // Add everything related to recovery system. 
       
    78 //    // If EStateWaiting is used, make sure in the callback 
       
    79 //    // function, the system may not be constructed yet.
       
    80 //    iRecoverySystem.AddL( TCallBack( LateConstructCallBackL, this ),
       
    81 //        CTeleRecoverySystem::EPhonePriorityStandard,
       
    82 //        CTeleRecoverySystem::EStateWaiting );
       
    83 //    }
       
    84 // void A::LateConstructL()
       
    85 //    {
       
    86 //    //Do the real construct
       
    87 //    //Note! CTeleRecoverySystem::AddL() shouldn't be called in this function.
       
    88 //    }
       
    89 // TInt A::LateConstructCallBackL( TAny* aAny )
       
    90 //    {
       
    91 //    A* self = reinterpret_cast< A* >( aAny );
       
    92 //    CleanupEarlyDestructPushL( *self );
       
    93 //    self->LateConstructL();
       
    94 //    CleanupStack::Pop();
       
    95 //    }
       
    96 // 
       
    97 // Call Resume() from time to time. For example in OfferKeyEventL, every 16th
       
    98 // key press calls Resume().
       
    99 //
       
   100 // TKeyResponse A::OfferKeyEventL(...)
       
   101 //    {
       
   102 //    iKeyCount++;
       
   103 //    iKeyCount %= 16;
       
   104 //    if ( !iKeyCount )
       
   105 //      {
       
   106 //      iRecoverySystem.Resume();
       
   107 //      iKeyCount = 0;
       
   108 //      }
       
   109 //    return EKeyWasNotConsumed;
       
   110 //    }
       
   111 //      
       
   112 
       
   113 /**
       
   114 * Template class to handle late construction.
       
   115 */
       
   116 template <class T>
       
   117 class CleanupEarlyDestruct
       
   118     {
       
   119     public:
       
   120         
       
   121         /**
       
   122         * Pushes an item to cleanup stack. 
       
   123         * If leave occurs, then EarlyDestruct 
       
   124         * method will be called.
       
   125         *
       
   126         * @param aRef instance of which EarlyDestruct
       
   127         *             needs to be called upon leave.
       
   128         */
       
   129         inline static void PushL( T& aRef );
       
   130         
       
   131     private:
       
   132         
       
   133         /**
       
   134         * Cleanup action.
       
   135         */
       
   136         static void Cleanup( TAny* aPtr );
       
   137         
       
   138     };
       
   139 
       
   140 /**
       
   141 * Helper function.
       
   142 */ 
       
   143 template <class T>
       
   144 inline void CleanupEarlyDestructPushL( T& aRef );
       
   145 
       
   146 // CLASS DECLARATION
       
   147 
       
   148 /**
       
   149 *  If an operation fails, user classes may be leaved to incorrect states. 
       
   150 *  By using recovery system, failed operation will be tried again some time 
       
   151 *  later. This may enhance the robustness of user class.
       
   152 */
       
   153 NONSHARABLE_CLASS( CTeleRecoverySystem ): public CActive
       
   154     {   
       
   155     public:
       
   156     // Enumerates priorities:
       
   157     // EPriorityDontCare - don't care
       
   158     // EPriorityLow - low
       
   159     // EPriorityStandard - standard
       
   160     // EPriorityHigh - high
       
   161     // EPriorityCritical - very critical.
       
   162     enum TPhonePriority
       
   163         {
       
   164         EPhonePriorityDontCare = -10,
       
   165         EPhonePriorityLow = -5,
       
   166         EPhonePriorityStandard = 0,
       
   167         EPhonePriorityHigh = 5,
       
   168         EPhonePriorityCritical = 100
       
   169         };
       
   170         
       
   171     // Enumerates states for a recovery item:
       
   172     // EStateIdle - solved, everything is fine
       
   173     // EStateWaiting - not solved, waiting
       
   174     // EStatePending - semipermanently not solved
       
   175     // EStateStarting - recovery for all steps started
       
   176     // EStateLockedPending - locked to pending state. it's not allowed 
       
   177     //                       to be moved back to waiting
       
   178     // EStatePrecond - precondition: light idle-phone is reached, 
       
   179     //                 but before PIN code query)must be satisfied 
       
   180     //                 before this can be run
       
   181     // EStatePrecondSim - precondition: SIM card is ready. This happens 
       
   182     //                 after PIN code is accepted.
       
   183     enum TRecoveryState
       
   184         {
       
   185         EPhoneStateIdle, 
       
   186         EPhoneStateWaiting,  
       
   187         EPhoneStatePending,  
       
   188         EPhoneStateStarting, 
       
   189         EPhoneStateLockedPending, 
       
   190         EPhoneStatePrecond, 
       
   191         EPhoneStatePrecondSim  
       
   192         };
       
   193 
       
   194     // Defines null id.
       
   195     enum 
       
   196         {
       
   197         KNullId = -1
       
   198         }; 
       
   199     
       
   200     public:  // Constructors and destructor
       
   201         
       
   202         /**
       
   203         * Two-phased constructor.
       
   204         */
       
   205         static CTeleRecoverySystem* NewL();
       
   206         
       
   207         /**
       
   208         * Destructor.
       
   209         */
       
   210         virtual ~CTeleRecoverySystem();
       
   211 
       
   212     public: // New functions
       
   213                
       
   214         /**
       
   215         * Adds a new item with given priority & state. Returns Null id if it
       
   216         * failed. Same item must not be added twice. Callback's return value
       
   217         * indicates how many steps there are to be done, 0 - no more steps. 
       
   218         * Error code or leave - failure.
       
   219         * Priority indicates importantance of the action.
       
   220         *
       
   221         * @param aCallBack callback.
       
   222         * @param aPriority priority.
       
   223         * @param aState start state.
       
   224         * @return identifier.
       
   225         */
       
   226         virtual TPhoneRecoveryId Add( TCallBack aCallBack, 
       
   227             TPhoneRecoveryPriority aPriority, 
       
   228             TRecoveryState aState );
       
   229 
       
   230         /**
       
   231         * Add a new item to the recovery system.
       
   232         * The function may leave if OOM. 
       
   233         *
       
   234         * @param aCallBack callback.
       
   235         * @param aPriority priority.
       
   236         * @param aState start state.
       
   237         * @return identifier.
       
   238         */
       
   239         virtual TPhoneRecoveryId AddL( TCallBack aCallBack,
       
   240             TPhoneRecoveryPriority aPriority,
       
   241             TRecoveryState aState );
       
   242     
       
   243         /**
       
   244         * Removes item.
       
   245         *
       
   246         * @param aId identifier of the item to be removed.
       
   247         */
       
   248         virtual void Remove( TPhoneRecoveryId aId );
       
   249     
       
   250         /**
       
   251         * Sets state. Note: The timer will not be started if the state is
       
   252         * set to be waiting.
       
   253         *
       
   254         * @param aId identifier.
       
   255         * @param aState new state.
       
   256         */
       
   257         virtual void SetState( TPhoneRecoveryId aId, TRecoveryState aState );
       
   258     
       
   259         /**
       
   260         * Resets pending recovery actions back to waiting.
       
   261         */
       
   262         virtual void ResetPending();
       
   263     
       
   264         /**
       
   265         * Recovers one step or all steps synchronously. Even if it's solved
       
   266         * it should call the method.
       
   267         *
       
   268         * @param aId identifier.
       
   269         * @param aPriority priority.
       
   270         * @param aAllSteps ETrue if all steps need to be run.
       
   271         * @return error code.
       
   272         */
       
   273         virtual TInt RecoverNow( TPhoneRecoveryId aId, 
       
   274             TPhoneRecoveryPriority aPriority, 
       
   275             TBool aAllSteps = EFalse );
       
   276 
       
   277         /**
       
   278         * Informs that precondition has been satisfied.
       
   279         */
       
   280         virtual void EnablePrecondition();
       
   281         
       
   282         /**
       
   283         * If recovery has been paused due to internal logic, requests
       
   284         * recovery to continue. 
       
   285         * User class should call this from time to time to improve robustness.
       
   286         * For example, user class could call this based on key events it receives.
       
   287         */
       
   288         virtual void Resume();
       
   289     
       
   290     public:
       
   291     
       
   292         /**
       
   293         * Recovers all steps synchronously.
       
   294         */
       
   295         void RecoverAllNow();
       
   296         
       
   297     public: // Functions from base classes
       
   298 
       
   299         /**
       
   300         * @see CActive::RunL.
       
   301         */
       
   302         void RunL();
       
   303 
       
   304         /**
       
   305         * @see CActive::DoCancel.
       
   306         */
       
   307         void DoCancel();
       
   308         
       
   309     private: // private structures
       
   310         
       
   311         // Defines structure containing information of a recovery item.
       
   312         struct TRecoveryItem
       
   313             {
       
   314             // Identifier
       
   315             TPhoneRecoveryId iId;
       
   316             // Callback
       
   317             TCallBack iCallBack;
       
   318             // Priority
       
   319             TPhoneRecoveryPriority iPriority;
       
   320             // State
       
   321             TRecoveryState iState;
       
   322             };
       
   323 
       
   324     private:
       
   325 
       
   326         /**
       
   327         * C++ default constructor.
       
   328         */
       
   329         CTeleRecoverySystem();
       
   330 
       
   331         /**
       
   332         * By default Symbian 2nd phase constructor is private.
       
   333         */
       
   334         void ConstructL();
       
   335         
       
   336         /**
       
   337         * Enable precondition Sim OK.
       
   338         */
       
   339         void EnablePrecondSimOk();
       
   340 
       
   341         /**
       
   342         * Do item call back
       
   343         */
       
   344         TInt DoItemCallBack( TRecoveryItem& aItem );
       
   345 
       
   346         /**
       
   347         * Call back funciton for CPhoneTimer.
       
   348         */
       
   349         static TInt HandleTimer( TAny* aAny );
       
   350 
       
   351         /**
       
   352         * Really handle function to handle timer.
       
   353         */
       
   354         TInt DoHandleTimer();
       
   355 
       
   356         /**
       
   357         * Call back funciton for AllStepsTimer.
       
   358         */
       
   359         static TInt HandleAllStepsTimer( TAny* aAny );
       
   360 
       
   361         /**
       
   362         * Handle AllStesTimer timeout.
       
   363         */
       
   364         TInt DoHandleAllStepsTimer();
       
   365 
       
   366         /**
       
   367         * Checks and updates state if precondition has been satisfied.
       
   368         */
       
   369         void CheckStateForPrecondition( TRecoveryState& aState );
       
   370 
       
   371         /**
       
   372         * It starts a timer if required when event state changes.
       
   373         *
       
   374         * @param aNewState new state of the event.
       
   375         */
       
   376         void StartTimerIfRequired( TRecoveryState aNewState );
       
   377 
       
   378         /**
       
   379         * It start the timer, but iTickCounter will not be cleared.
       
   380         */
       
   381         void StartTimerIfIdle();
       
   382 
       
   383         /**
       
   384         * Start timer.
       
   385         */
       
   386         void StartTimer();
       
   387 
       
   388         /**
       
   389         * Starts all steps timer.
       
   390         */
       
   391         void StartAllStepsTimer();
       
   392 
       
   393         /**
       
   394         * Starts timer for the next recovery attempt.
       
   395         */
       
   396         void StartNext( TInt aFound );
       
   397 
       
   398         /**
       
   399         * Determines which events will be run.
       
   400         */
       
   401         TInt DetermineRun() const;
       
   402 
       
   403         /**
       
   404         * Calculates next tick.
       
   405         */
       
   406         inline TInt NextTick( TInt aTicks ) const;
       
   407 
       
   408         /**
       
   409         * Finds by recovery id.
       
   410         *
       
   411         * @param aId id.
       
   412         * @return recovery id.
       
   413         */
       
   414         TInt Find( TPhoneRecoveryId aId ) const;                    
       
   415         
       
   416         /**
       
   417         * Issues new active object request.
       
   418         */
       
   419         void IssueRequest();
       
   420         
       
   421         /**
       
   422         * Is SIM now ok?
       
   423         */
       
   424         TBool IsSIMOk();
       
   425         
       
   426         /**
       
   427         * Check for SIM availability. 
       
   428         */
       
   429         TInt CheckSIMAvailable();
       
   430         
       
   431         /**
       
   432         * Checks if Idle is started.
       
   433         */
       
   434         TBool IsIdle();
       
   435                         
       
   436     private:    // Data
       
   437     
       
   438         // Currently handled request.
       
   439         enum TPhoneRequestStatus
       
   440             {
       
   441             EPhoneNone,
       
   442             EPhoneSIMPresent,
       
   443             EPhoneSIMStatus
       
   444             };
       
   445 
       
   446         // Timer for recovery
       
   447         CPhoneTimer* iTimer;
       
   448 
       
   449         // Timer for recovery all steps
       
   450         CPhoneTimer* iAllStepsTimer;
       
   451         
       
   452         // Timer for idle.
       
   453         CTeleWaitingIdle* iWaitingIdle;
       
   454 
       
   455         // Recoverty time array
       
   456         CArrayFixFlat< TRecoveryItem >* iRecoveryItems;
       
   457 
       
   458         // Id counter for TPhoneRecoveryId
       
   459         TInt iIdCounter;
       
   460 
       
   461         // Count the ticks
       
   462         TInt iTickCounter;
       
   463         
       
   464         // Sim present status. 
       
   465         TInt iSimAvailable;
       
   466 
       
   467 #ifdef _DEBUG
       
   468         // Debug: to prevent modification of recovery system in 
       
   469         // recovery callback.
       
   470         TBool iIsCallBack;
       
   471 #endif // _DEBUG
       
   472 
       
   473         // ETrue if precondition ok
       
   474         TBool iPrecondOk;
       
   475 
       
   476         // ETrue if precondition SIM ok
       
   477         TBool iPrecondSimOk;
       
   478         
       
   479         // Indicates what request is currently handled.
       
   480         TPhoneRequestStatus iRequest;
       
   481      
       
   482         //  Owned Publish and Subscribe property for SIM status.
       
   483         RProperty iSimStatusProperty;
       
   484         
       
   485         // Owned Publish and Subscribe property for SIM present.
       
   486         RProperty iSimPresentProperty;
       
   487         
       
   488         // Owned Publish and Subscribe property for Idle active.
       
   489         RProperty iIdleActive;
       
   490     };
       
   491 
       
   492 // Inline methods.
       
   493 #include "ctelerecoverysystem.inl"
       
   494 
       
   495 #endif      // CTELERECOVERYSYSTEM_H
       
   496 
       
   497 // End of File