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