Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1 | /* |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -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 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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; |
| 21 | import android.content.ClipDescription; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 22 | import android.content.ClipboardManager; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 23 | import android.content.Context; |
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; |
| 26 | import android.support.v4.content.ContextCompat; |
Justin Klaassen | f1b61f4 | 2016-04-27 16:00:11 -0700 | [diff] [blame] | 27 | import android.support.v4.os.BuildCompat; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 28 | import android.text.Layout; |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 29 | import android.text.Spannable; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 30 | import android.text.SpannableString; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 31 | import android.text.Spanned; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 32 | import android.text.TextPaint; |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 33 | import android.text.style.BackgroundColorSpan; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 34 | import android.text.style.ForegroundColorSpan; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 36 | import android.view.ActionMode; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 37 | import android.view.ContextMenu; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 38 | import android.view.GestureDetector; |
| 39 | import android.view.Menu; |
| 40 | import android.view.MenuInflater; |
| 41 | import android.view.MenuItem; |
| 42 | import android.view.MotionEvent; |
| 43 | import android.view.View; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 44 | import android.widget.OverScroller; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 45 | import android.widget.Toast; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 46 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 47 | // A text widget that is "infinitely" scrollable to the right, |
| 48 | // and obtains the text to display via a callback to Logic. |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 49 | public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenuItemClickListener { |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 50 | static final int MAX_RIGHT_SCROLL = 10000000; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 51 | static final int INVALID = MAX_RIGHT_SCROLL + 10000; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 52 | // A larger value is unlikely to avoid running out of space |
| 53 | final OverScroller mScroller; |
| 54 | final GestureDetector mGestureDetector; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 55 | private Evaluator mEvaluator; |
| 56 | private boolean mScrollable = false; |
| 57 | // A scrollable result is currently displayed. |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 58 | private boolean mValid = false; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 59 | // The result holds something valid; either a a number or an error |
| 60 | // message. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 61 | // A suffix of "Pos" denotes a pixel offset. Zero represents a scroll position |
| 62 | // in which the decimal point is just barely visible on the right of the display. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 63 | private int mCurrentPos;// Position of right of display relative to decimal point, in pixels. |
| 64 | // Large positive values mean the decimal point is scrolled off the |
| 65 | // left of the display. Zero means decimal point is barely displayed |
| 66 | // on the right. |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 67 | private int mLastPos; // Position already reflected in display. Pixels. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 68 | private int mMinPos; // Minimum position to avoid unnecessary blanks on the left. Pixels. |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 69 | private int mMaxPos; // Maximum position before we start displaying the infinite |
| 70 | // sequence of trailing zeroes on the right. Pixels. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 71 | private int mWholeLen; // Length of the whole part of current result. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 72 | // In the following, we use a suffix of Offset to denote a character position in a numeric |
| 73 | // string relative to the decimal point. Positive is to the right and negative is to |
| 74 | // the left. 1 = tenths position, -1 = units. Integer.MAX_VALUE is sometimes used |
| 75 | // for the offset of the last digit in an a nonterminating decimal expansion. |
| 76 | // We use the suffix "Index" to denote a zero-based index into a string representing a |
| 77 | // result. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 78 | private int mMaxCharOffset; // Character offset from decimal point of rightmost digit |
| 79 | // that should be displayed. Essentially the same as |
| 80 | private int mLsdOffset; // Position of least-significant digit in result |
| 81 | private int mLastDisplayedOffset; // Offset of last digit actually displayed after adding |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 82 | // exponent. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 83 | private final Object mWidthLock = new Object(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 84 | // Protects the next two fields. |
| 85 | private int mWidthConstraint = -1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 86 | // Our total width in pixels minus space for ellipsis. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 87 | private float mCharWidth = 1; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 88 | // Maximum character width. For now we pretend that all characters |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 89 | // have this width. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 90 | // TODO: We're not really using a fixed width font. But it appears |
| 91 | // to be close enough for the characters we use that the difference |
| 92 | // is not noticeable. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 93 | private static final int MAX_WIDTH = 100; |
| 94 | // Maximum number of digits displayed |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 95 | public static final int MAX_LEADING_ZEROES = 6; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 96 | // Maximum number of leading zeroes after decimal point before we |
| 97 | // switch to scientific notation with negative exponent. |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 98 | public static final int MAX_TRAILING_ZEROES = 6; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 99 | // Maximum number of trailing zeroes before the decimal point before |
| 100 | // we switch to scientific notation with positive exponent. |
| 101 | private static final int SCI_NOTATION_EXTRA = 1; |
| 102 | // Extra digits for standard scientific notation. In this case we |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 103 | // have a decimal point and no ellipsis. |
| 104 | // We assume that we do not drop digits to make room for the decimal |
| 105 | // point in ordinary scientific notation. Thus >= 1. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 106 | private static final int MAX_COPY_EXTRA = 100; |
| 107 | // The number of extra digits we are willing to compute to copy |
| 108 | // a result as an exact number. |
| 109 | private static final int MAX_RECOMPUTE_DIGITS = 2000; |
| 110 | // The maximum number of digits we're willing to recompute in the UI |
| 111 | // thread. We only do this for known rational results, where we |
| 112 | // can bound the computation cost. |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 113 | private final ForegroundColorSpan mExponentColorSpan; |
| 114 | private final BackgroundColorSpan mHighlightSpan; |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 115 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 116 | private ActionMode mActionMode; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 117 | private ActionMode.Callback mCopyActionModeCallback; |
| 118 | private ContextMenu mContextMenu; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 119 | |
| 120 | public CalculatorResult(Context context, AttributeSet attrs) { |
| 121 | super(context, attrs); |
| 122 | mScroller = new OverScroller(context); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 123 | mHighlightSpan = new BackgroundColorSpan(getHighlightColor()); |
| 124 | mExponentColorSpan = new ForegroundColorSpan( |
| 125 | ContextCompat.getColor(context, R.color.display_result_exponent_text_color)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 126 | mGestureDetector = new GestureDetector(context, |
| 127 | new GestureDetector.SimpleOnGestureListener() { |
| 128 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 129 | public boolean onDown(MotionEvent e) { |
| 130 | return true; |
| 131 | } |
| 132 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 133 | public boolean onFling(MotionEvent e1, MotionEvent e2, |
| 134 | float velocityX, float velocityY) { |
| 135 | if (!mScroller.isFinished()) { |
| 136 | mCurrentPos = mScroller.getFinalX(); |
| 137 | } |
| 138 | mScroller.forceFinished(true); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 139 | stopActionModeOrContextMenu(); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 140 | CalculatorResult.this.cancelLongPress(); |
| 141 | // Ignore scrolls of error string, etc. |
| 142 | if (!mScrollable) return true; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 143 | mScroller.fling(mCurrentPos, 0, - (int) velocityX, 0 /* horizontal only */, |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 144 | mMinPos, mMaxPos, 0, 0); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 145 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | @Override |
| 149 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
| 150 | float distanceX, float distanceY) { |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 151 | int distance = (int)distanceX; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 152 | if (!mScroller.isFinished()) { |
| 153 | mCurrentPos = mScroller.getFinalX(); |
| 154 | } |
| 155 | mScroller.forceFinished(true); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 156 | stopActionModeOrContextMenu(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 157 | CalculatorResult.this.cancelLongPress(); |
| 158 | if (!mScrollable) return true; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 159 | if (mCurrentPos + distance < mMinPos) { |
| 160 | distance = mMinPos - mCurrentPos; |
| 161 | } else if (mCurrentPos + distance > mMaxPos) { |
| 162 | distance = mMaxPos - mCurrentPos; |
| 163 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 164 | int duration = (int)(e2.getEventTime() - e1.getEventTime()); |
| 165 | if (duration < 1 || duration > 100) duration = 10; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 166 | mScroller.startScroll(mCurrentPos, 0, distance, 0, (int)duration); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 167 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 168 | return true; |
| 169 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void onLongPress(MotionEvent e) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 172 | if (mValid) { |
Justin Klaassen | 3a05c7e | 2016-03-04 12:40:02 -0800 | [diff] [blame] | 173 | performLongClick(); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 174 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 175 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 176 | }); |
Justin Klaassen | 3a05c7e | 2016-03-04 12:40:02 -0800 | [diff] [blame] | 177 | setOnTouchListener(new View.OnTouchListener() { |
| 178 | @Override |
| 179 | public boolean onTouch(View v, MotionEvent event) { |
| 180 | return mGestureDetector.onTouchEvent(event); |
| 181 | } |
| 182 | }); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 183 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 184 | setupActionMode(); |
| 185 | } else { |
| 186 | setupContextMenu(); |
| 187 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 188 | setHorizontallyScrolling(false); // do it ourselves |
| 189 | setCursorVisible(false); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void setEvaluator(Evaluator evaluator) { |
| 193 | mEvaluator = evaluator; |
| 194 | } |
| 195 | |
Hans Boehm | cd72f7e | 2016-06-01 16:21:25 -0700 | [diff] [blame] | 196 | // Compute maximum digit width the hard way. |
| 197 | private static float getMaxDigitWidth(TextPaint paint) { |
| 198 | // Compute the maximum advance width for each digit, thus accounting for between-character |
| 199 | // spaces. If we ever support other kinds of digits, we may have to avoid kerning effects |
| 200 | // that could reduce the advance width within this particular string. |
| 201 | final String allDigits = "0123456789"; |
| 202 | final float[] widths = new float[allDigits.length()]; |
| 203 | paint.getTextWidths(allDigits, widths); |
| 204 | float maxWidth = 0; |
| 205 | for (float x : widths) { |
| 206 | maxWidth = Math.max(x, maxWidth); |
| 207 | } |
| 208 | return maxWidth; |
| 209 | } |
| 210 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 211 | @Override |
| 212 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 213 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 214 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 215 | final TextPaint paint = getPaint(); |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 216 | final Context context = getContext(); |
Hans Boehm | cd72f7e | 2016-06-01 16:21:25 -0700 | [diff] [blame] | 217 | final float newCharWidth = getMaxDigitWidth(paint); |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 218 | // Digits are presumed to have no more than newCharWidth. |
| 219 | // We sometimes replace a character by an ellipsis or, due to SCI_NOTATION_EXTRA, add |
| 220 | // an extra decimal separator beyond the maximum number of characters we normally allow. |
| 221 | // Empirically, our minus sign is also slightly wider than a digit, so we have to |
| 222 | // account for that. We never have both an ellipsis and two minus signs, and |
| 223 | // we assume an ellipsis is no narrower than a minus sign. |
| 224 | final float decimalSeparatorWidth = Layout.getDesiredWidth( |
| 225 | context.getString(R.string.dec_point), paint); |
| 226 | final float minusExtraWidth = Layout.getDesiredWidth( |
| 227 | context.getString(R.string.op_sub), paint) - newCharWidth; |
| 228 | final float ellipsisExtraWidth = Layout.getDesiredWidth(KeyMaps.ELLIPSIS, paint) |
| 229 | - newCharWidth; |
| 230 | final int extraWidth = (int) (Math.ceil(Math.max(decimalSeparatorWidth + minusExtraWidth, |
| 231 | ellipsisExtraWidth)) + Math.max(minusExtraWidth, 0.0f)); |
| 232 | final int newWidthConstraint = MeasureSpec.getSize(widthMeasureSpec) |
| 233 | - (getPaddingLeft() + getPaddingRight()) - extraWidth; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 234 | synchronized(mWidthLock) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 235 | mWidthConstraint = newWidthConstraint; |
| 236 | mCharWidth = newCharWidth; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 240 | // Return the length of the exponent representation for the given exponent, in |
| 241 | // characters. |
| 242 | private final int expLen(int exp) { |
| 243 | if (exp == 0) return 0; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 244 | final int abs_exp_digits = (int) Math.ceil(Math.log10(Math.abs((double)exp)) |
| 245 | + 0.0000000001d /* Round whole numbers to next integer */); |
| 246 | return abs_exp_digits + (exp >= 0 ? 1 : 2); |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 249 | /** |
| 250 | * Initiate display of a new result. |
| 251 | * The parameters specify various properties of the result. |
| 252 | * @param initPrec Initial display precision computed by evaluator. (1 = tenths digit) |
| 253 | * @param msd Position of most significant digit. Offset from left of string. |
| 254 | Evaluator.INVALID_MSD if unknown. |
| 255 | * @param leastDigPos Position of least significant digit (1 = tenths digit) |
| 256 | * or Integer.MAX_VALUE. |
| 257 | * @param truncatedWholePart Result up to but not including decimal point. |
| 258 | Currently we only use the length. |
| 259 | */ |
| 260 | void displayResult(int initPrec, int msd, int leastDigPos, String truncatedWholePart) { |
| 261 | initPositions(initPrec, msd, leastDigPos, truncatedWholePart); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 262 | redisplay(); |
| 263 | } |
| 264 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 265 | /** |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 266 | * Set up scroll bounds (mMinPos, mMaxPos, etc.) and determine whether the result is |
| 267 | * scrollable, based on the supplied information about the result. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 268 | * This is unfortunately complicated because we need to predict whether trailing digits |
| 269 | * will eventually be replaced by an exponent. |
| 270 | * Just appending the exponent during formatting would be simpler, but would produce |
| 271 | * jumpier results during transitions. |
| 272 | */ |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 273 | private void initPositions(int initPrecOffset, int msdIndex, int lsdOffset, |
| 274 | String truncatedWholePart) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 275 | float charWidth; |
| 276 | int maxChars = getMaxChars(); |
| 277 | mLastPos = INVALID; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 278 | mLsdOffset = lsdOffset; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 279 | synchronized(mWidthLock) { |
| 280 | charWidth = mCharWidth; |
| 281 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 282 | mCurrentPos = mMinPos = (int) Math.round(initPrecOffset * charWidth); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 283 | // Prevent scrolling past initial position, which is calculated to show leading digits. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 284 | if (msdIndex == Evaluator.INVALID_MSD) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 285 | // Possible zero value |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 286 | if (lsdOffset == Integer.MIN_VALUE) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 287 | // Definite zero value. |
| 288 | mMaxPos = mMinPos; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 289 | mMaxCharOffset = (int) Math.round(mMaxPos/charWidth); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 290 | mScrollable = false; |
| 291 | } else { |
| 292 | // May be very small nonzero value. Allow user to find out. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 293 | mMaxPos = mMaxCharOffset = MAX_RIGHT_SCROLL; |
| 294 | mMinPos -= charWidth; // Allow for future minus sign. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 295 | mScrollable = true; |
| 296 | } |
| 297 | return; |
| 298 | } |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 299 | mWholeLen = truncatedWholePart.length(); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 300 | int negative = truncatedWholePart.charAt(0) == '-' ? 1 : 0; |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 301 | if (msdIndex > mWholeLen && msdIndex <= mWholeLen + 3) { |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 302 | // Avoid tiny negative exponent; pretend msdIndex is just to the right of decimal point. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 303 | msdIndex = mWholeLen - 1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 304 | } |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 305 | int minCharOffset = msdIndex - mWholeLen; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 306 | // Position of leftmost significant digit relative to dec. point. |
| 307 | // Usually negative. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 308 | mMaxCharOffset = MAX_RIGHT_SCROLL; // How far does it make sense to scroll right? |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 309 | // If msd is left of decimal point should logically be |
| 310 | // mMinPos = - (int) Math.ceil(getPaint().measureText(truncatedWholePart)), but |
| 311 | // we eventually translate to a character position by dividing by mCharWidth. |
| 312 | // To avoid rounding issues, we use the analogous computation here. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 313 | if (minCharOffset > -1 && minCharOffset < MAX_LEADING_ZEROES + 2) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 314 | // Small number of leading zeroes, avoid scientific notation. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 315 | minCharOffset = -1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 316 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 317 | if (lsdOffset < MAX_RIGHT_SCROLL) { |
| 318 | mMaxCharOffset = lsdOffset; |
| 319 | if (mMaxCharOffset < -1 && mMaxCharOffset > -(MAX_TRAILING_ZEROES + 2)) { |
| 320 | mMaxCharOffset = -1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 321 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 322 | // lsdOffset is positive or negative, never 0. |
| 323 | int currentExpLen = 0; // Length of required standard scientific notation exponent. |
| 324 | if (mMaxCharOffset < -1) { |
| 325 | currentExpLen = expLen(-minCharOffset - 1); |
| 326 | } else if (minCharOffset > -1 || mMaxCharOffset >= maxChars) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 327 | // Number either entirely to the right of decimal point, or decimal point not |
| 328 | // visible when scrolled to the right. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 329 | currentExpLen = expLen(-minCharOffset); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 330 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 331 | mScrollable = (mMaxCharOffset + currentExpLen - minCharOffset + negative >= maxChars); |
| 332 | int newMaxCharOffset; |
| 333 | if (currentExpLen > 0) { |
| 334 | if (mScrollable) { |
| 335 | // We'll use exponent corresponding to leastDigPos when scrolled to right. |
| 336 | newMaxCharOffset = mMaxCharOffset + expLen(-lsdOffset); |
| 337 | } else { |
| 338 | newMaxCharOffset = mMaxCharOffset + currentExpLen; |
| 339 | } |
| 340 | if (mMaxCharOffset <= -1 && newMaxCharOffset > -1) { |
| 341 | // Very unlikely; just drop exponent. |
| 342 | mMaxCharOffset = -1; |
| 343 | } else { |
| 344 | mMaxCharOffset = newMaxCharOffset; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 347 | mMaxPos = Math.min((int) Math.round(mMaxCharOffset * charWidth), MAX_RIGHT_SCROLL); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 348 | if (!mScrollable) { |
| 349 | // Position the number consistently with our assumptions to make sure it |
| 350 | // actually fits. |
| 351 | mCurrentPos = mMaxPos; |
| 352 | } |
| 353 | } else { |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 354 | mMaxPos = mMaxCharOffset = MAX_RIGHT_SCROLL; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 355 | mScrollable = true; |
| 356 | } |
| 357 | } |
| 358 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 359 | void displayError(int resourceId) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 360 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 361 | mScrollable = false; |
| 362 | setText(resourceId); |
| 363 | } |
| 364 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 365 | private final int MAX_COPY_SIZE = 1000000; |
| 366 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 367 | /* |
| 368 | * Return the most significant digit position in the given string or Evaluator.INVALID_MSD. |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 369 | * Unlike Evaluator.getMsdIndexOf, we treat a final 1 as significant. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 370 | */ |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 371 | public static int getNaiveMsdIndexOf(String s) { |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 372 | final int len = s.length(); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 373 | for (int i = 0; i < len; ++i) { |
| 374 | char c = s.charAt(i); |
| 375 | if (c != '-' && c != '.' && c != '0') { |
| 376 | return i; |
| 377 | } |
| 378 | } |
| 379 | return Evaluator.INVALID_MSD; |
| 380 | } |
| 381 | |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 382 | // Format a result returned by Evaluator.getString() into a single line containing ellipses |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 383 | // (if appropriate) and an exponent (if appropriate). precOffset is the value that was passed |
| 384 | // to getString and thus identifies the significance of the rightmost digit. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 385 | // A value of 1 means the rightmost digits corresponds to tenths. |
| 386 | // maxDigs is the maximum number of characters in the result. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 387 | // If lastDisplayedOffset is not null, we set lastDisplayedOffset[0] to the offset of |
| 388 | // the last digit actually appearing in the display. |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 389 | // If forcePrecision is true, we make sure that the last displayed digit corresponds to |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 390 | // precOffset, and allow maxDigs to be exceeded in adding the exponent. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 391 | // We add two distinct kinds of exponents: |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 392 | // (1) If the final result contains the leading digit we use standard scientific notation. |
| 393 | // (2) If not, we add an exponent corresponding to an interpretation of the final result as |
| 394 | // an integer. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 395 | // We add an ellipsis on the left if the result was truncated. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 396 | // We add ellipses and exponents in a way that leaves most digits in the position they |
| 397 | // would have been in had we not done so. |
| 398 | // This minimizes jumps as a result of scrolling. Result is NOT internationalized, |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 399 | // uses "E" for exponent. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 400 | public String formatResult(String in, int precOffset, int maxDigs, boolean truncated, |
| 401 | boolean negative, int lastDisplayedOffset[], boolean forcePrecision) { |
| 402 | final int minusSpace = negative ? 1 : 0; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 403 | final int msdIndex = truncated ? -1 : getNaiveMsdIndexOf(in); // INVALID_MSD is OK. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 404 | String result = in; |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 405 | if (truncated || (negative && result.charAt(0) != '-')) { |
| 406 | result = KeyMaps.ELLIPSIS + result.substring(1, result.length()); |
| 407 | // Ellipsis may be removed again in the type(1) scientific notation case. |
| 408 | } |
| 409 | final int decIndex = result.indexOf('.'); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 410 | if (lastDisplayedOffset != null) { |
| 411 | lastDisplayedOffset[0] = precOffset; |
| 412 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 413 | if ((decIndex == -1 || msdIndex != Evaluator.INVALID_MSD |
| 414 | && msdIndex - decIndex > MAX_LEADING_ZEROES + 1) && precOffset != -1) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 415 | // No decimal point displayed, and it's not just to the right of the last digit, |
| 416 | // or we should suppress leading zeroes. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 417 | // Add an exponent to let the user track which digits are currently displayed. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 418 | // Start with type (2) exponent if we dropped no digits. -1 accounts for decimal point. |
| 419 | final int initExponent = precOffset > 0 ? -precOffset : -precOffset - 1; |
| 420 | int exponent = initExponent; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 421 | boolean hasPoint = false; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 422 | if (!truncated && msdIndex < maxDigs - 1 |
| 423 | && result.length() - msdIndex + 1 + minusSpace |
| 424 | <= maxDigs + SCI_NOTATION_EXTRA) { |
| 425 | // Type (1) exponent computation and transformation: |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 426 | // Leading digit is in display window. Use standard calculator scientific notation |
| 427 | // with one digit to the left of the decimal point. Insert decimal point and |
| 428 | // delete leading zeroes. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 429 | // We try to keep leading digits roughly in position, and never |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 430 | // lengthen the result by more than SCI_NOTATION_EXTRA. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 431 | final int resLen = result.length(); |
| 432 | String fraction = result.substring(msdIndex + 1, resLen); |
| 433 | result = (negative ? "-" : "") + result.substring(msdIndex, msdIndex + 1) |
| 434 | + "." + fraction; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 435 | // Original exp was correct for decimal point at right of fraction. |
| 436 | // Adjust by length of fraction. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 437 | exponent = initExponent + resLen - msdIndex - 1; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 438 | hasPoint = true; |
| 439 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 440 | // Exponent can't be zero. |
| 441 | // Actually add the exponent of either type: |
| 442 | if (!forcePrecision) { |
| 443 | int dropDigits; // Digits to drop to make room for exponent. |
| 444 | if (hasPoint) { |
| 445 | // Type (1) exponent. |
| 446 | // Drop digits even if there is room. Otherwise the scrolling gets jumpy. |
| 447 | dropDigits = expLen(exponent); |
| 448 | if (dropDigits >= result.length() - 1) { |
| 449 | // Jumpy is better than no mantissa. Probably impossible anyway. |
| 450 | dropDigits = Math.max(result.length() - 2, 0); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 451 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 452 | } else { |
| 453 | // Type (2) exponent. |
| 454 | // Exponent depends on the number of digits we drop, which depends on |
| 455 | // exponent ... |
| 456 | for (dropDigits = 2; expLen(initExponent + dropDigits) > dropDigits; |
| 457 | ++dropDigits) {} |
| 458 | exponent = initExponent + dropDigits; |
| 459 | if (precOffset - dropDigits > mLsdOffset) { |
| 460 | // This can happen if e.g. result = 10^40 + 10^10 |
| 461 | // It turns out we would otherwise display ...10e9 because it takes |
| 462 | // the same amount of space as ...1e10 but shows one more digit. |
| 463 | // But we don't want to display a trailing zero, even if it's free. |
| 464 | ++dropDigits; |
| 465 | ++exponent; |
| 466 | } |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 467 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 468 | result = result.substring(0, result.length() - dropDigits); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 469 | if (lastDisplayedOffset != null) { |
| 470 | lastDisplayedOffset[0] -= dropDigits; |
| 471 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 472 | } |
| 473 | result = result + "E" + Integer.toString(exponent); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 474 | } |
| 475 | return result; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Get formatted, but not internationalized, result from mEvaluator. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 480 | * @param precOffset requested position (1 = tenths) of last included digit. |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 481 | * @param maxSize Maximum number of characters (more or less) in result. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 482 | * @param lastDisplayedOffset Zeroth entry is set to actual offset of last included digit, |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 483 | * after adjusting for exponent, etc. May be null. |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 484 | * @param forcePrecision Ensure that last included digit is at pos, at the expense |
| 485 | * of treating maxSize as a soft limit. |
| 486 | */ |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 487 | private String getFormattedResult(int precOffset, int maxSize, int lastDisplayedOffset[], |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 488 | boolean forcePrecision) { |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 489 | final boolean truncated[] = new boolean[1]; |
| 490 | final boolean negative[] = new boolean[1]; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 491 | final int requestedPrecOffset[] = {precOffset}; |
| 492 | final String rawResult = mEvaluator.getString(requestedPrecOffset, mMaxCharOffset, |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 493 | maxSize, truncated, negative); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 494 | return formatResult(rawResult, requestedPrecOffset[0], maxSize, truncated[0], negative[0], |
| 495 | lastDisplayedOffset, forcePrecision); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 498 | /** |
| 499 | * Return entire result (within reason) up to current displayed precision. |
| 500 | */ |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 501 | public String getFullText() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 502 | if (!mValid) return ""; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 503 | if (!mScrollable) return getText().toString(); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 504 | return KeyMaps.translateResult(getFormattedResult(mLastDisplayedOffset, MAX_COPY_SIZE, |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 505 | null, true)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 508 | public boolean fullTextIsExact() { |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 509 | return !mScrollable |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 510 | || mMaxCharOffset == getCurrentCharOffset() && mMaxCharOffset != MAX_RIGHT_SCROLL; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 513 | /** |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 514 | * Get entire result up to current displayed precision, or up to MAX_COPY_EXTRA additional |
| 515 | * digits, if it will lead to an exact result. |
| 516 | */ |
| 517 | public String getFullCopyText() { |
| 518 | if (!mValid |
| 519 | || mLsdOffset == Integer.MAX_VALUE |
| 520 | || fullTextIsExact() |
| 521 | || mWholeLen > MAX_RECOMPUTE_DIGITS |
| 522 | || mWholeLen + mLsdOffset > MAX_RECOMPUTE_DIGITS |
| 523 | || mLsdOffset - mLastDisplayedOffset > MAX_COPY_EXTRA) { |
| 524 | return getFullText(); |
| 525 | } |
| 526 | // It's reasonable to compute and copy the exact result instead. |
| 527 | final int nonNegLsdOffset = Math.max(0, mLsdOffset); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 528 | final String rawResult = mEvaluator.getResult().toStringTruncated(nonNegLsdOffset); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 529 | final String formattedResult = formatResult(rawResult, nonNegLsdOffset, MAX_COPY_SIZE, |
| 530 | false, rawResult.charAt(0) == '-', null, true); |
| 531 | return KeyMaps.translateResult(formattedResult); |
| 532 | } |
| 533 | |
| 534 | /** |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 535 | * Return the maximum number of characters that will fit in the result display. |
| 536 | * May be called asynchronously from non-UI thread. |
| 537 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 538 | int getMaxChars() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 539 | int result; |
| 540 | synchronized(mWidthLock) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 541 | result = (int) Math.floor(mWidthConstraint / mCharWidth); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 542 | // We can apparently finish evaluating before onMeasure in CalculatorText has been |
| 543 | // called, in which case we get 0 or -1 as the width constraint. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 544 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 545 | if (result <= 0) { |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 546 | // Return something conservatively big, to force sufficient evaluation. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 547 | return MAX_WIDTH; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 548 | } else { |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 549 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 553 | /** |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 554 | * @return {@code true} if the currently displayed result is scrollable |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 555 | */ |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 556 | public boolean isScrollable() { |
| 557 | return mScrollable; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 560 | int getCurrentCharOffset() { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 561 | synchronized(mWidthLock) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 562 | return (int) Math.round(mCurrentPos / mCharWidth); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 566 | void clear() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 567 | mValid = false; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 568 | mScrollable = false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 569 | setText(""); |
| 570 | } |
| 571 | |
| 572 | void redisplay() { |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 573 | int currentCharOffset = getCurrentCharOffset(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 574 | int maxChars = getMaxChars(); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 575 | int lastDisplayedOffset[] = new int[1]; |
| 576 | String result = getFormattedResult(currentCharOffset, maxChars, lastDisplayedOffset, false); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 577 | int expIndex = result.indexOf('E'); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 578 | result = KeyMaps.translateResult(result); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 579 | if (expIndex > 0 && result.indexOf('.') == -1) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 580 | // Gray out exponent if used as position indicator |
| 581 | SpannableString formattedResult = new SpannableString(result); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 582 | formattedResult.setSpan(mExponentColorSpan, expIndex, result.length(), |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 583 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 584 | setText(formattedResult); |
| 585 | } else { |
| 586 | setText(result); |
| 587 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 588 | mLastDisplayedOffset = lastDisplayedOffset[0]; |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 589 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | @Override |
| 593 | public void computeScroll() { |
| 594 | if (!mScrollable) return; |
| 595 | if (mScroller.computeScrollOffset()) { |
| 596 | mCurrentPos = mScroller.getCurrX(); |
| 597 | if (mCurrentPos != mLastPos) { |
| 598 | mLastPos = mCurrentPos; |
| 599 | redisplay(); |
| 600 | } |
| 601 | if (!mScroller.isFinished()) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 602 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 607 | /** |
| 608 | * Use ActionMode for copy support on M and higher. |
| 609 | */ |
| 610 | @TargetApi(Build.VERSION_CODES.M) |
| 611 | private void setupActionMode() { |
| 612 | mCopyActionModeCallback = new ActionMode.Callback2() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 613 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 614 | @Override |
| 615 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 616 | final MenuInflater inflater = mode.getMenuInflater(); |
| 617 | return createCopyMenu(inflater, menu); |
| 618 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 619 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 620 | @Override |
| 621 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 622 | return false; // Return false if nothing is done |
| 623 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 624 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 625 | @Override |
| 626 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 627 | if (onMenuItemClick(item)) { |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 628 | mode.finish(); |
| 629 | return true; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 630 | } else { |
| 631 | return false; |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 632 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 633 | } |
| 634 | |
| 635 | @Override |
| 636 | public void onDestroyActionMode(ActionMode mode) { |
| 637 | unhighlightResult(); |
| 638 | mActionMode = null; |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 643 | super.onGetContentRect(mode, view, outRect); |
| 644 | |
| 645 | outRect.left += view.getPaddingLeft(); |
| 646 | outRect.top += view.getPaddingTop(); |
| 647 | outRect.right -= view.getPaddingRight(); |
| 648 | outRect.bottom -= view.getPaddingBottom(); |
| 649 | final int width = (int) Layout.getDesiredWidth(getText(), getPaint()); |
| 650 | if (width < outRect.width()) { |
| 651 | outRect.left = outRect.right - width; |
| 652 | } |
| 653 | |
| 654 | if (!BuildCompat.isAtLeastN()) { |
| 655 | // The CAB (prior to N) only takes the translation of a view into account, so |
| 656 | // if a scale is applied to the view then the offset outRect will end up being |
| 657 | // positioned incorrectly. We workaround that limitation by manually applying |
| 658 | // the scale to the outRect, which the CAB will then offset to the correct |
| 659 | // position. |
| 660 | final float scaleX = view.getScaleX(); |
| 661 | final float scaleY = view.getScaleY(); |
| 662 | outRect.left *= scaleX; |
| 663 | outRect.right *= scaleX; |
| 664 | outRect.top *= scaleY; |
| 665 | outRect.bottom *= scaleY; |
| 666 | } |
| 667 | } |
| 668 | }; |
| 669 | setOnLongClickListener(new View.OnLongClickListener() { |
| 670 | @Override |
| 671 | public boolean onLongClick(View v) { |
| 672 | if (mValid) { |
| 673 | mActionMode = startActionMode(mCopyActionModeCallback, |
| 674 | ActionMode.TYPE_FLOATING); |
| 675 | return true; |
| 676 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 677 | return false; |
| 678 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 679 | }); |
| 680 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 681 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 682 | /** |
| 683 | * Use ContextMenu for copy support on L and lower. |
| 684 | */ |
| 685 | private void setupContextMenu() { |
| 686 | setOnCreateContextMenuListener(new OnCreateContextMenuListener() { |
| 687 | @Override |
| 688 | public void onCreateContextMenu(ContextMenu contextMenu, View view, |
| 689 | ContextMenu.ContextMenuInfo contextMenuInfo) { |
| 690 | final MenuInflater inflater = new MenuInflater(getContext()); |
| 691 | createCopyMenu(inflater, contextMenu); |
| 692 | mContextMenu = contextMenu; |
| 693 | for(int i = 0; i < contextMenu.size(); i ++) { |
| 694 | contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorResult.this); |
| 695 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 696 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 697 | }); |
| 698 | setOnLongClickListener(new View.OnLongClickListener() { |
| 699 | @Override |
| 700 | public boolean onLongClick(View v) { |
| 701 | if (mValid) { |
| 702 | return showContextMenu(); |
| 703 | } |
| 704 | return false; |
Justin Klaassen | f1b61f4 | 2016-04-27 16:00:11 -0700 | [diff] [blame] | 705 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 706 | }); |
| 707 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 708 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 709 | private boolean createCopyMenu(MenuInflater inflater, Menu menu) { |
| 710 | inflater.inflate(R.menu.copy, menu); |
| 711 | highlightResult(); |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | public boolean stopActionModeOrContextMenu() { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 716 | if (mActionMode != null) { |
| 717 | mActionMode.finish(); |
| 718 | return true; |
| 719 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 720 | if (mContextMenu != null) { |
| 721 | unhighlightResult(); |
| 722 | mContextMenu.close(); |
| 723 | return true; |
| 724 | } |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 725 | return false; |
| 726 | } |
| 727 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 728 | private void highlightResult() { |
| 729 | final Spannable text = (Spannable) getText(); |
| 730 | text.setSpan(mHighlightSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 731 | } |
| 732 | |
| 733 | private void unhighlightResult() { |
| 734 | final Spannable text = (Spannable) getText(); |
| 735 | text.removeSpan(mHighlightSpan); |
| 736 | } |
| 737 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 738 | private void setPrimaryClip(ClipData clip) { |
| 739 | ClipboardManager clipboard = (ClipboardManager) getContext(). |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 740 | getSystemService(Context.CLIPBOARD_SERVICE); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 741 | clipboard.setPrimaryClip(clip); |
| 742 | } |
| 743 | |
| 744 | private void copyContent() { |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 745 | final CharSequence text = getFullCopyText(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 746 | ClipboardManager clipboard = |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 747 | (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); |
| 748 | // We include a tag URI, to allow us to recognize our own results and handle them |
| 749 | // specially. |
| 750 | ClipData.Item newItem = new ClipData.Item(text, null, mEvaluator.capture()); |
| 751 | String[] mimeTypes = new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}; |
| 752 | ClipData cd = new ClipData("calculator result", mimeTypes, newItem); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 753 | clipboard.setPrimaryClip(cd); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 754 | Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame^] | 757 | @Override |
| 758 | public boolean onMenuItemClick(MenuItem item) { |
| 759 | switch (item.getItemId()) { |
| 760 | case R.id.menu_copy: |
| 761 | if (mEvaluator.reevaluationInProgress()) { |
| 762 | // Refuse to copy placeholder characters. |
| 763 | return false; |
| 764 | } else { |
| 765 | copyContent(); |
| 766 | unhighlightResult(); |
| 767 | return true; |
| 768 | } |
| 769 | default: |
| 770 | return false; |
| 771 | } |
| 772 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 773 | } |