examples/AppServices/AlarmServer/AlarmServerExample.cpp

00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 // System includes
00017 #include <e32base.h>
00018 #include <e32cons.h>
00019 #include <asshdalarm.h>
00020 #include <asclisession.h>
00021 
00022 // Local includes
00023 #include "AlarmServerExample.h"
00024 
00025 // Global definition
00026 static CConsoleBase* gConsole;
00027 // Strings for display
00028 _LIT(KConsoleStars,"\n*************************");
00029 
00030 CExampleAlarmServer* CExampleAlarmServer::NewL()
00031         {
00032         CExampleAlarmServer* self = new (ELeave) CExampleAlarmServer();
00033         CleanupStack::PushL(self);
00034         self->ConstructL();
00035         CleanupStack::Pop(self);
00036         return self;
00037         }
00038         
00039 void CExampleAlarmServer::ConstructL()
00040         {
00041         // connect to the alarm server
00042         User::LeaveIfError(iAlarmServerSession.Connect());
00043         }       
00044         
00045 CExampleAlarmServer::~CExampleAlarmServer()     
00046         {
00047         iAlarmServerSession.Close();
00048         }
00049 
00050 // Called each time the alarm server generates an event. Displays a description of the event.
00051 void CExampleAlarmServer::DisplayNotificationType(TAlarmChangeEvent aAlarmChangeEvent, TAlarmId /*aAlarmId*/)
00052         {
00053         switch (aAlarmChangeEvent)
00054                 {
00055                 case EAlarmChangeEventUndefined:
00056                         _LIT(KConsoleMsg, "\nAn undefined alarm event has occurred.");
00057                         gConsole->Printf(KConsoleMsg);
00058                         break;
00059                 case EAlarmChangeEventState:
00060                         _LIT(KConsoleMsg1, "\nAn alarm has changed state.");
00061                         gConsole->Printf(KConsoleMsg1);
00062                         break;
00063                 case EAlarmChangeEventStatus:
00064                         _LIT(KConsoleMsg2, "\nAn alarm has changed status.");
00065                         gConsole->Printf(KConsoleMsg2);
00066                         break;
00067                 case EAlarmChangeEventCharacteristics:
00068                         _LIT(KConsoleMsg3, "\nAn alarm has changed characteristics..");
00069                         gConsole->Printf(KConsoleMsg3);
00070                         break;
00071                 case EAlarmChangeEventAlarmDeletion:
00072                         _LIT(KConsoleMsg4, "\nAn alarm has been deleted from the queue.");
00073                         gConsole->Printf(KConsoleMsg4);
00074                         break;
00075                 case EAlarmChangeEventAlarmAddition:
00076                         _LIT(KConsoleMsg5, "\nAn alarm has been added to the queue.");
00077                         gConsole->Printf(KConsoleMsg5);
00078                         break;
00079                 case EAlarmChangeEventTimerExpired:
00080                         _LIT(KConsoleMsg6, "\nAn alarm has expired.");
00081                         gConsole->Printf(KConsoleMsg6);
00082                         break;
00083                 case EAlarmChangeEventSoundPlaying:
00084                         _LIT(KConsoleMsg7, "\nThe sound for an alarm has just started playing.");
00085                         gConsole->Printf(KConsoleMsg7);
00086                         break;
00087                 case EAlarmChangeEventSoundStopped:
00088                         _LIT(KConsoleMsg8, "\nThe sound for an alarm has just stopped playing.");
00089                         gConsole->Printf(KConsoleMsg8);
00090                         break;
00091                 case EAlarmChangeEventPlayIntervalsChanged:
00092                         _LIT(KConsoleMsg9, "\nThe sound intervals associated with sound timing have changed.");
00093                         gConsole->Printf(KConsoleMsg9);
00094                         break;
00095                 case EAlarmChangeEventGlobalSoundStateChanged:
00096                         _LIT(KConsoleMsg10, "\nThe global sound state (on/off) has changed.");
00097                         gConsole->Printf(KConsoleMsg10);
00098                         break;
00099                 case EAlarmChangeEventHeadQueueItemChanged:
00100                         _LIT(KConsoleMsg11, "\nThe next alarm at the head of the alarm queue has changed.");
00101                         gConsole->Printf(KConsoleMsg11);
00102                         break;
00103                 case EAlarmChangeEventSystemDateTimeChanged:
00104                         _LIT(KConsoleMsg12, "\nThe system date or time has changed, or the days defined as workdays have changed.");
00105                         gConsole->Printf(KConsoleMsg12);
00106                         break;
00107                 case EAlarmChangeEventAlarmUIVisible:
00108                         _LIT(KConsoleMsg13, "\nThe alarm alert server has been instructed to show the 'alarm expired' display.");
00109                         gConsole->Printf(KConsoleMsg13);
00110                         break;
00111                 case EAlarmChangeEventAlarmUIInvisible:
00112                         _LIT(KConsoleMsg14, "\nThe alarm alert server has been instructed to hide the 'alarm expired' display.");
00113                         gConsole->Printf(KConsoleMsg14);
00114                         break;
00115                 case EAlarmChangeEventSoundSilence:
00116                         _LIT(KConsoleMsg15, "\nAlarm sounds have been temporarily silenced, the current alarm has been paused or re-enabled.");
00117                         gConsole->Printf(KConsoleMsg15);
00118                         break;
00119                 case EAlarmChangeEventAlarmData:
00120                         _LIT(KConsoleMsg16, "\nThe data associated with an alarm has changed.");
00121                         gConsole->Printf(KConsoleMsg16);
00122                         break;
00123                 case EAlarmChangeEventRestoreStarted:
00124                         _LIT(KConsoleMsg17, "\nA restore from backup of the alarm server has started. Alarms cannot be added/deleted until this has finished.");
00125                         gConsole->Printf(KConsoleMsg17);
00126                         break;
00127                 case EAlarmChangeEventRestoreFailed:
00128                         _LIT(KConsoleMsg18, "\nA restore from backup of the alarm server has failed. Alarms can be added/deleted again.");
00129                         gConsole->Printf(KConsoleMsg18);
00130                         break;
00131                 case EAlarmChangeEventRestoreCompleted:
00132                         _LIT(KConsoleMsg19, "\nA restore from backup of the alarm server has completed. The alarm queue has changed.");
00133                         gConsole->Printf(KConsoleMsg19);
00134                         break;
00135                 case EAlarmChangeEventLast:
00136                         _LIT(KConsoleMsg20, "\nLast change event (anchor). This is always at the end of the list.");
00137                         gConsole->Printf(KConsoleMsg20);
00138                         break;
00139                 }
00140         }
00141         
00142 
00143 // Waits for the alarm to be cleared by the user.
00144 // In the meantime, prints out descriptions of all change events generated by server.
00145 void CExampleAlarmServer::WaitForAlarmToBeCleared()
00146         {
00147         TAlarmChangeEvent alarmChangeEvent;
00148         do
00149                 {
00150                 TRequestStatus status;  
00151                 TAlarmId alarmId;
00152                 iAlarmServerSession.NotifyChange(status, alarmId);
00153                 User::WaitForRequest(status);
00154                 alarmChangeEvent = static_cast<TAlarmChangeEvent>(status.Int());
00155                 DisplayNotificationType(alarmChangeEvent,alarmId);
00156                 }
00157         // EAlarmChangeEventAlarmUIInvisible is generated when the alarm is cleared
00158         while (alarmChangeEvent != EAlarmChangeEventAlarmUIInvisible);
00159         }
00160         
00161 // Sets a floating alarm and adds it to the alarmserver queue.
00162 // This is an alarm that is set using local time, for example a morning wake up call.
00163 void CExampleAlarmServer::SetFloatingAlarmL()
00164         {
00165         /*
00166         There are two types of alarms - floating and fixed.
00167         For a floating alarm, time is set in local time by using TASShdAlarm::NextDueTime().
00168         A floating alarm will be rescheduled when the UTC offset is changed.
00169 
00170         In case of fixed alarms, time is set in UTC by using TASShdAlarm::SetUtcNextDueTime().
00171         A fixed alarm will not be rescheduled when the UTC offset is changed.
00172         */
00173         _LIT(KConsoleMsg, "\nPress any key to add a floating alarm");
00174         gConsole->Printf(KConsoleMsg);
00175         _LIT(KConsoleMsg2,"\nWhen notification occurs, press Clear to continue");
00176         gConsole->Printf(KConsoleMsg2);
00177         gConsole->Getch();
00178         gConsole->Printf(KConsoleStars);
00179 
00180         TASShdAlarm alarm;
00181         alarm.Category() = KASCliCategoryClock;
00182         // Alarm occurs once, today. This is the default.
00183         alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatOnce;
00184         _LIT(KAlarmMessage,"\nThis is a floating alarm.");
00185         alarm.Message() = KAlarmMessage;
00186         // Get the local time and set it as the alarm's due time
00187         alarm.NextDueTime().HomeTime();
00188         // Set alarm to expire in the past - should go off immediately.
00189         // Note that if the alarm was of type EAlarmRepeatDefintionRepeatNext24Hours
00190         // it would expire tomorrow.
00191         alarm.NextDueTime() -= TTimeIntervalSeconds(2); 
00192         
00193         // WriteUserData capability is required to add an alarm
00194         User::LeaveIfError(iAlarmServerSession.AlarmAdd(alarm));
00195         // Wait for the user to clear the alarm
00196         WaitForAlarmToBeCleared();
00197         }
00198 
00199 // Displays the state of the alarm
00200 void CExampleAlarmServer::DisplayAlarmState(TAlarmState aState)
00201         {
00202         switch (aState)
00203                 {
00204                 case EAlarmStateInPreparation:
00205                         _LIT(KConsoleMsg1, "\nThe alarm state is not currently known by the alarm server..");
00206                         gConsole->Printf(KConsoleMsg1);
00207                         break;
00208                 case EAlarmStateQueued:
00209                         _LIT(KConsoleMsg2, "\nThe alarm is in the queue.");
00210                         gConsole->Printf(KConsoleMsg2);
00211                         break;
00212                 case EAlarmStateSnoozed:
00213                         _LIT(KConsoleMsg3, "\nThe alarm is snoozed. When the snooze period is over, the alarm expires again.");
00214                         gConsole->Printf(KConsoleMsg3);
00215                         break;
00216                 case EAlarmStateWaitingToNotify:
00217                         _LIT(KConsoleMsg4, "\nThe alarm is waiting to be notified.");
00218                         gConsole->Printf(KConsoleMsg4);
00219                         break;
00220                 case EAlarmStateNotifying:
00221                         _LIT(KConsoleMsg5, "\nThe alarm is currently notifying"); 
00222                         gConsole->Printf(KConsoleMsg5);
00223                         break;
00224                 case EAlarmStateNotified:
00225                         _LIT(KConsoleMsg6, "\nThe alarm has already notified and has been dismissed.");
00226                         gConsole->Printf(KConsoleMsg6);
00227                         break;
00228                 }
00229         }
00230         
00231 
00232 // Sets a fixed alarm and adds it to the alarmserver's queue.
00233 // This is an alarm that is set using UTC, so will not change if 
00234 // for instance the system time zone changes.
00235 // This function fetches and prints alarm details.
00236 void CExampleAlarmServer::AddFetchDeleteFixedAlarmL()
00237         {
00238         gConsole->Printf(KConsoleStars);
00239         _LIT(KConsoleMsg, "\nPress any key to add a fixed alarm");
00240         gConsole->Printf(KConsoleMsg);
00241         gConsole->Getch();
00242 
00243         TASShdAlarm alarm;
00244         TAlarmCategory alarmCategory = KASCliCategoryClock;
00245         alarm.Category()=alarmCategory;
00246         _LIT(KAlarmMessage, "This is a fixed alarm");
00247         alarm.Message() = KAlarmMessage;
00248         _LIT(KAlarmSound, "Chimes");
00249         alarm.SoundName() = KAlarmSound;
00250     // Alarm occurs once. 
00251     alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatOnce;
00252     
00253         TTime time;
00254         // Time is set in UTC for a fixed alarm 
00255         time.UniversalTime();
00256         // Set alarm to expire in 2 seconds. 
00257         time += TTimeIntervalSeconds(2);
00258         alarm.SetUtcNextDueTime(time);
00259         
00260         // WriteUserData capability is required to add an alarm
00261         User::LeaveIfError(iAlarmServerSession.AlarmAdd(alarm));
00262   
00263         // Fetch some details about the alarm from the server and print them
00264         const TAlarmId id = alarm.Id();
00265         TAlarmCategory category;
00266         User::LeaveIfError(iAlarmServerSession.GetAlarmCategory(id, category));
00267         User::LeaveIfError(iAlarmServerSession.GetAlarmDetails(id, alarm));
00268 
00269         TAlarmState state = alarm.State();
00270         DisplayAlarmState(state);
00271         
00272         TAlarmMessage message = alarm.Message();
00273         TBuf<256> messageBuf;
00274         messageBuf.Copy(message);
00275         _LIT(KConsoleMessage,"\nThe alarm's message is....:");
00276         gConsole->Printf(KConsoleMessage);
00277         gConsole->Printf(messageBuf);
00278         
00279         TAlarmSoundName soundName = alarm.SoundName();
00280         messageBuf.Copy(soundName);
00281         _LIT(KConsoleSoundName,"\nThe alarm's sound name is ....:");
00282         gConsole->Printf(KConsoleSoundName);
00283         gConsole->Printf(messageBuf);
00284                 
00285         _LIT(KDeleteMsg, "\n\nPress any key to finish");
00286         gConsole->Printf(KDeleteMsg);
00287         gConsole->Getch();
00288 
00289         User::LeaveIfError(iAlarmServerSession.AlarmDelete(id));
00290         }
00291         
00292 
00293 void CExampleAlarmServer::EgAlarmServerL()
00294         {
00295         SetFloatingAlarmL();
00296         AddFetchDeleteFixedAlarmL();
00297         }
00298 
00299 static void DoExampleL()
00300         {
00301         // Create the console to print the messages to.
00302         _LIT(KConsoleMessageDisplay, "Alarm Server Example");
00303         gConsole = Console::NewL(KConsoleMessageDisplay,TSize(KConsFullScreen,KConsFullScreen));
00304         CleanupStack::PushL(gConsole);
00305         gConsole->Printf(KConsoleMessageDisplay);
00306         gConsole->Printf(KConsoleStars);
00307 
00308         CExampleAlarmServer* egAlarmServer= CExampleAlarmServer::NewL();
00309         TRAPD(err, egAlarmServer->EgAlarmServerL());
00310         if (err)
00311                 {
00312                 _LIT(KFailed,"\n\nExample failed: leave code=%d");
00313                 gConsole->Printf(KFailed, err);
00314                 gConsole->Getch();
00315                 }
00316         delete egAlarmServer;   
00317         CleanupStack::PopAndDestroy(gConsole);
00318         }
00319 
00320 // Standard entry point function
00321 TInt E32Main()
00322         {
00323         __UHEAP_MARK;
00324         // Active scheduler required as this is a console app
00325         CActiveScheduler* scheduler=new CActiveScheduler;
00326         // If active scheduler has been created, install it.
00327         if (scheduler)
00328                 {
00329                 CActiveScheduler::Install(scheduler);
00330                 // Cleanup stack needed
00331                 CTrapCleanup* cleanup=CTrapCleanup::New();
00332                 if (cleanup)
00333                         {
00334                         TRAP_IGNORE(DoExampleL());
00335                         delete cleanup;
00336                         }
00337                 delete scheduler;
00338                 } 
00339         __UHEAP_MARKEND;
00340         return KErrNone;
00341         }

Generated by  doxygen 1.6.2