Commit 4e8da6c0 authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[Android] SelectionPopupController cleanup

This CL
1) Removed an obsolete TODO.
2) Removed MenuDescriptor, we are not taking adavantage of it.

Bug: 825381
Change-Id: Iba22809a0046150165ec4e1c265c00e77b567898
Reviewed-on: https://chromium-review.googlesource.com/978886Reviewed-by: default avatarPedro Amaral <amaralp@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#546179}
parent d3480ee1
......@@ -214,7 +214,6 @@ android_library("content_java") {
"java/src/org/chromium/content/browser/selection/HandleViewResources.java",
"java/src/org/chromium/content/browser/selection/LGEmailActionModeWorkaround.java",
"java/src/org/chromium/content/browser/selection/LegacyPastePopupMenu.java",
"java/src/org/chromium/content/browser/selection/MenuDescriptor.java",
"java/src/org/chromium/content/browser/selection/PastePopupMenu.java",
"java/src/org/chromium/content/browser/selection/SelectionEventProxyImpl.java",
"java/src/org/chromium/content/browser/selection/SelectionIndicesConverter.java",
......@@ -525,7 +524,6 @@ junit_binary("content_junit_tests") {
"junit/src/org/chromium/content/browser/input/ThreadedInputConnectionTest.java",
"junit/src/org/chromium/content/browser/picker/DateDialogNormalizerTest.java",
"junit/src/org/chromium/content/browser/remoteobjects/RemoteObjectImplTest.java",
"junit/src/org/chromium/content/browser/selection/MenuDescriptorTest.java",
"junit/src/org/chromium/content/browser/selection/SelectionPopupControllerTest.java",
"junit/src/org/chromium/content/browser/selection/SmartSelectionMetricsLoggerTest.java",
]
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content.browser.selection;
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuItem;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* MenuDescriptor contains the information on how a menu needs to be changed relative to
* its initial (inflated) state. It can be used to modify a menu, it can also be compared
* to MenuDescriptor to figure out whether a menu needs to be changed.
*/
@SuppressLint("UseSparseArrays")
public class MenuDescriptor {
/**
* ItemDescriptor contains the information about delayed menu.add() action.
*/
private static class ItemDescriptor {
public int mItemId;
public int mGroupId;
public int mOrder;
public CharSequence mTitle;
public Drawable mIcon;
public ItemDescriptor(
int groupId, int itemId, int order, CharSequence title, Drawable icon) {
mItemId = itemId;
mGroupId = groupId;
mOrder = order;
mTitle = title;
mIcon = icon;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof ItemDescriptor)) return false;
ItemDescriptor rhs = (ItemDescriptor) obj;
return mItemId == rhs.mItemId && mGroupId == rhs.mGroupId && mOrder == rhs.mOrder
&& isTitleEqual(mTitle, rhs.mTitle);
}
@Override
public int hashCode() {
return mItemId;
}
private static boolean isTitleEqual(CharSequence a, CharSequence b) {
if (a == null && b == null) return true;
if (a == null || b == null) return false;
return a.toString().contentEquals(b);
}
}
private Map<Integer, ItemDescriptor> mAdded = new HashMap<Integer, ItemDescriptor>();
private Set<Integer> mRemoved = new HashSet<Integer>();
public MenuDescriptor() {}
public void addItem(int groupId, int itemId, int order, CharSequence title, Drawable icon) {
mAdded.put(itemId, new ItemDescriptor(groupId, itemId, order, title, icon));
}
public void removeItem(int itemId) {
mRemoved.add(itemId);
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof MenuDescriptor)) return false;
MenuDescriptor rhs = (MenuDescriptor) obj;
return mRemoved.equals(rhs.mRemoved) && mAdded.equals(rhs.mAdded);
}
@Override
public int hashCode() {
return 1;
}
public void apply(Menu menu) {
for (int id : mRemoved) {
menu.removeItem(id);
}
for (Map.Entry<Integer, ItemDescriptor> entry : mAdded.entrySet()) {
ItemDescriptor descr = entry.getValue();
MenuItem item = menu.add(descr.mGroupId, descr.mItemId, descr.mOrder, descr.mTitle);
if (descr.mIcon != null) item.setIcon(descr.mIcon);
}
}
}
......@@ -88,9 +88,6 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
// A large value to force text processing menu items to be at the end of the
// context menu. Chosen to be bigger than the order of possible items in the
// XML template.
// TODO(timav): remove this constant and use show/hide for Assist item instead
// of adding and removing it once we switch to Android O SDK. The show/hide method
// does not require ordering information.
private static final int MENU_ITEM_ORDER_TEXT_PROCESS_START = 100;
// A flag to determine if we should get readback view from WindowAndroid.
......@@ -123,7 +120,6 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
private View mView;
private ActionMode mActionMode;
private MenuDescriptor mActionMenuDescriptor;
// Bit field for mappings from menu item to a flag indicating it is allowed.
private int mAllowedMenuItems;
......@@ -560,7 +556,6 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
// Should be nulled out in case #onDestroyActionMode() is not invoked in response.
mActionMode = null;
mActionMenuDescriptor = null;
}
}
......@@ -705,10 +700,8 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
private void createActionMenu(ActionMode mode, Menu menu) {
initializeMenu(mContext, mode, menu);
mActionMenuDescriptor = createActionMenuDescriptor();
mActionMenuDescriptor.apply(menu);
updateAssistMenuItem(menu);
removeActionMenuItemsIfNecessary(menu);
setPasteAsPlainTextMenuItemTitle(menu);
Context windowContext = mWindowAndroid.getContext().get();
......@@ -723,52 +716,42 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
initializeTextProcessingMenu(menu);
}
private MenuDescriptor createActionMenuDescriptor() {
MenuDescriptor descriptor = new MenuDescriptor();
updateAssistMenuItem(descriptor);
private void removeActionMenuItemsIfNecessary(Menu menu) {
if (!isFocusedNodeEditable() || !canPaste()) {
descriptor.removeItem(R.id.select_action_menu_paste);
descriptor.removeItem(R.id.select_action_menu_paste_as_plain_text);
menu.removeItem(R.id.select_action_menu_paste);
menu.removeItem(R.id.select_action_menu_paste_as_plain_text);
}
if (!canPasteAsPlainText()) {
descriptor.removeItem(R.id.select_action_menu_paste_as_plain_text);
menu.removeItem(R.id.select_action_menu_paste_as_plain_text);
}
if (!hasSelection()) {
descriptor.removeItem(R.id.select_action_menu_select_all);
descriptor.removeItem(R.id.select_action_menu_cut);
descriptor.removeItem(R.id.select_action_menu_copy);
descriptor.removeItem(R.id.select_action_menu_share);
descriptor.removeItem(R.id.select_action_menu_web_search);
return descriptor;
menu.removeItem(R.id.select_action_menu_select_all);
menu.removeItem(R.id.select_action_menu_cut);
menu.removeItem(R.id.select_action_menu_copy);
menu.removeItem(R.id.select_action_menu_share);
menu.removeItem(R.id.select_action_menu_web_search);
return;
}
if (!isFocusedNodeEditable()) {
descriptor.removeItem(R.id.select_action_menu_cut);
menu.removeItem(R.id.select_action_menu_cut);
}
if (isFocusedNodeEditable() || !isSelectActionModeAllowed(MENU_ITEM_SHARE)) {
descriptor.removeItem(R.id.select_action_menu_share);
menu.removeItem(R.id.select_action_menu_share);
}
if (isFocusedNodeEditable() || isIncognito()
|| !isSelectActionModeAllowed(MENU_ITEM_WEB_SEARCH)) {
descriptor.removeItem(R.id.select_action_menu_web_search);
menu.removeItem(R.id.select_action_menu_web_search);
}
if (isSelectionPassword()) {
descriptor.removeItem(R.id.select_action_menu_copy);
descriptor.removeItem(R.id.select_action_menu_cut);
menu.removeItem(R.id.select_action_menu_copy);
menu.removeItem(R.id.select_action_menu_cut);
}
return descriptor;
}
private boolean needsActionMenuUpdate() {
return !createActionMenuDescriptor().equals(mActionMenuDescriptor);
}
private boolean canPaste() {
......@@ -818,13 +801,14 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
return description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML);
}
private void updateAssistMenuItem(MenuDescriptor descriptor) {
private void updateAssistMenuItem(Menu menu) {
// There is no Assist functionality before Android O.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
if (mClassificationResult != null && mClassificationResult.hasNamedAction()) {
descriptor.addItem(R.id.select_action_menu_assist_items, android.R.id.textAssist, 1,
mClassificationResult.label, mClassificationResult.icon);
menu.add(R.id.select_action_menu_assist_items, android.R.id.textAssist, 1,
mClassificationResult.label)
.setIcon(mClassificationResult.icon);
}
}
......@@ -918,7 +902,6 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
@Override
public void onDestroyActionMode() {
mActionMode = null;
mActionMenuDescriptor = null;
if (mUnselectAllOnDismiss) {
mWebContents.dismissTextHandles();
clearSelection();
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content.browser.selection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.chromium.base.test.util.Feature;
/**
* Unit tests for MenuDescriptor.
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class MenuDescriptorTest {
@Test
@Feature({"TextInput"})
public void testRemoveSameItems() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.removeItem(1);
MenuDescriptor d2 = new MenuDescriptor();
d2.removeItem(1);
Assert.assertTrue(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testRemoveDifferentItems() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.removeItem(1);
MenuDescriptor d2 = new MenuDescriptor();
d2.removeItem(2);
Assert.assertFalse(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testAddSameItems() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.addItem(1, 1, 0, "title1", null);
MenuDescriptor d2 = new MenuDescriptor();
d2.addItem(1, 1, 0, "title1", null);
Assert.assertTrue(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testAddDifferentItemId() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.addItem(1, 1, 0, "title1", null);
MenuDescriptor d2 = new MenuDescriptor();
d2.addItem(1, 2, 0, "title1", null);
Assert.assertFalse(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testAddDifferentGroupId() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.addItem(1, 1, 0, "title1", null);
MenuDescriptor d2 = new MenuDescriptor();
d2.addItem(2, 1, 0, "title1", null);
Assert.assertFalse(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testAddDifferentOrder() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.addItem(1, 1, 0, "title1", null);
MenuDescriptor d2 = new MenuDescriptor();
d2.addItem(1, 1, 1, "title1", null);
Assert.assertFalse(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testAddDifferentTitle() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.addItem(1, 1, 0, "title1", null);
MenuDescriptor d2 = new MenuDescriptor();
d2.addItem(1, 1, 0, "title2", null);
Assert.assertFalse(d1.equals(d2));
}
@Test
@Feature({"TextInput"})
public void testSecondAdditionReplacesFirst() throws Exception {
MenuDescriptor d1 = new MenuDescriptor();
d1.addItem(1, 1, 0, "title1", null);
MenuDescriptor d2 = new MenuDescriptor();
d2.addItem(2, 1, 1, "title2", null);
// Second addition for the same item id, this time all fields are the same
// as in d1.
d2.addItem(1, 1, 0, "title1", null);
Assert.assertTrue(d1.equals(d2));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment