Use setCommunicationDevice to connect hearing aid BT devices. am: 3d7ee3709b

Original change: https://android-review.googlesource.com/c/platform/packages/services/Telecomm/+/2119548

Change-Id: Ib62b91956e48f362981fa41813ecfc8cc7ce4cc4
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/res/values-eu/strings.xml b/res/values-eu/strings.xml
index 92f088c..120dd6e 100644
--- a/res/values-eu/strings.xml
+++ b/res/values-eu/strings.xml
@@ -81,13 +81,13 @@
     <string name="blocked_numbers_number_already_blocked_message" msgid="2301270825735665458">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> blokeatuta dago dagoeneko."</string>
     <string name="toast_personal_call_msg" msgid="5817631570381795610">"Telefono pertsonala erabiltzen ari zara deia egiteko"</string>
     <string name="notification_incoming_call" msgid="1233481138362230894">"<xliff:g id="CALL_VIA">%1$s</xliff:g> deia (deitzailea: <xliff:g id="CALL_FROM">%2$s</xliff:g>)"</string>
-    <string name="notification_incoming_video_call" msgid="5795968314037063900">"<xliff:g id="CALL_VIA">%1$s</xliff:g> bideo-deia (deitzailea: <xliff:g id="CALL_FROM">%2$s</xliff:g>)"</string>
+    <string name="notification_incoming_video_call" msgid="5795968314037063900">"<xliff:g id="CALL_VIA">%1$s</xliff:g> bideodeia (deitzailea: <xliff:g id="CALL_FROM">%2$s</xliff:g>)"</string>
     <string name="answering_ends_other_call" msgid="8653544281903986641">"Erantzuten baduzu, amaitu egingo da <xliff:g id="CALL_VIA">%1$s</xliff:g> deia"</string>
     <string name="answering_ends_other_calls" msgid="3702302838456922535">"Erantzuten baduzu, amaitu egingo dira <xliff:g id="CALL_VIA">%1$s</xliff:g> deiak"</string>
-    <string name="answering_ends_other_video_call" msgid="8572022039304239958">"Erantzuten baduzu, amaitu egingo da <xliff:g id="CALL_VIA">%1$s</xliff:g> bideo-deia"</string>
+    <string name="answering_ends_other_video_call" msgid="8572022039304239958">"Erantzuten baduzu, amaitu egingo da <xliff:g id="CALL_VIA">%1$s</xliff:g> bideodeia"</string>
     <string name="answering_ends_other_managed_call" msgid="4031778317409881805">"Erantzuten baduzu, amaitu egingo da oraingo deia"</string>
     <string name="answering_ends_other_managed_calls" msgid="3974069768615307659">"Erantzuten baduzu, amaitu egingo dira oraingo deiak"</string>
-    <string name="answering_ends_other_managed_video_call" msgid="1988508241432031327">"Erantzuten baduzu, amaitu egingo da oraingo bideo-deia"</string>
+    <string name="answering_ends_other_managed_video_call" msgid="1988508241432031327">"Erantzuten baduzu, amaitu egingo da oraingo bideodeia"</string>
     <string name="answer_incoming_call" msgid="2045888814782215326">"Erantzun"</string>
     <string name="decline_incoming_call" msgid="922147089348451310">"Baztertu"</string>
     <string name="cant_call_due_to_no_supported_service" msgid="1635626384149947077">"Ezin da egin deia, ez dagoelako mota honetako deiak onartzen duen deiak egiteko konturik."</string>
diff --git a/res/values-hy/strings.xml b/res/values-hy/strings.xml
index 43c0568..f511fcd 100644
--- a/res/values-hy/strings.xml
+++ b/res/values-hy/strings.xml
@@ -79,7 +79,7 @@
     <string name="blocked_numbers_number_unblocked_message" msgid="2933071624674945601">"<xliff:g id="UNBLOCKED_NUMBER">%1$s</xliff:g> արգելահանվեց"</string>
     <string name="blocked_numbers_block_emergency_number_message" msgid="4198550501500893890">"Արտակարգ իրավիճակների հեռախոսահամարը հնարավոր չէ արգելափակել:"</string>
     <string name="blocked_numbers_number_already_blocked_message" msgid="2301270825735665458">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> համարն արդեն արգելափակված է:"</string>
-    <string name="toast_personal_call_msg" msgid="5817631570381795610">"Զանգելու նպատակով անհատական համարհավաքիչի օգտագործում"</string>
+    <string name="toast_personal_call_msg" msgid="5817631570381795610">"Զանգելու նպատակով անհատական համարահավաքիչի օգտագործում"</string>
     <string name="notification_incoming_call" msgid="1233481138362230894">"<xliff:g id="CALL_VIA">%1$s</xliff:g>-ի զանգ՝ <xliff:g id="CALL_FROM">%2$s</xliff:g>-ից"</string>
     <string name="notification_incoming_video_call" msgid="5795968314037063900">"<xliff:g id="CALL_VIA">%1$s</xliff:g>-ի տեսազանգ՝ <xliff:g id="CALL_FROM">%2$s</xliff:g>-ից"</string>
     <string name="answering_ends_other_call" msgid="8653544281903986641">"Եթե պատասխանեք այս զանգին, <xliff:g id="CALL_VIA">%1$s</xliff:g>-ի ընթացիկ զանգը կընդհատվի"</string>
