diff options
| author | 2022-11-04 22:16:19 +0000 | |
|---|---|---|
| committer | 2022-11-04 22:16:19 +0000 | |
| commit | d98c446b684a41d073a28093d139b94642f45ac9 (patch) | |
| tree | f8de1d3c8a4aa20a66d87dad9b88cd708bc068f9 /java | |
| parent | 53705d85ff4092c94592b281d3a05403bae01665 (diff) | |
| parent | 578b259bd38d2a75fc7b1ecca1bfeb2009d99e07 (diff) | |
Merge "Remove ChooserListAdapter notifyDataSetChange throttling" into tm-qpr-dev
Diffstat (limited to 'java')
3 files changed, 52 insertions, 169 deletions
diff --git a/java/src/com/android/intentresolver/ChooserActivity.java b/java/src/com/android/intentresolver/ChooserActivity.java index 76c6fb29..3ccefe1b 100644 --- a/java/src/com/android/intentresolver/ChooserActivity.java +++ b/java/src/com/android/intentresolver/ChooserActivity.java @@ -88,7 +88,6 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.util.HashedStringCache; import android.util.Log; -import android.util.Pair; import android.util.PluralsMessageFormatter; import android.util.Size; import android.util.Slog; @@ -111,6 +110,7 @@ import android.widget.ImageView; import android.widget.Space; import android.widget.TextView; +import androidx.annotation.MainThread; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager; @@ -238,22 +238,15 @@ public class ChooserActivity extends ResolverActivity implements private static final float DIRECT_SHARE_EXPANSION_RATE = 0.78f; private static final int DEFAULT_SALT_EXPIRATION_DAYS = 7; - private int mMaxHashSaltDays = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, + private final int mMaxHashSaltDays = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, SystemUiDeviceConfigFlags.HASH_SALT_MAX_DAYS, DEFAULT_SALT_EXPIRATION_DAYS); - private static final int DEFAULT_LIST_VIEW_UPDATE_DELAY_MS = 0; - private static final int URI_PERMISSION_INTENT_FLAGS = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; - @VisibleForTesting - int mListViewUpdateDelayMs = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, - SystemUiDeviceConfigFlags.SHARESHEET_LIST_VIEW_UPDATE_DELAY, - DEFAULT_LIST_VIEW_UPDATE_DELAY_MS); - private Bundle mReplacementExtras; private IntentSender mChosenComponentSender; private IntentSender mRefinementIntentSender; @@ -452,74 +445,6 @@ public class ChooserActivity extends ResolverActivity implements } } - private final ChooserHandler mChooserHandler = new ChooserHandler(); - - private class ChooserHandler extends Handler { - private static final int LIST_VIEW_UPDATE_MESSAGE = 6; - private static final int SHORTCUT_MANAGER_ALL_SHARE_TARGET_RESULTS = 7; - - private void removeAllMessages() { - removeMessages(LIST_VIEW_UPDATE_MESSAGE); - removeMessages(SHORTCUT_MANAGER_ALL_SHARE_TARGET_RESULTS); - } - - @Override - public void handleMessage(Message msg) { - if (mChooserMultiProfilePagerAdapter.getActiveListAdapter() == null || isDestroyed()) { - return; - } - - switch (msg.what) { - case LIST_VIEW_UPDATE_MESSAGE: - if (DEBUG) { - Log.d(TAG, "LIST_VIEW_UPDATE_MESSAGE; "); - } - - mChooserMultiProfilePagerAdapter - .getListAdapterForUserHandle((UserHandle) msg.obj) - .refreshListView(); - break; - - case SHORTCUT_MANAGER_ALL_SHARE_TARGET_RESULTS: - if (DEBUG) Log.d(TAG, "SHORTCUT_MANAGER_ALL_SHARE_TARGET_RESULTS"); - final Pair<UserHandle, ServiceResultInfo[]> args = - (Pair<UserHandle, ServiceResultInfo[]>) msg.obj; - final UserHandle userHandle = args.first; - final ServiceResultInfo[] resultInfos = args.second; - - for (ServiceResultInfo resultInfo : resultInfos) { - if (resultInfo.resultTargets != null) { - ChooserListAdapter adapterForUserHandle = - mChooserMultiProfilePagerAdapter.getListAdapterForUserHandle( - resultInfo.userHandle); - if (adapterForUserHandle != null) { - adapterForUserHandle.addServiceResults( - resultInfo.originalTarget, - resultInfo.resultTargets, - msg.arg1, - emptyIfNull(mDirectShareShortcutInfoCache), - emptyIfNull(mDirectShareAppTargetCache)); - } - } - } - - logDirectShareTargetReceived( - MetricsEvent.ACTION_DIRECT_SHARE_TARGETS_LOADED_SHORTCUT_MANAGER, - userHandle); - - sendVoiceChoicesIfNeeded(); - getChooserActivityLogger().logSharesheetDirectLoadComplete(); - - mChooserMultiProfilePagerAdapter.getActiveListAdapter() - .completeServiceTargetLoading(); - break; - - default: - super.handleMessage(msg); - } - } - }; - @Override protected void onCreate(Bundle savedInstanceState) { final long intentReceivedTime = System.currentTimeMillis(); @@ -789,8 +714,7 @@ public class ChooserActivity extends ResolverActivity implements new ComponentName( appTarget.getPackageName(), appTarget.getClassName()))); } - sendShareShortcutInfoList(shareShortcutInfos, chooserListAdapter, resultList, - chooserListAdapter.getUserHandle()); + sendShareShortcutInfoList(shareShortcutInfos, chooserListAdapter, resultList); }; } @@ -1620,7 +1544,6 @@ public class ChooserActivity extends ResolverActivity implements mRefinementResultReceiver.destroy(); mRefinementResultReceiver = null; } - mChooserHandler.removeAllMessages(); if (mPreviewCoord != null) mPreviewCoord.cancelLoads(); @@ -1978,7 +1901,7 @@ public class ChooserActivity extends ResolverActivity implements ShortcutManager sm = (ShortcutManager) selectedProfileContext .getSystemService(Context.SHORTCUT_SERVICE); List<ShortcutManager.ShareShortcutInfo> resultList = sm.getShareTargets(filter); - sendShareShortcutInfoList(resultList, adapter, null, userHandle); + sendShareShortcutInfoList(resultList, adapter, null); }); } @@ -2008,12 +1931,13 @@ public class ChooserActivity extends ResolverActivity implements private void sendShareShortcutInfoList( List<ShortcutManager.ShareShortcutInfo> resultList, ChooserListAdapter chooserListAdapter, - @Nullable List<AppTarget> appTargets, UserHandle userHandle) { + @Nullable List<AppTarget> appTargets) { if (appTargets != null && appTargets.size() != resultList.size()) { throw new RuntimeException("resultList and appTargets must have the same size." + " resultList.size()=" + resultList.size() + " appTargets.size()=" + appTargets.size()); } + UserHandle userHandle = chooserListAdapter.getUserHandle(); Context selectedProfileContext = createContextAsUser(userHandle, 0 /* flags */); for (int i = resultList.size() - 1; i >= 0; i--) { final String packageName = resultList.get(i).getTargetComponent().getPackageName(); @@ -2050,12 +1974,15 @@ public class ChooserActivity extends ResolverActivity implements mDirectShareShortcutInfoCache); ServiceResultInfo resultRecord = new ServiceResultInfo( - displayResolveInfo, chooserTargets, userHandle); + displayResolveInfo, chooserTargets); resultRecords.add(resultRecord); } - sendShortcutManagerShareTargetResults( - userHandle, shortcutType, resultRecords.toArray(new ServiceResultInfo[0])); + runOnUiThread(() -> { + if (!isDestroyed()) { + onShortcutsLoaded(chooserListAdapter, shortcutType, resultRecords); + } + }); } private List<ShortcutManager.ShareShortcutInfo> filterShortcutsByTargetComponentName( @@ -2069,15 +1996,6 @@ public class ChooserActivity extends ResolverActivity implements return matchingShortcuts; } - private void sendShortcutManagerShareTargetResults( - UserHandle userHandle, int shortcutType, ServiceResultInfo[] results) { - final Message msg = Message.obtain(); - msg.what = ChooserHandler.SHORTCUT_MANAGER_ALL_SHARE_TARGET_RESULTS; - msg.obj = Pair.create(userHandle, results); - msg.arg1 = shortcutType; - mChooserHandler.sendMessage(msg); - } - private boolean isPackageEnabled(Context context, String packageName) { if (TextUtils.isEmpty(packageName)) { return false; @@ -2554,14 +2472,6 @@ public class ChooserActivity extends ResolverActivity implements return mMaxTargetsPerRow; } - @Override // ChooserListCommunicator - public void sendListViewUpdateMessage(UserHandle userHandle) { - Message msg = Message.obtain(); - msg.what = ChooserHandler.LIST_VIEW_UPDATE_MESSAGE; - msg.obj = userHandle; - mChooserHandler.sendMessageDelayed(msg, mListViewUpdateDelayMs); - } - @Override public void onListRebuilt(ResolverListAdapter listAdapter, boolean rebuildComplete) { setupScrollListener(); @@ -2608,6 +2518,33 @@ public class ChooserActivity extends ResolverActivity implements queryDirectShareTargets(chooserListAdapter, false); } + @MainThread + private void onShortcutsLoaded( + ChooserListAdapter adapter, int targetType, List<ServiceResultInfo> resultInfos) { + UserHandle userHandle = adapter.getUserHandle(); + if (DEBUG) { + Log.d(TAG, "onShortcutsLoaded for user: " + userHandle); + } + for (ServiceResultInfo resultInfo : resultInfos) { + if (resultInfo.resultTargets != null) { + adapter.addServiceResults( + resultInfo.originalTarget, + resultInfo.resultTargets, + targetType, + emptyIfNull(mDirectShareShortcutInfoCache), + emptyIfNull(mDirectShareAppTargetCache)); + } + } + adapter.completeServiceTargetLoading(); + + logDirectShareTargetReceived( + MetricsEvent.ACTION_DIRECT_SHARE_TARGETS_LOADED_SHORTCUT_MANAGER, + userHandle); + + sendVoiceChoicesIfNeeded(); + getChooserActivityLogger().logSharesheetDirectLoadComplete(); + } + @VisibleForTesting protected boolean isUserRunning(UserHandle userHandle) { UserManager userManager = getSystemService(UserManager.class); @@ -2940,12 +2877,12 @@ public class ChooserActivity extends ResolverActivity implements */ @VisibleForTesting public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { - private ChooserListAdapter mChooserListAdapter; + private final ChooserListAdapter mChooserListAdapter; private final LayoutInflater mLayoutInflater; private DirectShareViewHolder mDirectShareViewHolder; private int mChooserTargetWidth = 0; - private boolean mShowAzLabelIfPoss; + private final boolean mShowAzLabelIfPoss; private boolean mLayoutRequested = false; private int mFooterHeight = 0; @@ -3678,13 +3615,10 @@ public class ChooserActivity extends ResolverActivity implements static class ServiceResultInfo { public final DisplayResolveInfo originalTarget; public final List<ChooserTarget> resultTargets; - public final UserHandle userHandle; - public ServiceResultInfo(DisplayResolveInfo ot, List<ChooserTarget> rt, - UserHandle userHandle) { + ServiceResultInfo(DisplayResolveInfo ot, List<ChooserTarget> rt) { originalTarget = ot; resultTargets = rt; - this.userHandle = userHandle; } } diff --git a/java/src/com/android/intentresolver/ChooserListAdapter.java b/java/src/com/android/intentresolver/ChooserListAdapter.java index 2443659f..92cd0043 100644 --- a/java/src/com/android/intentresolver/ChooserListAdapter.java +++ b/java/src/com/android/intentresolver/ChooserListAdapter.java @@ -33,7 +33,6 @@ import android.content.pm.ShortcutInfo; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Trace; -import android.os.UserHandle; import android.os.UserManager; import android.provider.DeviceConfig; import android.service.chooser.ChooserTarget; @@ -90,8 +89,6 @@ public class ChooserListAdapter extends ResolverListAdapter { private final List<TargetInfo> mServiceTargets = new ArrayList<>(); private final List<DisplayResolveInfo> mCallerTargets = new ArrayList<>(); - private boolean mListViewDataChanged = false; - // Sorted list of DisplayResolveInfos for the alphabetical app section. private List<DisplayResolveInfo> mSortedList = new ArrayList<>(); @@ -225,21 +222,6 @@ public class ChooserListAdapter extends ResolverListAdapter { } - @Override - public void notifyDataSetChanged() { - if (!mListViewDataChanged) { - mChooserListCommunicator.sendListViewUpdateMessage(getUserHandle()); - mListViewDataChanged = true; - } - } - - void refreshListView() { - if (mListViewDataChanged) { - super.notifyDataSetChanged(); - } - mListViewDataChanged = false; - } - private void createPlaceHolders() { mServiceTargets.clear(); for (int i = 0; i < mChooserListCommunicator.getMaxRankedTargets(); i++) { @@ -652,8 +634,6 @@ public class ChooserListAdapter extends ResolverListAdapter { int getMaxRankedTargets(); - void sendListViewUpdateMessage(UserHandle userHandle); - boolean isSendAction(Intent targetIntent); } diff --git a/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java b/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java index ee801101..dfdbeda7 100644 --- a/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java +++ b/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java @@ -636,7 +636,7 @@ public class UnbundledChooserActivityTest { } @Test @Ignore - public void hasOtherProfileOneOption() throws Exception { + public void hasOtherProfileOneOption() { List<ResolvedComponentInfo> personalResolvedComponentInfos = createResolvedComponentsForTestWithOtherProfile(2, /* userId */ 10); List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); @@ -662,7 +662,6 @@ public class UnbundledChooserActivityTest { List<ResolvedComponentInfo> stableCopy = createResolvedComponentsForTestWithOtherProfile(2, /* userId= */ 10); waitForIdle(); - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); onView(first(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))) .perform(click()); @@ -1278,7 +1277,7 @@ public class UnbundledChooserActivityTest { // This test is too long and too slow and should not be taken as an example for future tests. @Test @Ignore - public void testDirectTargetSelectionLogging() throws InterruptedException { + public void testDirectTargetSelectionLogging() { Intent sendIntent = createSendTextIntent(); // We need app targets for direct targets to get displayed List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); @@ -1321,11 +1320,6 @@ public class UnbundledChooserActivityTest { /* directShareToAppTargets */ null) ); - // Thread.sleep shouldn't be a thing in an integration test but it's - // necessary here because of the way the code is structured - // TODO: restructure the tests b/129870719 - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); - assertThat("Chooser should have 3 targets (2 apps, 1 direct)", activity.getAdapter().getCount(), is(3)); assertThat("Chooser should have exactly one selectable direct target", @@ -1356,7 +1350,7 @@ public class UnbundledChooserActivityTest { // This test is too long and too slow and should not be taken as an example for future tests. @Test @Ignore - public void testDirectTargetLoggingWithRankedAppTarget() throws InterruptedException { + public void testDirectTargetLoggingWithRankedAppTarget() { Intent sendIntent = createSendTextIntent(); // We need app targets for direct targets to get displayed List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); @@ -1399,10 +1393,6 @@ public class UnbundledChooserActivityTest { directShareToShortcutInfos, /* directShareToAppTargets */ null) ); - // Thread.sleep shouldn't be a thing in an integration test but it's - // necessary here because of the way the code is structured - // TODO: restructure the tests b/129870719 - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); assertThat("Chooser should have 3 targets (2 apps, 1 direct)", activity.getAdapter().getCount(), is(3)); @@ -1429,7 +1419,7 @@ public class UnbundledChooserActivityTest { } @Test @Ignore - public void testShortcutTargetWithApplyAppLimits() throws InterruptedException { + public void testShortcutTargetWithApplyAppLimits() { // Set up resources ChooserActivityOverrideData.getInstance().resources = Mockito.spy( InstrumentationRegistry.getInstrumentation().getContext().getResources()); @@ -1482,10 +1472,6 @@ public class UnbundledChooserActivityTest { directShareToShortcutInfos, /* directShareToAppTargets */ null) ); - // Thread.sleep shouldn't be a thing in an integration test but it's - // necessary here because of the way the code is structured - // TODO: restructure the tests b/129870719 - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); assertThat("Chooser should have 3 targets (2 apps, 1 direct)", wrapper.getAdapter().getCount(), is(3)); @@ -1498,7 +1484,7 @@ public class UnbundledChooserActivityTest { } @Test @Ignore - public void testShortcutTargetWithoutApplyAppLimits() throws InterruptedException { + public void testShortcutTargetWithoutApplyAppLimits() { setDeviceConfigProperty( SystemUiDeviceConfigFlags.APPLY_SHARING_APP_LIMITS_IN_SYSUI, Boolean.toString(false)); @@ -1554,10 +1540,6 @@ public class UnbundledChooserActivityTest { directShareToShortcutInfos, /* directShareToAppTargets */ null) ); - // Thread.sleep shouldn't be a thing in an integration test but it's - // necessary here because of the way the code is structured - // TODO: restructure the tests b/129870719 - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); assertThat("Chooser should have 4 targets (2 apps, 2 direct)", wrapper.getAdapter().getCount(), is(4)); @@ -1604,8 +1586,7 @@ public class UnbundledChooserActivityTest { } private void testDirectTargetLoggingWithAppTargetNotRanked( - int orientation, int appTargetsExpected - ) throws InterruptedException { + int orientation, int appTargetsExpected) { Configuration configuration = new Configuration(InstrumentationRegistry.getInstrumentation().getContext() .getResources().getConfiguration()); @@ -1643,9 +1624,8 @@ public class UnbundledChooserActivityTest { ResolveInfo ri = ResolverDataProvider.createResolveInfo(16, 0); // Start activity - final IChooserWrapper activity = (IChooserWrapper) + final IChooserWrapper wrapper = (IChooserWrapper) mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); - final IChooserWrapper wrapper = (IChooserWrapper) activity; // Insert the direct share target Map<ChooserTarget, ShortcutInfo> directShareToShortcutInfos = new HashMap<>(); directShareToShortcutInfos.put(serviceTargets.get(0), null); @@ -1662,10 +1642,6 @@ public class UnbundledChooserActivityTest { directShareToShortcutInfos, /* directShareToAppTargets */ null) ); - // Thread.sleep shouldn't be a thing in an integration test but it's - // necessary here because of the way the code is structured - // TODO: restructure the tests b/129870719 - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); assertThat( String.format("Chooser should have %d targets (%d apps, 1 direct, 15 A-Z)", @@ -1764,7 +1740,7 @@ public class UnbundledChooserActivityTest { } @Test @Ignore - public void testWorkTab_selectingWorkTabAppOpensAppInWorkProfile() throws InterruptedException { + public void testWorkTab_selectingWorkTabAppOpensAppInWorkProfile() { markWorkProfileUserAvailable(); List<ResolvedComponentInfo> personalResolvedComponentInfos = createResolvedComponentsForTestWithOtherProfile(3, /* userId */ 10); @@ -1780,13 +1756,10 @@ public class UnbundledChooserActivityTest { return true; }; - final IChooserWrapper activity = (IChooserWrapper) - mActivityRule.launchActivity(Intent.createChooser(sendIntent, "work tab test")); + mActivityRule.launchActivity(Intent.createChooser(sendIntent, "work tab test")); waitForIdle(); onView(withText(R.string.resolver_work_tab)).perform(click()); waitForIdle(); - // wait for the share sheet to expand - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); onView(first(allOf( withText(workResolvedComponentInfos.get(0) @@ -1957,7 +1930,7 @@ public class UnbundledChooserActivityTest { } @Test @Ignore - public void testDirectTargetLogging() throws InterruptedException { + public void testDirectTargetLogging() { Intent sendIntent = createSendTextIntent(); // We need app targets for direct targets to get displayed List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); @@ -1997,10 +1970,6 @@ public class UnbundledChooserActivityTest { directShareToShortcutInfos, /* directShareToAppTargets */ null) ); - // Thread.sleep shouldn't be a thing in an integration test but it's - // necessary here because of the way the code is structured - // TODO: restructure the tests b/129870719 - Thread.sleep(((ChooserActivity) activity).mListViewUpdateDelayMs); assertThat("Chooser should have 3 targets (2 apps, 1 direct)", activity.getAdapter().getCount(), is(3)); |