Add supported URI scheme to PhoneAccounts. (2/4)

Modify PhoneAccountRegistrar to load/save supported URI schemes to
the state XML file.
Added version check to state XML file and migration step to add
'sip' for SIPConnectionService PhoneAccounts, and 'tel' for all other
PhoneAccounts.

Bug: 17140110
Change-Id: Ic5d41254bade421cc39eb197a1d6fc2d6695f62f
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index d8877fb..8ae47d2 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -16,7 +16,8 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
           coreApp="true"
-          package="com.android.telecomm.tests">
+          package="com.android.telecomm.tests"
+          android:debuggable="true">
 
     <!-- Test connection service outgoing video preview. -->
     <uses-permission android:name="android.permission.CAMERA" />
@@ -24,7 +25,8 @@
             android:name="com.android.telecomm.permission.REGISTER_PROVIDER_OR_SUBSCRIPTION" />
     <uses-permission android:name="android.permission.CONTROL_INCALL_EXPERIENCE" />
 
-    <application android:label="@string/app_name">
+    <application android:label="@string/app_name"
+                 android:debuggable="true">
         <uses-library android:name="android.test.runner" />
 
         <!-- Miscellaneous telecomm app-related test activities. -->
@@ -116,5 +118,6 @@
     -->
     <instrumentation android:name="android.test.InstrumentationTestRunner"
             android:targetPackage="com.android.telecomm"
-            android:label="Telecomm application tests." />
+            android:label="Telecomm application tests."
+            android:debuggable="true"/>
 </manifest>
diff --git a/tests/src/com/android/telecomm/testapps/CallServiceNotifier.java b/tests/src/com/android/telecomm/testapps/CallServiceNotifier.java
index 5fd75e5..e8aa19f 100644
--- a/tests/src/com/android/telecomm/testapps/CallServiceNotifier.java
+++ b/tests/src/com/android/telecomm/testapps/CallServiceNotifier.java
@@ -109,6 +109,7 @@
                 .withIconResId(R.drawable.stat_sys_phone_call)
                 .withLabel("TelecommTestApp Call Provider")
                 .withShortDescription("a short description for the call provider")
+                .withSupportedUriScheme("tel")
                 .build());
 
         telecommManager.registerPhoneAccount(PhoneAccount.builder()
diff --git a/tests/src/com/android/telecomm/tests/unit/PhoneAccountRegistrarTest.java b/tests/src/com/android/telecomm/tests/unit/PhoneAccountRegistrarTest.java
index 9daa026..3522153 100644
--- a/tests/src/com/android/telecomm/tests/unit/PhoneAccountRegistrarTest.java
+++ b/tests/src/com/android/telecomm/tests/unit/PhoneAccountRegistrarTest.java
@@ -24,6 +24,7 @@
 import org.xmlpull.v1.XmlSerializer;
 
 import android.content.ComponentName;
+import android.content.Context;
 import android.net.Uri;
 import android.telecomm.PhoneAccount;
 import android.telecomm.PhoneAccountHandle;
@@ -35,6 +36,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.util.Arrays;
 
 public class PhoneAccountRegistrarTest extends AndroidTestCase {
 
@@ -61,7 +63,8 @@
                 .withSubscriptionNumber("555-1212")
                 .withCapabilities(
                         PhoneAccount.CAPABILITY_CALL_PROVIDER
-                                | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
+                                | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION
+                )
                 .withIconResId(0)
                 .withLabel("label1")
                 .withShortDescription("desc1")
@@ -86,6 +89,17 @@
                 .withLabel("label2")
                 .withShortDescription("desc2")
                 .build());
+        mRegistrar.registerPhoneAccount(PhoneAccount.builder()
+                .withAccountHandle(
+                        new PhoneAccountHandle(new ComponentName("sippkg", "sipcls"), "id4"))
+                .withHandle(Uri.parse("sip:test@sip.com"))
+                .withSubscriptionNumber("test")
+                .withCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
+                .withIconResId(0)
+                .withLabel("label2")
+                .withShortDescription("desc2")
+                .withSupportedUriScheme("tel")
+                .build());
     }
 
     @Override
@@ -97,7 +111,8 @@
     private static <T> T roundTrip(
             Object self,
             T input,
-            PhoneAccountRegistrar.XmlSerialization<T> xml)
+            PhoneAccountRegistrar.XmlSerialization<T> xml,
+            Context context)
             throws Exception {
         Log.d(self, "Input = %s", input);
 
@@ -118,7 +133,7 @@
             XmlPullParser parser = Xml.newPullParser();
             parser.setInput(new BufferedInputStream(new ByteArrayInputStream(data)), null);
             parser.nextTag();
-            result = xml.readFromXml(parser);
+            result = xml.readFromXml(parser, 0, context);
         }
 
         Log.d(self, "result = " + result);
