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; |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 23 | import android.text.Editable; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.AttributeSet; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 26 | import android.view.ActionMode; |
| 27 | import android.view.Menu; |
| 28 | import android.view.MenuItem; |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 29 | import android.view.MotionEvent; |
| 30 | import android.view.inputmethod.InputMethodManager; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 31 | import android.widget.EditText; |
| 32 | import android.widget.Toast; |
| 33 | |
| 34 | public class CalculatorEditText extends EditText { |
| 35 | |
| 36 | public CalculatorEditText(Context context, AttributeSet attrs) { |
| 37 | super(context, attrs); |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 38 | setCustomSelectionActionModeCallback(new NoTextSelectionMode()); |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Override |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 42 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 43 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 44 | |
| 45 | InputMethodManager imm = ((InputMethodManager) getContext(). |
| 46 | getSystemService(Context.INPUT_METHOD_SERVICE)); |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 47 | if (imm != null && imm.isActive(this)) { |
| 48 | imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0); |
| 49 | } |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean onTouchEvent(MotionEvent event) { |
| 54 | if (event.getActionMasked() == MotionEvent.ACTION_UP) { |
| 55 | // Hack to prevent keyboard and insertion handle from showing. |
| 56 | cancelLongPress(); |
| 57 | } |
| 58 | return super.onTouchEvent(event); |
| 59 | } |
| 60 | |
| 61 | @Override |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 62 | public boolean performLongClick() { |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 63 | final Editable text = getText(); |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 64 | if (TextUtils.isEmpty(text)) { |
| 65 | return false; |
| 66 | } |
| 67 | |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 68 | copyContent(); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | private void copyContent() { |
| 73 | final Editable text = getText(); |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 74 | setSelection(0, text.length()); |
| 75 | ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService( |
| 76 | Context.CLIPBOARD_SERVICE); |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 77 | clipboard.setPrimaryClip(ClipData.newPlainText(null, text)); |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 78 | Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show(); |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame^] | 79 | } |
| 80 | |
| 81 | class NoTextSelectionMode implements ActionMode.Callback { |
| 82 | @Override |
| 83 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 89 | copyContent(); |
| 90 | // Make the selection highlight blink |
| 91 | postDelayed(new Runnable() { |
| 92 | public void run() { |
| 93 | setSelection(getSelectionEnd()); |
| 94 | } |
| 95 | }, 200); |
| 96 | // Prevents the selection action mode on double tap. |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onDestroyActionMode(ActionMode mode) {} |
| 102 | |
| 103 | @Override |
| 104 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 105 | return false; |
| 106 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 107 | } |
| 108 | } |