SMS Sorter
Besides the message content, it is possible to fetch the timestamp, thesender’s phone number (address) and information on whether the message
has been read already using the Inbox object.
To show how to use these functions, we make an application that
includes an Exit key handler, an application title and a menu. This
application can be used to sort your SMS inbox according to various
attributes of text messages. You can choose, using the application
menu, whether the messages should be sorted by time, sender or unread
flag.
Example 16: Inbox sorter
import inbox, appuifw, e32
def show_list(msgs):
msgs.sort()
items = []
for msg in msgs:
items.append(msg[1][:15])
appuifw.selection_list(items)
def sort_time():
msgs = []
for sms_id in box.sms_messages():
msgs.append((-box.time(sms_id), box.content(sms_id)))
show_list(msgs)
def sort_sender():
msgs = []
for sms_id in box.sms_messages():
msgs.append((box.address(sms_id), box.content(sms_id)))
show_list(msgs)
def sort_unread():
msgs = []
for sms_id in box.sms_messages():
msgs.append((-box.unread(sms_id), box.content(sms_id)))
show_list(msgs)
def quit():
print "INBOX SORTER EXITS"
app_lock.signal()
box = inbox.Inbox()
appuifw.app.exit_key_handler = quit
appuifw.app.title = u"Inbox Sorter"
appuifw.app.menu = [(u"Sort by time", sort_time),
(u"Sort by sender", sort_sender),
(u"Unread first", sort_unread)]
print "INBOX SORTER STARTED"
app_lock = e32.Ao_lock()
app_lock.wait()
The code may seem long but notice that the functions sort time(),
sort sender() and sort unread() are almost identical. Each of
these functions creates a list, msgs. In each case, the list contains tuples
in which the first item is the key, according to which the messages should
be sorted, and the second item is the message content
No comments:
Post a Comment