The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.Context; |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 20 | import android.graphics.Rect; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 21 | import android.text.Editable; |
Gilles Debunne | 133f942 | 2011-11-02 18:39:50 -0700 | [diff] [blame] | 22 | import android.text.InputType; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 23 | import android.text.Spanned; |
| 24 | import android.text.method.NumberKeyListener; |
| 25 | import android.util.AttributeSet; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 26 | import android.view.animation.TranslateAnimation; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 27 | import android.widget.EditText; |
| 28 | import android.widget.ViewSwitcher; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 29 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Provides vertical scrolling for the input/result EditText. |
| 32 | */ |
| 33 | class CalculatorDisplay extends ViewSwitcher { |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 34 | |
| 35 | private static final String ATTR_MAX_DIGITS = "maxDigits"; |
| 36 | private static final int DEFAULT_MAX_DIGITS = 10; |
| 37 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 38 | // only these chars are accepted from keyboard |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 39 | private static final char[] ACCEPTED_CHARS = |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 40 | "0123456789.+-*/\u2212\u00d7\u00f7()!%^".toCharArray(); |
| 41 | |
| 42 | private static final int ANIM_DURATION = 500; |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 44 | enum Scroll { UP, DOWN, NONE } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 46 | TranslateAnimation inAnimUp; |
| 47 | TranslateAnimation outAnimUp; |
| 48 | TranslateAnimation inAnimDown; |
| 49 | TranslateAnimation outAnimDown; |
Mihai Preda | e08c830 | 2009-09-22 14:53:16 +0200 | [diff] [blame] | 50 | |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 51 | private int mMaxDigits = DEFAULT_MAX_DIGITS; |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 52 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 53 | public CalculatorDisplay(Context context, AttributeSet attrs) { |
| 54 | super(context, attrs); |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 55 | mMaxDigits = attrs.getAttributeIntValue(null, ATTR_MAX_DIGITS, DEFAULT_MAX_DIGITS); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 56 | } |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 57 | |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 58 | public int getMaxDigits() { |
| 59 | return mMaxDigits; |
Mihai Preda | e08c830 | 2009-09-22 14:53:16 +0200 | [diff] [blame] | 60 | } |
| 61 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 62 | protected void setLogic(Logic logic) { |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 63 | NumberKeyListener calculatorKeyListener = |
| 64 | new NumberKeyListener() { |
| 65 | public int getInputType() { |
Gilles Debunne | 133f942 | 2011-11-02 18:39:50 -0700 | [diff] [blame] | 66 | return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 67 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 68 | |
| 69 | @Override |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 70 | protected char[] getAcceptedChars() { |
| 71 | return ACCEPTED_CHARS; |
| 72 | } |
| 73 | |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 74 | @Override |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 75 | public CharSequence filter(CharSequence source, int start, int end, |
| 76 | Spanned dest, int dstart, int dend) { |
| 77 | /* the EditText should still accept letters (eg. 'sin') |
| 78 | coming from the on-screen touch buttons, so don't filter anything. |
| 79 | */ |
| 80 | return null; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | Editable.Factory factory = new CalculatorEditable.Factory(logic); |
| 85 | for (int i = 0; i < 2; ++i) { |
| 86 | EditText text = (EditText) getChildAt(i); |
| 87 | text.setBackgroundDrawable(null); |
| 88 | text.setEditableFactory(factory); |
| 89 | text.setKeyListener(calculatorKeyListener); |
Dmitri Plotnikov | 582273d | 2011-01-21 15:55:40 -0800 | [diff] [blame] | 90 | text.setSingleLine(); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void setOnKeyListener(OnKeyListener l) { |
| 96 | getChildAt(0).setOnKeyListener(l); |
| 97 | getChildAt(1).setOnKeyListener(l); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void onSizeChanged(int w, int h, int oldW, int oldH) { |
| 102 | inAnimUp = new TranslateAnimation(0, 0, h, 0); |
| 103 | inAnimUp.setDuration(ANIM_DURATION); |
| 104 | outAnimUp = new TranslateAnimation(0, 0, 0, -h); |
| 105 | outAnimUp.setDuration(ANIM_DURATION); |
| 106 | |
| 107 | inAnimDown = new TranslateAnimation(0, 0, -h, 0); |
| 108 | inAnimDown.setDuration(ANIM_DURATION); |
| 109 | outAnimDown = new TranslateAnimation(0, 0, 0, h); |
| 110 | outAnimDown.setDuration(ANIM_DURATION); |
| 111 | } |
| 112 | |
| 113 | void insert(String delta) { |
| 114 | EditText editor = (EditText) getCurrentView(); |
| 115 | int cursor = editor.getSelectionStart(); |
| 116 | editor.getText().insert(cursor, delta); |
| 117 | } |
| 118 | |
| 119 | EditText getEditText() { |
| 120 | return (EditText) getCurrentView(); |
| 121 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 122 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 123 | Editable getText() { |
| 124 | EditText text = (EditText) getCurrentView(); |
| 125 | return text.getText(); |
| 126 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 127 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 128 | void setText(CharSequence text, Scroll dir) { |
| 129 | if (getText().length() == 0) { |
| 130 | dir = Scroll.NONE; |
| 131 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 132 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 133 | if (dir == Scroll.UP) { |
| 134 | setInAnimation(inAnimUp); |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 135 | setOutAnimation(outAnimUp); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 136 | } else if (dir == Scroll.DOWN) { |
| 137 | setInAnimation(inAnimDown); |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 138 | setOutAnimation(outAnimDown); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 139 | } else { // Scroll.NONE |
| 140 | setInAnimation(null); |
| 141 | setOutAnimation(null); |
| 142 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 144 | EditText editText = (EditText) getNextView(); |
| 145 | editText.setText(text); |
| 146 | //Calculator.log("selection to " + text.length() + "; " + text); |
| 147 | editText.setSelection(text.length()); |
| 148 | showNext(); |
| 149 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 150 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 151 | int getSelectionStart() { |
| 152 | EditText text = (EditText) getCurrentView(); |
| 153 | return text.getSelectionStart(); |
| 154 | } |
Dmitri Plotnikov | 9476df2 | 2011-01-17 14:46:51 -0800 | [diff] [blame] | 155 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 156 | @Override |
| 157 | protected void onFocusChanged(boolean gain, int direction, Rect prev) { |
| 158 | //Calculator.log("focus " + gain + "; " + direction + "; " + prev); |
| 159 | if (!gain) { |
| 160 | requestFocus(); |
| 161 | } |
| 162 | } |
| 163 | } |