Don't Show Work Profile Toast During Setup

Before device setup is complete, we shouldn't ever show the "using this
app outside your work profile" toast.  This toast is confusing in the
context of Setup Wizard.

Fixes: 150451876
Test: atest com.android.internal.app.IntentForwarderActivityTest
Test: Manually verified no toast during setup
Test: Manually verified toast appears after setup
Change-Id: I661cc18c625609726388f3705c7b893517e23323
diff --git a/core/java/com/android/internal/app/IntentForwarderActivity.java b/core/java/com/android/internal/app/IntentForwarderActivity.java
index 7a0afa2..9bdfa4a 100644
--- a/core/java/com/android/internal/app/IntentForwarderActivity.java
+++ b/core/java/com/android/internal/app/IntentForwarderActivity.java
@@ -38,6 +38,7 @@
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.Settings;
 import android.util.Slog;
 import android.widget.Toast;
 
@@ -153,6 +154,9 @@
     }
 
     private boolean shouldShowDisclosure(@Nullable ResolveInfo ri, Intent intent) {
+        if (!isDeviceProvisioned()) {
+            return false;
+        }
         if (ri == null || ri.activityInfo == null) {
             return true;
         }
@@ -163,6 +167,11 @@
         return !isTargetResolverOrChooserActivity(ri.activityInfo);
     }
 
+    private boolean isDeviceProvisioned() {
+        return Settings.Global.getInt(getContentResolver(),
+                Settings.Global.DEVICE_PROVISIONED, /* def= */ 0) != 0;
+    }
+
     private boolean isTextMessageIntent(Intent intent) {
         return (Intent.ACTION_SENDTO.equals(intent.getAction()) || isViewActionIntent(intent))
                 && ALLOWED_TEXT_MESSAGE_SCHEMES.contains(intent.getScheme());
diff --git a/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java b/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java
index abfb4fb..8cf146e 100644
--- a/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java
+++ b/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java
@@ -48,6 +48,7 @@
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.Settings;
 
 import androidx.test.InstrumentationRegistry;
 import androidx.test.rule.ActivityTestRule;
@@ -56,6 +57,7 @@
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -87,6 +89,7 @@
     static {
         MANAGED_PROFILE_INFO.id = 10;
         MANAGED_PROFILE_INFO.flags = UserInfo.FLAG_MANAGED_PROFILE;
+        MANAGED_PROFILE_INFO.userType = UserManager.USER_TYPE_PROFILE_MANAGED;
     }
 
     private static UserInfo CURRENT_USER_INFO = new UserInfo();
@@ -116,12 +119,21 @@
 
     private Context mContext;
     public static final String PHONE_NUMBER = "123-456-789";
+    private int mDeviceProvisionedInitialValue;
 
     @Before
     public void setup() {
         MockitoAnnotations.initMocks(this);
         mContext = InstrumentationRegistry.getTargetContext();
         sInjector = spy(new TestInjector());
+        mDeviceProvisionedInitialValue = Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.DEVICE_PROVISIONED, /* def= */ 0);
+    }
+
+    @After
+    public void tearDown() {
+        Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,
+                mDeviceProvisionedInitialValue);
     }
 
     @Test
@@ -533,6 +545,22 @@
     }
 
     @Test
+    public void shouldSkipDisclosure_duringDeviceSetup() throws RemoteException {
+        setupShouldSkipDisclosureTest();
+        Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,
+                /* value= */ 0);
+        Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
+                .setAction(Intent.ACTION_VIEW)
+                .addCategory(Intent.CATEGORY_BROWSABLE)
+                .setData(Uri.fromParts("http", "apache.org", null));
+
+        mActivityRule.launchActivity(intent);
+
+        verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
+        verify(sInjector, never()).showToast(anyInt(), anyInt());
+    }
+
+    @Test
     public void forwardToManagedProfile_LoggingTest() throws Exception {
         sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
 
@@ -590,6 +618,8 @@
         sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
         sActivityName = "MyTestActivity";
         sPackageName = "test.package.name";
+        Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,
+                /* value= */ 1);
         when(mApplicationInfo.isSystemApp()).thenReturn(true);
         // Managed profile exists.
         List<UserInfo> profiles = new ArrayList<>();