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 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 19 | import android.content.ClipboardManager; |
| 20 | import android.content.ClipData; |
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; |
Hongwei Wang | 245925e | 2014-05-11 14:38:47 -0700 | [diff] [blame] | 23 | import android.graphics.Paint; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 24 | import android.graphics.Paint.FontMetricsInt; |
| 25 | import android.graphics.Rect; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 27 | import android.os.Parcelable; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 28 | import android.text.method.ScrollingMovementMethod; |
| 29 | import android.text.TextPaint; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 31 | import android.util.Log; |
Hongwei Wang | 245925e | 2014-05-11 14:38:47 -0700 | [diff] [blame] | 32 | import android.util.TypedValue; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 33 | import android.view.ActionMode; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 34 | import android.view.GestureDetector; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 35 | import android.view.Menu; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 36 | import android.view.MenuInflater; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 37 | import android.view.MenuItem; |
Gilles Debunne | 79525c3 | 2011-01-25 08:25:41 -0800 | [diff] [blame] | 38 | import android.view.MotionEvent; |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 39 | import android.view.View; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 40 | import android.widget.TextView; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 41 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 42 | /** |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 43 | * TextView adapted for Calculator display. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 44 | */ |
| 45 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 46 | public class CalculatorText extends TextView implements View.OnLongClickListener{ |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 47 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 48 | private ActionMode mActionMode; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 49 | |
| 50 | private final ActionMode.Callback mPasteActionModeCallback = |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 51 | new ActionMode.Callback() { |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 52 | @Override |
| 53 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 54 | switch (item.getItemId()) { |
| 55 | case R.id.menu_paste: |
| 56 | pasteContent(); |
| 57 | mode.finish(); |
| 58 | return true; |
| 59 | default: |
| 60 | return false; |
| 61 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 66 | ClipboardManager clipboard = |
| 67 | (ClipboardManager) getContext().getSystemService( |
| 68 | Context.CLIPBOARD_SERVICE); |
| 69 | if (clipboard.hasPrimaryClip()) { |
| 70 | MenuInflater inflater = mode.getMenuInflater(); |
| 71 | inflater.inflate(R.menu.paste, menu); |
| 72 | return true; |
| 73 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 74 | // Prevents the selection action mode on double tap. |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | @Override |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 79 | public void onDestroyActionMode(ActionMode mode) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 80 | mActionMode = null; |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 81 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 85 | return false; |
| 86 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 89 | private PasteListener mPasteListener; |
| 90 | |
| 91 | public void setPasteListener(PasteListener pasteListener) { |
| 92 | mPasteListener = pasteListener; |
| 93 | } |
| 94 | |
| 95 | private void pasteContent() { |
| 96 | ClipboardManager clipboard = |
| 97 | (ClipboardManager) getContext().getSystemService( |
| 98 | Context.CLIPBOARD_SERVICE); |
| 99 | ClipData cd = clipboard.getPrimaryClip(); |
| 100 | ClipData.Item item = cd.getItemAt(0); |
| 101 | // TODO: Should we handle multiple selections? |
| 102 | Uri uri = item.getUri(); |
| 103 | if (uri == null || !mPasteListener.paste(uri)) { |
| 104 | mPasteListener.paste(item.coerceToText(getContext()).toString()); |
| 105 | } |
| 106 | } |
| 107 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 108 | private final float mMaximumTextSize; |
| 109 | private final float mMinimumTextSize; |
| 110 | private final float mStepTextSize; |
| 111 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 112 | // Temporary objects for use in layout methods. |
| 113 | private final Paint mTempPaint = new TextPaint(); |
| 114 | private final Rect mTempRect = new Rect(); |
| 115 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 116 | private int mWidthConstraint = -1; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 117 | private OnTextSizeChangeListener mOnTextSizeChangeListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 118 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 119 | public CalculatorText(Context context) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 120 | this(context, null); |
| 121 | } |
| 122 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 123 | public CalculatorText(Context context, AttributeSet attrs) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 124 | this(context, attrs, 0); |
| 125 | } |
| 126 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 127 | public CalculatorText(Context context, AttributeSet attrs, int defStyle) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 128 | super(context, attrs, defStyle); |
| 129 | |
| 130 | final TypedArray a = context.obtainStyledAttributes( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 131 | attrs, R.styleable.CalculatorText, defStyle, 0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 132 | mMaximumTextSize = a.getDimension( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 133 | R.styleable.CalculatorText_maxTextSize, getTextSize()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 134 | mMinimumTextSize = a.getDimension( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 135 | R.styleable.CalculatorText_minTextSize, getTextSize()); |
| 136 | mStepTextSize = a.getDimension(R.styleable.CalculatorText_stepTextSize, |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 137 | (mMaximumTextSize - mMinimumTextSize) / 3); |
| 138 | |
| 139 | a.recycle(); |
| 140 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 141 | // Paste ActionMode is triggered explicitly, not through |
| 142 | // setCustomSelectionActionModeCallback. |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 143 | setOnLongClickListener(this); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 144 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 145 | // Enable scrolling |
| 146 | setMovementMethod(ScrollingMovementMethod.getInstance()); |
| 147 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 148 | setTextSize(TypedValue.COMPLEX_UNIT_PX, mMaximumTextSize); |
| 149 | setMinHeight(getLineHeight() + getCompoundPaddingBottom() + getCompoundPaddingTop()); |
| 150 | } |
| 151 | |
| 152 | @Override |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 153 | public boolean onLongClick(View v) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 154 | mActionMode = startActionMode(mPasteActionModeCallback, ActionMode.TYPE_FLOATING); |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 155 | return true; |
| 156 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 160 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 161 | |
| 162 | mWidthConstraint = |
| 163 | MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); |
| 164 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(getText().toString())); |
| 165 | } |
| 166 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 167 | public int getWidthConstraint() { return mWidthConstraint; } |
| 168 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 169 | @Override |
| 170 | protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
| 171 | super.onTextChanged(text, start, lengthBefore, lengthAfter); |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 172 | |
| 173 | final int textLength = text.length(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 174 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(text.toString())); |
| 175 | } |
| 176 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 177 | @Override |
| 178 | public void setTextSize(int unit, float size) { |
| 179 | final float oldTextSize = getTextSize(); |
| 180 | super.setTextSize(unit, size); |
| 181 | |
| 182 | if (mOnTextSizeChangeListener != null && getTextSize() != oldTextSize) { |
| 183 | mOnTextSizeChangeListener.onTextSizeChanged(this, oldTextSize); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | public void setOnTextSizeChangeListener(OnTextSizeChangeListener listener) { |
| 188 | mOnTextSizeChangeListener = listener; |
| 189 | } |
| 190 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 191 | public float getVariableTextSize(String text) { |
| 192 | if (mWidthConstraint < 0 || mMaximumTextSize <= mMinimumTextSize) { |
| 193 | // Not measured, bail early. |
| 194 | return getTextSize(); |
| 195 | } |
| 196 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 197 | // Capture current paint state. |
| 198 | mTempPaint.set(getPaint()); |
| 199 | |
| 200 | // Step through increasing text sizes until the text would no longer fit. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 201 | float lastFitTextSize = mMinimumTextSize; |
| 202 | while (lastFitTextSize < mMaximumTextSize) { |
| 203 | final float nextSize = Math.min(lastFitTextSize + mStepTextSize, mMaximumTextSize); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 204 | mTempPaint.setTextSize(nextSize); |
| 205 | if (mTempPaint.measureText(text) > mWidthConstraint) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 206 | break; |
| 207 | } else { |
| 208 | lastFitTextSize = nextSize; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return lastFitTextSize; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public int getCompoundPaddingTop() { |
| 217 | // Measure the top padding from the capital letter height of the text instead of the top, |
| 218 | // 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] | 219 | getPaint().getTextBounds("H", 0, 1, mTempRect); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 220 | |
| 221 | final FontMetricsInt fontMetrics = getPaint().getFontMetricsInt(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 222 | final int paddingOffset = -(fontMetrics.ascent + mTempRect.height()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 223 | return super.getCompoundPaddingTop() - Math.min(getPaddingTop(), paddingOffset); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public int getCompoundPaddingBottom() { |
| 228 | // Measure the bottom padding from the baseline of the text instead of the bottom, but don't |
| 229 | // remove more than the available bottom padding otherwise clipping may occur. |
| 230 | final FontMetricsInt fontMetrics = getPaint().getFontMetricsInt(); |
| 231 | return super.getCompoundPaddingBottom() - Math.min(getPaddingBottom(), fontMetrics.descent); |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 232 | } |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 233 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 234 | public boolean stopActionMode() { |
| 235 | if (mActionMode != null) { |
| 236 | mActionMode.finish(); |
| 237 | return true; |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 242 | public interface OnTextSizeChangeListener { |
| 243 | void onTextSizeChanged(TextView textView, float oldSize); |
| 244 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 245 | |
| 246 | public interface PasteListener { |
| 247 | void paste(String s); |
| 248 | boolean paste(Uri u); |
| 249 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 250 | } |