UX to switch to work profile for work sim

Guide users to switch to work profile app for sending sms/mms,
if subscription used to send sms/mms is assocaited with work profile.

Bug: 258629881
Test: Manual
Change-Id: Ia172e58f8fdb88334e7b4f55797ff40a61a71809
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 32733cc..22d007e 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -573,6 +573,14 @@
             android:configChanges="orientation|screenSize|keyboardHidden"
             android:theme="@style/Theme.Transparent"/>
 
+        <activity
+            android:name="com.android.phone.ErrorDialogActivity"
+            android:exported="false"
+            android:excludeFromRecents="true"
+            android:launchMode="singleTop"
+            android:configChanges="orientation|screenSize|keyboardHidden"
+            android:theme="@style/Theme.Telephony.Transparent"/>
+
         <service
             android:name="com.android.phone.vvm.RemoteVvmTaskManager"
             android:exported="false"/>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 79119a3..e21f761 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2203,4 +2203,10 @@
     <!-- Telephony notification channel name for a channel containing SIP accounts removed
      notificatios -->
     <string name="notification_channel_sip_account">Deprecated SIP accounts</string>
+
+    <string name="send_from_work_profile_title">Can\'t send message from this profile</string>
+    <string name="send_from_work_profile_description">Your work policy allows you to send message only from the work profile</string>
+    <string name="send_from_work_profile_cancel">Cancel</string>
+    <string name="send_from_work_profile_action_str">Switch to work profile</string>
+
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index d0f427c..19798f0 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -309,6 +309,17 @@
         <item name="android:backgroundDimEnabled">false</item>
     </style>
 
+    <style name="Theme.Telephony.Transparent" parent="@android:style/Theme.DeviceDefault.Light">
+        <item name="android:forceDarkAllowed">true</item>
+        <item name="android:windowIsTranslucent">true</item>
+        <item name="android:windowBackground">@android:color/transparent</item>
+        <item name="android:windowContentOverlay">@null</item>
+        <item name="android:windowNoTitle">true</item>
+        <item name="android:windowIsFloating">true</item>
+        <item name="android:backgroundDimEnabled">true</item>
+        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
+    </style>
+
     <style name="CallSettingsWithoutDividerTheme" parent="DialerSettingsLight">
         <item name="android:listDivider">@null</item>
     </style>
diff --git a/src/com/android/phone/ErrorDialogActivity.java b/src/com/android/phone/ErrorDialogActivity.java
new file mode 100644
index 0000000..11a92a8
--- /dev/null
+++ b/src/com/android/phone/ErrorDialogActivity.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.phone;
+
+import android.app.Activity;
+import android.app.ActivityOptions;
+import android.app.AlertDialog;
+import android.app.role.RoleManager;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.UserInfo;
+import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.util.Log;
+
+import java.util.List;
+
+/** Used to display an error dialog from Telephony service. */
+public class ErrorDialogActivity extends Activity {
+
+    private static final String TAG = ErrorDialogActivity.class.getSimpleName();
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        getWindow()
+                .addSystemFlags(
+                        android.view.WindowManager.LayoutParams
+                                .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
+        showDialog();
+    }
+
+    @Override
+    public void finish() {
+        super.finish();
+        // Don't show the return to previous task animation to avoid showing a black screen.
+        // Just dismiss the dialog and undim the previous activity immediately.
+        overridePendingTransition(0, 0);
+    }
+
+    private void showDialog() {
+        final DialogInterface.OnClickListener listener =
+                new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                        switch (which) {
+                            case DialogInterface.BUTTON_POSITIVE:
+                                switchToManagedProfile();
+                                finish();
+                                break;
+                            case DialogInterface.BUTTON_NEGATIVE:
+                            default:
+                                finish();
+                        }
+                    }
+                };
+
+        new AlertDialog.Builder(this)
+            .setTitle(R.string.send_from_work_profile_title)
+            .setMessage(R.string.send_from_work_profile_description)
+            .setPositiveButton(R.string.send_from_work_profile_action_str, listener)
+            .setNegativeButton(R.string.send_from_work_profile_cancel, listener)
+            .setOnCancelListener(
+                new DialogInterface.OnCancelListener() {
+                    @Override
+                    public void onCancel(DialogInterface dialog) {
+                        finish();
+                    }
+                })
+            .show();
+    }
+
+    private void switchToManagedProfile() {
+        try {
+            int managedProfileUserId =
+                    getManagedProfileUserId(
+                            getApplicationContext(), getApplicationContext().getUserId());
+            if (managedProfileUserId == UserHandle.USER_NULL) {
+                finish();
+            }
+
+            String defaultMessagesAppPackage =
+                    getBaseContext()
+                            .getSystemService(RoleManager.class)
+                            .getSmsRoleHolder(managedProfileUserId);
+
+            Intent sendIntent = new Intent(Intent.ACTION_MAIN);
+            sendIntent.addCategory(Intent.CATEGORY_DEFAULT);
+            sendIntent.addCategory(Intent.CATEGORY_LAUNCHER);
+            sendIntent.addCategory(Intent.CATEGORY_APP_MESSAGING);
+
+            sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+            if (defaultMessagesAppPackage != null) {
+                sendIntent.setPackage(defaultMessagesAppPackage);
+            }
+
+            startActivityAsUser(sendIntent,
+                    ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle(),
+                    UserHandle.of(managedProfileUserId));
+        } catch (Exception e) {
+            Log.e(TAG, "Failed to switch to managed profile.", e);
+        }
+    }
+
+    private static int getManagedProfileUserId(Context context, int userId) {
+        UserManager um = context.getSystemService(UserManager.class);
+        List<UserInfo> userProfiles = um.getProfiles(userId);
+        for (UserInfo uInfo : userProfiles) {
+            if (uInfo.id == userId) {
+                continue;
+            }
+            if (uInfo.isManagedProfile()) {
+                return uInfo.id;
+            }
+        }
+        return UserHandle.USER_NULL;
+    }
+}
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index cc5da54..23abadb 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -10207,6 +10207,16 @@
     }
 
     @Override
+    public void showSwitchToManagedProfileDialog() {
+        enforceModifyPermission();
+
+        Intent intent = new Intent();
+        intent.setClass(mApp, ErrorDialogActivity.class);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mApp.startActivity(intent);
+    }
+
+    @Override
     public String getMmsUAProfUrl(int subId) {
         //TODO investigate if this API should require proper permission check in R b/133791609
         final long identity = Binder.clearCallingIdentity();