Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 1 | /* |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 2 | * Copyright (C) 2014 The Android Open Source Project |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 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 | * |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Hongwei Wang | 245925e | 2014-05-11 14:38:47 -0700 | [diff] [blame] | 21 | import android.graphics.Paint; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 22 | import android.graphics.Paint.FontMetricsInt; |
| 23 | import android.graphics.Rect; |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 24 | import android.os.Parcelable; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 25 | import android.text.method.ScrollingMovementMethod; |
| 26 | import android.text.TextPaint; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Hongwei Wang | 245925e | 2014-05-11 14:38:47 -0700 | [diff] [blame] | 28 | import android.util.TypedValue; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 29 | import android.view.ActionMode; |
| 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; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 33 | import android.widget.EditText; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 34 | import android.widget.TextView; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 35 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame^] | 36 | /** |
| 37 | * EditText adapted for Calculator display. |
| 38 | */ |
| 39 | |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 40 | public class CalculatorEditText extends EditText { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 41 | |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 42 | private final static ActionMode.Callback NO_SELECTION_ACTION_MODE_CALLBACK = |
| 43 | new ActionMode.Callback() { |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 44 | @Override |
| 45 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 51 | // Prevents the selection action mode on double tap. |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | @Override |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 56 | public void onDestroyActionMode(ActionMode mode) { |
| 57 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 61 | return false; |
| 62 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | private final float mMaximumTextSize; |
| 66 | private final float mMinimumTextSize; |
| 67 | private final float mStepTextSize; |
| 68 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 69 | // Temporary objects for use in layout methods. |
| 70 | private final Paint mTempPaint = new TextPaint(); |
| 71 | private final Rect mTempRect = new Rect(); |
| 72 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 73 | private int mWidthConstraint = -1; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 74 | private OnTextSizeChangeListener mOnTextSizeChangeListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 75 | |
| 76 | public CalculatorEditText(Context context) { |
| 77 | this(context, null); |
| 78 | } |
| 79 | |
| 80 | public CalculatorEditText(Context context, AttributeSet attrs) { |
| 81 | this(context, attrs, 0); |
| 82 | } |
| 83 | |
| 84 | public CalculatorEditText(Context context, AttributeSet attrs, int defStyle) { |
| 85 | super(context, attrs, defStyle); |
| 86 | |
| 87 | final TypedArray a = context.obtainStyledAttributes( |
| 88 | attrs, R.styleable.CalculatorEditText, defStyle, 0); |
| 89 | mMaximumTextSize = a.getDimension( |
| 90 | R.styleable.CalculatorEditText_maxTextSize, getTextSize()); |
| 91 | mMinimumTextSize = a.getDimension( |
| 92 | R.styleable.CalculatorEditText_minTextSize, getTextSize()); |
| 93 | mStepTextSize = a.getDimension(R.styleable.CalculatorEditText_stepTextSize, |
| 94 | (mMaximumTextSize - mMinimumTextSize) / 3); |
| 95 | |
| 96 | a.recycle(); |
| 97 | |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 98 | setCustomSelectionActionModeCallback(NO_SELECTION_ACTION_MODE_CALLBACK); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 99 | if (isFocusable()) { |
| 100 | setMovementMethod(ScrollingMovementMethod.getInstance()); |
| 101 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 102 | setTextSize(TypedValue.COMPLEX_UNIT_PX, mMaximumTextSize); |
| 103 | setMinHeight(getLineHeight() + getCompoundPaddingBottom() + getCompoundPaddingTop()); |
| 104 | } |
| 105 | |
| 106 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 107 | public boolean onTouchEvent(MotionEvent event) { |
| 108 | if (event.getActionMasked() == MotionEvent.ACTION_UP) { |
| 109 | // Hack to prevent keyboard and insertion handle from showing. |
| 110 | cancelLongPress(); |
| 111 | } |
| 112 | return super.onTouchEvent(event); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 117 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 118 | |
| 119 | mWidthConstraint = |
| 120 | MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); |
| 121 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(getText().toString())); |
| 122 | } |
| 123 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame^] | 124 | public int getWidthConstraint() { return mWidthConstraint; } |
| 125 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 126 | @Override |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 127 | public Parcelable onSaveInstanceState() { |
| 128 | super.onSaveInstanceState(); |
| 129 | |
| 130 | // EditText will freeze any text with a selection regardless of getFreezesText() -> |
| 131 | // return null to prevent any state from being preserved at the instance level. |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 136 | protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
| 137 | super.onTextChanged(text, start, lengthBefore, lengthAfter); |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 138 | |
| 139 | final int textLength = text.length(); |
| 140 | if (getSelectionStart() != textLength || getSelectionEnd() != textLength) { |
| 141 | // Pin the selection to the end of the current text. |
| 142 | setSelection(textLength); |
| 143 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 144 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(text.toString())); |
| 145 | } |
| 146 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 147 | @Override |
| 148 | public void setTextSize(int unit, float size) { |
| 149 | final float oldTextSize = getTextSize(); |
| 150 | super.setTextSize(unit, size); |
| 151 | |
| 152 | if (mOnTextSizeChangeListener != null && getTextSize() != oldTextSize) { |
| 153 | mOnTextSizeChangeListener.onTextSizeChanged(this, oldTextSize); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public void setOnTextSizeChangeListener(OnTextSizeChangeListener listener) { |
| 158 | mOnTextSizeChangeListener = listener; |
| 159 | } |
| 160 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 161 | public float getVariableTextSize(String text) { |
| 162 | if (mWidthConstraint < 0 || mMaximumTextSize <= mMinimumTextSize) { |
| 163 | // Not measured, bail early. |
| 164 | return getTextSize(); |
| 165 | } |
| 166 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 167 | // Capture current paint state. |
| 168 | mTempPaint.set(getPaint()); |
| 169 | |
| 170 | // Step through increasing text sizes until the text would no longer fit. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 171 | float lastFitTextSize = mMinimumTextSize; |
| 172 | while (lastFitTextSize < mMaximumTextSize) { |
| 173 | final float nextSize = Math.min(lastFitTextSize + mStepTextSize, mMaximumTextSize); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 174 | mTempPaint.setTextSize(nextSize); |
| 175 | if (mTempPaint.measureText(text) > mWidthConstraint) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 176 | break; |
| 177 | } else { |
| 178 | lastFitTextSize = nextSize; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return lastFitTextSize; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public int getCompoundPaddingTop() { |
| 187 | // Measure the top padding from the capital letter height of the text instead of the top, |
| 188 | // but don't remove more than the available top padding otherwise clipping may occur. |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 189 | getPaint().getTextBounds("H", 0, 1, mTempRect); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 190 | |
| 191 | final FontMetricsInt fontMetrics = getPaint().getFontMetricsInt(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 192 | final int paddingOffset = -(fontMetrics.ascent + mTempRect.height()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 193 | return super.getCompoundPaddingTop() - Math.min(getPaddingTop(), paddingOffset); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public int getCompoundPaddingBottom() { |
| 198 | // Measure the bottom padding from the baseline of the text instead of the bottom, but don't |
| 199 | // remove more than the available bottom padding otherwise clipping may occur. |
| 200 | final FontMetricsInt fontMetrics = getPaint().getFontMetricsInt(); |
| 201 | return super.getCompoundPaddingBottom() - Math.min(getPaddingBottom(), fontMetrics.descent); |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 202 | } |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 203 | |
| 204 | public interface OnTextSizeChangeListener { |
| 205 | void onTextSizeChanged(TextView textView, float oldSize); |
| 206 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 207 | } |