Saturday, April 27, 2013

Popup SMS notifier made via SL4A python script launched by tasker

Popup SMS notifier made via SL4A python script launched by tasker



i didn't play popup sms before, cause i thought it is a waste of battery. but  i have changed my mind right after i saw some introductions about sms popup notifier apps.

i guess it would be much helpful and funny anyway. also it could save a little bit time since i don't have to get into message inbox to read it.

however it seems there is some kind of conflict or bugs maybe for those sms popup notifier apps. everything runs fine until i tap "mark as read" button, it would just crash and FC. i have already tested quite some different kind of simliar apps. same error occurs if i try to mark the message as read.

that makes me thinking of programming or scripting one myself. that is the starting of all of it.

firstly i tried to use tasker scene, or tasker popup alert. however, soon i realized neither of it is working. the reason is either tasker scene or tasker popup alert, they are actually just a scene. popup alert has a displaying timeout limit, if you set it up to forever, it could display right on the screen and never disappear. also the task action would get stuck right there until you stop it manually in the tasker task.

scene has a similar problem too, a scene would not disappear until it got destroyed by tasker.

so either popup alert or scene could not perform if there is more than 1 sms message incoming. it could not display more than 1 popup window.

so i have to figure another way out of it.and sl4a pythong script becomes my testing project.
via this script, i am able to see as many popup sms notifier windows as the sms messages i receive. i may simply tap on the reply button to open up another new window to input the reply message then send it out. also i may tap on mark as read button to mark this current sms message as read.

btw, if you click reply button, it could also mark this current sms message as read too. so i think it could be much more convenient for daily life.

and most funny part is you are able to check your sent message in the default android sms message outbox.


PS. i am still trying to improve this script, to see if i could add contact photo accordingly to the sender phone number. and hopefully i may change the
inputing reply message layout to be fullscreen. i guess that is everybody wants. there is still a long way for me to walk through, wish my poor brain could
still handle it.





========================= script content ===============

# coding=utf-8
import android
droid = android.Android()
import time
Date = droid.getIntent().result[u'extras'][u'%SMSRD']
Time = droid.getIntent().result[u'extras'][u'%SMSRT']
Name = droid.getIntent().result[u'extras'][u'%SMSRN']
.encode('utf-8')
Number = droid.getIntent().result[u'extras'][u'%SMSRF']
.encode('utf-8')
Body = droid.getIntent().result[u'extras'][u'%SMSRB'].encode('utf-8')
count = droid.smsGetMessageCount(True).result
droid.ttsSpeak("you have a new message from" + Name)
droid.dialogCreateAlert(Date +", "+ Time + "unread: "+ str(count), "From: " +Name + "\n" + "Tel: " +Number +"\n" + Body)
droid.dialogSetPositiveButtonText("Reply")
droid.dialogSetNegativeButtonText("Close")
droid.dialogSetNeutralButtonText("Mark as read")
droid.dialogShow()
response = droid.dialogGetResponse()
droid.dialogDismiss()
if response.result["which"] == "neutral" or response.result["which"] == "positive":
  msgIDs = droid.smsGetMessageIds(True, 'inbox').result
  if msgIDs > 0:
   for msgID in msgIDs:
     if (droid.smsGetMessageById(msgID).result['body'].encode('utf-8')).find(Body) >= 0:

      msgRead=[0]

      msgRead[0]=msgID
       droid.smsMarkMessageRead(msgRead,1)
if response.result["which"] == "positive":
 droid.dialogCreateInput(Date +", "+ Time + "unread: "+ str(count), "From: " +Name + "\n" + "Tel: " +Number)
 droid.dialogSetPositiveButtonText("Send")
 droid.dialogSetNegativeButtonText("Close")
 droid.dialogShow()
 response = droid.dialogGetResponse()
 droid.dialogDismiss()
 if response.result["which"] == "positive":
  class Task():
    SET_VARIABLE = 547
    def new_task(self):
        self.action_cnt = 0
        self.extras = {'version_number': '1.0', 'task_name': 'task' + str(time.time()), 'task_priority': 9 }
    def set_var(self, varname, value):
        self.action_cnt += 1
        self.extras['action' + str(self.action_cnt)] = {'action': self.SET_VARIABLE, 'arg:1': varname, 'arg:2': value, 'arg:3': False, 'arg:4': False,
'arg:5': False}
    def run_task(self):
        taskIntent = droid.makeIntent('net.dinglisch.android.tasker.ACTION_TASK', None, None, self.extras).result
        droid.sendBroadcastIntent(taskIntent)
    def set_var_now(self, varname, value):
        self.new_task()
        self.set_var(varname, value)
        self.run_task()


  t = Task()
  t.set_var_now("%REPLY_ID", Number)
  t.set_var_now("%REPLY_BODY", response.result["value"])
  droid.makeToast("message sent!")


============== please name the above python script as "popup sms notifier" or any others you like.==========





======================= tasker profile part ==============

profile 1: (popup sms notifier)

content: event-receive text

task:

action 1: variable-variable clear, name: %REPLY_BODY

action 2: script-run sl4a script, name: popup sms notifier   pass variable: %SMSRD,%SMSRT, %SMSRN, %SMSRF, %SMSRB

Hit Done!



profile 2: (popup sms reply)

context: state-variable value, name: %REPLY_BODY, OP: is set

task:

action 1: phone-send sms, number: %REPLY_ID, message: REPLY_BODY, Store in messaging app Checked

action 2: variable-variable clear, name: %REPLY_BODY

Hit Done!


2 comments:

  1. I tried to copy/paste this script into sl4a but nothing happens when I run the script. The examples that come with python for android all work though. Are there some settings that need to be changed or something?

    ReplyDelete
    Replies
    1. I got it working now. Turned out to be an unexpected indention that was preventing it from running. Nice work!

      Delete