examples/AppServices/AlarmServer/AlarmServerExample.cpp

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

Generated by  doxygen 1.6.2