blob: a61ad95b1e75d6516cf996bc041500f47eabc7b6 [file] [log] [blame]
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -08001/*
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
17package com.android.calculator2;
18
19import android.content.ClipData;
20import android.content.ClipboardManager;
21import android.content.Context;
Gilles Debunne79525c32011-01-25 08:25:41 -080022import android.graphics.Rect;
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -080023import android.text.Editable;
24import android.text.TextUtils;
25import android.util.AttributeSet;
Gilles Debunnef57b8b42011-01-27 10:54:07 -080026import android.view.ActionMode;
27import android.view.Menu;
28import android.view.MenuItem;
Gilles Debunne79525c32011-01-25 08:25:41 -080029import android.view.MotionEvent;
30import android.view.inputmethod.InputMethodManager;
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -080031import android.widget.EditText;
32import android.widget.Toast;
33
34public class CalculatorEditText extends EditText {
35
36 public CalculatorEditText(Context context, AttributeSet attrs) {
37 super(context, attrs);
Gilles Debunnef57b8b42011-01-27 10:54:07 -080038 setCustomSelectionActionModeCallback(new NoTextSelectionMode());
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -080039 }
40
41 @Override
Gilles Debunne79525c32011-01-25 08:25:41 -080042 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 Debunnef57b8b42011-01-27 10:54:07 -080047 if (imm != null && imm.isActive(this)) {
48 imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
49 }
Gilles Debunne79525c32011-01-25 08:25:41 -080050 }
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 Plotnikovde3eec22011-01-17 18:23:37 -080062 public boolean performLongClick() {
Gilles Debunnef57b8b42011-01-27 10:54:07 -080063 final Editable text = getText();
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -080064 if (TextUtils.isEmpty(text)) {
65 return false;
66 }
67
Gilles Debunnef57b8b42011-01-27 10:54:07 -080068 copyContent();
69 return true;
70 }
71
72 private void copyContent() {
73 final Editable text = getText();
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -080074 setSelection(0, text.length());
75 ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
76 Context.CLIPBOARD_SERVICE);
Gilles Debunnef57b8b42011-01-27 10:54:07 -080077 clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
Dmitri Plotnikovde3eec22011-01-17 18:23:37 -080078 Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show();
Gilles Debunnef57b8b42011-01-27 10:54:07 -080079 }
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 Plotnikovde3eec22011-01-17 18:23:37 -0800107 }
108}