Archived:SMS search example using PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

This PySymbian code snippet shows how to search within all SMS messages on the device.

Article Metadata
Tested with
Devices(s): E71, XpressMusic 5800
Platform(s): S60 3rd and 5th Editions
Keywords: search, SMS
Created: marcelobarrosalmeida (26 Jun 2009)
Last edited: hamishwillee (31 May 2013)


This article creates a small application for people (like me) that are always searching for something in their SMSs. Yes, newer phones have already a builtin application for this task. In this case, you may use it as an example for inbox API when programming in Python.

The searching algorithm is based upon Boyer-Moore string searching, a famous and efficient string searching algorithm (python code borrowed from active state site, by Nelson Rush)

The code (Python 1.9.5) and screenshots are below. You can download the sis or module window.py from the program repositorys.

Thanks to javsmo for drawing nice application icon !

MBA smsearch1.jpg . MBA smsearch2.jpg . MBA smsearch3.jpg

Source code

# -*- coding: utf-8 -*-
# Marcelo Barros de Almeida
# marcelobarrosalmeida@gmail.com
# License: GPL 3
from window import *
from appuifw import *
import inbox
import time
class ShowSMS(Dialog):
def __init__(self,cbk,title,msg):
class SMSearch(Application):
def __init__(self):
self.dlg = None
self.results = [u""]
self.terms = u""
body = Canvas()
menu = [(u"Search", self.get_pattern),
(u"About", self.about),
(u"Exit", self.close_app)]
def about(self):
note( u"SMSearch.\nMarcelo Barros de Almeida\nmarcelobarrosalmeida@gmail.com", "info" )
def lst_cbk(self):
idx = self.body.current()
(sms_id,txt,tmr,addr,fn) = self.results[idx]
msg = u"Address: " + addr + \
u"\nDate: " + tmr + \
u"\nBox: " + fn + \
u"\n\n" + txt
self.dlg = ShowSMS(lambda:self.refresh(),unicode(sms_id),msg)
def bmh_search(self, pattern, text):
# http://code.activestate.com/recipes/117223/
m = len(pattern)
n = len(text)
if m > n: return -1
skip = []
for k in range(256): skip.append(m)
for k in range(m - 1): skip[ord(pattern[k])] = m - k - 1
skip = tuple(skip)
k = m - 1
while k < n:
j = m - 1; i = k
while j >= 0 and text[i] == pattern[j]:
j -= 1; i -= 1
if j == -1: return i + 1
k += skip[ord(text[k])]
return -1
def search(self,pattern):
self.results = []
lst = []
folders = {inbox.EInbox:u"Inbox",
for f,fn in folders.iteritems():
ibx = inbox.Inbox(f)
for sms_id in ibx.sms_messages():
txt = ibx.content(sms_id)
txt_utf8 = txt.encode('utf-8')
pattern_utf8 = pattern.encode('utf-8')
if self.bmh_search(pattern_utf8.lower(),txt_utf8.lower()) > -1:
tm = ibx.time(sms_id)
dt = unicode(time.ctime(tm),'utf-8',errors='ignore')
if self.results:
# order following unix time and remove it after
lst = [x[1:] for x in lst]
self.body = Listbox(lst,self.lst_cbk)
app.screen = 'normal' # avoid wrong screen redraw
note(u"No results for " + self.terms,"info")
def get_pattern(self):
pattern = query(u"Search terms:", "text", self.terms)
if pattern is not None:
if pattern:
pattern = pattern.strip()
self.terms = pattern
if __name__ == "__main__":
sms = SMSearch()
This page was last modified on 31 May 2013, at 04:10.
52 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.