Friday, December 8, 2017

address book

import os
import sqlite3 as lite
import sqlite3
import sys
import re
import time



class SQLiteStorePipeline:
    # filename = 'data.sqlite'

    def __init__(self,filename):
        self.conn = None
        self.filename = filename

    def queryList(self, table):
        try:

            u = self.conn.execute('select * from ' + table)
            res = u.fetchall()

        except:
            print 'query Failed '
        return res
    def insert(self, table, itemlist):
        try:

          
            # u = self.conn.execute('insert into ' + table + "("+ field +") values('" + item +"'')")
            u = self.conn.execute('insert into ' + table + "(name,phone) values(?,?)", (itemlist))
    
            print "inserted " 
            self.conn.commit()
        except:
            print 'insert Failed '
    def remove(self, table, pid):
        try:

          
            # u = self.conn.execute('insert into ' + table + "("+ field +") values('" + item +"'')")
            u = self.conn.execute('DELETE from ' + table + " where id=?", (int(pid),))
    
            print "deleted " 
            self.conn.commit()
        except:
            print 'delete Failed '
      
 

    def initialize(self):
        if os.path.exists(self.filename):
            self.conn = sqlite3.connect(self.filename)
        else:
            self.conn = self.create_table()

    def finalize(self):
        if self.conn is not None:
            self.conn.commit()
            self.conn.close()
            self.conn = None

    def create_table(self):
        conn = sqlite3.connect(self.filename)
        conn.execute("""create table book
                     (id INTEGER PRIMARY KEY AUTOINCREMENT,name text, phone text)""")
  
        return conn

db = "/home/user1/phonebook.db"
u = SQLiteStorePipeline(db)
u.initialize()
comm = sys.argv[1]

if comm == "list":
    plist = u.queryList("book")
    for i in plist:
        print i
elif comm == "rm":
    # uid = int(sys.argv[2])
    uid = sys.argv[2]
    table = "book"
    u.remove(table,uid)
elif comm == "add":
    name = sys.argv[2]
    phone = sys.argv[3]
    toinsert = [name,phone]
    table = "book"
    u.insert(table,toinsert)

elif comm == "sync":
    plist = u.queryList("book")
    exit = "adb shell input keyevent 4"
    clean = "adb shell pm clear com.yourapp.package"
    exitcontact = "adb shell ps | grep com.android.contacts | awk '{print $2}' | xargs adb shell kill"
    clean1 = "adb shell input keyevent KEYCODE_APP_SWITCH"
    clean2 = "adb shell input keyevent 20"
    clean3 ="adb shell input keyevent DEL"
    clearcontact = "adb shell pm clear com.android.providers.contacts"
    os.system(clearcontact)
    for i in plist:
        name = i[1]
        phone = i[2]
        addcon = "adb shell am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name " + name+ " -e phone " + phone
        os.system(addcon)
        os.system(exit)
        # os.system(exit)
        os.system(exit)
        time.sleep(0.5)
        os.system(clean1)
        os.system(clean2)
        os.system(clean3)
    os.system(clean1)
    os.system(clean2)
    os.system(clean2)
    for i in range(7):
        os.system(clean3)

elif comm =="call":
    name = sys.argv[2]
    sterm = re.compile(name)
    plist = u.queryList("book")
    for i in plist:
        if name == str(i[0]):
            comd = "adb shell am start -a android.intent.action.CALL -d tel:"+i[2]
            os.system(comd)
elif comm =="hang":
    comd = "adb shell input keyevent 6"
    os.system(comd)
elif name == "answer":
    comd = "adb shell am start -n com.android.phone/.InCallScreen -a android.intent.action.ANSWER"
    os.system(comd)





1- usage

-add contact
$ ./adressbook add friend1 85566883390

- list contace
$ ./addressbook list

- remove contact
$ ./addressbook remove 1

- sync to book (add adb access)
$ ./addressbook sync

- call from contact index 1
$ ./addressbook call 1

- hangup call
$ ./addressbook hang

-answer
$ ./addressbook answer

No comments:

Post a Comment