Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [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 com.android.calculator2; |
| 18 | |
| 19 | import android.content.ClipData; |
| 20 | import android.content.ClipboardManager; |
| 21 | import android.content.Context; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 23 | import android.text.Editable; |
Gilles Debunne | ed0e217 | 2011-08-16 13:50:51 -0700 | [diff] [blame] | 24 | import android.text.InputType; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
| 26 | import android.util.AttributeSet; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 28 | import android.view.ActionMode; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 29 | import android.view.ContextMenu; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 30 | import android.view.Menu; |
| 31 | import android.view.MenuItem; |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 32 | import android.view.MotionEvent; |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 33 | import android.view.accessibility.AccessibilityEvent; |
| 34 | import android.view.accessibility.AccessibilityNodeInfo; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 35 | import android.widget.EditText; |
| 36 | import android.widget.Toast; |
| 37 | |
Alan Viverette | 8bea494 | 2014-03-03 14:25:51 -0800 | [diff] [blame] | 38 | import com.android.calculator2.R; |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 39 | |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 40 | import java.util.Collections; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 43 | |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 44 | public class CalculatorEditText extends EditText { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 45 | private static final String LOG_TAG = "Calculator2"; |
| 46 | private static final int CUT = 0; |
| 47 | private static final int COPY = 1; |
| 48 | private static final int PASTE = 2; |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 49 | |
| 50 | private static Map<String, String> sReplacementTable; |
| 51 | private static String[] sOperators; |
| 52 | |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 53 | private String[] mMenuItemsStrings; |
| 54 | |
Alan Viverette | 8bea494 | 2014-03-03 14:25:51 -0800 | [diff] [blame] | 55 | public CalculatorEditText(Context context) { |
| 56 | this(context, null); |
| 57 | } |
| 58 | |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 59 | public CalculatorEditText(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 61 | |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 62 | setCustomSelectionActionModeCallback(new NoTextSelectionMode()); |
Gilles Debunne | ed0e217 | 2011-08-16 13:50:51 -0700 | [diff] [blame] | 63 | setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); |
Alan Viverette | 8bea494 | 2014-03-03 14:25:51 -0800 | [diff] [blame] | 64 | setCursorVisible(false); |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean onTouchEvent(MotionEvent event) { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 69 | if (event.getActionMasked() == MotionEvent.ACTION_UP) { |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 70 | // Hack to prevent keyboard and insertion handle from showing. |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 71 | cancelLongPress(); |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 72 | } |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 73 | |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 74 | return super.onTouchEvent(event); |
| 75 | } |
| 76 | |
| 77 | @Override |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 78 | public boolean performLongClick() { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 79 | showContextMenu(); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 80 | |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 84 | @Override |
| 85 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 86 | super.onInitializeAccessibilityEvent(event); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 87 | |
| 88 | final String mathText = mathParse(getText().toString()); |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 89 | // Parse the string into something more "mathematical" sounding. |
| 90 | if (!TextUtils.isEmpty(mathText)) { |
| 91 | event.getText().clear(); |
| 92 | event.getText().add(mathText); |
| 93 | setContentDescription(mathText); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 99 | super.onInitializeAccessibilityNodeInfo(info); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 100 | |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 101 | info.setText(mathParse(getText().toString())); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 106 | // Do nothing. |
| 107 | } |
| 108 | |
| 109 | private String mathParse(String plainText) { |
| 110 | String parsedText = plainText; |
| 111 | if (!TextUtils.isEmpty(parsedText)) { |
| 112 | // Initialize replacement table. |
| 113 | initializeReplacementTable(); |
| 114 | for (String operator : sOperators) { |
| 115 | if (sReplacementTable.containsKey(operator)) { |
| 116 | parsedText = parsedText.replace(operator, sReplacementTable.get(operator)); |
| 117 | } |
| 118 | } |
| 119 | } |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 120 | |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 121 | return parsedText; |
| 122 | } |
| 123 | |
| 124 | private synchronized void initializeReplacementTable() { |
| 125 | if (sReplacementTable == null) { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 126 | final Resources res = getContext().getResources(); |
| 127 | final String[] descs = res.getStringArray(R.array.operatorDescs); |
| 128 | final String[] ops = res.getStringArray(R.array.operators); |
| 129 | final HashMap<String, String> table = new HashMap<String, String>(); |
| 130 | final int len = ops.length; |
| 131 | for (int i = 0; i < len; i++) { |
| 132 | table.put(ops[i], descs[i]); |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 133 | } |
Mindy Pereira | 88a84db | 2011-12-06 13:28:15 -0800 | [diff] [blame] | 134 | |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 135 | sOperators = ops; |
| 136 | sReplacementTable = Collections.unmodifiableMap(table); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | public boolean onTextContextMenuItem(CharSequence title) { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 141 | if (TextUtils.equals(title, mMenuItemsStrings[CUT])) { |
| 142 | cutContent(); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 143 | return true; |
| 144 | } else if (TextUtils.equals(title, mMenuItemsStrings[COPY])) { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 145 | copyContent(); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 146 | return true; |
| 147 | } else if (TextUtils.equals(title, mMenuItemsStrings[PASTE])) { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 148 | pasteContent(); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 149 | return true; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 150 | } |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 151 | |
| 152 | return false; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onCreateContextMenu(ContextMenu menu) { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 157 | if (mMenuItemsStrings == null) { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 158 | final Resources resources = getResources(); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 159 | mMenuItemsStrings = new String[3]; |
| 160 | mMenuItemsStrings[CUT] = resources.getString(android.R.string.cut); |
| 161 | mMenuItemsStrings[COPY] = resources.getString(android.R.string.copy); |
| 162 | mMenuItemsStrings[PASTE] = resources.getString(android.R.string.paste); |
| 163 | } |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 164 | |
| 165 | final MenuHandler handler = new MenuHandler(); |
| 166 | final int len = mMenuItemsStrings.length; |
| 167 | for (int i = 0; i < len; i++) { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 168 | menu.add(Menu.NONE, i, i, mMenuItemsStrings[i]).setOnMenuItemClickListener(handler); |
| 169 | } |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 170 | |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 171 | if (getText().length() == 0) { |
| 172 | menu.getItem(CUT).setVisible(false); |
| 173 | menu.getItem(COPY).setVisible(false); |
| 174 | } |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 175 | |
| 176 | final ClipData primaryClip = getPrimaryClip(); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 177 | if (primaryClip == null || primaryClip.getItemCount() == 0 |
| 178 | || !canPaste(primaryClip.getItemAt(0).coerceToText(getContext()))) { |
| 179 | menu.getItem(PASTE).setVisible(false); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private void setPrimaryClip(ClipData clip) { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 184 | final ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService( |
| 185 | Context.CLIPBOARD_SERVICE); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 186 | clipboard.setPrimaryClip(clip); |
| 187 | } |
| 188 | |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 189 | private void copyContent() { |
| 190 | final Editable text = getText(); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 191 | final int textLength = text.length(); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 192 | setSelection(0, textLength); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 193 | setPrimaryClip(ClipData.newPlainText(null, text)); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 194 | setSelection(textLength); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 195 | |
| 196 | Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show(); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | private void cutContent() { |
| 200 | final Editable text = getText(); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 201 | final int textLength = text.length(); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 202 | setSelection(0, textLength); |
| 203 | setPrimaryClip(ClipData.newPlainText(null, text)); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 204 | getText().delete(0, textLength); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 205 | setSelection(0); |
| 206 | } |
| 207 | |
| 208 | private ClipData getPrimaryClip() { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 209 | final ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService( |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 210 | Context.CLIPBOARD_SERVICE); |
| 211 | return clipboard.getPrimaryClip(); |
| 212 | } |
| 213 | |
| 214 | private void pasteContent() { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 215 | final ClipData clip = getPrimaryClip(); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 216 | if (clip != null) { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 217 | final int len = clip.getItemCount(); |
| 218 | for (int i = 0; i < len; i++) { |
| 219 | final CharSequence paste = clip.getItemAt(i).coerceToText(getContext()); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 220 | if (canPaste(paste)) { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 221 | getText().insert(getSelectionEnd(), paste); |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private boolean canPaste(CharSequence paste) { |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 228 | try { |
| 229 | Float.parseFloat(paste.toString()); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 230 | return true; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 231 | } catch (NumberFormatException e) { |
| 232 | Log.e(LOG_TAG, "Error turning string to integer. Ignoring paste.", e); |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 233 | return false; |
Mindy Pereira | 220cf99 | 2011-09-13 14:02:11 -0700 | [diff] [blame] | 234 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 237 | private class MenuHandler implements MenuItem.OnMenuItemClickListener { |
| 238 | @Override |
| 239 | public boolean onMenuItemClick(MenuItem item) { |
| 240 | return onTextContextMenuItem(item.getTitle()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private class NoTextSelectionMode implements ActionMode.Callback { |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 245 | @Override |
| 246 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 252 | copyContent(); |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 253 | // Prevents the selection action mode on double tap. |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | @Override |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame^] | 258 | public void onDestroyActionMode(ActionMode mode) { |
| 259 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 260 | |
| 261 | @Override |
| 262 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 263 | return false; |
| 264 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 265 | } |
| 266 | } |