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 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 20 | import android.content.ClipData; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 21 | import android.content.ClipboardManager; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 25 | import android.os.Build; |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 26 | import android.text.Layout; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 27 | import android.text.TextPaint; |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Annie Chin | 7d50adf | 2016-10-27 15:55:38 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Hongwei Wang | 245925e | 2014-05-11 14:38:47 -0700 | [diff] [blame] | 31 | import android.util.TypedValue; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 32 | import android.view.ActionMode; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 33 | import android.view.ContextMenu; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 34 | import android.view.Menu; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 35 | import android.view.MenuInflater; |
Gilles Debunne | f57b8b4 | 2011-01-27 10:54:07 -0800 | [diff] [blame] | 36 | import android.view.MenuItem; |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 37 | import android.view.View; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 38 | import android.widget.TextView; |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 39 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 40 | /** |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 41 | * TextView adapted for displaying the formula and allowing pasting. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 42 | */ |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 43 | public class CalculatorFormula extends AlignedTextView implements MenuItem.OnMenuItemClickListener, |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 44 | ClipboardManager.OnPrimaryClipChangedListener { |
Alan Viverette | 461992d | 2014-03-07 13:29:56 -0800 | [diff] [blame] | 45 | |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 46 | public static final String TAG_ACTION_MODE = "ACTION_MODE"; |
| 47 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 48 | // Temporary paint for use in layout methods. |
| 49 | private final TextPaint mTempPaint = new TextPaint(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 50 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 51 | private final float mMaximumTextSize; |
| 52 | private final float mMinimumTextSize; |
| 53 | private final float mStepTextSize; |
| 54 | |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 55 | private final ClipboardManager mClipboardManager; |
| 56 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 57 | private int mWidthConstraint = -1; |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame^] | 58 | private ActionMode mActionMode; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 59 | private ActionMode.Callback mPasteActionModeCallback; |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame^] | 60 | private ContextMenu mContextMenu; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 61 | private OnTextSizeChangeListener mOnTextSizeChangeListener; |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 62 | private OnFormulaContextMenuClickListener mOnContextMenuClickListener; |
| 63 | private Calculator.OnDisplayMemoryOperationsListener mOnDisplayMemoryOperationsListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 64 | |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 65 | public CalculatorFormula(Context context) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 66 | this(context, null /* attrs */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 69 | public CalculatorFormula(Context context, AttributeSet attrs) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 70 | this(context, attrs, 0 /* defStyleAttr */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 73 | public CalculatorFormula(Context context, AttributeSet attrs, int defStyleAttr) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 74 | super(context, attrs, defStyleAttr); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 75 | |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 76 | mClipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); |
| 77 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 78 | final TypedArray a = context.obtainStyledAttributes( |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 79 | attrs, R.styleable.CalculatorFormula, defStyleAttr, 0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 80 | mMaximumTextSize = a.getDimension( |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 81 | R.styleable.CalculatorFormula_maxTextSize, getTextSize()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 82 | mMinimumTextSize = a.getDimension( |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 83 | R.styleable.CalculatorFormula_minTextSize, getTextSize()); |
| 84 | mStepTextSize = a.getDimension(R.styleable.CalculatorFormula_stepTextSize, |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 85 | (mMaximumTextSize - mMinimumTextSize) / 3); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 86 | a.recycle(); |
| 87 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 88 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 89 | setupActionMode(); |
| 90 | } else { |
| 91 | setupContextMenu(); |
| 92 | } |
Hans Boehm | 76b7815 | 2015-04-17 10:50:35 -0700 | [diff] [blame] | 93 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Justin Klaassen | 0ace4eb | 2016-02-05 11:38:12 -0800 | [diff] [blame] | 97 | if (!isLaidOut()) { |
| 98 | // Prevent shrinking/resizing with our variable textSize. |
| 99 | setTextSizeInternal(TypedValue.COMPLEX_UNIT_PX, mMaximumTextSize, |
| 100 | false /* notifyListener */); |
Justin Klaassen | f3076c8 | 2016-06-03 13:18:55 -0700 | [diff] [blame] | 101 | setMinimumHeight(getLineHeight() + getCompoundPaddingBottom() |
Justin Klaassen | 0ace4eb | 2016-02-05 11:38:12 -0800 | [diff] [blame] | 102 | + getCompoundPaddingTop()); |
| 103 | } |
| 104 | |
Justin Klaassen | f3076c8 | 2016-06-03 13:18:55 -0700 | [diff] [blame] | 105 | // Ensure we are at least as big as our parent. |
| 106 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
| 107 | if (getMinimumWidth() != width) { |
| 108 | setMinimumWidth(width); |
| 109 | } |
| 110 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 111 | // Re-calculate our textSize based on new width. |
Justin Klaassen | 0ace4eb | 2016-02-05 11:38:12 -0800 | [diff] [blame] | 112 | mWidthConstraint = MeasureSpec.getSize(widthMeasureSpec) |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 113 | - getPaddingLeft() - getPaddingRight(); |
Justin Klaassen | 0ace4eb | 2016-02-05 11:38:12 -0800 | [diff] [blame] | 114 | final float textSize = getVariableTextSize(getText()); |
| 115 | if (getTextSize() != textSize) { |
| 116 | setTextSizeInternal(TypedValue.COMPLEX_UNIT_PX, textSize, false /* notifyListener */); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 123 | protected void onAttachedToWindow() { |
| 124 | super.onAttachedToWindow(); |
| 125 | |
| 126 | mClipboardManager.addPrimaryClipChangedListener(this); |
| 127 | onPrimaryClipChanged(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | protected void onDetachedFromWindow() { |
| 132 | super.onDetachedFromWindow(); |
| 133 | |
| 134 | mClipboardManager.removePrimaryClipChangedListener(this); |
| 135 | } |
| 136 | |
| 137 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 138 | protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
| 139 | super.onTextChanged(text, start, lengthBefore, lengthAfter); |
Justin Klaassen | bfc4e4d | 2014-08-27 10:56:49 -0700 | [diff] [blame] | 140 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 141 | setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(text.toString())); |
| 142 | } |
| 143 | |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame] | 144 | private void setTextSizeInternal(int unit, float size, boolean notifyListener) { |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 145 | final float oldTextSize = getTextSize(); |
| 146 | super.setTextSize(unit, size); |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame] | 147 | if (notifyListener && mOnTextSizeChangeListener != null && getTextSize() != oldTextSize) { |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 148 | mOnTextSizeChangeListener.onTextSizeChanged(this, oldTextSize); |
| 149 | } |
| 150 | } |
| 151 | |
Hans Boehm | 11e37a8 | 2015-10-01 14:41:37 -0700 | [diff] [blame] | 152 | @Override |
| 153 | public void setTextSize(int unit, float size) { |
| 154 | setTextSizeInternal(unit, size, true); |
| 155 | } |
| 156 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 157 | public float getMinimumTextSize() { |
| 158 | return mMinimumTextSize; |
| 159 | } |
| 160 | |
| 161 | public float getMaximumTextSize() { |
| 162 | return mMaximumTextSize; |
| 163 | } |
| 164 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 165 | public float getVariableTextSize(CharSequence text) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 166 | if (mWidthConstraint < 0 || mMaximumTextSize <= mMinimumTextSize) { |
| 167 | // Not measured, bail early. |
| 168 | return getTextSize(); |
| 169 | } |
| 170 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 171 | // Capture current paint state. |
| 172 | mTempPaint.set(getPaint()); |
| 173 | |
| 174 | // Step through increasing text sizes until the text would no longer fit. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 175 | float lastFitTextSize = mMinimumTextSize; |
| 176 | while (lastFitTextSize < mMaximumTextSize) { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 177 | mTempPaint.setTextSize(Math.min(lastFitTextSize + mStepTextSize, mMaximumTextSize)); |
| 178 | if (Layout.getDesiredWidth(text, mTempPaint) > mWidthConstraint) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 179 | break; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 180 | } |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 181 | lastFitTextSize = mTempPaint.getTextSize(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return lastFitTextSize; |
| 185 | } |
| 186 | |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 187 | /** |
| 188 | * Functionally equivalent to setText(), but explicitly announce changes. |
| 189 | * If the new text is an extension of the old one, announce the addition. |
| 190 | * Otherwise, e.g. after deletion, announce the entire new text. |
| 191 | */ |
| 192 | public void changeTextTo(CharSequence newText) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 193 | final CharSequence oldText = getText(); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 194 | final char separator = KeyMaps.translateResult(",").charAt(0); |
| 195 | final CharSequence added = StringUtils.getExtensionIgnoring(newText, oldText, separator); |
| 196 | if (added != null) { |
| 197 | if (added.length() == 1) { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 198 | // The algorithm for pronouncing a single character doesn't seem |
| 199 | // to respect our hints. Don't give it the choice. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 200 | final char c = added.charAt(0); |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 201 | final int id = KeyMaps.keyForChar(c); |
| 202 | final String descr = KeyMaps.toDescriptiveString(getContext(), id); |
| 203 | if (descr != null) { |
| 204 | announceForAccessibility(descr); |
| 205 | } else { |
| 206 | announceForAccessibility(String.valueOf(c)); |
| 207 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 208 | } else if (added.length() != 0) { |
| 209 | announceForAccessibility(added); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 210 | } |
| 211 | } else { |
| 212 | announceForAccessibility(newText); |
| 213 | } |
Justin Klaassen | d183141 | 2016-07-19 21:59:10 -0700 | [diff] [blame] | 214 | setText(newText, BufferType.SPANNABLE); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 217 | public boolean stopActionModeOrContextMenu() { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 218 | if (mActionMode != null) { |
| 219 | mActionMode.finish(); |
| 220 | return true; |
| 221 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 222 | if (mContextMenu != null) { |
| 223 | mContextMenu.close(); |
| 224 | return true; |
| 225 | } |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 229 | public void setOnTextSizeChangeListener(OnTextSizeChangeListener listener) { |
| 230 | mOnTextSizeChangeListener = listener; |
| 231 | } |
| 232 | |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 233 | public void setOnContextMenuClickListener(OnFormulaContextMenuClickListener listener) { |
| 234 | mOnContextMenuClickListener = listener; |
| 235 | } |
| 236 | |
| 237 | public void setOnDisplayMemoryOperationsListener( |
| 238 | Calculator.OnDisplayMemoryOperationsListener listener) { |
| 239 | mOnDisplayMemoryOperationsListener = listener; |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Use ActionMode for paste support on M and higher. |
| 244 | */ |
| 245 | @TargetApi(Build.VERSION_CODES.M) |
| 246 | private void setupActionMode() { |
| 247 | mPasteActionModeCallback = new ActionMode.Callback2() { |
| 248 | |
| 249 | @Override |
| 250 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 251 | if (onMenuItemClick(item)) { |
| 252 | mode.finish(); |
| 253 | return true; |
| 254 | } else { |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 261 | mode.setTag(TAG_ACTION_MODE); |
| 262 | final MenuInflater inflater = mode.getMenuInflater(); |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 263 | return createContextMenu(inflater, menu); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onDestroyActionMode(ActionMode mode) { |
| 273 | mActionMode = null; |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 278 | super.onGetContentRect(mode, view, outRect); |
| 279 | outRect.top += getTotalPaddingTop(); |
| 280 | outRect.right -= getTotalPaddingRight(); |
| 281 | outRect.bottom -= getTotalPaddingBottom(); |
Christine Franks | be739a1 | 2016-10-20 12:45:55 -0700 | [diff] [blame] | 282 | // Encourage menu positioning over the rightmost 10% of the screen. |
| 283 | outRect.left = (int) (outRect.right * 0.9f); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 284 | } |
| 285 | }; |
| 286 | setOnLongClickListener(new View.OnLongClickListener() { |
| 287 | @Override |
| 288 | public boolean onLongClick(View v) { |
| 289 | mActionMode = startActionMode(mPasteActionModeCallback, ActionMode.TYPE_FLOATING); |
| 290 | return true; |
| 291 | } |
| 292 | }); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Use ContextMenu for paste support on L and lower. |
| 297 | */ |
| 298 | private void setupContextMenu() { |
| 299 | setOnCreateContextMenuListener(new OnCreateContextMenuListener() { |
| 300 | @Override |
| 301 | public void onCreateContextMenu(ContextMenu contextMenu, View view, |
| 302 | ContextMenu.ContextMenuInfo contextMenuInfo) { |
| 303 | final MenuInflater inflater = new MenuInflater(getContext()); |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 304 | createContextMenu(inflater, contextMenu); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 305 | mContextMenu = contextMenu; |
Hans Boehm | 9c160b4 | 2016-12-02 11:55:12 -0800 | [diff] [blame] | 306 | for (int i = 0; i < contextMenu.size(); i++) { |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 307 | contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorFormula.this); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | }); |
| 311 | setOnLongClickListener(new View.OnLongClickListener() { |
| 312 | @Override |
| 313 | public boolean onLongClick(View v) { |
| 314 | return showContextMenu(); |
| 315 | } |
| 316 | }); |
| 317 | } |
| 318 | |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 319 | private boolean createContextMenu(MenuInflater inflater, Menu menu) { |
Christine Franks | 61c0ed9 | 2016-12-08 15:03:53 -0800 | [diff] [blame] | 320 | final boolean isPasteEnabled = isPasteEnabled(); |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 321 | final boolean isMemoryEnabled = isMemoryEnabled(); |
| 322 | if (!isPasteEnabled && !isMemoryEnabled) { |
| 323 | return false; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 324 | } |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 325 | |
| 326 | bringPointIntoView(length()); |
| 327 | inflater.inflate(R.menu.menu_formula, menu); |
| 328 | final MenuItem pasteItem = menu.findItem(R.id.menu_paste); |
| 329 | final MenuItem memoryRecallItem = menu.findItem(R.id.memory_recall); |
| 330 | pasteItem.setEnabled(isPasteEnabled); |
| 331 | memoryRecallItem.setEnabled(isMemoryEnabled); |
| 332 | return true; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 335 | private void paste() { |
Christine Franks | 61c0ed9 | 2016-12-08 15:03:53 -0800 | [diff] [blame] | 336 | final ClipData primaryClip = mClipboardManager.getPrimaryClip(); |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 337 | if (primaryClip != null && mOnContextMenuClickListener != null) { |
| 338 | mOnContextMenuClickListener.onPaste(primaryClip); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 342 | @Override |
| 343 | public boolean onMenuItemClick(MenuItem item) { |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 344 | switch (item.getItemId()) { |
| 345 | case R.id.memory_recall: |
| 346 | mOnContextMenuClickListener.onMemoryRecall(); |
| 347 | return true; |
| 348 | case R.id.menu_paste: |
| 349 | paste(); |
| 350 | return true; |
| 351 | default: |
| 352 | return false; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 353 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 356 | @Override |
| 357 | public void onPrimaryClipChanged() { |
Christine Franks | 61c0ed9 | 2016-12-08 15:03:53 -0800 | [diff] [blame] | 358 | setLongClickable(isPasteEnabled() || isMemoryEnabled()); |
| 359 | } |
| 360 | |
| 361 | public void onMemoryStateChanged() { |
| 362 | setLongClickable(isPasteEnabled() || isMemoryEnabled()); |
| 363 | } |
| 364 | |
| 365 | private boolean isMemoryEnabled() { |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame^] | 366 | return mOnDisplayMemoryOperationsListener != null |
Christine Franks | 61c0ed9 | 2016-12-08 15:03:53 -0800 | [diff] [blame] | 367 | && mOnDisplayMemoryOperationsListener.shouldDisplayMemory(); |
| 368 | } |
| 369 | |
| 370 | private boolean isPasteEnabled() { |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 371 | final ClipData clip = mClipboardManager.getPrimaryClip(); |
| 372 | if (clip == null || clip.getItemCount() == 0) { |
Christine Franks | 61c0ed9 | 2016-12-08 15:03:53 -0800 | [diff] [blame] | 373 | return false; |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 374 | } |
Annie Chin | 7d50adf | 2016-10-27 15:55:38 -0700 | [diff] [blame] | 375 | CharSequence clipText = null; |
| 376 | try { |
| 377 | clipText = clip.getItemAt(0).coerceToText(getContext()); |
| 378 | } catch (Exception e) { |
| 379 | Log.i("Calculator", "Error reading clipboard:", e); |
| 380 | } |
Christine Franks | 61c0ed9 | 2016-12-08 15:03:53 -0800 | [diff] [blame] | 381 | return !TextUtils.isEmpty(clipText); |
Christine Franks | afe28bb | 2016-07-29 17:24:52 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 384 | public interface OnTextSizeChangeListener { |
| 385 | void onTextSizeChanged(TextView textView, float oldSize); |
| 386 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 387 | |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 388 | public interface OnFormulaContextMenuClickListener { |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 389 | boolean onPaste(ClipData clip); |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 390 | void onMemoryRecall(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 391 | } |
Dmitri Plotnikov | de3eec2 | 2011-01-17 18:23:37 -0800 | [diff] [blame] | 392 | } |