SL4A (Scripting Layer for Android) as name suggest is designed to run scripts on android. I am using it for running python, but support java, shell, perl and many others.
I am using an android mobile, but it didn't had a way to block series of phone numbers (last three number could be anything). So I was trying to turn off the ringer. Here is the code:
import android
import time
droid = android.Android()
droid.startTrackingPhoneState() #this is needed for keeping track of state of phone
while True: #infinite loop
try:
res=droid.eventWait() #wait for an event to occur and returns a dictionary
val=res.result[u'data'] #u'data' (a internally defined constant) is where all the needed information is stored
if val[u'state']==u'ringing': #if phone is ringing
number=val[u'incomingNumber'] #get phone number
if number.find("1408959")!=-1: #if phone number contain "1408959" then go to silent mode
state=droid.checkRingerSilentMode() #store the ringer state
droid.toggleRingerSilentMode(True) #switch to silent mode
time.sleep(30) #wait for the call to end (takes about 30sec)
droid.toggleRingerSilentMode(state.result) #get back to original ringer state
except :
print "error"
time.sleep(1)
droid.stopTrackingPhoneState()
this program keeps running in background by help of SL4A. As of yet there are some problem, as it seem to crash when contact or call log is opened.