| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.app.SearchManager; | 8 import android.app.SearchManager; |
| 9 import android.content.ClipboardManager; | 9 import android.content.ClipboardManager; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.content.Intent; | 11 import android.content.Intent; |
| 12 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
| 13 import android.content.res.TypedArray; | |
| 14 import android.provider.Browser; | 13 import android.provider.Browser; |
| 15 import android.text.TextUtils; | 14 import android.text.TextUtils; |
| 16 import android.view.ActionMode; | 15 import android.view.ActionMode; |
| 17 import android.view.Menu; | 16 import android.view.Menu; |
| 18 import android.view.MenuItem; | 17 import android.view.MenuItem; |
| 19 | 18 |
| 20 import org.chromium.content.R; | 19 import org.chromium.content.R; |
| 21 | 20 |
| 22 /** | 21 /** |
| 23 * An ActionMode.Callback for in-page selection. This class handles both the edi
table and | 22 * An ActionMode.Callback for in-page selection. This class handles both the edi
table and |
| 24 * non-editable cases. | 23 * non-editable cases. |
| 25 */ | 24 */ |
| 26 public class SelectActionModeCallback implements ActionMode.Callback { | 25 public class SelectActionModeCallback implements ActionMode.Callback { |
| 27 private static final int SELECT_ALL_ATTR_INDEX = 0; | |
| 28 private static final int CUT_ATTR_INDEX = 1; | |
| 29 private static final int COPY_ATTR_INDEX = 2; | |
| 30 private static final int PASTE_ATTR_INDEX = 3; | |
| 31 private static final int SHARE_ATTR_INDEX = 4; | |
| 32 private static final int WEB_SEARCH_ATTR_INDEX = 5; | |
| 33 private static final int[] ACTION_MODE_ATTRS = { | |
| 34 android.R.attr.actionModeSelectAllDrawable, | |
| 35 android.R.attr.actionModeCutDrawable, | |
| 36 android.R.attr.actionModeCopyDrawable, | |
| 37 android.R.attr.actionModePasteDrawable, | |
| 38 R.attr.action_mode_share_drawable, | |
| 39 R.attr.action_mode_web_search_drawable | |
| 40 }; | |
| 41 | |
| 42 private static final int ID_SELECTALL = 0; | |
| 43 private static final int ID_COPY = 1; | |
| 44 private static final int ID_SHARE = 2; | |
| 45 private static final int ID_SEARCH = 3; | |
| 46 private static final int ID_CUT = 4; | |
| 47 private static final int ID_PASTE = 5; | |
| 48 | |
| 49 /** | 26 /** |
| 50 * An interface to retrieve information about the current selection, and als
o to perform | 27 * An interface to retrieve information about the current selection, and als
o to perform |
| 51 * actions based on the selection or when the action bar is dismissed. | 28 * actions based on the selection or when the action bar is dismissed. |
| 52 */ | 29 */ |
| 53 public interface ActionHandler { | 30 public interface ActionHandler { |
| 54 /** | 31 /** |
| 55 * Perform a select all action. | 32 * Perform a select all action. |
| 56 */ | 33 */ |
| 57 void selectAll(); | 34 void selectAll(); |
| 58 | 35 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 if (mEditable != isEditableNow) { | 110 if (mEditable != isEditableNow) { |
| 134 mEditable = isEditableNow; | 111 mEditable = isEditableNow; |
| 135 menu.clear(); | 112 menu.clear(); |
| 136 createActionMenu(mode, menu); | 113 createActionMenu(mode, menu); |
| 137 return true; | 114 return true; |
| 138 } | 115 } |
| 139 return false; | 116 return false; |
| 140 } | 117 } |
| 141 | 118 |
| 142 private void createActionMenu(ActionMode mode, Menu menu) { | 119 private void createActionMenu(ActionMode mode, Menu menu) { |
| 143 TypedArray styledAttributes = getContext().obtainStyledAttributes( | 120 mode.getMenuInflater().inflate(R.menu.select_action_menu, menu); |
| 144 R.style.ContentActionBar, ACTION_MODE_ATTRS); | 121 if (!mEditable || !canPaste()) { |
| 145 | 122 menu.removeItem(R.id.select_action_menu_paste); |
| 146 menu.add(Menu.NONE, ID_SELECTALL, Menu.NONE, android.R.string.selectAll)
. | |
| 147 setAlphabeticShortcut('a'). | |
| 148 setIcon(styledAttributes.getResourceId(SELECT_ALL_ATTR_INDEX, 0)). | |
| 149 setShowAsAction( | |
| 150 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WIT
H_TEXT); | |
| 151 | |
| 152 if (mEditable) { | |
| 153 menu.add(Menu.NONE, ID_CUT, Menu.NONE, android.R.string.cut). | |
| 154 setIcon(styledAttributes.getResourceId(CUT_ATTR_INDEX, 0)). | |
| 155 setAlphabeticShortcut('x'). | |
| 156 setShowAsAction( | |
| 157 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WIT
H_TEXT); | |
| 158 } | |
| 159 | |
| 160 menu.add(Menu.NONE, ID_COPY, Menu.NONE, android.R.string.copy). | |
| 161 setIcon(styledAttributes.getResourceId(COPY_ATTR_INDEX, 0)). | |
| 162 setAlphabeticShortcut('c'). | |
| 163 setShowAsAction( | |
| 164 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WIT
H_TEXT); | |
| 165 | |
| 166 if (mEditable && canPaste()) { | |
| 167 menu.add(Menu.NONE, ID_PASTE, Menu.NONE, android.R.string.paste). | |
| 168 setIcon(styledAttributes.getResourceId(PASTE_ATTR_INDEX, 0)). | |
| 169 setAlphabeticShortcut('v'). | |
| 170 setShowAsAction( | |
| 171 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION
_WITH_TEXT); | |
| 172 } | 123 } |
| 173 | 124 |
| 174 if (!mEditable) { | 125 if (!mEditable) { |
| 175 if (mActionHandler.isShareAvailable()) { | 126 menu.removeItem(R.id.select_action_menu_cut); |
| 176 menu.add(Menu.NONE, ID_SHARE, Menu.NONE, R.string.actionbar_shar
e). | |
| 177 setIcon(styledAttributes.getResourceId(SHARE_ATTR_INDEX, 0))
. | |
| 178 setShowAsAction( | |
| 179 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_AC
TION_WITH_TEXT); | |
| 180 } | |
| 181 | |
| 182 if (!mIncognito && mActionHandler.isWebSearchAvailable()) { | |
| 183 menu.add(Menu.NONE, ID_SEARCH, Menu.NONE, R.string.actionbar_web
_search). | |
| 184 setIcon(styledAttributes.getResourceId(WEB_SEARCH_ATTR_INDEX
, 0)). | |
| 185 setShowAsAction( | |
| 186 MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_AC
TION_WITH_TEXT); | |
| 187 } | |
| 188 } | 127 } |
| 189 | 128 |
| 190 styledAttributes.recycle(); | 129 if (mEditable || !mActionHandler.isShareAvailable()) { |
| 130 menu.removeItem(R.id.select_action_menu_share); |
| 131 } |
| 132 |
| 133 if (mEditable || mIncognito || !mActionHandler.isWebSearchAvailable()) { |
| 134 menu.removeItem(R.id.select_action_menu_web_search); |
| 135 } |
| 191 } | 136 } |
| 192 | 137 |
| 193 @Override | 138 @Override |
| 194 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { | 139 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 195 switch(item.getItemId()) { | 140 int id = item.getItemId(); |
| 196 case ID_SELECTALL: | 141 |
| 197 mActionHandler.selectAll(); | 142 if (id == R.id.select_action_menu_select_all) { |
| 198 break; | 143 mActionHandler.selectAll(); |
| 199 case ID_CUT: | 144 } else if (id == R.id.select_action_menu_cut) { |
| 200 mActionHandler.cut(); | 145 mActionHandler.cut(); |
| 201 break; | 146 } else if (id == R.id.select_action_menu_copy) { |
| 202 case ID_COPY: | 147 mActionHandler.copy(); |
| 203 mActionHandler.copy(); | 148 mode.finish(); |
| 204 mode.finish(); | 149 } else if (id == R.id.select_action_menu_paste) { |
| 205 break; | 150 mActionHandler.paste(); |
| 206 case ID_PASTE: | 151 } else if (id == R.id.select_action_menu_share) { |
| 207 mActionHandler.paste(); | 152 mActionHandler.share(); |
| 208 break; | 153 mode.finish(); |
| 209 case ID_SHARE: | 154 } else if (id == R.id.select_action_menu_web_search) { |
| 210 mActionHandler.share(); | 155 mActionHandler.search(); |
| 211 mode.finish(); | 156 mode.finish(); |
| 212 break; | 157 } else { |
| 213 case ID_SEARCH: | 158 return false; |
| 214 mActionHandler.search(); | |
| 215 mode.finish(); | |
| 216 break; | |
| 217 default: | |
| 218 return false; | |
| 219 } | 159 } |
| 220 return true; | 160 return true; |
| 221 } | 161 } |
| 222 | 162 |
| 223 @Override | 163 @Override |
| 224 public void onDestroyActionMode(ActionMode mode) { | 164 public void onDestroyActionMode(ActionMode mode) { |
| 225 mActionHandler.onDestroyActionMode(); | 165 mActionHandler.onDestroyActionMode(); |
| 226 } | 166 } |
| 227 | 167 |
| 228 private boolean canPaste() { | 168 private boolean canPaste() { |
| 229 ClipboardManager clipMgr = (ClipboardManager) | 169 ClipboardManager clipMgr = (ClipboardManager) |
| 230 getContext().getSystemService(Context.CLIPBOARD_SERVICE); | 170 getContext().getSystemService(Context.CLIPBOARD_SERVICE); |
| 231 return clipMgr.hasPrimaryClip(); | 171 return clipMgr.hasPrimaryClip(); |
| 232 } | 172 } |
| 233 } | 173 } |
| OLD | NEW |