From 8f3aa5f6cb9f6c2d829de6224e9ce50edfe0c23e Mon Sep 17 00:00:00 2001 From: Chao Zhang Date: Fri, 25 Mar 2016 16:52:41 +0800 Subject: Gallery2: enhance performance of update supported operation. getSelectedMediaObjects() in updateSupportedOperation() is low performance if there are lots of photos, so menu options will not in correct state immediately, then crash occurred if click menu option. Use mSelectionManager.getSelectedCount() before getSelectedMediaObjects() to disable menu options and share items if need. Change-Id: I937a219ed294151c464f1d7b6ba2e526eef00c82 CRs-Fixed: 985315 --- res/menu/operation.xml | 2 +- .../android/gallery3d/ui/ActionModeHandler.java | 83 ++++++++++++++++------ 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/res/menu/operation.xml b/res/menu/operation.xml index 2403f09fc..2429c48b9 100644 --- a/res/menu/operation.xml +++ b/res/menu/operation.xml @@ -33,7 +33,7 @@ android:icon="@drawable/multiselect_share" android:title="@string/share" android:visible="false" - android:showAsAction="never"> + android:showAsAction="ifRoom"> 1) { + operation &= SUPPORT_MULTIPLE_MASK; + } return operation; } private boolean computeCanShare(ArrayList selected, int max) { int numSelected = selected.size(); - if (numSelected > max) { - return false; - } + boolean ret = computeCanShare(numSelected, max); + if (!ret) return false; numSelected = 0; for (MediaObject mediaObject : selected) { @@ -335,13 +341,16 @@ public class ActionModeHandler implements Callback, PopupList.OnPopupItemClickLi } else { numSelected = numSelected + 1; } - if (numSelected > max) { - return false; - } + ret = computeCanShare(numSelected, max); + if (!ret) return false; } return true; } + private boolean computeCanShare(int size, int max) { + return size <= max; + } + @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN) private void setNfcBeamPushUris(Uri[] uris) { if (mNfcAdapter != null && ApiHelper.HAS_SET_BEAM_PUSH_URIS) { @@ -447,6 +456,28 @@ public class ActionModeHandler implements Callback, PopupList.OnPopupItemClickLi mMenuTask = mActivity.getThreadPool().submit(new Job() { @Override public Void run(final JobContext jc) { + // use selected count to disable menu options and share items if need + // to enhance performance. + int multipleOperation = SUPPORT_MULTIPLE_MASK; + int count = mSelectionManager.getSelectedCount(); + final int operationPre = computeMenuOptionsMultiple(multipleOperation, count); + final boolean canSharePanoramasPre = computeCanShare(count, + MAX_SELECTED_ITEMS_FOR_PANORAMA_SHARE_INTENT); + final boolean canSharePre = computeCanShare(count, + MAX_SELECTED_ITEMS_FOR_SHARE_INTENT); + + mMainHandler.post(new Runnable() { + @Override + public void run() { + if (jc.isCancelled()) return; + MenuExecutor.updateMenuOperation(mMenu, operationPre); + if (mShareMenuItem != null && !canSharePre) { + mShareMenuItem.setEnabled(false); + showShareMaxDialogIfNeed(false); + } + } + }); + // Pass1: Deal with unexpanded media object list for menu operation. ArrayList selected = getSelectedMediaObjects(jc); if (selected == null) { @@ -465,10 +496,12 @@ public class ActionModeHandler implements Callback, PopupList.OnPopupItemClickLi if (jc.isCancelled()) { return null; } - final boolean canSharePanoramas = computeCanShare(selected, - MAX_SELECTED_ITEMS_FOR_PANORAMA_SHARE_INTENT); - final boolean canShare = computeCanShare(selected, - MAX_SELECTED_ITEMS_FOR_SHARE_INTENT); + + // use selected items to compute menu options and share items again. + final boolean canShare = canSharePre + && computeCanShare(selected, MAX_SELECTED_ITEMS_FOR_SHARE_INTENT); + final boolean canSharePanoramas = canSharePanoramasPre + && computeCanShare(selected, MAX_SELECTED_ITEMS_FOR_PANORAMA_SHARE_INTENT); final GetAllPanoramaSupports supportCallback = canSharePanoramas ? new GetAllPanoramaSupports(selected, jc) @@ -512,18 +545,7 @@ public class ActionModeHandler implements Callback, PopupList.OnPopupItemClickLi mSharePanoramaActionProvider.setShareIntent(share_panorama_intent); } if (mShareMenuItem != null) { - if (!canShare && !mShareMaxDialog) { - AlertDialog.Builder shareMaxDialog = new AlertDialog.Builder(mActivity); - shareMaxDialog - .setMessage(R.string.cannot_share_items) - .setPositiveButton(R.string.ok, null) - .create(); - shareMaxDialog.show(); - mShareMaxDialog = true; - } - if (canShare && mShareMaxDialog) { - mShareMaxDialog = false; - } + showShareMaxDialogIfNeed(canShare); mShareMenuItem.setEnabled(canShare); shareIntent = share_intent; @@ -535,6 +557,21 @@ public class ActionModeHandler implements Callback, PopupList.OnPopupItemClickLi }); } + private void showShareMaxDialogIfNeed(boolean canShare) { + if (!canShare && !mShareMaxDialog) { + AlertDialog.Builder shareMaxDialog = new AlertDialog.Builder(mActivity); + shareMaxDialog + .setMessage(R.string.cannot_share_items) + .setPositiveButton(R.string.ok, null) + .create(); + shareMaxDialog.show(); + mShareMaxDialog = true; + } + if (canShare && mShareMaxDialog) { + mShareMaxDialog = false; + } + } + public void pause() { if (mMenuTask != null) { mMenuTask.cancel(); -- cgit v1.2.3