Add work profile support for registering phone accounts with Telecom

When registering a phone account for a user not associated with the
phone process, we want to grab the user associated with the
phone subscription. Currently, we enable MULTI_USER_CAPABILITY for all
phone accounts; we should disable this when registering another user.
We will also need the INTERACT_ACROSS_USERS permission to perform
actions across users (other than the primary).

Bug: 255342474
Test: PhoneUtilsTest.java
Change-Id: I78c2e879f0890cef09c3e95045bada4c5bbf1a01
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index d0aad4a..5f14387 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -31,6 +31,7 @@
 import android.os.Handler;
 import android.os.Message;
 import android.os.PersistableBundle;
+import android.os.UserHandle;
 import android.os.VibrationEffect;
 import android.os.Vibrator;
 import android.telecom.PhoneAccount;
@@ -701,31 +702,28 @@
         Log.d(LOG_TAG, msg);
     }
 
-    public static PhoneAccountHandle makePstnPhoneAccountHandle(String id) {
-        return makePstnPhoneAccountHandleWithPrefix(id, "", false);
-    }
-
-    public static PhoneAccountHandle makePstnPhoneAccountHandle(int phoneId) {
-        return makePstnPhoneAccountHandle(PhoneFactory.getPhone(phoneId));
-    }
-
     public static PhoneAccountHandle makePstnPhoneAccountHandle(Phone phone) {
-        return makePstnPhoneAccountHandleWithPrefix(phone, "", false);
+        return makePstnPhoneAccountHandleWithPrefix(phone, "",
+                false, phone.getUserHandle());
     }
 
     public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
-            Phone phone, String prefix, boolean isEmergency) {
+            Phone phone, String prefix, boolean isEmergency, UserHandle userHandle) {
         // TODO: Should use some sort of special hidden flag to decorate this account as
         // an emergency-only account
         String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix +
                 String.valueOf(phone.getSubId());
-        return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency);
+        return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency, userHandle);
     }
 
     public static PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(
-            String id, String prefix, boolean isEmergency) {
+            String id, String prefix, boolean isEmergency, UserHandle userHandle) {
         ComponentName pstnConnectionServiceName = getPstnConnectionServiceName();
-        return new PhoneAccountHandle(pstnConnectionServiceName, id);
+        // If user handle is null, resort to default constructor to use phone process's
+        // user handle
+        return userHandle == null
+                ? new PhoneAccountHandle(pstnConnectionServiceName, id)
+                : new PhoneAccountHandle(pstnConnectionServiceName, id, userHandle);
     }
 
     public static int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
diff --git a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
index 49e1379..b6aaebe 100644
--- a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
+++ b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
@@ -387,7 +387,7 @@
 
     private PhoneAccountHandle getEmergencyPhoneAccount() {
         return PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
-                (Phone) null, "" /* prefix */, true /* isEmergency */);
+                (Phone) null, "" /* prefix */, true /* isEmergency */, null /* userHandle */);
     }
 
     public static Intent buildPhoneAccountConfigureIntent(
diff --git a/src/com/android/services/telephony/PstnIncomingCallNotifier.java b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
index 8615325..f78a9b9 100644
--- a/src/com/android/services/telephony/PstnIncomingCallNotifier.java
+++ b/src/com/android/services/telephony/PstnIncomingCallNotifier.java
@@ -403,7 +403,8 @@
         // receives an MT call while in ECM. Use the Emergency PhoneAccount to receive the account
         // if it exists.
         PhoneAccountHandle emergencyHandle =
-                PhoneUtils.makePstnPhoneAccountHandleWithPrefix(mPhone, "", true);
+                PhoneUtils.makePstnPhoneAccountHandleWithPrefix(mPhone,
+                        "", true, mPhone.getUserHandle());
         if(telecomAccountRegistry.hasAccountEntryForPhoneAccount(emergencyHandle)) {
             Log.i(this, "Receiving MT call in ECM. Using Emergency PhoneAccount Instead.");
             return emergencyHandle;
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index 3d2fb85..10f1f86 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -285,13 +285,18 @@
         private PhoneAccount buildPstnPhoneAccount(boolean isEmergency, boolean isTestAccount) {
             String testPrefix = isTestAccount ? "Test " : "";
 
+            // Check if we are registering another user. If we are, ensure that the account
+            // is registered to that user handle.
+            int subId = mPhone.getSubId();
+            // Get user handle from phone's sub id (if we get null, then system user will be used)
+            UserHandle userToRegister = mPhone.getUserHandle();
+
             // Build the Phone account handle.
             PhoneAccountHandle phoneAccountHandle =
                     PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
-                            mPhone, testPrefix, isEmergency);
+                            mPhone, testPrefix, isEmergency, userToRegister);
 
             // Populate the phone account data.
-            int subId = mPhone.getSubId();
             String subscriberId = mPhone.getSubscriberId();
             int color = PhoneAccount.NO_HIGHLIGHT_COLOR;
             int slotId = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
@@ -357,8 +362,12 @@
 
             // By default all SIM phone accounts can place emergency calls.
             int capabilities = PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION |
-                    PhoneAccount.CAPABILITY_CALL_PROVIDER |
-                    PhoneAccount.CAPABILITY_MULTI_USER;
+                    PhoneAccount.CAPABILITY_CALL_PROVIDER;
+
+            // This is enabled by default. To support work profiles, it should not be enabled.
+            if (userToRegister == null) {
+                capabilities |= PhoneAccount.CAPABILITY_MULTI_USER;
+            }
 
             if (mContext.getResources().getBoolean(R.bool.config_pstnCanPlaceEmergencyCalls)) {
                 capabilities |= PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS;
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index cb1277f..ef77483 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -368,7 +368,8 @@
         @Override
         public PhoneAccountHandle makePstnPhoneAccountHandleWithPrefix(Phone phone, String prefix,
                 boolean isEmergency) {
-            return PhoneUtils.makePstnPhoneAccountHandleWithPrefix(phone, prefix, isEmergency);
+            return PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
+                    phone, prefix, isEmergency, phone.getUserHandle());
         }
     };