I happen to find that for GSM standard phone, call duration would be created into sql database only if the outgoing call is being answered, or after hanging up if it is not being answered.
So i guess this method is meant for CDMA android phone only. cause we could not detect if outgoing call is offhook by the callee via tasker or sl4a.
Java code would be the solution i think, by monitoring the phone state.
in my earlier entry, i have mentioned the method about how to get it done by python script.
however it doesn't work on android phone but android pad.
i guess the reason is the contacts dabase is encrypted by default, most likely not encrypted in android pad, since it doesn't actually have mobile communication modules built-in.
well, i have been thinking if there is another way to solve this still via python script?
after some testings, problem finally solved.
if you are interested, please read on.
#### script content ###############
import android
droid = android.Android()
import time
time.sleep(3)
a = droid.queryContent('content://call_log/calls',['duration'],'type <= 2',None,None).result ## returns a list of dictionary
while a is not None:
b =a[-1]
c = b.get('duration')
d = int(c)
if d == 1 or d == 2:
break
droid.vibrate(200)
Monday, April 29, 2013
How to get contact phone number via contact name?
in some specific situations, if we got a bunch of contacts names, how can we get the matched phone number from the database?
the answer is yes, we may get it done by python sl4a script or sqlite3 command.
for sqlite3 command requirement, please check my previous entry for sure.
hereinafter it is the specific command to get the matches mobile phone number by name.
## type represents the phone number type, 2 means mobile phone number.
## LIMIT 1, cause there might be more than 1 mobile phone number for this contact person.
### %Contact_name is the variable set from tasker
sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db 'SELECT number FROM view_v1_phones WHERE name="%Contact_name" AND type=2 LIMIT 1';
via python sl4a script, it is also simple that way. if you wanna sepcify the search result for mobile phone number only, you may use for statement combined with find function with regex match.
### %Contact_name is the variable set from tasker
#### script content ###########
import android
droid = android.Android()
contacts = droid.queryContent('content://com.android.contacts/data/phones',['data1'],'display_name = "%Contact_name"',None,None).result
a = contacts[0]['data1']
print a
## returns specific phone number in interger
the answer is yes, we may get it done by python sl4a script or sqlite3 command.
for sqlite3 command requirement, please check my previous entry for sure.
hereinafter it is the specific command to get the matches mobile phone number by name.
## type represents the phone number type, 2 means mobile phone number.
## LIMIT 1, cause there might be more than 1 mobile phone number for this contact person.
### %Contact_name is the variable set from tasker
sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db 'SELECT number FROM view_v1_phones WHERE name="%Contact_name" AND type=2 LIMIT 1';
via python sl4a script, it is also simple that way. if you wanna sepcify the search result for mobile phone number only, you may use for statement combined with find function with regex match.
### %Contact_name is the variable set from tasker
#### script content ###########
import android
droid = android.Android()
contacts = droid.queryContent('content://com.android.contacts/data/phones',['data1'],'display_name = "%Contact_name"',None,None).result
a = contacts[0]['data1']
print a
## returns specific phone number in interger
SL4A python script to manage android default sms message
SL4A python script to manage android default sms message
since i have been studying on sl4a python script these days, i think it could be helpful if i post how sl4a script manages android default sms message.
in my earlier entry, i already posted the script about how to mark specific sms as read via keyword and sender phone number.
if you are interested, you may take a look as well.
## unread sms count ####
import android
droid = android.Android()
count = droid.smsGetMessageCount(True).result
print count ## you will see the unread sms count result in there
## send sms ###
# coding:utf-8
import android
droid = android.Android()
number = "........."
message = "........."
droid.smsSend(number, message.encode("utf-8"))
## mark all unread sms as read ####
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(True, 'inbox').result
if msgIDs > 0:
droid.smsMarkMessageRead(megIDs, 1)
### delete all unread sms #######
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(True, 'inbox').result
if msgIDs > 0:
for msgID in msgIDs:
droid.smsDeleteMessage(msgID)
#### delete specific message in outbox ####
# coding:utf-8
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(False, 'sent').result
if msgIDs > 0:
for msgID in msgIDs:
message = droid.smsGetMessageById(msgID, ['address','body']).result
str1 = message['address'].encode('utf-8')
str2 = message['body'].encode('utf-8')
if str1.find('XXXXX') >= 0 and str2.find('YYYY') >= 0: ##XXXX reprents phone number, YYYY represents message keywords
droid.smsDeleteMessage(msgID)
#### delete specific message in inbox and outbox ####
# coding:utf-8
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(False, 'inbox').result
msgIDs.extend(droid.smsGetMessageIds(False, 'sent').result
if msgIDs > 0:
for msgID in msgIDs:
message = droid.smsGetMessageById(msgID, ['address','body']).result
str1 = message['address'].encode('utf-8')
str2 = message['body'].encode('utf-8')
if str1.find('XXXX') >= 0 and str2.find('YYYY') >= 0: ##XXXX reprents phone number, YYYY represents message keywords
droid.smsDeleteMessage(msgID)
since i have been studying on sl4a python script these days, i think it could be helpful if i post how sl4a script manages android default sms message.
in my earlier entry, i already posted the script about how to mark specific sms as read via keyword and sender phone number.
if you are interested, you may take a look as well.
## unread sms count ####
import android
droid = android.Android()
count = droid.smsGetMessageCount(True).result
print count ## you will see the unread sms count result in there
## send sms ###
# coding:utf-8
import android
droid = android.Android()
number = "........."
message = "........."
droid.smsSend(number, message.encode("utf-8"))
## mark all unread sms as read ####
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(True, 'inbox').result
if msgIDs > 0:
droid.smsMarkMessageRead(megIDs, 1)
### delete all unread sms #######
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(True, 'inbox').result
if msgIDs > 0:
for msgID in msgIDs:
droid.smsDeleteMessage(msgID)
#### delete specific message in outbox ####
# coding:utf-8
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(False, 'sent').result
if msgIDs > 0:
for msgID in msgIDs:
message = droid.smsGetMessageById(msgID, ['address','body']).result
str1 = message['address'].encode('utf-8')
str2 = message['body'].encode('utf-8')
if str1.find('XXXXX') >= 0 and str2.find('YYYY') >= 0: ##XXXX reprents phone number, YYYY represents message keywords
droid.smsDeleteMessage(msgID)
#### delete specific message in inbox and outbox ####
# coding:utf-8
import android
droid = android.Android()
msgIDs = droid.smsGetMessageIds(False, 'inbox').result
msgIDs.extend(droid.smsGetMessageIds(False, 'sent').result
if msgIDs > 0:
for msgID in msgIDs:
message = droid.smsGetMessageById(msgID, ['address','body']).result
str1 = message['address'].encode('utf-8')
str2 = message['body'].encode('utf-8')
if str1.find('XXXX') >= 0 and str2.find('YYYY') >= 0: ##XXXX reprents phone number, YYYY represents message keywords
droid.smsDeleteMessage(msgID)
Sunday, April 28, 2013
Another version of my Popup SMS notifier
in my earlier entry, i have posted the script about how to make the popup sms notifier via python sl4a script.
and i also said i am wanting to improve it with full screen reply inputting layout.
i am sure python sl4a script can make it, but to do that, i will have to learn how to make the layout done by javascript first.
it is not gonna be easy, since i don't have too much time, so i have to get another easier way.
according to a suggestion of my friend, i have done some modifications for the reply message part.
now you will see full screen layout by then. below it is the screenshot to show you the demostration.
here you go~~~~
and i also said i am wanting to improve it with full screen reply inputting layout.
i am sure python sl4a script can make it, but to do that, i will have to learn how to make the layout done by javascript first.
it is not gonna be easy, since i don't have too much time, so i have to get another easier way.
according to a suggestion of my friend, i have done some modifications for the reply message part.
now you will see full screen layout by then. below it is the screenshot to show you the demostration.
here you go~~~~
Python script to get android phone vibrate while the callee answers
I happen to find that for GSM standard phone, call duration would be created into sql database only if the outgoing call is being answered, or after hanging up if it is not being answered.
So i guess this method is meant for CDMA android phone only. cause we could not detect if outgoing call is offhook by the callee via tasker or sl4a.
Java code would be the solution i think, by monitoring the phone state.
In my earlier entry, i have posted an idea about to your android droid vibrate while the callee answers via sqlite3 command.
according to the feedback from a user, reporting a bug, which indicates the run shell action with root most likely would run kinda slow in some low RAM like 512M or 215M smartphone.
i guess the loop interval is also a problem, which make it worse in such a short while.
so i was thinking of finding another workaround.
after done some homework, i have tested working in my android tablet pad. running kinda fast, however, when i moved it to my HTC droid, it failed, error says "sqlite database disk image is malformed".
i got scared ATM, and quickly run some checks on my database file, whereas everything is fine, i opened it via RE and sqlite editor, both are fine. no corruption at all. when i switched back to the python script, it still ran end up with the same error.
well, i am not sure if there is some kind of secure system preventing me from accessing the database.
if you guys are interested, please run some testings for me, will be greatly appreciated.
First move, change the database file permission to be rw-rw-rw, or else, python could fail to access it.
and then you may run this script with tasker outgoing call context.
###### script content ########
import android
droid = android.Android()
import sqlite3
import time
time.sleep(3)
con = sqlite3.connect("/data/data/com.android.providers.contacts/databases/contacts2.db")
con.isolation_level = None
while True:
cur = con.cursor()
cur.execute("SELECT duration FROM calls WHERE type <= 2 ORDER BY date DESC LIMIT 1")
for i in cur:
if i[0] == 1 or i[0] == 2:
break
print i[0]
droid.vibrate(200)
cur.close()
con.close()
So i guess this method is meant for CDMA android phone only. cause we could not detect if outgoing call is offhook by the callee via tasker or sl4a.
Java code would be the solution i think, by monitoring the phone state.
In my earlier entry, i have posted an idea about to your android droid vibrate while the callee answers via sqlite3 command.
according to the feedback from a user, reporting a bug, which indicates the run shell action with root most likely would run kinda slow in some low RAM like 512M or 215M smartphone.
i guess the loop interval is also a problem, which make it worse in such a short while.
so i was thinking of finding another workaround.
after done some homework, i have tested working in my android tablet pad. running kinda fast, however, when i moved it to my HTC droid, it failed, error says "sqlite database disk image is malformed".
i got scared ATM, and quickly run some checks on my database file, whereas everything is fine, i opened it via RE and sqlite editor, both are fine. no corruption at all. when i switched back to the python script, it still ran end up with the same error.
well, i am not sure if there is some kind of secure system preventing me from accessing the database.
if you guys are interested, please run some testings for me, will be greatly appreciated.
First move, change the database file permission to be rw-rw-rw, or else, python could fail to access it.
and then you may run this script with tasker outgoing call context.
###### script content ########
import android
droid = android.Android()
import sqlite3
import time
time.sleep(3)
con = sqlite3.connect("/data/data/com.android.providers.contacts/databases/contacts2.db")
con.isolation_level = None
while True:
cur = con.cursor()
cur.execute("SELECT duration FROM calls WHERE type <= 2 ORDER BY date DESC LIMIT 1")
for i in cur:
if i[0] == 1 or i[0] == 2:
break
print i[0]
droid.vibrate(200)
cur.close()
con.close()
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!
Thursday, April 25, 2013
Tasker to detect and vibrate once the ougoing call is being answered
Tasker to detect and vibrate once the ougoing call is being answered
I happen to find that for GSM standard phone, call duration would be created into sql database only if the outgoing call is being answered, or after hanging up if it is not being answered.
So i guess this method is meant for CDMA android phone only. cause we could not detect if outgoing call is offhook by the callee via tasker or sl4a.
Java code would be the solution i think, by monitoring the phone state.
If you are interested, please follow my profile instruction below and run some test, please further advise me if it is working or any issues. many thanks!
NB.: to run sqlite 3 command, you must make sure your phone has installed the proper sqlite3 file and you must root your phone first to run sqlite3 command.
Tips:
download the sqlite3 file via the links below and renane it as "sqlite3" if it is not, then put it under the phone root directory /system/bin folder, and don't forget to modify the permission for the sqlite3 file to be
rwxr-xr-x
android 4.0 or earlier: http://bit.ly/sqlite3
android 4.1+: http://bit.ly/sqlite3-for-jb
profile:
context: state-phone-call, type:outgoing
task:
action 1: script-run shell,
command: sqlite3 /data/data/com.android. providers.contacts/databases/ contacts2.db "select duration from calls where type <= 2 order by date desc limit 1;"
use root checked, store result in %VIBRATING
action 2: alert-vibrate, 200ms, if %VIBRATING ~ 1
action 3: task-goto, action number 2, if %VIBRATING !~ 1
Action 4: task-stop, if %VIBRATING ~ 1
Subscribe to:
Posts (Atom)