public void QueueMessage(ContentInfo contentInfo, bool isNew, Context c)
        {
            context = c;
            if (isNew)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    dbm.InsertOrUpdateContentInfoItems(new List<ContentInfo>() { contentInfo });
                });
            }//end if

            this.ContentInfoQ.Enqueue(contentInfo);

            if (!this.IsSendingMessage)
            {

                this.IsSendingMessage = true;

                ContentInfo toSend = this.ContentInfoQ.Dequeue();
                toSend.Message.MessageSent = DateTime.Now;

                try
                {
                    LOLMessageClient service = new LOLMessageClient(LOLConstants.DefaultHttpBinding, LOLConstants.LOLMessageEndpoint);
                    service.MessageCreateCompleted += Service_MessageCreateCompleted;
                    service.MessageCreateAsync(contentInfo.Message,
                                           contentInfo.Message.MessageSteps,
                                           contentInfo.Recipients, AndroidData.CurrentUser.AccountID,
                                           new Guid(AndroidData.ServiceAuthToken), toSend);
                } catch (Exception e)
                {
                    #if DEBUG
                    System.Diagnostics.Debug.WriteLine("Exception thrown - {0}--{1}", e.Message, e.StackTrace);
                    #endif
                }

            }//end if else
        }
        public void DeleteContentInfoIfExists(ContentInfo contentInfo)
        {
            lock (this.dbLock)
            {

                using (SQLiteConnection sqlCon = new SQLiteConnection(this.DBPath))
                {

                    sqlCon.Execute(WZConstants.DBClauseSyncOff);

                    sqlCon.BeginTransaction();

                    try
                    {

                        ContentInfo contentInfoToDelete =
                            sqlCon.Query<ContentInfo>("SELECT * FROM ContentInfo WHERE ID=?", contentInfo.ID)
                                .FirstOrDefault();

                        if (null != contentInfoToDelete)
                        {

                            // Take care of the message first.
                            MessageDB messageDB = sqlCon.Query<MessageDB>("SELECT * FROM MessageDB WHERE ID=?", contentInfo.MessageDBID)
                                .FirstOrDefault();
                            if (messageDB.MessageID == default(Guid))
                            {
                                // Delete it, because the message has already been inserted from message create.
                                sqlCon.Execute("DELETE FROM MessageDB WHERE ID=?", messageDB.ID);
                            }//end if

                            sqlCon.Execute("DELETE FROM VoiceCache WHERE ContentInfoID=?", contentInfo.ID);
                            sqlCon.Execute("DELETE FROM PollingStepDBCache WHERE ContentInfoID=?", contentInfo.ID);
                            sqlCon.Execute("DELETE FROM MessageRecipientDBCache WHERE MessageDBID=?", messageDB.ID);
                            sqlCon.Execute("DELETE FROM MessageStepDBCache WHERE MessageDBID=?", messageDB.ID);
                            sqlCon.Execute("DELETE FROM ContentState WHERE ContentInfoID=?", contentInfo.ID);

                            sqlCon.Execute("DELETE FROM ContentInfo WHERE ID=?", contentInfo.ID);

                        }//end if

                        sqlCon.Commit();

                    } catch (Exception ex)
                    {

            #if(DEBUG)
                        Console.WriteLine("Error deleting content info! {0}--{1}", ex.Message, ex.StackTrace);
            #endif
                        sqlCon.Rollback();

                    }//end try catch

                }//end using sqlCon

            }//end lock
        }
 public MessagePlaybackController(ContentInfo contentInfo, Dictionary<int, ContentPackItem> contentPackItem, Context c)
 {
     MessagePlaybackUtil.voiceRecordingData = contentInfo.VoiceRecordings;
     MessagePlaybackUtil.messageSteps = MessageDB.ConvertFromMessage (contentInfo.Message).MessageStepDBList;
     MessagePlaybackUtil.contentPackItems = contentPackItem;
     MessagePlaybackUtil.pollSteps = contentInfo.PollingSteps;
     setupTypes (c);
     Intent i = new Intent (c, typeof(MessagePlayback));
     c.StartActivity (i);
 }
        private void SendMessage()
        {
            List<Guid> recipientIDs = new List<Guid>();
            foreach (ContactDB contact in Contacts.SelectContactsUtil.selectedContacts)
                recipientIDs.Add(contact.ContactAccountID);

            ComposeMessageMainUtil.message.MessageSteps = ComposeMessageMainUtil.msgSteps;
            List<int> t = new List<int>();
            for (int l = 0; l < 6; ++l)
                if (ComposeMessageMainUtil.contentPackID [l] != -1)
                    t.Add(ComposeMessageMainUtil.contentPackID [l]);
            for (int m = 0; m < t.Count; ++m)
            {
                ComposeMessageMainUtil.message.MessageSteps [m].ContentPackItemID = t [m];
            }

            ContentInfo contentInfo = new ContentInfo(ComposeMessageMainUtil.message, recipientIDs);
            if (ComposeMessageMainUtil.pollSteps != null)
            {
                foreach (PollingStep pollStep in ComposeMessageMainUtil.pollSteps)
                {
                    if (pollStep.PollingAnswer1 != null || pollStep.PollingData1 != null)
                    {
                        contentInfo.PollingSteps.Add(pollStep.StepNumber, pollStep);
                    }
                }
            }

            for (int m = 0; m < 6; ++m)
                ComposeMessageMainUtil.contentPackID [m] = ComposeMessageMainUtil.currentPosition [m] = 0;

            mmg.QueueMessage(contentInfo, true, context);
        }
        private void SendVoiceData(ContentInfo contentInfo, LOLMessageClient service)
        {
            #if(DEBUG)
            System.Diagnostics.Debug.WriteLine("Send voice data!");
            #endif

            int stepNumber = contentInfo.ContentState.VoiceIDQ.Peek();
            if (contentInfo.VoiceRecordings.Count != 0)
            {
                SaveVoiceRecordingFile(contentInfo.VoiceRecordings [stepNumber], contentInfo.Message.MessageID, stepNumber);

                // Upload voice recordings
                service.MessageStepDataSaveCompleted += Service_MessageStepDataSaveCompleted;
                service.MessageStepDataSaveAsync(contentInfo.Message.MessageID, stepNumber, contentInfo.VoiceRecordings [stepNumber],
                                                  AndroidData.CurrentUser.AccountID, new Guid(AndroidData.ServiceAuthToken), contentInfo);
            } else
                this.SendNextMessage(service);
        }
 private void SendNextStep(ContentInfo contentInfo, LOLMessageClient service)
 {
     #if(DEBUG)
     System.Diagnostics.Debug.WriteLine("Send next step!");
     #endif
     if (contentInfo.ContentState != null)
     {
         if (contentInfo.ContentState.HasVoiceRecordings)
         {
             this.SendVoiceData(contentInfo, service);
         } else
         if (contentInfo.ContentState.HasPollingSteps)
         {
             this.SendPollingSteps(contentInfo, service);
         } else
         {
             service.MessageConfirmSendCompleted += Service_MessageConfirmSendCompleted;
             service.MessageConfirmSendAsync(contentInfo.Message.MessageID, AndroidData.CurrentUser.AccountID, new Guid(AndroidData.ServiceAuthToken), contentInfo);
         }
     }
 }
        private void SendPollingSteps(ContentInfo contentInfo, LOLMessageClient service)
        {
            #if(DEBUG)
            System.Diagnostics.Debug.WriteLine("Send polling steps!");
            #endif

            PollingStep pollStepToSend = contentInfo.PollingSteps [contentInfo.ContentState.PollingIDQ.Peek()];
            pollStepToSend.MessageID = contentInfo.Message.MessageID;
            contentInfo.PollingSteps [contentInfo.ContentState.PollingIDQ.Peek()] = pollStepToSend;

            if (pollStepToSend is PollingStepDB)
            {
                PollingStepDB converted = (PollingStepDB)pollStepToSend;
                pollStepToSend = PollingStepDB.ConvertFromPollingStepDB(converted);
            }//end if

            // Upload step data
            service.PollingStepSaveCompleted += Service_PollingStepSaveCompleted;
            service.PollingStepSaveAsync(pollStepToSend, AndroidData.CurrentUser.AccountID, new Guid(AndroidData.ServiceAuthToken), contentInfo);
        }
        private void SendAnimationSteps(ContentInfo contentInfo, LOLMessageClient service)
        {
            #if(DEBUG)
            Console.WriteLine("Send animation steps!");
            #endif

            AnimationInfo animationInfoToSend = contentInfo.AnimationItems [contentInfo.ContentState.AnimationIDQ.Peek()];
            animationInfoToSend.MessageID = contentInfo.Message.MessageID;
            contentInfo.AnimationItems [contentInfo.ContentState.AnimationIDQ.Peek()] = animationInfoToSend;

            foreach (FrameInfo eachFrameInfo in animationInfoToSend.FrameItems.Values)
            {
                Console.WriteLine("In frame: {0}", eachFrameInfo.ID);
                foreach (LayerInfo eachLayerItem in eachFrameInfo.Layers.Values)
                {
                    Console.WriteLine("\tIn layer: {0}", eachLayerItem.ID);
                    foreach (TransitionInfo eachTrItem in eachLayerItem.Transitions.Values)
                    {
                        foreach (TransitionEffectSettings efSetting in eachTrItem.Settings.Values)
                        {
                            Console.WriteLine("\t\tSetting: {0}. Duration: {1}.", efSetting.EffectType, efSetting.Duration);
                        }//end foreach
                    }//end foreach
                }//end foreach
            }//end foreach
            AnimationStep animStep = animationInfoToSend.CreateAnimationStep();
            foreach (AnimationTransition eachTransition in animStep.TransitionItems)
            {
                Console.WriteLine("Step transition duration: {0}", eachTransition.TransitionDuration);

                foreach (KeyValuePair<AnimationTypesTransitionEffectType, double> eachSetting in eachTransition.EffectDelays)
                {
                    Console.WriteLine("\tStep duration: {0}", eachTransition.EffectDurations [eachSetting.Key]);
                    Console.WriteLine("\tStep delay: {0}", eachTransition.EffectDelays [eachSetting.Key]);
                    Console.WriteLine("\tStep rotation: {0}", eachTransition.EffectRotations [eachSetting.Key]);
                }//end foreach
            }//end foreach

            // Upload animation step
            service.AnimationStepSaveCompleted += Service_AnimationStepSaveCompleted;
            service.AnimationStepSaveAsync(animStep,
                                           AndroidData.CurrentUser.AccountID,
                                           new Guid(AndroidData.ServiceAuthToken), contentInfo);
        }
        private void SendAudioMessage()
        {
            ar.EndRecording();
            MessageStepDB messageStep = new MessageStepDB();
            messageStep.StepNumber = 1;
            messageStep.StepType = MessageStep.StepTypes.Voice;

            MessageDB message = new MessageDB();
            message.FromAccountID = AndroidData.CurrentUser.AccountID;
            message.MessageStepDBList = new List<MessageStepDB>() { messageStep };

            List<Guid> recipientGuids = new List<Guid>();
            recipientGuids = Contacts.SelectContactsUtil.selectedContacts.Select(s => s.ContactAccountID).ToList();

            ContentInfo contentInfo = new ContentInfo(MessageDB.ConvertFromMessageDB(message), recipientGuids);

            try
            {
                byte[] audioFile = File.ReadAllBytes(filename);
                contentInfo.VoiceRecordings.Add(messageStep.StepNumber, audioFile);
            #if DEBUG
                System.Diagnostics.Debug.WriteLine("audioFile.Length = {0}", audioFile.Length);
            #endif
            } catch (IOException e)
            {
            #if DEBUG
                System.Diagnostics.Debug.WriteLine("Exception thrown (audioFile read) : {0} - {1}", e.Message, e.StackTrace);
            #endif
                return;
            }

            #if DEBUG
            File.Delete(Android.OS.Environment.ExternalStorageDirectory + "/wz/audio.3gp");
            File.Copy(path, Android.OS.Environment.ExternalStorageDirectory + "/wz/audio.3gp", true);
            #endif

            RunOnUiThread(delegate
            {
                mmg.QueueMessage(contentInfo, false, context);

                if (null != wowZapp.LaffOutOut.Singleton.OnGlobalReceived)
                    wowZapp.LaffOutOut.Singleton.OnGlobalReceived(this, new MessageStepCreatedEventArgs(messageStep, null, path, null, null));

                Finish();
            });
        }
        private void SendContentPackMessage()
        {
            MessageStepDB messageStep = new MessageStepDB();
            messageStep.StepNumber = 1;
            switch (option)
            {
                case 1:
                    messageStep.StepType = MessageStep.StepTypes.Comix;
                    break;
                case 2:
                    messageStep.StepType = MessageStep.StepTypes.Comicon;
                    break;
                case 5:
                    messageStep.StepType = MessageStep.StepTypes.SoundFX;
                    break;
                case 6:
                    messageStep.StepType = MessageStep.StepTypes.Emoticon;
                    break;
                case 7:
                    messageStep.StepType = MessageStep.StepTypes.Animation;
                    break;
            }
            if (option != 7)
                messageStep.ContentPackItemID = ContentPackItemsUtil.contentPackItemID;
            //else
            //    messageStep.AnimationData = Animations.AnimationUtil.animation;

            MessageDB message = new MessageDB();
            message.FromAccountID = AndroidData.CurrentUser.AccountID;
            message.MessageStepDBList = new List<MessageStepDB>() { messageStep };

            List<Guid> recipientGuids = new List<Guid>();
            recipientGuids = Contacts.SelectContactsUtil.selectedContacts.Select(s => s.ContactAccountID).ToList();

            ContentInfo contentInfo = new ContentInfo(MessageDB.ConvertFromMessageDB(message), recipientGuids);
            //RunOnUiThread (delegate {
            mmg.QueueMessage(contentInfo, false, context);

            if (null != wowZapp.LaffOutOut.Singleton.OnGlobalReceived)
                wowZapp.LaffOutOut.Singleton.OnGlobalReceived(this, new MessageStepCreatedEventArgs(messageStep, null, null, null, null));
            if (MessageReceivedUtil.FromMessages)
                MessageReceivedUtil.FromMessagesDone = true;
            Finish();
            //});
        }
 private void PlayUnsentMessage(ContentInfo contentInfo)
 {
     // TODO : Somesort of activity
     MessagePlaybackController playbackController = new MessagePlaybackController(contentInfo, this.contentPackItems, context);
 }
        private void SendTextMessage()
        {
            List<MessageStep> msgSteps = new List<MessageStep> ();

            MessageStepDB msgStep = new MessageStepDB ();
            msgStep.MessageText = text.Text;
            msgStep.StepNumber = 1;
            msgStep.StepType = MessageStep.StepTypes.Text;
            //msgSteps.Add (msgStep);

            List<Guid> toAccounts = new List<Guid> ();

            if (Contacts.SelectContactsUtil.selectedUserContacts == null) {
                foreach (ContactDB toAccount in Contacts.SelectContactsUtil.selectedContacts) {
                    toAccounts.Add (toAccount.ContactUser.AccountID);
                }
            } else {
                foreach (UserDB toAccount in Contacts.SelectContactsUtil.selectedUserContacts)
                    toAccounts.Add (toAccount.AccountID);
                Contacts.SelectContactsUtil.selectedUserContacts.Clear ();
            }
            MessageDB message = new MessageDB ();
            message.FromAccountID = AndroidData.CurrentUser.AccountID;
            message.MessageStepDBList = new List<MessageStepDB> (){msgStep};
            ContentInfo contentInfo = new ContentInfo (MessageDB.ConvertFromMessageDB (message), toAccounts);
            mmg.QueueMessage (contentInfo, true, context);
            if (Messages.MessageReceivedUtil.FromMessages)
                Messages.MessageReceivedUtil.FromMessagesDone = true;

            Finish ();
        }