1 /* 2 * Copyright (C) 2012 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 17 package com.android.smsautoreply; 18 19 import android.app.Activity; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.net.ConnectivityManager; 25 import android.os.Bundle; 26 import android.os.Environment; 27 import android.telephony.SmsManager; 28 import android.telephony.SmsMessage; 29 import android.util.Log; 30 31 import java.io.File; 32 import java.io.FileOutputStream; 33 import java.io.IOException; 34 import java.text.SimpleDateFormat; 35 import java.util.Date; 36 37 /** 38 * A sms reply activity which auto-replies the received sms message to the sender 39 * This is used as the receiver for 1:M sms stress test. 40 * Keep the app in the foreground when running the test. 41 */ 42 public class AutoReplyActivity extends Activity { 43 private static final String TAG = "AutoReplyActivity"; 44 private static final String LOG_FILE = "received_sms_log.txt"; 45 private SmsMessageReceiver mSmsMsgReceiver = null; 46 47 @Override 48 public void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 setContentView(R.layout.main); 51 mSmsMsgReceiver = new SmsMessageReceiver(); 52 registerReceiver(mSmsMsgReceiver, 53 new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); 54 } 55 56 private class SmsMessageReceiver extends BroadcastReceiver { 57 @Override 58 public void onReceive(Context context, Intent intent) { 59 Bundle extras = intent.getExtras(); 60 if (extras == null) 61 return; 62 63 Object[] pdus = (Object[]) extras.get("pdus"); 64 65 for (int i = 0; i < pdus.length; i++) { 66 SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]); 67 Log.d(TAG, String.format("SMS received from %s, body: %s", 68 message.getOriginatingAddress(), message.getMessageBody())); 69 logMessage(message); 70 replyMessage(context, message); 71 } 72 } 73 74 // Log received sms message into an output file 75 private void logMessage(SmsMessage msg) { 76 File logFile = new File(Environment.getExternalStorageDirectory(), LOG_FILE); 77 78 String logMsg = String.format("SMS: from: %s body: %s", 79 msg.getOriginatingAddress(),msg.getMessageBody()); 80 try { 81 String currentDateTimeString = 82 new SimpleDateFormat("MM-dd HH:mm:ss.SSS").format(new Date()); 83 logMsg = String.format("%s: %s\n", currentDateTimeString, logMsg); 84 FileOutputStream fos = new FileOutputStream(logFile, true); 85 fos.write(logMsg.getBytes()); 86 fos.flush(); 87 fos.close(); 88 } catch (IOException ioe) { 89 Log.e(TAG, "failed to log SMS", ioe); 90 Log.d(TAG, logMsg); 91 } 92 } 93 94 private void replyMessage(Context context, SmsMessage msg) { 95 SmsManager sms = SmsManager.getDefault(); 96 String message = msg.getMessageBody(); 97 sms.sendTextMessage(msg.getOriginatingAddress(), null, message, null, null); 98 } 99 } 100 101 @Override 102 protected void onPause() { 103 super.onPause(); 104 if (mSmsMsgReceiver != null) { 105 unregisterReceiver(mSmsMsgReceiver); 106 } 107 } 108 } 109