diff --git a/res/values-ja/strings.xml b/res/values-ja/strings.xml
index 2b000bb..50956bf 100644
--- a/res/values-ja/strings.xml
+++ b/res/values-ja/strings.xml
@@ -24,7 +24,7 @@
     <string name="notification_missedCallsTitle" msgid="3910479625507893809">"不在着信"</string>
     <string name="notification_missedCallsMsg" msgid="5055782736170916682">"不在着信<xliff:g id="NUM_MISSED_CALLS">%s</xliff:g>件"</string>
     <string name="notification_missedCallTicker" msgid="6731461957487087769">"<xliff:g id="MISSED_CALL_FROM">%s</xliff:g>さんからの不在着信"</string>
-    <string name="notification_missedCall_call_back" msgid="7900333283939789732">"コールバック"</string>
+    <string name="notification_missedCall_call_back" msgid="7900333283939789732">"かけ直す"</string>
     <string name="notification_missedCall_message" msgid="4054698824390076431">"メッセージ"</string>
     <string name="notification_disconnectedCall_title" msgid="1790131923692416928">"通話が切断されました"</string>
     <string name="notification_disconnectedCall_body" msgid="600491714584417536">"緊急通報番号宛に発信中のため、<xliff:g id="CALLER">%s</xliff:g> さんとの通話が切断されました。"</string>
diff --git a/res/values-mr/strings.xml b/res/values-mr/strings.xml
index d4ddf6d..1def59a 100644
--- a/res/values-mr/strings.xml
+++ b/res/values-mr/strings.xml
@@ -33,7 +33,7 @@
     <string name="notification_audioProcessing_body" msgid="6397005913770420388">"<xliff:g id="AUDIO_PROCESSING_APP_NAME">%s</xliff:g> यांनी कॉल बॅकग्राउंडवर ठेवला आहे हे अ‍ॅप कदाचित कॉलद्वारे ऑडिओ ॲक्सेस आणि प्ले करत आहे."</string>
     <string name="notification_incallservice_not_responding_title" msgid="5347557574288598548">"<xliff:g id="IN_CALL_SERVICE_APP_NAME">%s</xliff:g> ने प्रतिसाद देणे थांबवले आहे"</string>
     <string name="notification_incallservice_not_responding_body" msgid="9209308270131968623">"तुमच्या कॉलने डिव्हाइससोबत दिलेले फोन अ‍ॅप वापरले आहे"</string>
-    <string name="accessibility_call_muted" msgid="2968461092554300779">"कॉल नि.शब्‍द केला."</string>
+    <string name="accessibility_call_muted" msgid="2968461092554300779">"कॉल म्यूट केला."</string>
     <string name="accessibility_speakerphone_enabled" msgid="555386652061614267">"स्‍पीकरफोन सक्षम केला."</string>
     <string name="respond_via_sms_canned_response_1" msgid="6332561460870382561">"आत्ता बोलू शकत नाही. कशासाठी कॉल केला होता?"</string>
     <string name="respond_via_sms_canned_response_2" msgid="2052951316129952406">"मी तुम्हाला परत कॉल करेन."</string>
@@ -70,15 +70,15 @@
     <string name="add_blocked_dialog_body" msgid="8599974422407139255">"यावरील कॉल आणि एसएमएस ब्लॉक करा"</string>
     <string name="add_blocked_number_hint" msgid="8769422085658041097">"फोन नंबर"</string>
     <string name="block_button" msgid="485080149164258770">"ब्लॉक करा"</string>
-    <string name="non_primary_user" msgid="315564589279622098">"फक्त डिव्हाइस मालक अवरोधित केलेले नंबर पाहू आणि व्यवस्थापित करू शकतो."</string>
+    <string name="non_primary_user" msgid="315564589279622098">"फक्त डिव्हाइस मालक ब्लॉक केलेले नंबर पाहू आणि व्यवस्थापित करू शकतो."</string>
     <string name="delete_icon_description" msgid="5335959254954774373">"ब्लॉक करा"</string>
-    <string name="blocked_numbers_butter_bar_title" msgid="582982373755950791">"अवरोधित करणे तात्पुरते बंद आहे"</string>
-    <string name="blocked_numbers_butter_bar_body" msgid="1261213114919301485">"तुम्ही एखादा आणीबाणी नंबर डायल केला किंवा त्यावर मजकूर पाठविल्यानंतर, आणीबाणी सेवा आपल्याशी संपर्क साधू शकतात हे सुनिश्चित करण्यासाठी अवरोधित करणे बंद करते."</string>
+    <string name="blocked_numbers_butter_bar_title" msgid="582982373755950791">"ब्लॉक करणे तात्पुरते बंद आहे"</string>
+    <string name="blocked_numbers_butter_bar_body" msgid="1261213114919301485">"तुम्ही एखादा आणीबाणी नंबर डायल केला किंवा त्यावर मेसेज पाठवल्यानंतर, आणीबाणी सेवा तुमच्याशी संपर्क साधू शकतात याची खात्री करण्यासाठी ब्लॉक करणे बंद केले जाते."</string>
     <string name="blocked_numbers_butter_bar_button" msgid="2704456308072489793">"आता पुन्हा-सक्षम करा"</string>
