Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.webkit; |
| 18 | |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame] | 19 | import android.app.SearchManager; |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 20 | import android.content.ClipboardManager; |
Adam Powell | f8419a0 | 2011-10-03 12:08:54 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame] | 22 | import android.content.Intent; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 23 | import android.provider.Browser; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 24 | import android.view.ActionMode; |
| 25 | import android.view.Menu; |
| 26 | import android.view.MenuItem; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 27 | |
| 28 | class SelectActionModeCallback implements ActionMode.Callback { |
| 29 | private WebView mWebView; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 30 | private ActionMode mActionMode; |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 31 | private boolean mIsTextSelected = true; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 32 | |
| 33 | void setWebView(WebView webView) { |
| 34 | mWebView = webView; |
| 35 | } |
| 36 | |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 37 | void setTextSelected(boolean isTextSelected) { |
| 38 | mIsTextSelected = isTextSelected; |
| 39 | } |
| 40 | |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 41 | void finish() { |
Leon Scroggins | 8a4fd2f | 2010-12-16 18:17:23 -0500 | [diff] [blame] | 42 | // 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 Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // ActionMode.Callback implementation |
| 50 | |
| 51 | @Override |
| 52 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Adam Powell | f8419a0 | 2011-10-03 12:08:54 -0700 | [diff] [blame] | 53 | mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, menu); |
| 54 | |
| 55 | final Context context = mWebView.getContext(); |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame^] | 56 | mode.setTitle(context.getString(com.android.internal.R.string.textSelectionCABTitle)); |
| 57 | mode.setTitleOptionalHint(true); |
Adam Powell | f8419a0 | 2011-10-03 12:08:54 -0700 | [diff] [blame] | 58 | |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 59 | // 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 Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 78 | 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 Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 90 | case android.R.id.cut: |
| 91 | mWebView.cutSelection(); |
| 92 | mode.finish(); |
| 93 | break; |
| 94 | |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 95 | case android.R.id.copy: |
| 96 | mWebView.copySelection(); |
| 97 | mode.finish(); |
| 98 | break; |
| 99 | |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 100 | case android.R.id.paste: |
| 101 | mWebView.pasteFromClipboard(); |
| 102 | mode.finish(); |
| 103 | break; |
| 104 | |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 105 | 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 Scroggins | 571354f | 2011-01-04 10:12:41 -0500 | [diff] [blame] | 118 | mWebView.showFindDialog(sel, false); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 119 | break; |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame] | 120 | case com.android.internal.R.id.websearch: |
| 121 | mode.finish(); |
| 122 | Intent i = new Intent(Intent.ACTION_WEB_SEARCH); |
Leon Scroggins | af5b406 | 2011-02-22 16:35:13 -0500 | [diff] [blame] | 123 | i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true); |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame] | 124 | i.putExtra(SearchManager.QUERY, mWebView.getSelection()); |
| 125 | mWebView.getContext().startActivity(i); |
| 126 | break; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 127 | |
| 128 | default: |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onDestroyActionMode(ActionMode mode) { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 136 | mWebView.selectionDone(); |
| 137 | } |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 138 | |
| 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 Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 145 | } |