Merge remote-tracking branch 'origin/LA.QSSI.11.0.R1.11.00.00.668.170.00' into fp4t-target-0414
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index c51aeba..46c73ea 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -133,6 +133,7 @@
     public static final String FILE_NAME = "phone-account-registrar-state.xml";
     @VisibleForTesting
     public static final int EXPECTED_STATE_VERSION = 9;
+    public static final int MAX_PHONE_ACCOUNT_REGISTRATIONS = 10;
 
     /** Keep in sync with the same in SipSettings.java */
     private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
@@ -770,8 +771,13 @@
         return account.isSelfManaged();
     }
 
-    // TODO: Should we implement an artificial limit for # of accounts associated with a single
-    // ComponentName?
+    /**
+     * Performs checks before calling addOrReplacePhoneAccount(PhoneAccount)
+     *
+     * @param account The {@code PhoneAccount} to add or replace.
+     * @throws SecurityException if package does not have BIND_TELECOM_CONNECTION_SERVICE permission
+     * @throws IllegalArgumentException if MAX_PHONE_ACCOUNT_REGISTRATIONS are reached
+     */
     public void registerPhoneAccount(PhoneAccount account) {
         // Enforce the requirement that a connection service for a phone account has the correct
         // permission.
@@ -782,6 +788,19 @@
             throw new SecurityException("PhoneAccount connection service requires "
                     + "BIND_TELECOM_CONNECTION_SERVICE permission.");
         }
+        //Enforce an upper bound on the number of PhoneAccount's a package can register.
+        // Most apps should only require 1-2.
+        if (getPhoneAccountsForPackage(
+                account.getAccountHandle().getComponentName().getPackageName(),
+                account.getAccountHandle().getUserHandle()).size()
+                >= MAX_PHONE_ACCOUNT_REGISTRATIONS) {
+            Log.w(this, "Phone account %s reached max registration limit for package",
+                    account.getAccountHandle());
+            throw new IllegalArgumentException(
+                    "Error, cannot register phone account " + account.getAccountHandle()
+                            + " because the limit, " + MAX_PHONE_ACCOUNT_REGISTRATIONS
+                            + ", has been reached");
+        }
 
         addOrReplacePhoneAccount(account);
     }
diff --git a/src/com/android/server/telecom/ServiceBinder.java b/src/com/android/server/telecom/ServiceBinder.java
index aa2e2a2..7274993 100644
--- a/src/com/android/server/telecom/ServiceBinder.java
+++ b/src/com/android/server/telecom/ServiceBinder.java
@@ -150,7 +150,6 @@
                     Log.i(this, "Service bound %s", componentName);
 
                     Log.addEvent(mCall, LogUtils.Events.CS_BOUND, componentName);
-                    mCall = null;
 
                     // Unbind request was queued so unbind immediately.
                     if (mIsBindingAborted) {
@@ -192,6 +191,30 @@
                 Log.endSession();
             }
         }
+
+        /**
+         * Handles the case where the {@link ConnectionService} we bound to returned a null binding.
+         * We want to unbind from the service and cleanup and call resources at this time.
+         * @param componentName The component of the {@link ConnectionService}.
+         */
+        @Override
+        public void onNullBinding(ComponentName componentName) {
+            try {
+                Log.startSession("SBC.oNB");
+                synchronized (mLock) {
+                    Log.w(this, "Null binding %s", componentName);
+                    Log.addEvent(mCall, "NULL_BINDING", componentName);
+                    String componentStr = componentName == null ? "null" : componentName.toString();
+                    android.util.EventLog.writeEvent(0x534e4554, "211114016", -1, componentStr);
+                    logServiceDisconnected("onNullBinding");
+                    mContext.unbindService(this);
+                    clearAbort();
+                    handleFailedConnection();
+                }
+            } finally {
+                Log.endSession();
+            }
+        }
     }
 
     private void handleDisconnect() {