internal Task <IEnumerable <Message> > SearchMessageAsync(MessagesSearchFilter filter)
        {
            return(Task.Run <IEnumerable <Message> >(() =>
            {
                List <Message> messageList = new List <Message>();
                int ret;

                Interop.Messages.MessageSearchCallback callback = (IntPtr messageHandle, int index, int resultCount, int totalCount, IntPtr userData) =>
                {
                    try
                    {
                        if (messageHandle != IntPtr.Zero)
                        {
                            IntPtr duplicatedMessageHandle = IntPtr.Zero;

                            DuplicateMessageHandle(messageHandle, out duplicatedMessageHandle);
                            if (duplicatedMessageHandle != IntPtr.Zero)
                            {
                                int type = (int)MessageType.Unknown;
                                int result = Interop.Messages.GetMessageType(duplicatedMessageHandle, out type);
                                if (result != (int)MessagesError.None)
                                {
                                    Log.Error(Globals.LogTag, "Failed to get message type, Error - " + (MessagesError)result);
                                }

                                switch ((MessageType)type)
                                {
                                case MessageType.Sms:
                                    {
                                        var messageItem = new SmsMessage(duplicatedMessageHandle);
                                        messageList.Add(messageItem);
                                        break;
                                    }

                                case MessageType.Mms:
                                    {
                                        var messageItem = new MmsMessage(duplicatedMessageHandle);
                                        messageList.Add(messageItem);
                                        break;
                                    }

                                case MessageType.CellBroadcast:
                                    {
                                        var messageItem = new CBMessage(duplicatedMessageHandle);
                                        messageList.Add(messageItem);
                                        break;
                                    }

                                case MessageType.Push:
                                    {
                                        var messageItem = new PushMessage(duplicatedMessageHandle);
                                        messageList.Add(messageItem);
                                        break;
                                    }

                                default:
                                    {
                                        Log.Error(Globals.LogTag, "Invaild message type - " + type);
                                        break;
                                    }
                                }

                                return true;
                            }
                        }
                    }
                    catch
                    {
                        Log.Error(Globals.LogTag, "Exception in Callback");
                    }

                    return false;
                };

                ret = Interop.Messages.SearchMessage(_MessageServiceHandle, (int)filter.MessageBoxType, (int)filter.MessageType, filter.TextKeyword, filter.AddressKeyword, 0, 0, callback, IntPtr.Zero);
                if (ret != (int)MessagesError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to search message, Error - " + (MessagesError)ret);
                    MessagesErrorFactory.ThrowMessagesException(ret, _MessageServiceHandle);
                }

                return messageList;
            }));
        }
示例#2
0
        private void RegisterMessageReceivedEvent()
        {
            _messageReceivedCallback = (IntPtr messageHandle, IntPtr userData) =>
            {
                try
                {
                    IntPtr duplicatedMessageHandle = IntPtr.Zero;

                    DuplicateMessageHandle(messageHandle, out duplicatedMessageHandle);
                    if (duplicatedMessageHandle != IntPtr.Zero)
                    {
                        int type   = (int)MessageType.Unknown;
                        int result = Interop.Messages.GetMessageType(duplicatedMessageHandle, out type);
                        if (result != (int)MessagesError.None)
                        {
                            Log.Error(Globals.LogTag, "Failed to get message type, Error - " + (MessagesError)result);
                        }

                        switch ((MessageType)type)
                        {
                        case MessageType.Sms:
                        {
                            var receivedMessage           = new SmsMessage(duplicatedMessageHandle);
                            MessageReceivedEventArgs args = new MessageReceivedEventArgs(receivedMessage);
                            _messageReceived?.Invoke(null, args);
                            break;
                        }

                        case MessageType.Mms:
                        {
                            var receivedMessage           = new MmsMessage(duplicatedMessageHandle);
                            MessageReceivedEventArgs args = new MessageReceivedEventArgs(receivedMessage);
                            _messageReceived?.Invoke(null, args);
                            break;
                        }

                        case MessageType.CellBroadcast:
                        {
                            var receivedMessage           = new CBMessage(duplicatedMessageHandle);
                            MessageReceivedEventArgs args = new MessageReceivedEventArgs(receivedMessage);
                            _messageReceived?.Invoke(null, args);
                            break;
                        }

                        case MessageType.Push:
                        {
                            var receivedMessage           = new PushMessage(duplicatedMessageHandle);
                            MessageReceivedEventArgs args = new MessageReceivedEventArgs(receivedMessage);
                            _messageReceived?.Invoke(null, args);
                            break;
                        }

                        default:
                        {
                            Log.Error(Globals.LogTag, "Invaild message type - " + type);
                            break;
                        }
                        }
                    }
                }
                catch
                {
                    Log.Error(Globals.LogTag, "Exception in Callback");
                }
            };

            int ret = Interop.Messages.SetMessageIncomingCb(_MessageServiceHandle, _messageReceivedCallback, IntPtr.Zero);

            if (ret != (int)MessagesError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set message incoming callback, Error - " + (MessagesError)ret);
            }
        }