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() { |
| 37 | mActionMode.finish(); |
| 38 | } |
| 39 | |
| 40 | // ActionMode.Callback implementation |
| 41 | |
| 42 | @Override |
| 43 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 44 | mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, |
| 45 | menu); |
| 46 | mode.setTitle(com.android.internal.R.string.textSelectionCABTitle); |
| 47 | mActionMode = mode; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 58 | switch(item.getItemId()) { |
| 59 | case android.R.id.copy: |
| 60 | mWebView.copySelection(); |
| 61 | mode.finish(); |
| 62 | break; |
| 63 | |
| 64 | case com.android.internal.R.id.share: |
| 65 | String selection = mWebView.getSelection(); |
| 66 | Browser.sendString(mWebView.getContext(), selection); |
| 67 | mode.finish(); |
| 68 | break; |
| 69 | |
| 70 | case com.android.internal.R.id.select_all: |
| 71 | mWebView.selectAll(); |
| 72 | break; |
| 73 | |
| 74 | case com.android.internal.R.id.find: |
| 75 | String sel= mWebView.getSelection(); |
| 76 | mode.finish(); |
| 77 | mWebView.showFindDialog(sel); |
| 78 | break; |
Ben Murdoch | 0fa484a | 2010-12-09 19:48:13 +0000 | [diff] [blame^] | 79 | case com.android.internal.R.id.websearch: |
| 80 | mode.finish(); |
| 81 | Intent i = new Intent(Intent.ACTION_WEB_SEARCH); |
| 82 | i.putExtra(SearchManager.QUERY, mWebView.getSelection()); |
| 83 | mWebView.getContext().startActivity(i); |
| 84 | break; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 85 | |
| 86 | default: |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onDestroyActionMode(ActionMode mode) { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 94 | mWebView.selectionDone(); |
| 95 | } |
| 96 | } |