Add SMS autoreply activity

Change-Id: If05b51dcbdfec699eca0a7c02ec378ab538761d5
diff --git a/tests/SmsAutoReply/Android.mk b/tests/SmsAutoReply/Android.mk
new file mode 100644
index 0000000..5c465bc
--- /dev/null
+++ b/tests/SmsAutoReply/Android.mk
@@ -0,0 +1,28 @@
+# Copyright 2012, 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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+# We only want this apk build for tests.
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_JAVA_LIBRARIES := android.test.runner
+
+# Include all test java files.
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := SmsAutoReply
+
+include $(BUILD_PACKAGE)
diff --git a/tests/SmsAutoReply/AndroidManifest.xml b/tests/SmsAutoReply/AndroidManifest.xml
new file mode 100644
index 0000000..0bae38a
--- /dev/null
+++ b/tests/SmsAutoReply/AndroidManifest.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="com.android.smsautoreply"
+      android:versionCode="1"
+      android:versionName="1.0">
+    <uses-sdk android:minSdkVersion="4" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
+    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
+    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
+    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
+    <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>
+
+    <application android:icon="@drawable/icon" android:label="@string/app_name">
+        <activity android:name=".AutoReplyActivity"
+                  android:label="@string/app_name">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest>
diff --git a/tests/SmsAutoReply/res/drawable/icon.png b/tests/SmsAutoReply/res/drawable/icon.png
new file mode 100644
index 0000000..a07c69f
--- /dev/null
+++ b/tests/SmsAutoReply/res/drawable/icon.png
Binary files differ
diff --git a/tests/SmsAutoReply/res/layout/main.xml b/tests/SmsAutoReply/res/layout/main.xml
new file mode 100644
index 0000000..32bcfc3
--- /dev/null
+++ b/tests/SmsAutoReply/res/layout/main.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    >
+    <TextView android:id="@+id/logView" android:layout_height="wrap_content" android:layout_width="fill_parent"></TextView>
+</LinearLayout>
diff --git a/tests/SmsAutoReply/res/values/strings.xml b/tests/SmsAutoReply/res/values/strings.xml
new file mode 100644
index 0000000..4c630c9
--- /dev/null
+++ b/tests/SmsAutoReply/res/values/strings.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="hello">Hello World, AutoReplyActivity!</string>
+    <string name="app_name">SmsAutoReply</string>
+</resources>
diff --git a/tests/SmsAutoReply/src/com/android/smsautoreply/AutoReplyActivity.java b/tests/SmsAutoReply/src/com/android/smsautoreply/AutoReplyActivity.java
new file mode 100644
index 0000000..e94025d
--- /dev/null
+++ b/tests/SmsAutoReply/src/com/android/smsautoreply/AutoReplyActivity.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+package com.android.smsautoreply;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.os.Bundle;
+import android.os.Environment;
+import android.telephony.SmsManager;
+import android.telephony.SmsMessage;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * A sms reply activity which auto-replies the received sms message to the sender
+ * This is used as the receiver for 1:M sms stress test.
+ * Keep the app in the foreground when running the test.
+ */
+public class AutoReplyActivity extends Activity {
+    private static final String TAG = "AutoReplyActivity";
+    private static final String LOG_FILE = "received_sms_log.txt";
+    private SmsMessageReceiver mSmsMsgReceiver = null;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+        mSmsMsgReceiver = new SmsMessageReceiver();
+        registerReceiver(mSmsMsgReceiver,
+                new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
+    }
+
+    private class SmsMessageReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            Bundle extras = intent.getExtras();
+            if (extras == null)
+                return;
+
+            Object[] pdus = (Object[]) extras.get("pdus");
+
+            for (int i = 0; i < pdus.length; i++) {
+                SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
+                Log.d(TAG, String.format("SMS received from %s, body: %s",
+                        message.getOriginatingAddress(), message.getMessageBody()));
+                logMessage(message);
+                replyMessage(context, message);
+            }
+        }
+
+        // Log received sms message into an output file
+        private void logMessage(SmsMessage msg) {
+            File logFile = new File(Environment.getExternalStorageDirectory(), LOG_FILE);
+
+            String logMsg = String.format("SMS: from: %s body: %s",
+                    msg.getOriginatingAddress(),msg.getMessageBody());
+            try {
+                String currentDateTimeString =
+                    new SimpleDateFormat("MM-dd HH:mm:ss.SSS").format(new Date());
+                logMsg = String.format("%s: %s\n", currentDateTimeString, logMsg);
+                FileOutputStream fos = new FileOutputStream(logFile, true);
+                fos.write(logMsg.getBytes());
+                fos.flush();
+                fos.close();
+            } catch (IOException ioe) {
+                Log.e(TAG, "failed to log SMS", ioe);
+                Log.d(TAG, logMsg);
+            }
+        }
+
+        private void replyMessage(Context context, SmsMessage msg) {
+            SmsManager sms = SmsManager.getDefault();
+            String message = msg.getMessageBody();
+            sms.sendTextMessage(msg.getOriginatingAddress(), null, message, null, null);
+        }
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        if (mSmsMsgReceiver != null) {
+            unregisterReceiver(mSmsMsgReceiver);
+        }
+    }
+}
diff --git a/tests/src/com/android/mms/SmsTestRunner.java b/tests/src/com/android/mms/SmsTestRunner.java
index f0af1f6..7d3504c 100644
--- a/tests/src/com/android/mms/SmsTestRunner.java
+++ b/tests/src/com/android/mms/SmsTestRunner.java
@@ -21,7 +21,6 @@
 import android.test.InstrumentationTestSuite;
 
 import com.android.mms.ui.MultiPartSmsTests;
