Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java b/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| index 679523249b08b25c8a1cf50a9441352dc7952ea9..4095c0a3c598b74314d578d321f01bb7d64afd41 100644 |
| --- a/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| +++ b/content/public/android/java/src/org/chromium/content/browser/SelectionPopupController.java |
| @@ -54,7 +54,7 @@ import java.util.List; |
| */ |
| @TargetApi(Build.VERSION_CODES.M) |
| public class SelectionPopupController extends ActionModeCallbackHelper { |
| - private static final String TAG = "cr.SelectionPopCtlr"; // 20 char limit |
| + private static final String TAG = "SelectionPopupCtlr"; // 20 char limit |
| /** |
| * Android Intent size limitations prevent sending over a megabyte of data. Limit |
| @@ -115,6 +115,16 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| // The client that processes textual selection, or null if none exists. |
| private SelectionClient mSelectionClient; |
| + // The classificaton result of the selected text if the selection exists and |
| + // ContextSelectionProvider was able to classify it, otherwise null. |
| + private ContextSelectionProvider.Result mClassificationResult; |
| + |
| + // The resource ID for Assist menu item. |
| + private int mAssistMenuItemId; |
| + |
| + // This variable is set to true when the classification request is in progress. |
| + private boolean mPendingClassificationRequest; |
| + |
| /** |
| * Create {@link SelectionPopupController} instance. |
| * @param context Context for action mode. |
| @@ -145,6 +155,13 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| hideActionModeTemporarily(hideDuration); |
| } |
| }; |
| + |
| + mSelectionClient = |
| + ContextSelectionClient.create(new ContextSelectionCallback(), window, webContents); |
| + |
| + // TODO(timav): Use android.R.id.textAssist for the Assist item id once we switch to |
| + // Android O SDK and remove |mAssistMenuItemId|. |
| + mAssistMenuItemId = mContext.getResources().getIdentifier("textAssist", "id", "android"); |
| } |
| /** |
| @@ -189,15 +206,15 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| * |
| * <p>Action mode in floating mode is tried first, and then falls back to |
| * a normal one. |
| - * @return {@code true} if the action mode started successfully or is already on. |
| + * <p> If the action mode cannot be created the selection is cleared. |
| */ |
| - public boolean showActionMode() { |
| - if (isEmpty()) return false; |
| + public void showActionModeOrClearOnFailure() { |
| + if (isEmpty()) return; |
|
amaralp
2017/03/25 00:52:27
Wouldn't you also want to clear the selection here
Tima Vaisburd
2017/03/25 02:57:16
I assumed no real action happens before setCallbac
boliu
2017/03/25 21:19:10
It is true. Should rename isEmpty to something lik
Tima Vaisburd
2017/03/27 06:33:04
Does isReady() sound good?
boliu
2017/03/27 17:25:07
Not really. isReady implies it might not be ready.
Tima Vaisburd
2017/03/27 18:19:45
I renamed to isActionModeSupported(), if you like
|
| destroyActionModeAndKeepSelection(); |
| if (mView.getParent() != null) { |
| - // On ICS, startActionMode throws an NPE when getParent() is null. |
| + // On ICS, startActionMode throws an NPE when getParent() is null. |
| assert mWebContents != null; |
| ActionMode actionMode = supportsFloatingActionMode() |
| ? startFloatingActionMode() |
| @@ -209,7 +226,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| mActionMode = actionMode; |
| } |
| mUnselectAllOnDismiss = true; |
| - return isActionModeValid(); |
| + if (!isActionModeValid()) clearSelection(); |
| } |
| @TargetApi(Build.VERSION_CODES.M) |
| @@ -284,6 +301,10 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| */ |
| @Override |
| public void finishActionMode() { |
| + mPendingClassificationRequest = false; |
| + mHidden = false; |
|
amaralp
2017/03/25 00:52:27
My CL crrev.com/2777663002 should make this and th
Tima Vaisburd
2017/03/27 06:33:04
I kept all the checks since I thought invalidate()
|
| + mView.removeCallbacks(mRepeatingHideRunnable); |
| + |
| if (isActionModeValid()) { |
| mActionMode.finish(); |
| @@ -295,7 +316,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| /** |
| * @see ActionMode#invalidateContentRect() |
| */ |
| - public void invalidateContentRect() { |
| + private void invalidateContentRect() { |
| if (supportsFloatingActionMode()) { |
| if (mHidden) { |
| mPendingInvalidateContentRect = true; |
| @@ -329,6 +350,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| } else { |
| mHidden = false; |
| mView.removeCallbacks(mRepeatingHideRunnable); |
| + // To show the action mode that is being hidden call hide() again with a short delay. |
| hideActionModeTemporarily(SHOW_DELAY_MS); |
| if (mPendingInvalidateContentRect) { |
| mPendingInvalidateContentRect = false; |
| @@ -398,6 +420,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| private void createActionMenu(ActionMode mode, Menu menu) { |
| mNeedsPrepare = false; |
| initializeMenu(mContext, mode, menu); |
| + updateAssistMenuItem(menu); |
| if (!isSelectionEditable() || !canPaste()) { |
| menu.removeItem(R.id.select_action_menu_paste); |
| @@ -440,6 +463,21 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| return clipMgr.hasPrimaryClip(); |
| } |
| + private void updateAssistMenuItem(Menu menu) { |
| + // The assist menu item ID has to be equal to android.R.id.textAssist. Until we compile |
| + // with Android O SDK where this ID is defined we replace the corresponding inflated |
| + // item with an item with the proper ID. |
| + // TODO(timav): Use android.R.id.textAssist for the Assist item id once we switch to |
| + // Android O SDK and remove |mAssistMenuItemId|. |
| + menu.removeItem(R.id.select_action_menu_assist); |
| + if (mAssistMenuItemId == 0) return; |
| + |
| + if (mClassificationResult != null && mClassificationResult.hasNamedAction()) { |
| + menu.add(mAssistMenuItemId, mAssistMenuItemId, 1, mClassificationResult.label) |
| + .setIcon(mClassificationResult.icon); |
| + } |
| + } |
| + |
| /** |
| * Intialize the menu items for processing text, if there is any. |
| */ |
| @@ -452,10 +490,11 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| PackageManager packageManager = mContext.getPackageManager(); |
| List<ResolveInfo> supportedActivities = |
| packageManager.queryIntentActivities(createProcessTextIntent(), 0); |
| + final int order = menu.size(); |
| for (int i = 0; i < supportedActivities.size(); i++) { |
| ResolveInfo resolveInfo = supportedActivities.get(i); |
| CharSequence label = resolveInfo.loadLabel(mContext.getPackageManager()); |
| - menu.add(R.id.select_action_menu_text_processing_menus, Menu.NONE, i, label) |
| + menu.add(R.id.select_action_menu_text_processing_menus, Menu.NONE, order, label) |
| .setIntent(createProcessTextIntentForResolveInfo(resolveInfo)) |
| .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| } |
| @@ -481,7 +520,10 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| int id = item.getItemId(); |
| int groupId = item.getGroupId(); |
| - if (id == R.id.select_action_menu_select_all) { |
| + if (id == mAssistMenuItemId) { |
| + doAssistAction(); |
| + mode.finish(); |
| + } else if (id == R.id.select_action_menu_select_all) { |
| selectAll(); |
| } else if (id == R.id.select_action_menu_cut) { |
| cut(); |
| @@ -540,6 +582,30 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| } |
| /** |
| + * Perform an action that depends on the semantics of the selected text. |
| + */ |
| + @VisibleForTesting |
| + void doAssistAction() { |
| + if (mClassificationResult == null || !mClassificationResult.hasNamedAction()) return; |
| + |
| + assert mClassificationResult.onClickListener != null |
| + || mClassificationResult.intent != null; |
| + |
| + if (mClassificationResult.onClickListener != null) { |
| + mClassificationResult.onClickListener.onClick(mView); |
| + return; |
| + } |
| + |
| + if (mClassificationResult.intent != null) { |
| + Context context = mWindowAndroid.getContext().get(); |
| + if (context == null) return; |
| + |
| + context.startActivity(mClassificationResult.intent); |
| + return; |
| + } |
| + } |
| + |
| + /** |
| * Perform a select all action. |
| */ |
| @VisibleForTesting |
| @@ -717,7 +783,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| void restoreSelectionPopupsIfNecessary() { |
| if (mHasSelection && !isActionModeValid()) { |
| - if (!showActionMode()) clearSelection(); |
| + showActionModeOrClearOnFailure(); |
| } |
| } |
| @@ -734,7 +800,13 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| mSelectionRect.set(left, top, right, bottom); |
| mHasSelection = true; |
| mUnselectAllOnDismiss = true; |
| - if (!showActionMode()) clearSelection(); |
| + if (mSelectionClient != null && mSelectionClient.sendsSelectionPopupUpdates()) { |
| + // Rely on |mSelectionClient| sending a classification request and the request |
| + // always calling onClassified() callback. |
| + mPendingClassificationRequest = true; |
| + } else { |
| + showActionModeOrClearOnFailure(); |
| + } |
| break; |
| case SelectionEventType.SELECTION_HANDLES_MOVED: |
| @@ -754,7 +826,13 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| break; |
| case SelectionEventType.SELECTION_HANDLE_DRAG_STOPPED: |
| - hideActionMode(false); |
| + if (mSelectionClient != null && mSelectionClient.sendsSelectionPopupUpdates()) { |
| + // Rely on |mSelectionClient| sending a classification request and the request |
| + // always calling onClassified() callback. |
| + mPendingClassificationRequest = true; |
| + } else { |
| + hideActionMode(false); |
| + } |
| break; |
| case SelectionEventType.INSERTION_HANDLE_SHOWN: |
| @@ -817,6 +895,7 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| void clearSelection() { |
| if (mWebContents == null || isEmpty()) return; |
| mWebContents.collapseSelection(); |
| + mClassificationResult = null; |
| } |
| void onSelectionChanged(String text) { |
| @@ -829,6 +908,11 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| // The client that implements selection augmenting functionality, or null if none exists. |
| void setSelectionClient(SelectionClient selectionClient) { |
| mSelectionClient = selectionClient; |
| + |
| + mClassificationResult = null; |
| + |
| + assert !mPendingClassificationRequest; |
| + assert !mHidden; |
| } |
| void onShowUnhandledTapUIIfNeeded(int x, int y) { |
| @@ -883,4 +967,37 @@ public class SelectionPopupController extends ActionModeCallbackHelper { |
| return mContext.getPackageManager().queryIntentActivities(intent, |
| PackageManager.MATCH_DEFAULT_ONLY).size() > 0; |
| } |
| + |
| + // The callback class that delivers result from a ContextSelectionClient. |
| + private class ContextSelectionCallback implements ContextSelectionProvider.ResultCallback { |
| + @Override |
| + public void onClassified(ContextSelectionProvider.Result result) { |
| + boolean pendingClassificationRequest = mPendingClassificationRequest; |
| + mPendingClassificationRequest = false; |
| + |
| + // If the selection does not exist any more, discard |result|. |
| + if (!mHasSelection) { |
| + assert !mHidden; |
| + assert mClassificationResult == null; |
| + return; |
| + } |
| + |
| + mClassificationResult = result; |
| + |
| + // The request might be cancelled by finishActionMode(). Keep the result and quit. |
|
amaralp
2017/03/25 00:52:27
Why do you want to keep the result if the request
Tima Vaisburd
2017/03/25 02:57:16
This onClassified() may come after finish and recr
Tima Vaisburd
2017/03/27 06:33:04
Updated.
|
| + if (!pendingClassificationRequest) { |
| + assert !mHidden; |
| + return; |
| + } |
| + |
| + // Update the selection range if needed. |
| + if (!(result.startAdjust == 0 && result.endAdjust == 0)) { |
| + // This call causes SELECTION_HANDLES_MOVED event |
| + mWebContents.adjustSelectionByCharacterOffset(result.startAdjust, result.endAdjust); |
| + } |
| + |
| + // Rely on this method to clear mHidden and unhide the action mode. |
| + showActionModeOrClearOnFailure(); |
| + } |
| + }; |
| } |