Read the default connection manager from resources

Allow the default connection manager to be set in config.xml so that
it can be customized with a vendor overlay.

Change-Id: I3db83d246efbbd6f9d145f45b641f85dfe3fb33f
diff --git a/res/values/config.xml b/res/values/config.xml
index 365e758..6c6f215 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -38,4 +38,9 @@
 
     <!-- Flag indicating if the tty is enabled -->
     <bool name="tty_enabled">false</bool>
+
+    <!-- ComponentName for the default connection manager component. If this cannot be resolved, or
+         if this package has not registered any accounts, then it will be ignored.
+         [DO NOT TRANSLATE] -->
+    <string name="default_connection_manager_component" translatable="false"></string>
 </resources>
diff --git a/src/com/android/telecomm/PhoneAccountRegistrar.java b/src/com/android/telecomm/PhoneAccountRegistrar.java
index 09ff547..c9e0e46 100644
--- a/src/com/android/telecomm/PhoneAccountRegistrar.java
+++ b/src/com/android/telecomm/PhoneAccountRegistrar.java
@@ -16,12 +16,18 @@
 
 package com.android.telecomm;
 
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.res.Resources;
+import android.telecomm.ConnectionService;
 import android.telecomm.PhoneAccount;
 import android.telecomm.PhoneAccountHandle;
 import android.content.ComponentName;
 import android.content.Context;
 import android.net.Uri;
 import android.telecomm.TelecommManager;
+import android.text.TextUtils;
 import android.util.AtomicFile;
 import android.util.Xml;
 
@@ -29,6 +35,10 @@
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.XmlUtils;
 
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlSerializer;
+
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
@@ -41,10 +51,6 @@
 import java.util.Objects;
 import java.util.concurrent.CopyOnWriteArrayList;
 
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-import org.xmlpull.v1.XmlSerializer;
-
 /**
  * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
  * delegate for all the account handling methods on {@link TelecommManager} as implemented in
@@ -163,6 +169,36 @@
                 }
             }
         }
+
+        // See if the OEM has specified a default one.
+        Context context = TelecommApp.getInstance();
+        String defaultConnectionMgr =
+                context.getResources().getString(R.string.default_connection_manager_component);
+        if (!TextUtils.isEmpty(defaultConnectionMgr)) {
+            PackageManager pm = context.getPackageManager();
+
+            ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
+            Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
+            intent.setComponent(componentName);
+
+            // Make sure that the component can be resolved.
+            List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0);
+            if (!resolveInfos.isEmpty()) {
+                // See if there is registered PhoneAccount by this component.
+                List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
+                for (PhoneAccountHandle handle : handles) {
+                    if (componentName.equals(handle.getComponentName())) {
+                        return handle;
+                    }
+                }
+                Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
+            } else {
+                Log.d(this, "%s could not be resolved; not using as default", componentName);
+            }
+        } else {
+            Log.v(this, "No default connection manager specified");
+        }
+
         return null;
     }