1#!/bin/bash
2#  Copyright (C) 2015 The Android Open Source Project
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15###################################################################
16## Script that generates SMS and MMS messages and fills the Android
17## telephony provider mmssms.db. This is used for testing SMS/MMS.
18###################################################################
19
20AREA_CODE=605
21
22TABLE_CANONICAL_ADDRESSES_START_ID=100
23TABLE_THREADS_START_ID=100
24TABLE_SMS_START_ID=1000
25
26START_TIMESTAMP_IN_SECONDS=1357683093 # 1/8/2013 2:11:33 PM
27TIMESTAMP_INC_IN_SECONDS=120
28
29PART_DIR="/data/data/com.android.providers.telephony/app_parts"
30
31USAGE='fillsms [-f] [-x] <device_phone_number> <# of threads> <# of sms per thread> <# of mms per thread> <image list file> <sql file>
32    -f -- Only generates the SQL file, do not push to the device
33    -x -- Only execute a SQL file
34    -g -- For GB devices
35Examples:
36    # Generate 2 threads each with 10 SMSes and 10 MMSes on device with phone
37    # number +16508619525. MMS messages use images listed in ./images, which list
38    # *.jpg and *.gif files in local directory. The SQL commands are in sql.txt
39    fillsms +16508619525 2 10 10 images sql.txt
40
41    # Same as above but only creating the SQL command file without pushing to
42    # device
43    fillsms -f +16508619525 2 10 10 images sql.txt
44
45    # Just push the sql.txt to device without generating new SQLs
46    fillsms -x +16508619525 2 10 10 images sql.txt
47'
48
49SMIL='<smil> <head> <layout> <root-layout height="%dpx" width="%dpx"> <region fit="meet" height="%dpx" id="Image" left="0" top="0" width="%dpx"/></root-layout> </layout> </head> <body> <par dur="5000ms"> <img region="Image" src="%s"/> </par> </body> </smil>'
50
51MAX_WORDS_PER_MESSAGE=15
52
53DICT=american-english
54
55# don't actually run the sql on device
56opt_sql_only=0
57opt_exec_only=0
58opt_for_gb=0
59
60while test $# -gt 0
61do
62  case $1 in
63    -f)
64      opt_sql_only=1
65      shift
66      ;;
67    -x)
68      opt_exec_only=1
69      shift
70      ;;
71    -g)
72      opt_for_gb=1
73      shift
74      ;;
75    *)
76      break;
77  esac
78done
79
80
81if [ $opt_sql_only -eq "1" -a $opt_exec_only -eq "1" ]; then
82  echo "-f and -x can not coexist"
83  echo "$USAGE"
84  exit 1
85fi
86
87if [ $# -lt 6 ]; then
88  echo "$USAGE"
89  exit 1
90fi
91device_phone=$1
92shift
93num_of_threads=$1
94shift
95sms_per_thread=$1
96shift
97mms_per_thread=$1
98shift
99image_list_file=$1
100shift
101sql_file=$1
102shift
103echo $image_list_file
104
105dict_lines=`wc -l < $DICT`
106image_files=`wc -l < $image_list_file`
107echo $image_files
108
109if [ $mms_per_thread -gt "0" ]; then
110  if [ ! -f $image_list_file ]; then
111    echo "No image files for creating MMS messages"
112    exit 1
113  fi
114fi
115
116echoerr ()
117{
118  echo "$@" 1>&2;
119}
120
121random_value ()
122{
123  echo $(( $RANDOM % $1 + 1 ))
124}
125
126dict_word ()
127{
128  local v=$(random_value $dict_lines)
129  sed $v"q;d" $DICT
130}
131
132gen_message ()
133{
134  local words=$(random_value $MAX_WORDS_PER_MESSAGE)
135  local message=
136  for k in `seq 1 $words`;
137  do
138    local word=$(dict_word)
139    message="$message $word"
140  done
141  echo $message | sed -e "s/'//g"
142}
143
144random_image ()
145{
146  local v=$(random_value $image_files)
147  sed $v"q;d" $image_list_file
148}
149
150add_sql ()
151{
152  echo $1 >> $sql_file
153}
154
155adb_sql ()
156{
157  echo $1
158  adb shell sqlite3 data/data/com.android.providers.telephony/databases/mmssms.db "$1"
159}
160
161adb_sql_with_quotes ()
162{
163  echo $1
164  adb shell "sqlite3 data/data/com.android.providers.telephony/databases/mmssms.db \"$1\""
165}
166######################################################################################
167######################################################################################
168
169if [ $opt_exec_only -eq "0" ]; then
170  # clean up sql file
171  rm -f $sql_file
172
173  add_sql "PRAGMA trusted_schema=1;"
174  # add sql to clean up database
175  add_sql "delete from pdu where _id>=$TABLE_SMS_START_ID;"
176  add_sql "delete from part where _id>=$TABLE_SMS_START_ID;"
177  add_sql "delete from addr where _id>=$TABLE_SMS_START_ID;"
178  add_sql "delete from sms where _id>=$TABLE_SMS_START_ID;"
179  add_sql "delete from threads where _id>=$TABLE_THREADS_START_ID;"
180  add_sql "delete from canonical_addresses where _id>=$TABLE_CANONICAL_ADDRESSES_START_ID;"
181
182  for i in `seq 1 $num_of_threads`;
183  do
184    echo
185    echo "Creating thread $i ......"
186    echo
187
188    # Get random phone number
189    value=$(random_value 1000)
190    middle=$(printf '%03d' $value)
191    value=$(random_value 10000)
192    last=$(printf '%04d' $value)
193    phone="+1$AREA_CODE$middle$last"
194    echo $phone
195    echo
196
197    timestamp=$(( $START_TIMESTAMP_IN_SECONDS + 5 * $TIMESTAMP_INC_IN_SECONDS * $i ))
198
199    # Generate threads
200    addr_id=$(( $TABLE_CANONICAL_ADDRESSES_START_ID + $i ))
201    add_sql "insert into canonical_addresses (_id,address) values ($addr_id,'$phone');"
202
203    thread_id=$(( $TABLE_THREADS_START_ID + $i ))
204    add_sql "insert into threads (_id,date,message_count,recipient_ids,snippet,snippet_cs,read,type,error,has_attachment) values ($thread_id, $timestamp, $sms_per_thread, $addr_id, 'snippet', 0, 1, 0, 0, 0);"
205
206    # Generate SMS
207    if [ $sms_per_thread -gt "0" ]; then
208      add_sql "PRAGMA trusted_schema=1;"
209      half_timestamp_inc=$(( 500 + ((($sms_per_thread + $mms_per_thread) * $TIMESTAMP_INC_IN_SECONDS) * 500 / $sms_per_thread) ))
210      for j in `seq 1 $sms_per_thread`;
211      do
212        message=$(gen_message)
213        date=$(( ( 1000 * $timestamp ) - $half_timestamp_inc * ( 2 * ($sms_per_thread - $j) + ( $i % 2 ) ) ))
214        message_id=$(( $TABLE_SMS_START_ID + $sms_per_thread * $i * 2 + (2 * $j) ))
215        message_type=$(( $j % 2 + 1 ))
216        add_sql "insert into sms (_id,thread_id,address,person,date,status,type,body,read,seen) values ($message_id, $thread_id, '$phone', '$phone', $date, -1, $message_type, '$message', 1, 1);"
217      done
218    fi
219
220    # Generate MMS
221    if [ $mms_per_thread -gt "0" ]; then
222      half_timestamp_inc=$(( 1 + ((($sms_per_thread + $mms_per_thread) * $TIMESTAMP_INC_IN_SECONDS) / ( 2 * $mms_per_thread) ) ))
223      for j in `seq 1 $mms_per_thread`;
224      do
225        image_line=$(random_image)
226        image=`echo $image_line | awk '{ print $1 }'`
227        width=`echo $image_line | awk '{ print $2 }'`
228        height=`echo $image_line | awk '{ print $3 }'`
229        size=`echo $image_line | awk '{ print $4 }'`
230        date=$(( $timestamp - $half_timestamp_inc * ( 2 * ($mms_per_thread - $j) + ( ($i+1) % 2 ) ) ))
231        message_id=$(( $TABLE_SMS_START_ID + $sms_per_thread * $i * 2 + (2 * $j + 1) ))
232        message_type=$(( $j % 2 + 1 ))
233        if [ $message_type -eq '1' ]; then
234          m_type=132
235        else
236          m_type=128
237        fi
238        if [ $opt_for_gb -eq "0" ]; then
239          add_sql "insert into pdu (_id,thread_id,date,date_sent,msg_box,read,m_id,sub,sub_cs,ct_t,m_cls,m_type,v,m_size,pri,rr,tr_id,d_rpt,locked,seen,text_only) values ($message_id, $thread_id, $date, 0, $message_type, 1, 'hmma3p5s1a3m526@w.tmomail.net', 'no subject', 106, 'application/vnd.wap.multipart.related', 'personal', $m_type, 18, $size, 129, 129 , '$message_id', 129, 0, 1, 0);"
240        else
241          add_sql "insert into pdu (_id,thread_id,date,msg_box,read,m_id,sub,sub_cs,ct_t,m_cls,m_type,v,m_size,pri,rr,tr_id,d_rpt,locked,seen) values ($message_id, $thread_id, $date, $message_type, 1, 'hmma3p5s1a3m526@w.tmomail.net', 'no subject', 106, 'application/vnd.wap.multipart.related', 'personal', $m_type, 18, $size, 129, 129 , '$message_id', 129, 0, 1);"
242        fi
243        id_1=$(( $message_id ))
244        id_2=$(( $message_id + 1 ))
245        smil=$(printf "$SMIL" $height $width $height $width $image)
246        add_sql "insert into part (_id,mid,seq,ct,cid,cl,text) values ($id_1, $message_id, -1, 'application/smil', '<smil>', 'smil.xml', '$smil');"
247        image_no_suffix=${image%.*}
248        add_sql "insert into part (_id,mid,seq,ct,cid,cl,_data) values ($id_2, $message_id, 0, 'image/jpeg', '$image_no_suffix', '$image', '$PART_DIR/$image');"
249        if [ $message_type -eq '1' ]; then
250          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_1, $message_id, '$phone', 137, 106);"
251          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_2, $message_id, '$device_phone', 151, 106);"
252        else
253          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_1, $message_id, 'insert-address-token', 137, 106);"
254          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_2, $message_id, '$phone', 151, 106);"
255        fi
256      done
257    fi
258  done
259fi
260
261# Push to device
262if [ $opt_sql_only -eq "0" ]; then
263  # make sure we have access
264  adb root
265
266  # Push all local jpgs or gifs to device, being lazy here.
267  if [ $mms_per_thread -gt "0" ]; then
268    for file in `ls *.jpg *.gif`;
269    do
270      echo "adb push $file $PART_DIR/$file"
271      adb push $file $PART_DIR/$file
272    done
273  fi
274
275  echo "adb push $sql_file /data/fillsms"
276  adb push $sql_file /data/fillsms
277  echo
278  adb_sql ".read /data/fillsms"
279  echo
280  add_sql "PRAGMA trusted_schema=1;"
281  echo
282  adb_sql_with_quotes "select count(*) from canonical_addresses where _id>=$TABLE_CANONICAL_ADDRESSES_START_ID;"
283  echo
284  adb_sql_with_quotes "select count(*) from threads where _id>=$TABLE_THREADS_START_ID;"
285  echo
286  if [ $sms_per_thread -gt "0" ]; then
287    adb_sql_with_quotes "select count(*) from sms where _id>=$TABLE_SMS_START_ID;"
288    echo
289  fi
290  if [ $mms_per_thread -gt "0" ]; then
291    adb_sql_with_quotes "select count(*) from pdu where _id>=$TABLE_SMS_START_ID;"
292    echo
293    adb_sql_with_quotes "select count(*) from part where _id>=$TABLE_SMS_START_ID;"
294    echo
295    adb_sql_with_quotes "select count(*) from addr where _id>=$TABLE_SMS_START_ID;"
296    echo
297  fi
298fi
299