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

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

Issue 2407303005: Let embedder provide select action mode (Closed)
Patch Set: Move FloatingPaste into WebActionMode Created 4 years, 2 months 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..afd1d57b5e9fe25661125c5427b1c15a260bd5e2
--- /dev/null
+++ b/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
@@ -0,0 +1,87 @@
+// 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.WebActionMode;
+import org.chromium.content.browser.WebActionModeDelegate;
+
+/**
+ * A class that handles selection action mode for Android WebView.
+ */
+public class AwActionModeCallback implements ActionMode.Callback {
+ private final AwContents mAwContents;
+ private final WebActionModeDelegate mDelegate;
+
+ public AwActionModeCallback(AwContents awContents, WebActionModeDelegate delegate) {
+ mAwContents = awContents;
+ mDelegate = delegate;
+ 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);
+ mDelegate.setAllowedMenuItems(allowedMenuItems, false);
+ }
+
+ private void setMenuAllowed(SparseBooleanArray menus, int menuItem) {
+ menus.put(menuItem, mAwContents.isSelectActionModeAllowed(menuItem));
+ }
+
+ @Override
+ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
+ return mDelegate.onCreateActionMode(mode, menu);
+ }
+
+ @Override
+ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
+ return mDelegate.onPrepareActionMode(mode, menu);
+ }
+
+ @Override
+ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+ if (!mDelegate.isValid()) 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 mDelegate.onActionItemClicked(mode, item);
+ }
+ return true;
+ }
+
+ @Override
+ public void onDestroyActionMode(ActionMode mode) {
+ mDelegate.onDestroyActionMode();
+ }
+
+ private void processText(Intent intent) {
+ RecordUserAction.record("MobileActionMode.ProcessTextIntent");
+ assert Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
+
+ String query = WebActionMode.sanitizeQuery(mDelegate.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