blob: 104deb1c82f02aafbb894e1f0b6b0204e76c2b8d [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;
20import android.content.Intent;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040021import android.provider.Browser;
22import android.webkit.WebView;
23import android.view.ActionMode;
24import android.view.Menu;
25import android.view.MenuItem;
26import android.view.View;
27
28class SelectActionModeCallback implements ActionMode.Callback {
29 private WebView mWebView;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040030 private ActionMode mActionMode;
31
32 void setWebView(WebView webView) {
33 mWebView = webView;
34 }
35
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040036 void finish() {
Leon Scroggins8a4fd2f2010-12-16 18:17:23 -050037 // 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 Scrogginsfe026bd2010-08-24 14:16:09 -040042 }
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 Scroggins571354f2011-01-04 10:12:41 -050081 mWebView.showFindDialog(sel, false);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040082 break;
Ben Murdoch0fa484a2010-12-09 19:48:13 +000083 case com.android.internal.R.id.websearch:
84 mode.finish();
85 Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
Leon Scrogginsaf5b4062011-02-22 16:35:13 -050086 i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true);
Ben Murdoch0fa484a2010-12-09 19:48:13 +000087 i.putExtra(SearchManager.QUERY, mWebView.getSelection());
88 mWebView.getContext().startActivity(i);
89 break;
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040090
91 default:
92 return false;
93 }
94 return true;
95 }
96
97 @Override
98 public void onDestroyActionMode(ActionMode mode) {
Leon Scrogginsfe026bd2010-08-24 14:16:09 -040099 mWebView.selectionDone();
100 }
101}