@@ -140,7 +155,8 @@
 
     public void testPhoneAccountHandle() throws Exception {
         PhoneAccountHandle input = new PhoneAccountHandle(new ComponentName("pkg0", "cls0"), "id0");
-        PhoneAccountHandle result = roundTrip(this, input, PhoneAccountRegistrar.sPhoneAccountHandleXml);
+        PhoneAccountHandle result = roundTrip(this, input,
+                PhoneAccountRegistrar.sPhoneAccountHandleXml, mContext);
         assertPhoneAccountHandleEquals(input, result);
         PhoneAccountHandle inputN =
                 new PhoneAccountHandle(
@@ -148,7 +164,8 @@
                                 "pkg0",  // ctor does not allow null
                                 "cls0"), // ctor does not allow null
                         null);
-        PhoneAccountHandle resultN = roundTrip(this, inputN, PhoneAccountRegistrar.sPhoneAccountHandleXml);
+        PhoneAccountHandle resultN = roundTrip(this, inputN,
+                PhoneAccountRegistrar.sPhoneAccountHandleXml, mContext);
         Log.i(this, "inputN = %s, resultN = %s", inputN, resultN);
         assertPhoneAccountHandleEquals(inputN, resultN);
     }
@@ -162,15 +179,18 @@
             assertEquals(a.getIconResId(), b.getIconResId());
             assertEquals(a.getLabel(), b.getLabel());
             assertEquals(a.getShortDescription(), b.getShortDescription());
+            assertEquals(a.getSupportedUriSchemes(), b.getSupportedUriSchemes());
         }
     }
 
     public void testPhoneAccount() throws Exception {
         PhoneAccount input = makeQuickAccount("pkg0", "cls0", "id0", 0);
-        PhoneAccount result = roundTrip(this, input, PhoneAccountRegistrar.sPhoneAccountXml);
+        PhoneAccount result = roundTrip(this, input, PhoneAccountRegistrar.sPhoneAccountXml,
+                mContext);
         assertPhoneAccountEquals(input, result);
         PhoneAccount inputN = PhoneAccount.builder().build();
-        PhoneAccount resultN = roundTrip(this, inputN, PhoneAccountRegistrar.sPhoneAccountXml);
+        PhoneAccount resultN = roundTrip(this, inputN, PhoneAccountRegistrar.sPhoneAccountXml,
+                mContext);
         assertPhoneAccountEquals(inputN, resultN);
     }
 
@@ -185,7 +205,8 @@
 
     public void testState() throws Exception {
         PhoneAccountRegistrar.State input = makeQuickState();
-        PhoneAccountRegistrar.State result = roundTrip(this, input, PhoneAccountRegistrar.sStateXml);
+        PhoneAccountRegistrar.State result = roundTrip(this, input, PhoneAccountRegistrar.sStateXml,
+                mContext);
         assertStateEquals(input, result);
     }
 
@@ -193,7 +214,7 @@
         assertEquals(4, mRegistrar.getAllPhoneAccountHandles().size());
         assertEquals(3, mRegistrar.getOutgoingPhoneAccounts().size());
         assertEquals(null, mRegistrar.getSimCallManager());
-        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
     }
 
     public void testSimCallManager() throws Exception {
@@ -219,23 +240,23 @@
 
     public void testDefaultOutgoing() {
         // Establish initial conditions
-        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         PhoneAccountHandle h = new PhoneAccountHandle(new ComponentName("pkg0", "cls0"), "id1");
         mRegistrar.setDefaultOutgoingPhoneAccount(h);
-        assertPhoneAccountHandleEquals(h, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertPhoneAccountHandleEquals(h, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         // If account is un-registered, querying returns null
         mRegistrar.unregisterPhoneAccount(h);
-        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         // But if account is re-registered, setting comes back
         mRegistrar.registerPhoneAccount(makeQuickAccount("pkg0", "cls0", "id1", 99));
-        assertPhoneAccountHandleEquals(h, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertPhoneAccountHandleEquals(h, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         // De-register by setting to null
         mRegistrar.setDefaultOutgoingPhoneAccount(null);
-        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         // If argument not have CALL_PROVIDER capability, this is a no-op
         mRegistrar.setDefaultOutgoingPhoneAccount(
                 new PhoneAccountHandle(new ComponentName("pkg0", "cls0"), "id0"));
-        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         // If only have one account, it is the default
         mRegistrar.unregisterPhoneAccount(
                 new PhoneAccountHandle(new ComponentName("pkg0", "cls0"), "id0"));
@@ -245,7 +266,7 @@
                 new PhoneAccountHandle(new ComponentName("pkg1", "cls1"), "id2"));
         assertPhoneAccountHandleEquals(
                 new PhoneAccountHandle(new ComponentName("pkg1", "cls1"), "id3"),
-                mRegistrar.getDefaultOutgoingPhoneAccount());
+                mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
         // If have one account but not suitable, default returns null
         mRegistrar.unregisterPhoneAccount(
                 new PhoneAccountHandle(new ComponentName("pkg1", "cls1"), "id3"));
@@ -258,7 +279,7 @@
                 .withLabel("label0")
                 .withShortDescription("desc0")
                 .build());
-        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount());
+        assertEquals(null, mRegistrar.getDefaultOutgoingPhoneAccount("tel"));
     }
 
     private static PhoneAccount makeQuickAccount(String pkg, String cls, String id, int idx) {