Show a spinner when waiting for the work profile to turn on.

This covers the usecase when we show the share sheet or
intent resolver on a device with a work profile, and
the work profile is turned off. In that case, when
sharing from the personal profile, we show an empty
state screen with the option to turn on the work profile.
When we tap that button, we now show a spinner, as there
is a delay until the profile is actually enabled.

Fixes: 149821684
Test: manual

Change-Id: Id90b21a61b10ecce8172bddc42fdeec5fb61c5fe
diff --git a/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java b/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java
index b4a0208..353522e 100644
--- a/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java
+++ b/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java
@@ -64,6 +64,7 @@
     private final UserHandle mPersonalProfileUserHandle;
     private final UserHandle mWorkProfileUserHandle;
     private Injector mInjector;
+    private boolean mIsWaitingToEnableWorkProfile;
 
     AbstractMultiProfilePagerAdapter(Context context, int currentPage,
             UserHandle personalProfileUserHandle,
@@ -90,10 +91,19 @@
             @Override
             public void requestQuietModeEnabled(boolean enabled, UserHandle workProfileUserHandle) {
                 userManager.requestQuietModeEnabled(enabled, workProfileUserHandle);
+                mIsWaitingToEnableWorkProfile = true;
             }
         };
     }
 
+    protected void markWorkProfileEnabledBroadcastReceived() {
+        mIsWaitingToEnableWorkProfile = false;
+    }
+
+    protected boolean isWaitingToEnableWorkProfile() {
+        return mIsWaitingToEnableWorkProfile;
+    }
+
     /**
      * Overrides the default {@link Injector} for testing purposes.
      */
@@ -294,8 +304,12 @@
                     R.drawable.ic_work_apps_off,
                     R.string.resolver_turn_on_work_apps,
                     R.string.resolver_turn_on_work_apps_explanation,
-                    (View.OnClickListener) v ->
-                            mInjector.requestQuietModeEnabled(false, mWorkProfileUserHandle));
+                    (View.OnClickListener) v -> {
+                        ProfileDescriptor descriptor = getItem(
+                                userHandleToPageIndex(activeListAdapter.getUserHandle()));
+                        showSpinner(descriptor.getEmptyStateView());
+                        mInjector.requestQuietModeEnabled(false, mWorkProfileUserHandle);
+                    });
             return false;
         }
         if (UserHandle.myUserId() != listUserHandle.getIdentifier()) {
@@ -355,7 +369,8 @@
         ProfileDescriptor descriptor = getItem(
                 userHandleToPageIndex(activeListAdapter.getUserHandle()));
         descriptor.rootView.findViewById(R.id.resolver_list).setVisibility(View.GONE);
-        View emptyStateView = descriptor.rootView.findViewById(R.id.resolver_empty_state);
+        View emptyStateView = descriptor.getEmptyStateView();
+        resetViewVisibilities(emptyStateView);
         emptyStateView.setVisibility(View.VISIBLE);
 
         ImageView icon = emptyStateView.findViewById(R.id.resolver_empty_state_icon);
@@ -372,6 +387,23 @@
         button.setOnClickListener(buttonOnClick);
     }
 
