Wednesday, 16 January 2013

SMS Search


SMS Search

Example 15 is a useful script: a search tool for your SMS inbox. First
the script asks for some text for which to search. Then it goes through
your SMS inbox and shows excerpts of messages which contain the given
string. You can see the full message by selecting a desired list item.
Example 15: Inbox search
import inbox, appuifw
box = inbox.Inbox()
query = appuifw.query(u"Search for:", "text").lower()
hits = []
ids = []
for sms_id in box.sms_messages():
msg = box.content(sms_id).lower()
if msg.find(query) != -1:
hits.append(msg[:25])
ids.append(sms_id)
index = appuifw.selection_list(hits, 1)
if index >= 0:
appuifw.note(box.content(ids[index]))
First, the string for which to search is stored in the variable query. As
in the previous example, we use the sms messages() function of the
Inbox object to retrieve a list of all message IDs.
The program loops through all messages, retrieves the contents of each
message with the content() function and tries to find the query string
in each message. If the find() function reports that the query string
occurs in the message, we add the message ID to the list ids so that it
can be found later. Also, we add an excerpt (the first 25 characters) of the
message contents to the list hits, so that we can present this match to
the user.
When we have processed all messages, we show the hits in a selection
list using the message excerpts as list items. When the user chooses an
item, the list dialog returns the item’s index in the list. Using this index,
we find the corresponding message ID in the ids list and the full message
content can be fetched from the phone’s SMS inbox and shown to the
user.

No comments:

Post a Comment