• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Shell utility functions for mediaprovider developers.
2# sudo apt-get install rlwrap to have a more fully featured sqlite CLI
3set -x # enable debugging
4
5function add-media-grant () {
6# add a media grant to -p package for -id file_id
7
8  function usage() {
9
10    cat <<EOF
11
12    Usage: $(basename "$BASH_SOURCE[0]") [-i] id value [-p] package value
13
14    Adds a media grant for specified package and file._id
15
16    Available Options:
17
18    -i, --id         The files._id in mediaprovider database
19    -p, --package    Package name i.e. com.android.package
20
21EOF
22
23
24  }
25
26  # If we don't have any params, just print the documentation.
27  if [ -z "$1" ]
28  then
29
30    usage
31
32  else
33
34      # parse incoming arguments
35      while [[ "$#" -gt 0 ]]
36      do case $1 in
37        -i|--id) id="$2"
38        shift;;
39      -p|--package) packagename="$2"
40        shift;;
41      *) usage; return
42      esac
43      shift
44    done
45
46  echo "Adding media_grant for id=$id to package $packagename"
47
48  uri='content\\://media/picker/0/com.android.providers.media.photopicker/media/'
49  uriWithId="${uri}$id"
50
51  if [ -z "$id" ] || [ -z "$packagename" ]
52  then
53    usage; return
54  fi
55
56
57  adb wait-for-device
58  adb shell content call --method 'grant_media_read_for_package' \
59    --uri 'content://media' \
60    --extra 'uri':s:"$uriWithId" \
61    --extra 'android.intent.extra.PACKAGE_NAME':s:"$packagename"
62
63  fi
64}
65
66function sqlite3-pull () {
67    adb root
68    if [ -z "$1" ]
69    then
70        dir=$(pwd)
71    else
72        dir=$1
73    fi
74    package=$(get-package)
75
76    if [ -f "$dir/external.db" ]; then
77      rm "$dir/external.db"
78    fi
79    if [ -f "$dir/external.db-wal" ]; then
80      rm "$dir/external.db-wal"
81    fi
82
83    adb pull /data/user/0/$package/databases/external.db $dir/external.db
84    adb pull /data/user/0/$package/databases/external.db-wal "$dir/external.db-wal"
85
86    sqlite3 $dir/external.db "drop trigger files_insert"
87    sqlite3 $dir/external.db "drop trigger files_update"
88    sqlite3 $dir/external.db "drop trigger files_delete"
89
90    rlwrap sqlite3 $dir/external.db
91}
92
93function sqlite3-push () {
94    adb root
95    if [ -z "$1" ]
96    then
97        dir=$(pwd)
98    else
99        dir=$1
100    fi
101    package=$(get-package)
102
103    adb push $dir/external.db /data/user/0/$package/databases/external.db
104    adb push $dir/external.db-wal /data/user/0/$package/databases/external.db-wal
105
106    sqlite3-trigger-upgrade
107}
108
109function sqlite3-trigger-upgrade () {
110    package=$(get-package)
111
112    # Doesn't actually upgrade the db because db version is hardcoded in code
113    # It however triggers upgrade path
114    check_string="/data/user/0/$package/databases/external.db \"pragma user_version\""
115    version=$(adb shell sqlite3 $check_string)
116    echo "Old version: $version"
117
118    version=$((version+1))
119    upgrade_string="/data/user/0/$package/databases/external.db \"pragma user_version=$version\""
120    adb shell sqlite3 $upgrade_string
121
122    version=$(adb shell sqlite3 $check_string)
123    echo "New version: $version"
124
125    adb shell am force-stop $package
126}
127
128function get-id-from-data () {
129    adb root
130    path="$1"
131    package=$(get-package)
132    dir="/data/user/0/$package/databases/external.db"
133    clause="\"select _id from files where _data='$path';\""
134    echo $clause
135    adb shell sqlite3 $dir $clause
136}
137
138function get-data-from-id () {
139    adb root
140    _id="$1"
141    package=$(get-package)
142    dir="/data/user/0/$package/databases/external.db"
143    clause="\"select _data from files where _id='$_id';\""
144    echo $clause
145    adb shell sqlite3 $dir $clause
146}
147
148function get-package() {
149    if [ -z "$(adb shell pm list package com.android.providers.media.module)" ]
150    then
151        echo "com.google.android.providers.media.module"
152    else
153        echo "com.android.providers.media.module"
154    fi
155}
156
157set +x  # disable debugging
158