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