AppSharing: Better handling of Work Profiles

This change improves the Work Profile use case in two ways:
1. When sharing is disabled for the profile, the Share App button is grayed out, and an appropriate message is displayed if the user taps it.
2. When sharing is enabled for the profile, it actually works.

Bug: 175159046
Bug: 210168826
Test: Manual (toggled DISALLOW_BLUETOOTH_SHARING via Test DPC on local device)
Test: m -j RunLauncherGoGoogleRoboTests ROBOTEST_FILTER=com.android.launcher3.AppSharingTest
Change-Id: Id7ba8efc587d0b94aa1f9b2004bf45254b39f992
diff --git a/go/quickstep/src/com/android/launcher3/AppSharing.java b/go/quickstep/src/com/android/launcher3/AppSharing.java
index 7c66f6e..c252fba 100644
--- a/go/quickstep/src/com/android/launcher3/AppSharing.java
+++ b/go/quickstep/src/com/android/launcher3/AppSharing.java
@@ -22,6 +22,9 @@
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.net.Uri;
+import android.os.Process;
+import android.os.UserHandle;
+import android.os.UserManager;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
@@ -98,14 +101,19 @@
      * The Share App system shortcut, used to initiate p2p sharing of a given app
      */
     public final class Share extends SystemShortcut<Launcher> {
-        private PopupDataProvider mPopupDataProvider;
+        private final PopupDataProvider mPopupDataProvider;
+        private final boolean mSharingEnabledForUser;
 
         public Share(Launcher target, ItemInfo itemInfo) {
             super(R.drawable.ic_share, R.string.app_share_drop_target_label, target, itemInfo);
             mPopupDataProvider = target.getPopupDataProvider();
 
-            if (ENABLE_SHAREABILITY_CHECK) {
-                mShareabilityMgr = AppShareabilityManager.INSTANCE.get(target);
+            mSharingEnabledForUser = bluetoothSharingEnabled(target);
+            if (!mSharingEnabledForUser) {
+                setEnabled(false);
+            } else if (ENABLE_SHAREABILITY_CHECK) {
+                mShareabilityMgr =
+                        AppShareabilityManager.INSTANCE.get(target.getApplicationContext());
                 checkShareability(/* requestUpdateIfUnknown */ true);
             }
         }
@@ -144,7 +152,12 @@
             sendIntent.setType(APP_MIME_TYPE);
             sendIntent.setComponent(ComponentName.unflattenFromString(mSharingComponent));
 
-            mTarget.startActivitySafely(view, sendIntent, mItemInfo);
+            UserHandle user = mItemInfo.user;
+            if (user != null && !user.equals(Process.myUserHandle())) {
+                mTarget.startActivityAsUser(sendIntent, user);
+            } else {
+                mTarget.startActivitySafely(view, sendIntent, mItemInfo);
+            }
 
             AbstractFloatingView.closeAllOpenViews(mTarget);
         }
@@ -170,8 +183,15 @@
             }
         }
 
+        private boolean bluetoothSharingEnabled(Context context) {
+            return !context.getSystemService(UserManager.class)
+                    .hasUserRestriction(UserManager.DISALLOW_BLUETOOTH_SHARING, mItemInfo.user);
+        }
+
         private void showCannotShareToast(Context context) {
-            CharSequence text = context.getText(R.string.toast_p2p_app_not_shareable);
+            CharSequence text = (mSharingEnabledForUser)
+                    ? context.getText(R.string.toast_p2p_app_not_shareable)
+                    : context.getText(R.string.blocked_by_policy);
             int duration = Toast.LENGTH_SHORT;
             Toast.makeText(context, text, duration).show();
         }
diff --git a/go/quickstep/src/com/android/launcher3/model/AppShareabilityManager.java b/go/quickstep/src/com/android/launcher3/model/AppShareabilityManager.java
index cf80c35..0d0f700 100644
--- a/go/quickstep/src/com/android/launcher3/model/AppShareabilityManager.java
+++ b/go/quickstep/src/com/android/launcher3/model/AppShareabilityManager.java
@@ -47,7 +47,7 @@
  * Each app's status is retrieved from the Play Store's API. Statuses are cached in order
  * to limit extraneous calls to that API (which can be time-consuming).
  */
-public final class AppShareabilityManager {
+public class AppShareabilityManager {
     @Retention(SOURCE)
     @IntDef({
         ShareabilityStatus.UNKNOWN,