+    private void showSpinner(View emptyStateView) {
+        emptyStateView.findViewById(R.id.resolver_empty_state_icon).setVisibility(View.INVISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_title).setVisibility(View.INVISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_subtitle)
+                .setVisibility(View.INVISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_button).setVisibility(View.INVISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_progress).setVisibility(View.VISIBLE);
+    }
+
+    private void resetViewVisibilities(View emptyStateView) {
+        emptyStateView.findViewById(R.id.resolver_empty_state_icon).setVisibility(View.VISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_title).setVisibility(View.VISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_subtitle).setVisibility(View.VISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_button).setVisibility(View.INVISIBLE);
+        emptyStateView.findViewById(R.id.resolver_empty_state_progress).setVisibility(View.GONE);
+    }
+
     private void showListView(ResolverListAdapter activeListAdapter) {
         ProfileDescriptor descriptor = getItem(
                 userHandleToPageIndex(activeListAdapter.getUserHandle()));
@@ -409,8 +441,14 @@
 
     protected class ProfileDescriptor {
         final ViewGroup rootView;
+        private final ViewGroup mEmptyStateView;
         ProfileDescriptor(ViewGroup rootView) {
             this.rootView = rootView;
+            mEmptyStateView = rootView.findViewById(R.id.resolver_empty_state);
+        }
+
+        private ViewGroup getEmptyStateView() {
+            return mEmptyStateView;
         }
     }
 
diff --git a/core/java/com/android/internal/app/ResolverActivity.java b/core/java/com/android/internal/app/ResolverActivity.java
index ed1b6a3..0790f21 100644
--- a/core/java/com/android/internal/app/ResolverActivity.java
+++ b/core/java/com/android/internal/app/ResolverActivity.java
@@ -756,7 +756,7 @@
 
     private void registerWorkProfileStateReceiver() {
         IntentFilter filter = new IntentFilter();
-        filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
+        filter.addAction(Intent.ACTION_USER_UNLOCKED);
         filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
         registerReceiverAsUser(mWorkProfileStateReceiver, UserHandle.ALL, filter, null, null);
     }
@@ -1729,6 +1729,14 @@
     @Override // ResolverListCommunicator
     public void onHandlePackagesChanged(ResolverListAdapter listAdapter) {
         if (listAdapter == mMultiProfilePagerAdapter.getActiveListAdapter()) {
+            if (listAdapter.getUserHandle() == getWorkProfileUserHandle()
+                    && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) {
+                // We have just turned on the work profile and entered the pass code to start it,
+                // now we are waiting to receive the ACTION_USER_UNLOCKED broadcast. There is no
+                // point in reloading the list now, since the work profile user is still
+                // turning on.
+                return;
+            }
             boolean listRebuilt = mMultiProfilePagerAdapter.rebuildActiveTab(true);
             if (listRebuilt) {
                 ResolverListAdapter activeListAdapter =
@@ -1749,10 +1757,18 @@
             @Override
             public void onReceive(Context context, Intent intent) {
                 String action = intent.getAction();
-                if (!TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
+                if (!TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
                         && !TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
                     return;
                 }
+                int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
+                if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
+                        && userHandle != getWorkProfileUserHandle().getIdentifier()) {
+                    return;
+                }
+                if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)) {
+                    mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived();
+                }
                 if (mMultiProfilePagerAdapter.getCurrentUserHandle()
                         == getWorkProfileUserHandle()) {
                     mMultiProfilePagerAdapter.rebuildActiveTab(true);
diff --git a/core/res/res/layout/resolver_empty_states.xml b/core/res/res/layout/resolver_empty_states.xml
index 3cfa826..619b392 100644
--- a/core/res/res/layout/resolver_empty_states.xml
+++ b/core/res/res/layout/resolver_empty_states.xml
@@ -14,7 +14,7 @@
   ~ limitations under the License.
   -->
 
-<LinearLayout
+<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/resolver_empty_state"
     android:layout_width="match_parent"
@@ -28,25 +28,31 @@
         android:id="@+id/resolver_empty_state_icon"
         android:layout_marginTop="48dp"
         android:layout_width="24dp"
-        android:layout_height="24dp"/>
+        android:layout_height="24dp"
+        android:layout_centerHorizontal="true" />
     <TextView
         android:id="@+id/resolver_empty_state_title"
+        android:layout_below="@+id/resolver_empty_state_icon"
         android:layout_marginTop="8dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:fontFamily="@string/config_headlineFontFamilyMedium"
         android:textColor="@color/resolver_empty_state_text"
-        android:textSize="14sp"/>
+        android:textSize="18sp"
+        android:layout_centerHorizontal="true" />
     <TextView
         android:id="@+id/resolver_empty_state_subtitle"
+        android:layout_below="@+id/resolver_empty_state_title"
         android:layout_marginTop="4dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="@color/resolver_empty_state_text"
-        android:textSize="12sp"
-        android:gravity="center_horizontal" />
+        android:textSize="14sp"
+        android:gravity="center_horizontal"
+        android:layout_centerHorizontal="true" />
     <Button
         android:id="@+id/resolver_empty_state_button"
+        android:layout_below="@+id/resolver_empty_state_subtitle"
         android:layout_marginTop="16dp"
         android:text="@string/resolver_switch_on_work"
         android:layout_width="wrap_content"
@@ -54,5 +60,16 @@
         android:background="@null"
         android:fontFamily="@string/config_headlineFontFamilyMedium"
         android:textSize="14sp"
-        android:textColor="@color/resolver_tabs_active_color"/>
-</LinearLayout>
\ No newline at end of file
+        android:textColor="@color/resolver_tabs_active_color"
+        android:layout_centerHorizontal="true" />
+    <ProgressBar
+        android:id="@+id/resolver_empty_state_progress"
+        style="@style/Widget.Material.Light.ProgressBar"
+        android:visibility="gone"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:indeterminate="true"
+        android:layout_centerHorizontal="true"
+        android:layout_below="@+id/resolver_empty_state_subtitle"
+        android:indeterminateTint="@color/resolver_tabs_active_color"/>
+</RelativeLayout>
\ No newline at end of file
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 61e4000a..0f9de4f 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -3879,6 +3879,7 @@
   <java-symbol type="id" name="resolver_empty_state_title" />
   <java-symbol type="id" name="resolver_empty_state_subtitle" />
   <java-symbol type="id" name="resolver_empty_state_button" />
+  <java-symbol type="id" name="resolver_empty_state_progress" />
   <java-symbol type="id" name="resolver_tab_divider" />
   <java-symbol type="string" name="resolver_cant_share_with_work_apps" />
   <java-symbol type="string" name="resolver_cant_share_with_personal_apps" />