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