-    <string name="blocked_numbers_number_blocked_message" msgid="4314736791180919167">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> अवरोधित केला"</string>
+    <string name="blocked_numbers_number_blocked_message" msgid="4314736791180919167">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> ब्लॉक केला"</string>
     <string name="blocked_numbers_number_unblocked_message" msgid="2933071624674945601">"<xliff:g id="UNBLOCKED_NUMBER">%1$s</xliff:g> अनब्लॉक केला"</string>
-    <string name="blocked_numbers_block_emergency_number_message" msgid="4198550501500893890">"आणीबाणी नंबर अवरोधित करण्यात अक्षम."</string>
-    <string name="blocked_numbers_number_already_blocked_message" msgid="2301270825735665458">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> आधीपासून अवरोधित केला आहे."</string>
+    <string name="blocked_numbers_block_emergency_number_message" msgid="4198550501500893890">"आणीबाणी नंबर ब्लॉक करता आला नाही."</string>
+    <string name="blocked_numbers_number_already_blocked_message" msgid="2301270825735665458">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> आधीपासून ब्लॉक केला आहे."</string>
     <string name="toast_personal_call_msg" msgid="5817631570381795610">"कॉल करण्यासाठी वैयक्तिक डायलर वापरणे"</string>
     <string name="notification_incoming_call" msgid="1233481138362230894">"<xliff:g id="CALL_FROM">%2$s</xliff:g> कडील <xliff:g id="CALL_VIA">%1$s</xliff:g> मधील कॉल"</string>
     <string name="notification_incoming_video_call" msgid="5795968314037063900">"<xliff:g id="CALL_FROM">%2$s</xliff:g> कडील <xliff:g id="CALL_VIA">%1$s</xliff:g> मधील व्हिडिओ कॉल"</string>
diff --git a/res/values-ne/strings.xml b/res/values-ne/strings.xml
index 9eae7a0..8741730 100644
--- a/res/values-ne/strings.xml
+++ b/res/values-ne/strings.xml
@@ -74,7 +74,7 @@
     <string name="delete_icon_description" msgid="5335959254954774373">"अनब्लक गर्नुहोस्"</string>
     <string name="blocked_numbers_butter_bar_title" msgid="582982373755950791">"रोक लगाउने काम अस्थायी रूपमा निष्क्रिय छ"</string>
     <string name="blocked_numbers_butter_bar_body" msgid="1261213114919301485">"तपाईँले आपत्‌कालीन नम्बरमा डायल गरेपछि वा टेक्स्ट म्यासेज पठाएपछि आपत्‌कालीन सेवाहरूले तपाईँलाई सम्पर्क गर्न सकून् भन्ने कुरा सुनिश्चित गर्न कलमाथिको अवरोध निष्क्रिय गरिन्छ।"</string>
-    <string name="blocked_numbers_butter_bar_button" msgid="2704456308072489793">"अब पुन:-सक्रिय गर्नुहोस्"</string>
+    <string name="blocked_numbers_butter_bar_button" msgid="2704456308072489793">"अब पुन:-अन गर्नुहोस्"</string>
     <string name="blocked_numbers_number_blocked_message" msgid="4314736791180919167">"<xliff:g id="BLOCKED_NUMBER">%1$s</xliff:g> माथि रोक लगाइयो"</string>
     <string name="blocked_numbers_number_unblocked_message" msgid="2933071624674945601">"<xliff:g id="UNBLOCKED_NUMBER">%1$s</xliff:g> माथिको रोक हटाइयो"</string>
     <string name="blocked_numbers_block_emergency_number_message" msgid="4198550501500893890">"आपत्‌कालीन नम्बरमाथि रोक लगाउन सकिएन।"</string>
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 06d8fdc..766cb12 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -947,6 +947,15 @@
         }
     };
 
+    private final BroadcastReceiver mUserAddedReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) {
+                restrictPhoneCallOps();
+            }
+        }
+    };
+
     private final SystemStateListener mSystemStateListener = new SystemStateListener() {
         @Override
         public void onCarModeChanged(int priority, String packageName, boolean isCarMode) {
@@ -1055,6 +1064,7 @@
         mSystemStateHelper.addListener(mSystemStateListener);
         mClockProxy = clockProxy;
         restrictPhoneCallOps();
+        mContext.registerReceiver(mUserAddedReceiver, new IntentFilter(Intent.ACTION_USER_ADDED));
     }
 
     private void restrictPhoneCallOps() {
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index bce6e43..d8f2d22 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -134,6 +134,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";
@@ -766,8 +767,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.
@@ -778,6 +784,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);
     }