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.Activity; 20 import android.app.SearchManager; 21 import android.content.ClipboardManager; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.provider.Browser; 25 import android.view.ActionMode; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 29 class SelectActionModeCallback implements ActionMode.Callback { 30 private WebViewClassic mWebView; 31 private ActionMode mActionMode; 32 private boolean mIsTextSelected = true; 33 34 void setWebView(WebViewClassic webView) { 35 mWebView = webView; 36 } 37 38 void setTextSelected(boolean isTextSelected) { 39 mIsTextSelected = isTextSelected; 40 } 41 42 void finish() { 43 // It is possible that onCreateActionMode was never called, in the case 44 // where there is no ActionBar, for example. 45 if (mActionMode != null) { 46 mActionMode.finish(); 47 } 48 } 49 50 // ActionMode.Callback implementation 51 52 @Override 53 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 54 mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, menu); 55 56 final Context context = mWebView.getContext(); 57 mode.setTitle(context.getString(com.android.internal.R.string.textSelectionCABTitle)); 58 mode.setTitleOptionalHint(true); 59 60 // If the action mode UI we're running in isn't capable of taking window focus 61 // the user won't be able to type into the find on page UI. Disable this functionality. 62 // (Note that this should only happen in floating dialog windows.) 63 // This can be removed once we can handle multiple focusable windows at a time 64 // in a better way. 65 ClipboardManager cm = (ClipboardManager)(context 66 .getSystemService(Context.CLIPBOARD_SERVICE)); 67 boolean isFocusable = mode.isUiFocusable(); 68 boolean isEditable = mWebView.focusCandidateIsEditableText(); 69 boolean canPaste = isEditable && cm.hasPrimaryClip() && isFocusable; 70 boolean canFind = !isEditable && isFocusable; 71 boolean canCut = isEditable && mIsTextSelected && isFocusable; 72 boolean canCopy = mIsTextSelected; 73 boolean canWebSearch = mIsTextSelected; 74 setMenuVisibility(menu, canFind, com.android.internal.R.id.find); 75 setMenuVisibility(menu, canPaste, com.android.internal.R.id.paste); 76 setMenuVisibility(menu, canCut, com.android.internal.R.id.cut); 77 setMenuVisibility(menu, canCopy, com.android.internal.R.id.copy); 78 setMenuVisibility(menu, canWebSearch, com.android.internal.R.id.websearch); 79 mActionMode = mode; 80 return true; 81 } 82 83 @Override 84 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 85 return true; 86 } 87 88 @Override 89 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 90 switch(item.getItemId()) { 91 case android.R.id.cut: 92 mWebView.cutSelection(); 93 mode.finish(); 94 break; 95 96 case android.R.id.copy: 97 mWebView.copySelection(); 98 mode.finish(); 99 break; 100 101 case android.R.id.paste: 102 mWebView.pasteFromClipboard(); 103 mode.finish(); 104 break; 105 106 case com.android.internal.R.id.share: 107 String selection = mWebView.getSelection(); 108 Browser.sendString(mWebView.getContext(), selection); 109 mode.finish(); 110 break; 111 112 case com.android.internal.R.id.select_all: 113 mWebView.selectAll(); 114 break; 115 116 case com.android.internal.R.id.find: 117 String sel= mWebView.getSelection(); 118 mode.finish(); 119 mWebView.showFindDialog(sel, false); 120 break; 121 case com.android.internal.R.id.websearch: 122 mode.finish(); 123 Intent i = new Intent(Intent.ACTION_WEB_SEARCH); 124 i.putExtra(SearchManager.EXTRA_NEW_SEARCH, true); 125 i.putExtra(SearchManager.QUERY, mWebView.getSelection()); 126 if (!(mWebView.getContext() instanceof Activity)) { 127 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 128 } 129 mWebView.getContext().startActivity(i); 130 break; 131 132 default: 133 return false; 134 } 135 return true; 136 } 137 138 @Override 139 public void onDestroyActionMode(ActionMode mode) { 140 mWebView.selectionDone(); 141 } 142 143 private void setMenuVisibility(Menu menu, boolean visible, int resourceId) { 144 final MenuItem item = menu.findItem(resourceId); 145 if (item != null) { 146 item.setVisible(visible); 147 } 148 } 149 } 150