1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.app.Activity;
8 import android.app.SearchManager;
9 import android.content.ClipboardManager;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.pm.PackageManager;
13 import android.provider.Browser;
14 import android.text.TextUtils;
15 import android.view.ActionMode;
16 import android.view.Menu;
17 import android.view.MenuItem;
19 import org.chromium.content.R;
22 * An ActionMode.Callback for in-page selection. This class handles both the editable and
25 public class SelectActionModeCallback implements ActionMode.Callback {
27 * An interface to retrieve information about the current selection, and also to perform
28 * actions based on the selection or when the action bar is dismissed.
30 public interface ActionHandler {
32 * Perform a select all action.
37 * Perform a copy (to clipboard) action.
42 * Perform a cut (to clipboard) action.
47 * Perform a paste action.
52 * Perform a share action.
57 * Perform a search action.
62 * @return true iff the current selection is editable (e.g. text within an input field).
64 boolean isSelectionEditable();
67 * Called when the onDestroyActionMode of the SelectActionmodeCallback is called.
69 void onDestroyActionMode();
72 * @return Whether or not share is available.
74 boolean isShareAvailable();
77 * @return Whether or not web search is available.
79 boolean isWebSearchAvailable();
82 private Context mContext;
83 private ActionHandler mActionHandler;
84 private final boolean mIncognito;
85 private boolean mEditable;
87 protected SelectActionModeCallback(
88 Context context, ActionHandler actionHandler, boolean incognito) {
90 mActionHandler = actionHandler;
91 mIncognito = incognito;
94 protected Context getContext() {
99 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
101 mode.setSubtitle(null);
102 mEditable = mActionHandler.isSelectionEditable();
103 createActionMenu(mode, menu);
108 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
109 boolean isEditableNow = mActionHandler.isSelectionEditable();
110 if (mEditable != isEditableNow) {
111 mEditable = isEditableNow;
113 createActionMenu(mode, menu);
119 private void createActionMenu(ActionMode mode, Menu menu) {
120 mode.getMenuInflater().inflate(R.menu.select_action_menu, menu);
121 if (!mEditable || !canPaste()) {
122 menu.removeItem(R.id.select_action_menu_paste);
126 menu.removeItem(R.id.select_action_menu_cut);
129 if (mEditable || !mActionHandler.isShareAvailable()) {
130 menu.removeItem(R.id.select_action_menu_share);
133 if (mEditable || mIncognito || !mActionHandler.isWebSearchAvailable()) {
134 menu.removeItem(R.id.select_action_menu_web_search);
139 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
140 int id = item.getItemId();
142 if (id == R.id.select_action_menu_select_all) {
143 mActionHandler.selectAll();
144 } else if (id == R.id.select_action_menu_cut) {
145 mActionHandler.cut();
146 } else if (id == R.id.select_action_menu_copy) {
147 mActionHandler.copy();
149 } else if (id == R.id.select_action_menu_paste) {
150 mActionHandler.paste();
151 } else if (id == R.id.select_action_menu_share) {
152 mActionHandler.share();
154 } else if (id == R.id.select_action_menu_web_search) {
155 mActionHandler.search();
164 public void onDestroyActionMode(ActionMode mode) {
165 mActionHandler.onDestroyActionMode();
168 private boolean canPaste() {
169 ClipboardManager clipMgr = (ClipboardManager)
170 getContext().getSystemService(Context.CLIPBOARD_SERVICE);
171 return clipMgr.hasPrimaryClip();