package com.themorphicgroup.callafriend.service{ /** * RibbitServiceManager Class * * Manager Class to Ribbit API - singleton * * @author Dennis Biron - The Morphic Group * */ import flash.events.Event; import flash.events.EventDispatcher; import com.ribbit.api.events.CallLogEvent; import com.ribbit.api.RibbitServices; import com.ribbit.api.interfaces.ICallObject; import com.ribbit.api.objects.LoginParam; import com.ribbit.api.objects.CallState; import com.ribbit.api.events.AuthenticationEvent; import com.ribbit.api.events.CallEvent; import com.ribbit.api.interfaces.ICallLogList; import com.ribbit.api.interfaces.ICallLog; import com.ribbit.api.interfaces.ICallLogDestination; import com.themorphicgroup.callafriend.model.ApplicationModelLocator; public class RibbitServiceManager extends EventDispatcher{ /** Public variable **/ public static var LOGIN:String = "ribbitLogin"; public static var LOGOUT:String = "ribbitLogout"; public static var LOGGING_IN:String = "loggingIn"; public static var LOGGED_IN:String = "loggedIn"; public static var LOGGED_OUT:String = "loggedOut"; public static var CALL_RINGING:String = "callRinging"; public static var CALL_ANSWERED:String = "callAnswer"; public static var CALL_LOG_CREATED:String = "callLogCreated"; public static var CALL_DISCONNECTED:String = "callDisconnected"; /** Private variables **/ private var _ribbitService:RibbitServices; private var _ribbitCall:ICallObject; private var _userName:String; private var _userPassword:String; private var _developerID:String; private var _appID:String; private var _applicationModelLocator:ApplicationModelLocator = ApplicationModelLocator.getInstance(); private static var _instance:RibbitServiceManager; /** Constructor **/ public function RibbitServiceManager(singletonEnforcer:Enforcer){} /** * getInstance - get instance of singleton * * @param none * @return RibbitServiceManager * */ public static function getInstance():RibbitServiceManager{ if(_instance == null){ _instance = new RibbitServiceManager(new Enforcer()); } return RibbitServiceManager._instance; } /** * setService - gets Ribbit API Service, sets variables for IDs * * @param none * @return void * */ public function setService():void{ //get an instance of RibbitServices _ribbitService = new RibbitServices(); //provide from applicationModelLocator or test with local variables _userName = "[Username]"; _userPassword = "[Password]"; _developerID = "[your developer ID]"; _appID = "[your application ID]"; } /** * loginToRibbit - sets IDs, adds listeners, calls login method from service * * @param none * @return void * */ public function loginToRibbit():void { //add listeners api_addListeners(); //provide from applicationModelLocator or test with local variables //var usr:String = _applicationModelLocator.userName; //var pws:String = _applicationModelLocator.password; //do the login and pass all arguments _ribbitService.login(usr, pws, _developerID, _appID, null); } /** * logoutToRibbit - removes listeners, calls logout method from service * * @param none * @return void * */ public function logoutToRibbit():void{ //remove listeners api_removeListeners(); try{ _ribbitService.logout(); }catch (e:Error){ trace(e.message); } } /** * dial - calls dial method from service * * @param String * @return void * */ public function dial(phoneNumber:String):void{ _ribbitService.callManager.dial(phoneNumber); } /** * hangUp - calls callManager.hangup method from service * * @param String * @return void * */ public function hangUp():void{ //note: _ribbitCall is callobject reference created when connection is made. if(_ribbitCall != null){ _ribbitService.callManager.hangup(_ribbitCall); } } /** * api_addListeners - Add API event listeners. * * @param String * @return void * */ private function api_addListeners():void { if (_ribbitService != null){ _ribbitService.addEventListener(AuthenticationEvent.LOGGING_IN, onAuthenticationEvent_LoggingIn); _ribbitService.addEventListener(AuthenticationEvent.LOGGED_IN, onAuthenticationEvent_LoggedIn); _ribbitService.addEventListener(AuthenticationEvent.LOGGED_OUT, onAuthenticationEvent_LoggedOut); _ribbitService.addEventListener(AuthenticationEvent.ERROR, onAuthenticationEvent_Error); _ribbitService.callManager.addEventListener(CallEvent.CONNECTED, onCallEvent_Connected); _ribbitService.callManager.addEventListener(CallEvent.DISCONNECTED, onCallEvent_Disconnected); _ribbitService.callManager.addEventListener(CallEvent.CALL_STATE_CHANGE, onCallEvent_CallStateChange); } } /** * api_removeListeners - removes API event listeners. * * @param String * @return void * */ private function api_removeListeners():void{ if (_ribbitService != null){ _ribbitService.removeEventListener(AuthenticationEvent.LOGGING_IN, onAuthenticationEvent_LoggingIn); _ribbitService.removeEventListener(AuthenticationEvent.LOGGED_IN, onAuthenticationEvent_LoggedIn); _ribbitService.removeEventListener(AuthenticationEvent.LOGGED_OUT, onAuthenticationEvent_LoggedOut); _ribbitService.removeEventListener(AuthenticationEvent.ERROR, onAuthenticationEvent_Error); _ribbitService.callManager.removeEventListener(CallEvent.CONNECTED, onCallEvent_Connected); _ribbitService.callManager.removeEventListener(CallEvent.DISCONNECTED, onCallEvent_Disconnected); _ribbitService.callManager.removeEventListener(CallEvent.CALL_STATE_CHANGE, onCallEvent_CallStateChange); } } /** * onAuthenticationEvent_LoggingIn - handles AuthenticationEvent * * @param AuthenticationEvent * @return void * */ private function onAuthenticationEvent_LoggingIn(event:AuthenticationEvent):void { dispatchEvent(new Event(LOGGING_IN)); } /** * onAuthenticationEvent_LoggedIn - handles AuthenticationEvent * * @param AuthenticationEvent * @return void * */ private function onAuthenticationEvent_LoggedIn(event:AuthenticationEvent):void{ _ribbitService.callLogManager.getCallLogs(); _ribbitService.callLogManager.addEventListener(CallLogEvent.SUMMARY, callLogsLoaded); _ribbitService.callLogManager.addEventListener(CallLogEvent.FOUND, callLogsLoaded); _ribbitService.callLogManager.addEventListener(CallLogEvent.NEW_CALL_LOG, onNewCallLog); dispatchEvent(new Event(LOGGED_IN)); } /** * onAuthenticationEvent_LoggedOut - handles AuthenticationEvent * * @param AuthenticationEvent * @return void * */ private function onAuthenticationEvent_LoggedOut(event:AuthenticationEvent):void { _ribbitService.callLogManager.removeEventListener(CallLogEvent.SUMMARY, callLogsLoaded); _ribbitService.callLogManager.removeEventListener(CallLogEvent.FOUND, callLogsLoaded); _ribbitService.callLogManager.removeEventListener(CallLogEvent.NEW_CALL_LOG, onNewCallLog); dispatchEvent(new Event(LOGGED_OUT)); } /** * onAuthenticationEvent_Error - handles AuthenticationEvent * * @param AuthenticationEvent * @return void * */ private function onAuthenticationEvent_Error(event:AuthenticationEvent):void { trace("RibbitServiceManager - error " + AuthenticationEvent(event).target.toString()); } /** * onCallEvent_Connected - handles CallEvent * * @param CallEvent * @return void * */ private function onCallEvent_Connected(event:CallEvent):void{ //trace("RibbitServiceManager - call connected"); //Note: callObject referenced - used for hangup method _ribbitCall = event.getValue("callObject"); if (event.newState == CallState.RINGING){ dispatchEvent(new Event(CALL_RINGING)); } } /** * onCallEvent_CallStateChange - handles CallEvent * * @param CallEvent * @return void * */ private function onCallEvent_CallStateChange(event:CallEvent):void { //trace("RibbitServiceManager - call state change"); if(event.newState == CallState.ANSWERED){ dispatchEvent(new Event(CALL_ANSWERED)); } } /** * onCallEvent_Disconnected - handles CallEvent * * @param CallEvent * @return void * */ private function onCallEvent_Disconnected(event:CallEvent):void{ trace("RibbitServiceManager - call disconnected"); dispatchEvent(new Event(CALL_DISCONNECTED)); } /** * callLogsLoaded - handles CallLogEvent * * @param CallLogEvent * @return void * */ public function callLogsLoaded(event:CallLogEvent):void{ var messageList:Array = new Array(); if (event.callLogList != null) { messageList = new Array(event.callLogList.toArray()); //Return array of CallLog Objects to application model or locator //_applicationModelLocator.callLogList = messageList; dispatchEvent(new Event(CALL_LOG_CREATED)); } } /** * onNewCallLog - handles CallLogEvent * * @param CallLogEvent * @return void * */ public function onNewCallLog(event:CallLogEvent):void{ var callLog:ICallLog = event.getValue("callLog") as ICallLog; if (callLog != null){ //Note: returned value endtime.month is incorrect; var formatedDate:String = callLog.endTime.getMonth() + "/" + callLog.endTime.getDate() + "/" + callLog.endTime.getFullYear()+ " " + callLog.endTime.toTimeString(); /* Note: Returns consistent CallLog by creating gentric Object - You cannot create CallLog or CallLogDestination object directly callLog.id , callLog.name, callLog["called"] (Destination object returned as null - callMeNumber) , formatedDate */ var newCallLog:Object = new Object(); newCallLog.id = callLog.id; newCallLog.name = callLog.name; newCallLog.formatedDate = formatedDate; var newCallLogDestination:Object = new Object(); newCallLogDestination.callMeNumber = callLog["called"]; newCallLog.destination = newCallLogDestination; //Return new item to array from application model or locator //_applicationModelLocator.callLogList[0].push(newCallLog); dispatchEvent(new Event(CALL_LOG_CREATED)); } } } } class Enforcer {}