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