aboutsummaryrefslogtreecommitdiff
path: root/tools/messagegen/fillsms
blob: dc2e5076f7f0e6dbb5b3912981c66f5ea35dd66e (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/bash
#  Copyright (C) 2015 The Android Open Source Project
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###################################################################
## Script that generates SMS and MMS messages and fills the Android
## telephony provider mmssms.db. This is used for testing SMS/MMS.
###################################################################

AREA_CODE=605

TABLE_CANONICAL_ADDRESSES_START_ID=100
TABLE_THREADS_START_ID=100
TABLE_SMS_START_ID=1000

START_TIMESTAMP_IN_SECONDS=1357683093 # 1/8/2013 2:11:33 PM
TIMESTAMP_INC_IN_SECONDS=120

PART_DIR="/data/data/com.android.providers.telephony/app_parts"

USAGE='fillsms [-f] [-x] <device_phone_number> <# of threads> <# of sms per thread> <# of mms per thread> <image list file> <sql file>
    -f -- Only generates the SQL file, do not push to the device
    -x -- Only execute a SQL file
    -g -- For GB devices
Examples:
    # Generate 2 threads each with 10 SMSes and 10 MMSes on device with phone
    # number +16508619525. MMS messages use images listed in ./images, which list
    # *.jpg and *.gif files in local directory. The SQL commands are in sql.txt
    fillsms +16508619525 2 10 10 images sql.txt

    # Same as above but only creating the SQL command file without pushing to
    # device
    fillsms -f +16508619525 2 10 10 images sql.txt

    # Just push the sql.txt to device without generating new SQLs
    fillsms -x +16508619525 2 10 10 images sql.txt
'

SMIL='<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>'

MAX_WORDS_PER_MESSAGE=15

DICT=american-english

# don't actually run the sql on device
opt_sql_only=0
opt_exec_only=0
opt_for_gb=0

while test $# -gt 0
do
  case $1 in
    -f)
      opt_sql_only=1
      shift
      ;;
    -x)
      opt_exec_only=1
      shift
      ;;
    -g)
      opt_for_gb=1
      shift
      ;;
    *)
      break;
  esac
done


if [ $opt_sql_only -eq "1" -a $opt_exec_only -eq "1" ]; then
  echo "-f and -x can not coexist"
  echo "$USAGE"
  exit 1
fi

