blob: 2a770f5d986c0c02542b756183d5559b4140243b [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
Ben Murdoch0fa484a2010-12-09 19:48:13 +000019import android.app.SearchManager;
George Mount3d095312012-01-10 11:24:02 -080020import android.content.ClipboardManager;
Adam Powellf8419a02011-10-03 12:08:54 -070021import android.content.Context;
Ben Murdoch0fa484a2010-12-09 19:48:13 +000022import android.content.Intent;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040023import android.provider.Browser;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040024import android.view.ActionMode;
25import android.view.Menu;
26import android.view.MenuItem;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040027
28class SelectActionModeCallback implements ActionMode.Callback {
29 private WebView mWebView;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040030 private ActionMode mActionMode;
George Mount3d095312012-01-10 11:24:02 -080031 private boolean mIsTextSelected = true;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040032
33 void setWebView(WebView webView) {
34 mWebView = webView;
35 }
36
George Mount3d095312012-01-10 11:24:02 -080037 void setTextSelected(boolean isTextSelected) {
38 mIsTextSelected = isTextSelected;
39 }
40
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040041 void finish() {
Leon Scroggins8a4fd2f2010-12-16 18:17:23 -050042 // It is possible that onCreateActionMode was never called, in the case
43 // where there is no ActionBar, for example.
44 if (mActionMode != null) {
45 mActionMode.finish();
46 }
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040047 }
48
49 // ActionMode.Callback implementation
50
51 @Override
52 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Adam Powellf8419a02011-10-03 12:08:54 -070053 mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, menu);
54
55 final Context context = mWebView.getContext();
Adam Powellb98a81f2012-02-24 11:09:07 -080056 mode.setTitle(context.getString(com.android.internal.R.string.textSelectionCABTitle));
57 mode.setTitleOptionalHint(true);
Adam Powellf8419a02011-10-03 12:08:54 -070058
George Mount3d095312012-01-10 11:24:02 -080059 // If the action mode UI we're running in isn't capable of taking window focus
60 // the user won't be able to type into the find on page UI. Disable this functionality.
61 // (Note that this should only happen in floating dialog windows.)
62 // This can be removed once we can handle multiple focusable windows at a time
63 // in a better way.
64 ClipboardManager cm = (ClipboardManager)(context
65 .getSystemService(Context.CLIPBOARD_SERVICE));
66 boolean isFocusable = mode.isUiFocusable();
67 boolean isEditable = mWebView.focusCandidateIsEditableText();
68 boolean canPaste = isEditable && cm.hasPrimaryClip() && isFocusable;
69 boolean canFind = !isEditable && isFocusable;
70 boolean canCut = isEditable && mIsTextSelected && isFocusable;
71 boolean canCopy = mIsTextSelected;
72 boolean canWebSearch = mIsTextSelected;
73 setMenuVisibility(menu, canFind, com.android.internal.R.id.find);
74 setMenuVisibility(menu, canPaste, com.android.internal.R.id.paste);
75 setMenuVisibility(menu, canCut, com.android.internal.R.id.cut);
76 setMenuVisibility(menu, canCopy, com.android.internal.R.id.copy);
77 setMenuVisibility(menu, canWebSearch, com.android.internal.R.id.websearch);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040078 mActionMode = mode;
79 return true;
80 }
81
82 @Override
83 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
84 return true;
85 }
86
87 @Override
88 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
89 switch(item.getItemId()) {
George Mount3d095312012-01-10 11:24:02 -080090 case android.R.id.cut:
91 mWebView.cutSelection();
92 mode.finish();
93 break;
94
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040095 case android.R.id.copy:
96 mWebView.copySelection();
97 mode.finish();
98 break;
99
George Mount3d095312012-01-10 11:24:02 -0800100 case android.R.id.paste:
101 mWebView.pasteFromClipboard();
102 mode.finish();
103 break;
104
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400105 case com.android.internal.R.id.share:
106 String selection = mWebView.getSelection();
107 Browser.sendString(mWebView.getContext(), selection);
108 mode.finish();
109 break;
110
111 case com.android.internal.R.id.select_all:
112 mWebView.selectAll();
113 break;
114
115 case com.android.internal.R.id.find:
116 String sel= mWebView.getSelection();
117 mode.finish();
Leon Scroggins571354f2011-01-04 10:12:41 -0500118 mWebView.showFindDialog(sel, false);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400119 break;
Ben Murdoch0fa484a2010-12-09 19:48:13 +0000120 case com.android.internal.R.id.websearch:
121 mode.finish();
122 Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
Leon Scrogginsaf5b4062011-02-22 16:35:13 -0500123 i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true);
Ben Murdoch0fa484a2010-12-09 19:48:13 +0000124 i.putExtra(SearchManager.QUERY, mWebView.getSelection());
125 mWebView.getContext().startActivity(i);
126 break;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400127
128 default:
129 return false;
130 }
131 return true;
132 }
133
134 @Override
135 public void onDestroyActionMode(ActionMode mode) {
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400136 mWebView.selectionDone();
137 }
George Mount3d095312012-01-10 11:24:02 -0800138
139 private void setMenuVisibility(Menu menu, boolean visible, int resourceId) {
140 final MenuItem item = menu.findItem(resourceId);
141 if (item != null) {
142 item.setVisible(visible);
143 }
144 }
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400145}