From 7f8743d50cc99a118293f7efde5a1a424bbe2d4e Mon Sep 17 00:00:00 2001 From: arangelov Date: Thu, 13 Feb 2020 20:34:30 +0000 Subject: [PATCH] Refresh list on work profile state change. When the work profile is enabled or disabled from anywhere on the device, the share sheet and the intent picker will refresh to either show the list of work targets or show the empty state screen that the work profile is not enabled. Showing that empty state screen also has a button to enable the work profile. Pressing it enables the work profile and also refreshes the list. Fixes: 149497248 Test: manually toggled the work profile from quick settings, from the empty state screen button and from launcher for both share sheet and intent picker. Change-Id: I25e38b0cb5824ebdcf6255ed1959e9d7c6fac445 --- .../app/AbstractMultiProfilePagerAdapter.java | 9 ++++ .../android/internal/app/ChooserActivity.java | 1 - .../internal/app/ResolverActivity.java | 42 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java b/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java index efcd54f623eec..e8f84aacb7c9f 100644 --- a/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java +++ b/core/java/com/android/internal/app/AbstractMultiProfilePagerAdapter.java @@ -307,6 +307,7 @@ public abstract class AbstractMultiProfilePagerAdapter extends PagerAdapter { return false; } } + showListView(activeListAdapter); return activeListAdapter.rebuildList(doPostProcessing); } @@ -349,6 +350,14 @@ public abstract class AbstractMultiProfilePagerAdapter extends PagerAdapter { button.setOnClickListener(buttonOnClick); } + private void showListView(ResolverListAdapter activeListAdapter) { + ProfileDescriptor descriptor = getItem( + userHandleToPageIndex(activeListAdapter.getUserHandle())); + descriptor.rootView.findViewById(R.id.resolver_list).setVisibility(View.VISIBLE); + View emptyStateView = descriptor.rootView.findViewById(R.id.resolver_empty_state); + emptyStateView.setVisibility(View.GONE); + } + private boolean hasCrossProfileIntents(List intents, int source, int target) { IPackageManager packageManager = AppGlobals.getPackageManager(); ContentResolver contentResolver = mContext.getContentResolver(); diff --git a/core/java/com/android/internal/app/ChooserActivity.java b/core/java/com/android/internal/app/ChooserActivity.java index 07ca79acc293b..dc801a996dbba 100644 --- a/core/java/com/android/internal/app/ChooserActivity.java +++ b/core/java/com/android/internal/app/ChooserActivity.java @@ -1476,7 +1476,6 @@ public class ChooserActivity extends ResolverActivity implements @Override public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) { - mChooserMultiProfilePagerAdapter.getActiveAdapterView().setVisibility(View.VISIBLE); if (mCallerChooserTargets != null && mCallerChooserTargets.length > 0) { mChooserMultiProfilePagerAdapter.getActiveListAdapter().addServiceResults( /* origTarget */ null, diff --git a/core/java/com/android/internal/app/ResolverActivity.java b/core/java/com/android/internal/app/ResolverActivity.java index b2d62aca74fda..5dc8b0b2b21d7 100644 --- a/core/java/com/android/internal/app/ResolverActivity.java +++ b/core/java/com/android/internal/app/ResolverActivity.java @@ -34,6 +34,7 @@ import android.app.VoiceInteractor.PickOptionRequest; import android.app.VoiceInteractor.PickOptionRequest.Option; import android.app.VoiceInteractor.Prompt; import android.compat.annotation.UnsupportedAppUsage; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -169,6 +170,8 @@ public class ResolverActivity extends Activity implements // Intent extra for connected audio devices public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device"; + private BroadcastReceiver mWorkProfileStateReceiver; + /** * Get the string resource to be used as a label for the link to the resolver activity for an * action. @@ -739,6 +742,22 @@ public class ResolverActivity extends Activity implements updateProfileViewButton(); } + @Override + protected void onStart() { + super.onStart(); + if (hasWorkProfile() && ENABLE_TABBED_VIEW) { + mWorkProfileStateReceiver = createWorkProfileStateReceiver(); + registerWorkProfileStateReceiver(); + } + } + + private void registerWorkProfileStateReceiver() { + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); + filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); + registerReceiverAsUser(mWorkProfileStateReceiver, UserHandle.ALL, filter, null, null); + } + @Override protected void onStop() { super.onStop(); @@ -763,6 +782,10 @@ public class ResolverActivity extends Activity implements finish(); } } + if (mWorkPackageMonitor != null) { + unregisterReceiver(mWorkProfileStateReceiver); + mWorkPackageMonitor = null; + } } @Override @@ -1681,6 +1704,25 @@ public class ResolverActivity extends Activity implements } } + private BroadcastReceiver createWorkProfileStateReceiver() { + return new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (!TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_AVAILABLE) + && !TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { + return; + } + if (mMultiProfilePagerAdapter.getCurrentUserHandle() + == getWorkProfileUserHandle()) { + mMultiProfilePagerAdapter.rebuildActiveTab(true); + } else { + mMultiProfilePagerAdapter.clearInactiveProfileCache(); + } + } + }; + } + @VisibleForTesting public static final class ResolvedComponentInfo { public final ComponentName name;