Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 1 | /* |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 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 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 19 | import android.content.ClipData; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 20 | import android.content.ClipboardManager; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 24 | import android.text.Layout; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 25 | import android.text.TextPaint; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 26 | import android.text.method.ScrollingMovementMethod; |
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; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 31 | import android.view.MenuInflater; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 32 | import android.view.MenuItem; |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 33 | import android.view.View; |
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 | /** |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 37 | * TextView adapted for Calculator display. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 38 | */ |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 39 | public class CalculatorText extends AlignedTextView implements View.OnLongClickListener { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 40 | |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 41 | private final ActionMode.Callback2 mPasteActionModeCallback = new ActionMode.Callback2() { |
| 42 | |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 43 | @Override |
| 44 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 45 | if (item.getItemId() == R.id.menu_paste) { |
| 46 | paste(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 47 | mode.finish(); |
| 48 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 49 | } |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 50 | return false; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 55 | final ClipboardManager clipboard = (ClipboardManager) getContext() |
| 56 | .getSystemService(Context.CLIPBOARD_SERVICE); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 57 | if (clipboard.hasPrimaryClip()) { |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 58 | bringPointIntoView(length()); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 59 | MenuInflater inflater = mode.getMenuInflater(); |
| 60 | inflater.inflate(R.menu.paste, menu); |
| 61 | return true; |
| 62 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 63 | // Prevents the selection action mode on double tap. |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | @Override |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 68 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 69 | return false; |
| 70 | } |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public void onDestroyActionMode(ActionMode mode) { |
| 74 | mActionMode = null; |
| 75 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 79 | super.onGetContentRect(mode, view, outRect); |
| 80 | outRect.top += getTotalPaddingTop(); |
| 81 | outRect.right -= getTotalPaddingRight(); |
| 82 | outRect.bottom -= getTotalPaddingBottom(); |
| 83 | // Encourage menu positioning towards the right, possibly over formula. |
| 84 | outRect.left = outRect.right; |
| 85 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 88 | // Temporary paint for use in layout methods. |
| 89 | private final TextPaint mTempPaint = new TextPaint(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 90 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 91 | private final float mMaximumTextSize; |
| 92 | private final float mMinimumTextSize; |
| 93 | private final float mStepTextSize; |
| 94 | |
| 95 | private int mWidthConstraint = -1; |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 96 | |
| 97 | private ActionMode mActionMode; |
| 98 | |
| 99 | private OnPasteListener mOnPasteListener; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 100 | private OnTextSizeChangeListener mOnTextSizeChangeListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 101 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 102 | public CalculatorText(Context context) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 103 | this(context, null /* attrs */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 106 | public CalculatorText(Context context, AttributeSet attrs) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 107 | this(context, attrs, 0 /* defStyleAttr */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 110 | public CalculatorText(Context context, AttributeSet attrs, int defStyleAttr) { |
| 111 | super(context, attrs, defStyleAttr); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 112 | |
| 113 | final TypedArray a = context.obtainStyledAttributes( |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 114 | attrs, R.styleable.CalculatorText, defStyleAttr, 0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 115 | mMaximumTextSize = a.getDimension( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 116 | R.styleable.CalculatorText_maxTextSize, getTextSize()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 117 | mMinimumTextSize = a.getDimension( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 118 | R.styleable.CalculatorText_minTextSize, getTextSize()); |
| 119 | mStepTextSize = a.getDimension(R.styleable.CalculatorText_stepTextSize, |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 120 | (mMaximumTextSize - mMinimumTextSize) / 3); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 121 | a.recycle(); |
| 122 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 123 | // Allow scrolling by default. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 124 | setMovementMethod(ScrollingMovementMethod.getInstance()); |
| 125 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 126 | // Reset the clickable flag, which is added when specifying a movement method. |
| 127 | setClickable(false); |
| 128 | |
| 129 | // Add a long click to start the ActionMode manually. |
| 130 | setOnLongClickListener(this); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 134 | public boolean onLongClick(View v) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 135 | mActionMode = startActionMode(mPasteActionModeCallback, ActionMode.TYPE_FLOATING); |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 136 | return true; |
| 137 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 141 | // Re-calculate our textSize based on new width. |
| 142 | final int width = MeasureSpec.getSize(widthMeasureSpec) |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 143 | - getPaddingLeft() - getPaddingRight(); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 144 | if (mWidthConstraint != width) { |
| 145 | mWidthConstraint = width; |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame^] | 146 | |
| 147 | if (!isLaidOut()) { |
| 148 | // Prevent shrinking/resizing with our variable textSize. |
| 149 | setTextSizeInternal(TypedValue.COMPLEX_UNIT_PX, mMaximumTextSize, |
| 150 | false /* notifyListener */); |
| 151 | setMinHeight(getLineHeight() + getCompoundPaddingBottom() |
| 152 | + getCompoundPaddingTop()); |
| 153 | } |
| 154 | |
| 155 | setTextSizeInternal(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(getText()), |
| 156 | false); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 162 | public int getWidthConstraint() { return mWidthConstraint; } |
| 163 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 164 | @Override |
| 165 | protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
| 166 | super.onTextChanged(text, start, lengthBefore, lengthAfter); |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 167 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 168 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(text.toString())); |
| 169 | } |
| 170 | |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame^] | 171 | private void setTextSizeInternal(int unit, float size, boolean notifyListener) { |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 172 | final float oldTextSize = getTextSize(); |
| 173 | super.setTextSize(unit, size); |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame^] | 174 | if (notifyListener && mOnTextSizeChangeListener != null && getTextSize() != oldTextSize) { |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 175 | mOnTextSizeChangeListener.onTextSizeChanged(this, oldTextSize); |
| 176 | } |
| 177 | } |
| 178 | |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame^] | 179 | @Override |
| 180 | public void setTextSize(int unit, float size) { |
| 181 | setTextSizeInternal(unit, size, true); |
| 182 | } |
| 183 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 184 | public float getMinimumTextSize() { |
| 185 | return mMinimumTextSize; |
| 186 | } |
| 187 | |
| 188 | public float getMaximumTextSize() { |
| 189 | return mMaximumTextSize; |
| 190 | } |
| 191 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 192 | public float getVariableTextSize(CharSequence text) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 193 | if (mWidthConstraint < 0 || mMaximumTextSize <= mMinimumTextSize) { |
| 194 | // Not measured, bail early. |
| 195 | return getTextSize(); |
| 196 | } |
| 197 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 198 | // Capture current paint state. |
| 199 | mTempPaint.set(getPaint()); |
| 200 | |
| 201 | // Step through increasing text sizes until the text would no longer fit. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 202 | float lastFitTextSize = mMinimumTextSize; |
| 203 | while (lastFitTextSize < mMaximumTextSize) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 204 | mTempPaint.setTextSize(Math.min(lastFitTextSize + mStepTextSize, mMaximumTextSize)); |
| 205 | if (Layout.getDesiredWidth(text, mTempPaint) > mWidthConstraint) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 206 | break; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 207 | } |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 208 | lastFitTextSize = mTempPaint.getTextSize(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return lastFitTextSize; |
| 212 | } |
| 213 | |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 214 | private static boolean startsWith(CharSequence whole, CharSequence prefix) { |
| 215 | int wholeLen = whole.length(); |
| 216 | int prefixLen = prefix.length(); |
| 217 | if (prefixLen > wholeLen) { |
| 218 | return false; |
| 219 | } |
| 220 | for (int i = 0; i < prefixLen; ++i) { |
| 221 | if (prefix.charAt(i) != whole.charAt(i)) { |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Functionally equivalent to setText(), but explicitly announce changes. |
| 230 | * If the new text is an extension of the old one, announce the addition. |
| 231 | * Otherwise, e.g. after deletion, announce the entire new text. |
| 232 | */ |
| 233 | public void changeTextTo(CharSequence newText) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 234 | final CharSequence oldText = getText(); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 235 | if (startsWith(newText, oldText)) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 236 | final int newLen = newText.length(); |
| 237 | final int oldLen = oldText.length(); |
| 238 | if (newLen == oldLen + 1) { |
| 239 | // The algorithm for pronouncing a single character doesn't seem |
| 240 | // to respect our hints. Don't give it the choice. |
| 241 | final char c = newText.charAt(oldLen); |
| 242 | final int id = KeyMaps.keyForChar(c); |
| 243 | final String descr = KeyMaps.toDescriptiveString(getContext(), id); |
| 244 | if (descr != null) { |
| 245 | announceForAccessibility(descr); |
| 246 | } else { |
| 247 | announceForAccessibility(String.valueOf(c)); |
| 248 | } |
| 249 | } else if (newLen > oldLen) { |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 250 | announceForAccessibility(newText.subSequence(oldLen, newLen)); |
| 251 | } |
| 252 | } else { |
| 253 | announceForAccessibility(newText); |
| 254 | } |
| 255 | setText(newText); |
| 256 | } |
| 257 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 258 | public boolean stopActionMode() { |
| 259 | if (mActionMode != null) { |
| 260 | mActionMode.finish(); |
| 261 | return true; |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 266 | public void setOnTextSizeChangeListener(OnTextSizeChangeListener listener) { |
| 267 | mOnTextSizeChangeListener = listener; |
| 268 | } |
| 269 | |
| 270 | public void setOnPasteListener(OnPasteListener listener) { |
| 271 | mOnPasteListener = listener; |
| 272 | } |
| 273 | |
| 274 | private void paste() { |
| 275 | final ClipboardManager clipboard = (ClipboardManager) getContext() |
| 276 | .getSystemService(Context.CLIPBOARD_SERVICE); |
| 277 | final ClipData primaryClip = clipboard.getPrimaryClip(); |
| 278 | if (primaryClip != null && mOnPasteListener != null) { |
| 279 | mOnPasteListener.onPaste(primaryClip); |
| 280 | } |
| 281 | } |
| 282 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 283 | public interface OnTextSizeChangeListener { |
| 284 | void onTextSizeChanged(TextView textView, float oldSize); |
| 285 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 286 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 287 | public interface OnPasteListener { |
| 288 | boolean onPaste(ClipData clip); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 289 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 290 | } |