-import com.android.mms.ui.LongThreadTest;
 import com.android.mms.ui.SmsStressTest;
 
 import junit.framework.TestSuite;
@@ -29,7 +28,7 @@
 /**
  * TestRunner for Sms tests
  * To run the test type command
- * adb shell am instrument -e recipeint 6509339530 -e messages 10
+ * adb shell am instrument -e recipient 6509339530 -e messages 10
  * -e messagefile words -e recipientfile recipients -e receivetimer 180
  * -e sendinterval 10 -w com.android.mms.tests/com.android.mms.SmsTestRunner
  */
diff --git a/tests/src/com/android/mms/ui/SmsStressTest.java b/tests/src/com/android/mms/ui/SmsStressTest.java
index d94a818..2def4cb 100644
--- a/tests/src/com/android/mms/ui/SmsStressTest.java
+++ b/tests/src/com/android/mms/ui/SmsStressTest.java
@@ -16,36 +16,13 @@
 
 package com.android.mms.ui;
 
-import static com.android.mms.ui.MessageListAdapter.COLUMN_ID;
-import static com.android.mms.ui.MessageListAdapter.COLUMN_MSG_TYPE;
-
-import com.android.mms.R;
-import com.android.mms.data.Conversation;
-import com.android.mms.data.Contact;
-import com.android.mms.data.ContactList;
-import com.android.mms.ui.ComposeMessageActivity;
-import com.android.mms.ui.RecipientsEditor;
-import com.android.mms.SmsTestRunner;
 import android.os.Environment;
-import android.os.SystemClock;
-import android.database.Cursor;
-import android.content.Context;
-import android.content.Intent;
-import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.LargeTest;
-import android.view.View;
-import android.view.ViewStub;
-import android.widget.EditText;
-import android.widget.TextView;
-import android.widget.Button;
-import android.widget.ImageButton;
 import android.util.Log;
 
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
-import java.io.IOException;
-import java.util.List;
 
 /**
  * Sms stress test. Send muliptle sms each with multiple segments.
@@ -74,7 +51,6 @@
                                  mIteration, mSendInterval));
         mWriter = new BufferedWriter(new FileWriter(new File(
             Environment.getExternalStorageDirectory(), OUTPUT), true));
-
     }
 
     @Override
diff --git a/tests/src/com/android/mms/ui/SmsTest.java b/tests/src/com/android/mms/ui/SmsTest.java
index f2076bb..89c04c1 100644
--- a/tests/src/com/android/mms/ui/SmsTest.java
+++ b/tests/src/com/android/mms/ui/SmsTest.java
@@ -20,26 +20,16 @@
 import static com.android.mms.ui.MessageListAdapter.COLUMN_MSG_TYPE;
 
 import com.android.mms.R;
-import com.android.mms.data.Conversation;
-import com.android.mms.data.Contact;
-import com.android.mms.data.ContactList;
 import com.android.mms.ui.ComposeMessageActivity;
 import com.android.mms.ui.RecipientsEditor;
 import com.android.mms.SmsTestRunner;
 
-import android.os.SystemClock;
 import android.database.Cursor;
 import android.content.Context;
-import android.content.Intent;
-import android.telephony.PhoneNumberUtils;
 import android.test.ActivityInstrumentationTestCase2;
-import android.test.suitebuilder.annotation.LargeTest;
-import android.text.TextUtils;
 import android.view.View;
 import android.view.ViewStub;
 import android.widget.EditText;
-import android.widget.TextView;
-import android.widget.Button;
 import android.widget.ImageButton;
 import android.util.Log;
 
@@ -50,7 +40,6 @@
 import java.util.List;
 import java.util.ArrayList;
 
-
 /**
  * Base class for sms tests.
  */
@@ -111,8 +100,8 @@
         }
         loadRecipientsList();
         loadMessage();
-        Log.v(TAG, String.format("mReceiveTimer: %d, mRecipient: %s",
-                                 mReceiveTimer, mRecipient));
+        Log.v(TAG, String.format("mReceiveTimer: %d, mRecipient: %s, mMessage: ",
+                                 mReceiveTimer, mRecipient, mMessage));
     }
 
     @Override
@@ -164,6 +153,7 @@
 
         Context targetAppContext = mInst.getTargetContext().getApplicationContext();
         String filePath = String.format("%s/%s", targetAppContext.getFilesDir(), messageFileName);
+        Log.v(TAG, "filePath: " + filePath);
         // Read messages from a file
         byte[] buffer = new byte[(int) new File(filePath).length()];
         BufferedInputStream bf = null;