if [ $# -lt 6 ]; then
  echo "$USAGE"
  exit 1
fi
device_phone=$1
shift
num_of_threads=$1
shift
sms_per_thread=$1
shift
mms_per_thread=$1
shift
image_list_file=$1
shift
sql_file=$1
shift
echo $image_list_file

dict_lines=`wc -l < $DICT`
image_files=`wc -l < $image_list_file`
echo $image_files

if [ $mms_per_thread -gt "0" ]; then
  if [ ! -f $image_list_file ]; then
    echo "No image files for creating MMS messages"
    exit 1
  fi
fi

echoerr ()
{
  echo "$@" 1>&2;
}

random_value ()
{
  echo $(( $RANDOM % $1 + 1 ))
}

dict_word ()
{
  local v=$(random_value $dict_lines)
  sed $v"q;d" $DICT
}

gen_message ()
{
  local words=$(random_value $MAX_WORDS_PER_MESSAGE)
  local message=
  for k in `seq 1 $words`;
  do
    local word=$(dict_word)
    message="$message $word"
  done
  echo $message | sed -e "s/'//g"
}

random_image ()
{
  local v=$(random_value $image_files)
  sed $v"q;d" $image_list_file
}

add_sql ()
{
  echo $1 >> $sql_file
}

adb_sql ()
{
  echo $1
  adb shell sqlite3 data/data/com.android.providers.telephony/databases/mmssms.db "$1"
}

adb_sql_with_quotes ()
{
  echo $1
  adb shell "sqlite3 data/data/com.android.providers.telephony/databases/mmssms.db \"$1\""
}
######################################################################################
######################################################################################

if [ $opt_exec_only -eq "0" ]; then
  # clean up sql file
  rm -f $sql_file

  add_sql "PRAGMA trusted_schema=1;"
  # add sql to clean up database
  add_sql "delete from pdu where _id>=$TABLE_SMS_START_ID;"
  add_sql "delete from part where _id>=$TABLE_SMS_START_ID;"
  add_sql "delete from addr where _id>=$TABLE_SMS_START_ID;"
  add_sql "delete from sms where _id>=$TABLE_SMS_START_ID;"
  add_sql "delete from threads where _id>=$TABLE_THREADS_START_ID;"
  add_sql "delete from canonical_addresses where _id>=$TABLE_CANONICAL_ADDRESSES_START_ID;"

  for i in `seq 1 $num_of_threads`;
  do
    echo
    echo "Creating thread $i ......"
    echo

    # Get random phone number
    value=$(random_value 1000)
    middle=$(printf '%03d' $value)
    value=$(random_value 10000)
    last=$(printf '%04d' $value)
    phone="+1$AREA_CODE$middle$last"
    echo $phone
    echo

    timestamp=$(( $START_TIMESTAMP_IN_SECONDS + 5 * $TIMESTAMP_INC_IN_SECONDS * $i ))

    # Generate threads
    addr_id=$(( $TABLE_CANONICAL_ADDRESSES_START_ID + $i ))
    add_sql "insert into canonical_addresses (_id,address) values ($addr_id,'$phone');"

    thread_id=$(( $TABLE_THREADS_START_ID + $i ))
    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);"

    # Generate SMS
    if [ $sms_per_thread -gt "0" ]; then
      add_sql "PRAGMA trusted_schema=1;"
      half_timestamp_inc=$(( 500 + ((($sms_per_thread + $mms_per_thread) * $TIMESTAMP_INC_IN_SECONDS) * 500 / $sms_per_thread) ))
      for j in `seq 1 $sms_per_thread`;
      do
        message=$(gen_message)
        date=$(( ( 1000 * $timestamp ) - $half_timestamp_inc * ( 2 * ($sms_per_thread - $j) + ( $i % 2 ) ) ))
        message_id=$(( $TABLE_SMS_START_ID + $sms_per_thread * $i * 2 + (2 * $j) ))
        message_type=$(( $j % 2 + 1 ))
        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);"
      done
    fi

    # Generate MMS
    if [ $mms_per_thread -gt "0" ]; then
      half_timestamp_inc=$(( 1 + ((($sms_per_thread + $mms_per_thread) * $TIMESTAMP_INC_IN_SECONDS) / ( 2 * $mms_per_thread) ) ))
      for j in `seq 1 $mms_per_thread`;
      do
        image_line=$(random_image)
        image=`echo $image_line | awk '{ print $1 }'`
        width=`echo $image_line | awk '{ print $2 }'`
        height=`echo $image_line | awk '{ print $3 }'`
        size=`echo $image_line | awk '{ print $4 }'`
        date=$(( $timestamp - $half_timestamp_inc * ( 2 * ($mms_per_thread - $j) + ( ($i+1) % 2 ) ) ))
        message_id=$(( $TABLE_SMS_START_ID + $sms_per_thread * $i * 2 + (2 * $j + 1) ))
        message_type=$(( $j % 2 + 1 ))
        if [ $message_type -eq '1' ]; then
          m_type=132
        else
          m_type=128
        fi
        if [ $opt_for_gb -eq "0" ]; then
          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);"
        else
          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);"
        fi
        id_1=$(( $message_id ))
        id_2=$(( $message_id + 1 ))
        smil=$(printf "$SMIL" $height $width $height $width $image)
        add_sql "insert into part (_id,mid,seq,ct,cid,cl,text) values ($id_1, $message_id, -1, 'application/smil', '<smil>', 'smil.xml', '$smil');"
        image_no_suffix=${image%.*}
        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');"
        if [ $message_type -eq '1' ]; then
          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_1, $message_id, '$phone', 137, 106);"
          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_2, $message_id, '$device_phone', 151, 106);"
        else
          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_1, $message_id, 'insert-address-token', 137, 106);"
          add_sql "insert into addr (_id,msg_id,address,type,charset) values ($id_2, $message_id, '$phone', 151, 106);"
        fi
      done
    fi
  done
fi

# Push to device
if [ $opt_sql_only -eq "0" ]; then
  # make sure we have access
  adb root

  # Push all local jpgs or gifs to device, being lazy here.
  if [ $mms_per_thread -gt "0" ]; then
    for file in `ls *.jpg *.gif`;
    do
      echo "adb push $file $PART_DIR/$file"
      adb push $file $PART_DIR/$file
    done
  fi

  echo "adb push $sql_file /data/fillsms"
  adb push $sql_file /data/fillsms
  echo
  adb_sql ".read /data/fillsms"
  echo
  add_sql "PRAGMA trusted_schema=1;"
  echo
  adb_sql_with_quotes "select count(*) from canonical_addresses where _id>=$TABLE_CANONICAL_ADDRESSES_START_ID;"
  echo
  adb_sql_with_quotes "select count(*) from threads where _id>=$TABLE_THREADS_START_ID;"
  echo
  if [ $sms_per_thread -gt "0" ]; then
    adb_sql_with_quotes "select count(*) from sms where _id>=$TABLE_SMS_START_ID;"
    echo
  fi
  if [ $mms_per_thread -gt "0" ]; then
    adb_sql_with_quotes "select count(*) from pdu where _id>=$TABLE_SMS_START_ID;"
    echo
    adb_sql_with_quotes "select count(*) from part where _id>=$TABLE_SMS_START_ID;"
    echo
    adb_sql_with_quotes "select count(*) from addr where _id>=$TABLE_SMS_START_ID;"
    echo
  fi
fi