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; |
Hongwei Wang | 245925e | 2014-05-11 14:38:47 -0700 | [diff] [blame] | 23 | import android.graphics.Paint; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
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 | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 41 | private ActionMode mActionMode; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 42 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 43 | private final ActionMode.Callback mPasteActionModeCallback = 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) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 46 | switch (item.getItemId()) { |
| 47 | case R.id.menu_paste: |
| 48 | pasteContent(); |
| 49 | mode.finish(); |
| 50 | return true; |
| 51 | default: |
| 52 | return false; |
| 53 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 58 | ClipboardManager clipboard = |
| 59 | (ClipboardManager) getContext().getSystemService( |
| 60 | Context.CLIPBOARD_SERVICE); |
| 61 | if (clipboard.hasPrimaryClip()) { |
| 62 | MenuInflater inflater = mode.getMenuInflater(); |
| 63 | inflater.inflate(R.menu.paste, menu); |
| 64 | return true; |
| 65 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 66 | // Prevents the selection action mode on double tap. |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | @Override |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 71 | public void onDestroyActionMode(ActionMode mode) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 72 | mActionMode = null; |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 73 | } |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 74 | |
| 75 | @Override |
| 76 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 77 | return false; |
| 78 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 81 | private PasteListener mPasteListener; |
| 82 | |
| 83 | public void setPasteListener(PasteListener pasteListener) { |
| 84 | mPasteListener = pasteListener; |
| 85 | } |
| 86 | |
| 87 | private void pasteContent() { |
| 88 | ClipboardManager clipboard = |
| 89 | (ClipboardManager) getContext().getSystemService( |
| 90 | Context.CLIPBOARD_SERVICE); |
| 91 | ClipData cd = clipboard.getPrimaryClip(); |
| 92 | ClipData.Item item = cd.getItemAt(0); |
| 93 | // TODO: Should we handle multiple selections? |
| 94 | Uri uri = item.getUri(); |
| 95 | if (uri == null || !mPasteListener.paste(uri)) { |
| 96 | mPasteListener.paste(item.coerceToText(getContext()).toString()); |
| 97 | } |
| 98 | } |
| 99 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 100 | private final float mMaximumTextSize; |
| 101 | private final float mMinimumTextSize; |
| 102 | private final float mStepTextSize; |
| 103 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 104 | // Temporary paint for use in layout methods. |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 105 | private final Paint mTempPaint = new TextPaint(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 106 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 107 | private int mWidthConstraint = -1; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 108 | private OnTextSizeChangeListener mOnTextSizeChangeListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 109 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 110 | public CalculatorText(Context context) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 111 | this(context, null); |
| 112 | } |
| 113 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 114 | public CalculatorText(Context context, AttributeSet attrs) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 115 | this(context, attrs, 0); |
| 116 | } |
| 117 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 118 | public CalculatorText(Context context, AttributeSet attrs, int defStyle) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 119 | super(context, attrs, defStyle); |
| 120 | |
| 121 | final TypedArray a = context.obtainStyledAttributes( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 122 | attrs, R.styleable.CalculatorText, defStyle, 0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 123 | mMaximumTextSize = a.getDimension( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 124 | R.styleable.CalculatorText_maxTextSize, getTextSize()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 125 | mMinimumTextSize = a.getDimension( |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 126 | R.styleable.CalculatorText_minTextSize, getTextSize()); |
| 127 | mStepTextSize = a.getDimension(R.styleable.CalculatorText_stepTextSize, |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 128 | (mMaximumTextSize - mMinimumTextSize) / 3); |
| 129 | |
| 130 | a.recycle(); |
| 131 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 132 | // Paste ActionMode is triggered explicitly, not through |
| 133 | // setCustomSelectionActionModeCallback. |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 134 | setOnLongClickListener(this); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 135 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 136 | // Enable scrolling |
| 137 | setMovementMethod(ScrollingMovementMethod.getInstance()); |
| 138 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 139 | setTextSize(TypedValue.COMPLEX_UNIT_PX, mMaximumTextSize); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 143 | public boolean onLongClick(View v) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 144 | mActionMode = startActionMode(mPasteActionModeCallback, ActionMode.TYPE_FLOATING); |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 145 | return true; |
| 146 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 147 | |
| 148 | @Override |
| 149 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 150 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 151 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 152 | // Prevent shrinking/resizing with our variable textSize. |
| 153 | if (!isLaidOut()) { |
| 154 | setMinHeight(getLineHeight() + getCompoundPaddingBottom() + getCompoundPaddingTop()); |
| 155 | } |
| 156 | |
| 157 | mWidthConstraint = MeasureSpec.getSize(widthMeasureSpec) |
| 158 | - getPaddingLeft() - getPaddingRight(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 159 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(getText().toString())); |
| 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 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 171 | @Override |
| 172 | public void setTextSize(int unit, float size) { |
| 173 | final float oldTextSize = getTextSize(); |
| 174 | super.setTextSize(unit, size); |
| 175 | |
| 176 | if (mOnTextSizeChangeListener != null && getTextSize() != oldTextSize) { |
| 177 | mOnTextSizeChangeListener.onTextSizeChanged(this, oldTextSize); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public void setOnTextSizeChangeListener(OnTextSizeChangeListener listener) { |
| 182 | mOnTextSizeChangeListener = listener; |
| 183 | } |
| 184 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 185 | public float getMinimumTextSize() { |
| 186 | return mMinimumTextSize; |
| 187 | } |
| 188 | |
| 189 | public float getMaximumTextSize() { |
| 190 | return mMaximumTextSize; |
| 191 | } |
| 192 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 193 | public float getVariableTextSize(String text) { |
| 194 | if (mWidthConstraint < 0 || mMaximumTextSize <= mMinimumTextSize) { |
| 195 | // Not measured, bail early. |
| 196 | return getTextSize(); |
| 197 | } |
| 198 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 199 | // Capture current paint state. |
| 200 | mTempPaint.set(getPaint()); |
| 201 | |
| 202 | // Step through increasing text sizes until the text would no longer fit. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 203 | float lastFitTextSize = mMinimumTextSize; |
| 204 | while (lastFitTextSize < mMaximumTextSize) { |
| 205 | final float nextSize = Math.min(lastFitTextSize + mStepTextSize, mMaximumTextSize); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 206 | mTempPaint.setTextSize(nextSize); |
| 207 | if (mTempPaint.measureText(text) > mWidthConstraint) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 208 | break; |
| 209 | } else { |
| 210 | lastFitTextSize = nextSize; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return lastFitTextSize; |
| 215 | } |
| 216 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 217 | public boolean stopActionMode() { |
| 218 | if (mActionMode != null) { |
| 219 | mActionMode.finish(); |
| 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 225 | public interface OnTextSizeChangeListener { |
| 226 | void onTextSizeChanged(TextView textView, float oldSize); |
| 227 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 228 | |
| 229 | public interface PasteListener { |
| 230 | void paste(String s); |
| 231 | boolean paste(Uri u); |
| 232 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 233 | } |