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 19 import android.app.SearchManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.provider.Browser; 23 import android.view.ActionMode; 24 import android.view.Menu; 25 import android.view.MenuItem; 26 27 class SelectActionModeCallback implements ActionMode.Callback { 28 private WebView mWebView; 29 private ActionMode mActionMode; 30 31 void setWebView(WebView webView) { 32 mWebView = webView; 33 } 34 35 void finish() { 36 // It is possible that onCreateActionMode was never called, in the case 37 // where there is no ActionBar, for example. 38 if (mActionMode != null) { 39 mActionMode.finish(); 40 } 41 } 42 43 // ActionMode.Callback implementation 44 45 @Override 46 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 47 mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, menu); 48 49 final Context context = mWebView.getContext(); 50 boolean allowText = context.getResources().getBoolean( 51 com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); 52 mode.setTitle(allowText ? 53 context.getString(com.android.internal.R.string.textSelectionCABTitle) : null); 54 55 if (!mode.isUiFocusable()) { 56 // If the action mode UI we're running in isn't capable of taking window focus 57 // the user won't be able to type into the find on page UI. Disable this functionality. 58 // (Note that this should only happen in floating dialog windows.) 59 // This can be removed once we can handle multiple focusable windows at a time 60 // in a better way. 61 final MenuItem findOnPageItem = menu.findItem(com.android.internal.R.id.find); 62 if (findOnPageItem != null) { 63 findOnPageItem.setVisible(false); 64 } 65 } 66 mActionMode = mode; 67 return true; 68 } 69 70 @Override 71 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 72 return true; 73 } 74 75 @Override 76 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 77 switch(item.getItemId()) { 78 case android.R.id.copy: 79 mWebView.copySelection(); 80 mode.finish(); 81 break; 82 83 case com.android.internal.R.id.share: 84 String selection = mWebView.getSelection(); 85 Browser.sendString(mWebView.getContext(), selection); 86 mode.finish(); 87 break; 88 89 case com.android.internal.R.id.select_all: 90 mWebView.selectAll(); 91 break; 92 93 case com.android.internal.R.id.find: 94 String sel= mWebView.getSelection(); 95 mode.finish(); 96 mWebView.showFindDialog(sel, false); 97 break; 98 case com.android.internal.R.id.websearch: 99 mode.finish(); 100 Intent i = new Intent(Intent.ACTION_WEB_SEARCH); 101 i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true); 102 i.putExtra(SearchManager.QUERY, mWebView.getSelection()); 103 mWebView.getContext().startActivity(i); 104 break; 105 106 default: 107 return false; 108 } 109 return true; 110 } 111 112 @Override 113 public void onDestroyActionMode(ActionMode mode) { 114 mWebView.selectionDone(); 115 } 116 } 117