Esempio n. 1
0
        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;
            }));
        }