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; |
| 20 | import android.content.Intent; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 21 | import android.provider.Browser; |
| 22 | import android.webkit.WebView; |
| 23 | import android.view.ActionMode; |
| 24 | import android.view.Menu; |
| 25 | import android.view.MenuItem; |
| 26 | import android.view.View; |
| 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; |
| 31 | |
| 32 | void setWebView(WebView webView) { |
| 33 | mWebView = webView; |
| 34 | } |
| 35 | |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 36 | void finish() { |
Leon Scroggins | 8a4fd2f | 2010-12-16 18:17:23 -0500 | [diff] [blame] | 37 | // It is possible that onCreateActionMode was never called, in the case |
| 38 | // where there is no ActionBar, for example. |
| 39 | if (mActionMode != null) { |
| 40 | mActionMode.finish(); |
| 41 | } |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // ActionMode.Callback implementation |
| 45 | |
| 46 | @Override |
| 47 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 48 | mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, |
| 49 | menu); |
| 50 | mode.setTitle(com.android.internal.R.string.textSelectionCABTitle); |
| 51 | mActionMode = mode; |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 62 | switch(item.getItemId()) { |
| 63 | case android.R.id.copy: |
| 64 | mWebView.copySelection(); |
| 65 | mode.finish(); |
| 66 | break; |
| 67 | |
| 68 | case com.android.internal.R.id.share: |
| 69 | String selection = mWebView.getSelection(); |
| 70 | Browser.sendString(mWebView.getContext(), selection); |
| 71 | mode.finish(); |
| 72 | break; |
| 73 | |
| 74 | case com.android.internal.R.id.select_all: |
| 75 | mWebView.selectAll(); |
| 76 | break; |
| 77 | |
| 78 | case com.android.internal.R.id.find: |
| 79 | String sel= mWebView.getSelection(); |
| 80 | mode.finish(); |
Leon Scroggins | 571354f | 2011-01-04 10:12:41 -0500 | [diff] [blame] | 81 | mWebView.showFindDialog(sel, false); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 82 | break; |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame] | 83 | case com.android.internal.R.id.websearch: |
| 84 | mode.finish(); |
| 85 | Intent i = new Intent(Intent.ACTION_WEB_SEARCH); |
Leon Scroggins | af5b406 | 2011-02-22 16:35:13 -0500 | [diff] [blame] | 86 | i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true); |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame] | 87 | i.putExtra(SearchManager.QUERY, mWebView.getSelection()); |
| 88 | mWebView.getContext().startActivity(i); |
| 89 | break; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 90 | |
| 91 | default: |
| 92 | return false; |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onDestroyActionMode(ActionMode mode) { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 99 | mWebView.selectionDone(); |
| 100 | } |
| 101 | } |