Add test activity to run SIM Activation.

Change-Id: I0c387dbdc461ae2ddd0e9a25010c925a0c849df2
diff --git a/src/com/android/services/telephony/activation/SimActivationActivity.java b/src/com/android/services/telephony/activation/SimActivationActivity.java
index acc0cdf..3833706 100644
--- a/src/com/android/services/telephony/activation/SimActivationActivity.java
+++ b/src/com/android/services/telephony/activation/SimActivationActivity.java
@@ -17,8 +17,11 @@
 package com.android.services.telephony.activation;
 
 import android.app.Activity;
+import android.app.PendingIntent;
+import android.app.PendingIntent.CanceledException;
 import android.content.Intent;
 import android.os.Bundle;
+import android.telephony.TelephonyManager;
 
 import com.android.services.telephony.Log;
 
@@ -30,11 +33,31 @@
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
+        Log.i(this, "onCreate");
 
         Intent intent = getIntent();
         if (Intent.ACTION_SIM_ACTIVATION_REQUEST.equals(intent.getAction())) {
             Log.i(this, "Activation requested " + intent);
+
+            runActivation(intent);
         }
         finish();
     }
+
+    private void runActivation(Intent intent) {
+        PendingIntent response = intent.getParcelableExtra(Intent.EXTRA_SIM_ACTIVATION_RESPONSE);
+
+        Log.i(this, "Running activation w/ response " + response);
+        // TODO: Actually do activation
+
+        // Return the response as an activity result and the pending intent.
+        setResult(TelephonyManager.SIM_ACTIVATION_RESULT_COMPLETE);
+        if (response != null) {
+            try {
+                response.send();
+            } catch (CanceledException e) {
+                Log.w(this, "Could not respond to SIM Activation.");
+            }
+        }
+    }
 }
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index af3f7e4..8900568 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -19,6 +19,7 @@
 
     <uses-permission android:name="android.permission.CALL_PHONE" />
     <uses-permission android:name="android.permission.PERFORM_CDMA_PROVISIONING" />
+    <uses-permission android:name="android.permission.PERFORM_SIM_ACTIVATION" />
 
     <application android:label="@string/app_name">
         <uses-library android:name="android.test.runner" />
@@ -56,6 +57,8 @@
                 <data android:scheme="smsto" />
             </intent-filter>
         </service>
+
+        <receiver android:name="com.android.services.telephony.activation.ResponseReceiver" />
     </application>
 
     <!--
diff --git a/tests/src/com/android/services/telephony/activation/ResponseReceiver.java b/tests/src/com/android/services/telephony/activation/ResponseReceiver.java
new file mode 100644
index 0000000..33e7f00
--- /dev/null
+++ b/tests/src/com/android/services/telephony/activation/ResponseReceiver.java
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+package com.android.services.telephony.activation;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+
+import com.android.services.telephony.Log;
+
+public class ResponseReceiver extends BroadcastReceiver {
+    volatile public static boolean responseReceived = false;
+    public static final String ACTION_ACTIVATION_RESPONSE =
+            "com.android.services.telephony.ACTIVATION_RESPONSE";
+
+    private final Object mLock;
+    private Context mContext;
+
+    ResponseReceiver(Object lock) {
+        mLock = lock;
+    }
+
+    /** ${inheritDoc} */
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (!ACTION_ACTIVATION_RESPONSE.equals(intent.getAction())) {
+            Log.e(this, null, "Unexpected intent: " + intent.getAction());
+            return;
+        }
+
+        responseReceived = true;
+        Log.i(this, "received intent");
+
+        if (mLock != null) {
+            synchronized(mLock) {
+                Log.i(this, "notifying");
+                mLock.notify();
+            }
+        }
+    }
+
+    void register(Context context) {
+        context.registerReceiver(this, new IntentFilter(ACTION_ACTIVATION_RESPONSE));
+        mContext = context;
+    }
+
+    void unregister() {
+        mContext.unregisterReceiver(this);
+    }
+}
diff --git a/tests/src/com/android/services/telephony/activation/SimActivationTest.java b/tests/src/com/android/services/telephony/activation/SimActivationTest.java
new file mode 100644
index 0000000..f6060dc
--- /dev/null
+++ b/tests/src/com/android/services/telephony/activation/SimActivationTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+
+package com.android.services.telephony.activation;
+
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.services.telephony.Log;
+
+public class SimActivationTest extends AndroidTestCase {
+
+    private static Object mActivationLock = new Object();
+    private ResponseReceiver mResponseReceiver;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mResponseReceiver = new ResponseReceiver(mActivationLock);
+        mResponseReceiver.register(context());
+    }
+
+    /** ${inheritDoc} */
+    @Override
+    protected void tearDown() throws Exception {
+        mResponseReceiver.unregister();
+        super.tearDown();
+    }
+
+    @SmallTest
+    public void testSimActivationResponse() throws Exception {
+        Log.i(this, "Running activation test");
+
+        Intent responseIntent = new Intent(ResponseReceiver.ACTION_ACTIVATION_RESPONSE);
+        responseIntent.setPackage(context().getPackageName());
+        PendingIntent pendingResponse = PendingIntent.getBroadcast(
+                context(), 0, responseIntent, 0);
+
+        Intent intent = new Intent(Intent.ACTION_SIM_ACTIVATION_REQUEST);
+        intent.putExtra(Intent.EXTRA_SIM_ACTIVATION_RESPONSE, pendingResponse);
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+        Log.i(this, "sent intent");
+        context().startActivity(intent);
+        synchronized (mActivationLock) {
+            Log.i(this, "waiting ");
+            mActivationLock.wait(5000);
+            Log.i(this, "unwaiting");
+        }
+        assertTrue(ResponseReceiver.responseReceived);
+    }
+
+    private Context context() {
+        return getContext();
+    }
+}