CalculatorEditText.java revision ed0e21727bf258442ea6a7b18cc78b9aa5d2fdd8
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
17package com.android.calculator2;
18
19import android.content.ClipData;
20import android.content.ClipboardManager;
21import android.content.Context;
22import android.text.Editable;
23import android.text.InputType;
24import android.text.TextUtils;
25import android.util.AttributeSet;
26import android.view.ActionMode;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.MotionEvent;
30import android.widget.EditText;
31import android.widget.Toast;
32
33public class CalculatorEditText extends EditText {
34
35    public CalculatorEditText(Context context, AttributeSet attrs) {
36        super(context, attrs);
37        setCustomSelectionActionModeCallback(new NoTextSelectionMode());
38        setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
39    }
40
41    @Override
42    public boolean onTouchEvent(MotionEvent event) {
43        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
44            // Hack to prevent keyboard and insertion handle from showing.
45            cancelLongPress();
46        }
47        return super.onTouchEvent(event);
48    }
49
50    @Override
51    public boolean performLongClick() {
52        final Editable text = getText();
53        if (TextUtils.isEmpty(text)) {
54            return false;
55        }
56
57        copyContent();
58        return true;
59    }
60
61    private void copyContent() {
62        final Editable text = getText();
63        setSelection(0, text.length());
64        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
65                Context.CLIPBOARD_SERVICE);
66        clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
67        Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show();
68    }
69
70    class NoTextSelectionMode implements ActionMode.Callback {
71        @Override
72        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
73            return false;
74        }
75
76        @Override
77        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
78            copyContent();
79            // Make the selection highlight blink
80            postDelayed(new Runnable() {
81                public void run() {
82                    setSelection(getSelectionEnd());
83                }
84            }, 200);
85            // Prevents the selection action mode on double tap.
86            return false;
87        }
88
89        @Override
90        public void onDestroyActionMode(ActionMode mode) {}
91
92        @Override
93        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
94            return false;
95        }
96    }
97}
98