blob: f9f5b03327416bbf2572ff3f6ca8013655d2b968 [file] [log] [blame]
Leon Scrogginsfe026bd2010-08-24 14:16:09 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
Michael Kolb3fe0bca2012-05-21 09:38:59 -070019import android.app.Activity;
Ben Murdoch0fa484a2010-12-09 19:48:13 +000020import android.app.SearchManager;
George Mount3d095312012-01-10 11:24:02 -080021import android.content.ClipboardManager;
Adam Powellf8419a02011-10-03 12:08:54 -070022import android.content.Context;
Ben Murdoch0fa484a2010-12-09 19:48:13 +000023import android.content.Intent;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040024import android.provider.Browser;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040025import android.view.ActionMode;
26import android.view.Menu;
27import android.view.MenuItem;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040028
29class SelectActionModeCallback implements ActionMode.Callback {
Jonathan Dixon3c909522012-02-28 18:45:06 +000030 private WebViewClassic mWebView;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040031 private ActionMode mActionMode;
George Mount3d095312012-01-10 11:24:02 -080032 private boolean mIsTextSelected = true;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040033
Jonathan Dixon3c909522012-02-28 18:45:06 +000034 void setWebView(WebViewClassic webView) {
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040035 mWebView = webView;
36 }
37
George Mount3d095312012-01-10 11:24:02 -080038 void setTextSelected(boolean isTextSelected) {
39 mIsTextSelected = isTextSelected;
40 }
41
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040042 void finish() {
Leon Scroggins8a4fd2f2010-12-16 18:17:23 -050043 // It is possible that onCreateActionMode was never called, in the case
44 // where there is no ActionBar, for example.
45 if (mActionMode != null) {
46 mActionMode.finish();
47 }
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040048 }
49
50 // ActionMode.Callback implementation
51
52 @Override
53 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Adam Powellf8419a02011-10-03 12:08:54 -070054 mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, menu);
55
56 final Context context = mWebView.getContext();
Adam Powellb98a81f2012-02-24 11:09:07 -080057 mode.setTitle(context.getString(com.android.internal.R.string.textSelectionCABTitle));
58 mode.setTitleOptionalHint(true);
Adam Powellf8419a02011-10-03 12:08:54 -070059
George Mount3d095312012-01-10 11:24:02 -080060 // If the action mode UI we're running in isn't capable of taking window focus
61 // the user won't be able to type into the find on page UI. Disable this functionality.
62 // (Note that this should only happen in floating dialog windows.)
63 // This can be removed once we can handle multiple focusable windows at a time
64 // in a better way.
65 ClipboardManager cm = (ClipboardManager)(context
66 .getSystemService(Context.CLIPBOARD_SERVICE));
67 boolean isFocusable = mode.isUiFocusable();
68 boolean isEditable = mWebView.focusCandidateIsEditableText();
69 boolean canPaste = isEditable && cm.hasPrimaryClip() && isFocusable;
70 boolean canFind = !isEditable && isFocusable;
71 boolean canCut = isEditable && mIsTextSelected && isFocusable;
72 boolean canCopy = mIsTextSelected;
73 boolean canWebSearch = mIsTextSelected;
74 setMenuVisibility(menu, canFind, com.android.internal.R.id.find);
75 setMenuVisibility(menu, canPaste, com.android.internal.R.id.paste);
76 setMenuVisibility(menu, canCut, com.android.internal.R.id.cut);
77 setMenuVisibility(menu, canCopy, com.android.internal.R.id.copy);
78 setMenuVisibility(menu, canWebSearch, com.android.internal.R.id.websearch);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040079 mActionMode = mode;
80 return true;
81 }
82
83 @Override
84 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
85 return true;
86 }
87
88 @Override
89 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
90 switch(item.getItemId()) {
George Mount3d095312012-01-10 11:24:02 -080091 case android.R.id.cut:
92 mWebView.cutSelection();
93 mode.finish();
94 break;
95
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040096 case android.R.id.copy:
97 mWebView.copySelection();
98 mode.finish();
99 break;
100
George Mount3d095312012-01-10 11:24:02 -0800101 case android.R.id.paste:
102 mWebView.pasteFromClipboard();
103 mode.finish();
104 break;
105
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400106 case com.android.internal.R.id.share:
107 String selection = mWebView.getSelection();
108 Browser.sendString(mWebView.getContext(), selection);
109 mode.finish();
110 break;
111
112 case com.android.internal.R.id.select_all:
113 mWebView.selectAll();
114 break;
115
116 case com.android.internal.R.id.find:
117 String sel= mWebView.getSelection();
118 mode.finish();
Leon Scroggins571354f2011-01-04 10:12:41 -0500119 mWebView.showFindDialog(sel, false);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400120 break;
Ben Murdoch0fa484a2010-12-09 19:48:13 +0000121 case com.android.internal.R.id.websearch:
122 mode.finish();
123 Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
Leon Scrogginsaf5b4062011-02-22 16:35:13 -0500124 i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true);
Ben Murdoch0fa484a2010-12-09 19:48:13 +0000125 i.putExtra(SearchManager.QUERY, mWebView.getSelection());
Michael Kolb3fe0bca2012-05-21 09:38:59 -0700126 if (!(mWebView.getContext() instanceof Activity)) {
127 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
128 }
Ben Murdoch0fa484a2010-12-09 19:48:13 +0000129 mWebView.getContext().startActivity(i);
130 break;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400131
132 default:
133 return false;
134 }
135 return true;
136 }
137
138 @Override
139 public void onDestroyActionMode(ActionMode mode) {
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400140 mWebView.selectionDone();
141 }
George Mount3d095312012-01-10 11:24:02 -0800142
143 private void setMenuVisibility(Menu menu, boolean visible, int resourceId) {
144 final MenuItem item = menu.findItem(resourceId);
145 if (item != null) {
146 item.setVisible(visible);
147 }
148 }
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400149}