1# Shell utility functions for contactsprovider developers.
2# sudo apt-get install rlwrap to have a more fully featured sqlite CLI
3# sudo apt-get install sqlitebrowser to explore the database with GUI
4
5CONTACTS_DB="contacts2.db"
6
7function contacts-pull () {
8    adb root && adb wait-for-device
9    dir=$(get-dir $1)
10    package=$(get-package)
11
12    if [ -f "$dir/$CONTACTS_DB" ]; then
13      rm "$dir/$CONTACTS_DB"
14    fi
15    if [ -f "$dir/$CONTACTS_DB-wal" ]; then
16      rm "$dir/$CONTACTS_DB-wal"
17    fi
18    adb pull /data/user/0/$package/databases/$CONTACTS_DB $dir/$CONTACTS_DB
19    adb pull /data/user/0/$package/databases/$CONTACTS_DB-wal $dir/$CONTACTS_DB-wal
20}
21
22function get-dir (){
23    if [ -z "$1" ]
24    then
25        dir=$(pwd)
26    else
27        dir=$1
28    fi
29    echo "$dir"
30}
31
32function sqlite3-pull () {
33    dir="$(get-dir $1)"
34    contacts-pull $dir
35    rlwrap sqlite3 $dir/$CONTACTS_DB
36}
37
38function sqlitebrowser-pull () {
39    dir="$(get-dir $1)"
40    contacts-pull $dir
41    sqlitebrowser $dir/$CONTACTS_DB
42}
43
44function sqlite3-push () {
45    adb root && adb wait-for-device
46    if [ -z "$1" ]
47    then
48        dir=$(pwd)
49    else
50        dir=$1
51    fi
52    package=$(get-package)
53
54    adb push $dir/$CONTACTS_DB /data/user/0/$package/databases/$CONTACTS_DB
55    adb push $dir/$CONTACTS_DB-wal /data/user/0/$package/databases/$CONTACTS_DB-wal
56
57    sqlite3-trigger-upgrade
58}
59
60function sqlite3-trigger-upgrade () {
61    package=$(get-package)
62
63    # Doesn't actually upgrade the db because db version is hardcoded in code
64    # It however triggers upgrade path
65    check_string="/data/user/0/$package/databases/$CONTACTS_DB \"pragma user_version\""
66    version=$(adb shell sqlite3 $check_string)
67    echo "Old version: $version"
68
69    version=$((version+1))
70    upgrade_string="/data/user/0/$package/databases/$CONTACTS_DB \"pragma user_version=$version\""
71    adb shell sqlite3 $upgrade_string
72
73    version=$(adb shell sqlite3 $check_string)
74    echo "New version: $version"
75
76    adb shell am force-stop $package
77}
78
79function get-package() {
80    echo "com.android.providers.contacts"
81}
82
83set +x  # disable debugging
84