Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2116)

Unified Diff: android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java

Issue 2407303005: Let embedder provide select action mode (Closed)
Patch Set: more comments addressed Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
diff --git a/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java b/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
new file mode 100644
index 0000000000000000000000000000000000000000..592365a4ad83c0e7af081095d1093c9c3b30db7b
--- /dev/null
+++ b/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
@@ -0,0 +1,97 @@
+// Copyright 2016 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.android_webview;
+
+import android.content.Intent;
+import android.os.Build;
+import android.text.TextUtils;
+import android.util.SparseBooleanArray;
+import android.view.ActionMode;
+import android.view.Menu;
+import android.view.MenuItem;
+
+import org.chromium.base.metrics.RecordUserAction;
+import org.chromium.content.R;
+import org.chromium.content.browser.ActionModeCallbackHelper;
+import org.chromium.content.browser.WebActionMode;
+
+/**
+ * A class that handles selection action mode for Android WebView.
+ */
+public class AwActionModeCallback implements ActionMode.Callback {
+ private final AwContents mAwContents;
+ private ActionModeCallbackHelper mHelper;
+ private boolean mMenuSetup;
+
+ public AwActionModeCallback(AwContents awContents) {
+ mAwContents = awContents;
+ }
+
+ private ActionModeCallbackHelper getHelper() {
+ if (mHelper == null) {
+ mHelper = mAwContents.getContentViewCore().getActionModeCallbackHelper();
+ }
+ return mHelper;
+ }
+
+ @Override
+ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
+ if (!mMenuSetup) {
+ SparseBooleanArray allowedMenuItems = new SparseBooleanArray(3);
+ setMenuAllowed(allowedMenuItems, WebActionMode.MENU_ITEM_SHARE);
+ setMenuAllowed(allowedMenuItems, WebActionMode.MENU_ITEM_WEB_SEARCH);
+ setMenuAllowed(allowedMenuItems, WebActionMode.MENU_ITEM_PROCESS_TEXT);
+ getHelper().setAllowedMenuItems(allowedMenuItems, false);
+ mMenuSetup = true;
+ }
+ return getHelper().onCreateActionMode(mode, menu);
+ }
+
+ private void setMenuAllowed(SparseBooleanArray menus, int menuItem) {
+ menus.put(menuItem, mAwContents.isSelectActionModeAllowed(menuItem));
+ }
+
+ @Override
+ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
+ return getHelper().onPrepareActionMode(mode, menu);
+ }
+
+ @Override
+ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+ if (!getHelper().isActionModeValid()) return true;
+
+ int groupId = item.getGroupId();
+
+ if (groupId == R.id.select_action_menu_text_processing_menus) {
+ processText(item.getIntent());
+ // The ActionMode is not dismissed to match the behavior with
+ // TextView in Android M.
+ } else {
+ return getHelper().onActionItemClicked(mode, item);
+ }
+ return true;
+ }
+
+ @Override
+ public void onDestroyActionMode(ActionMode mode) {
+ getHelper().onDestroyActionMode();
+ }
+
+ private void processText(Intent intent) {
+ RecordUserAction.record("MobileActionMode.ProcessTextIntent");
+ assert Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
+
+ String query = WebActionMode.sanitizeQuery(getHelper().getSelectedText(),
+ WebActionMode.MAX_SEARCH_QUERY_LENGTH);
+ if (TextUtils.isEmpty(query)) return;
+
+ intent.putExtra(Intent.EXTRA_PROCESS_TEXT, query);
+ try {
+ mAwContents.startProcessTextIntent(intent);
+ } catch (android.content.ActivityNotFoundException ex) {
+ // If no app handles it, do nothing.
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698