Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1 | /* |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 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 | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 35 | import android.text.style.RelativeSizeSpan; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 37 | import android.view.ActionMode; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 38 | import android.view.ContextMenu; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 39 | import android.view.GestureDetector; |
| 40 | import android.view.Menu; |
| 41 | import android.view.MenuInflater; |
| 42 | import android.view.MenuItem; |
| 43 | import android.view.MotionEvent; |
| 44 | import android.view.View; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 45 | import android.widget.OverScroller; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 46 | import android.widget.Toast; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 47 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 48 | // A text widget that is "infinitely" scrollable to the right, |
| 49 | // and obtains the text to display via a callback to Logic. |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 50 | public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenuItemClickListener { |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 51 | static final int MAX_RIGHT_SCROLL = 10000000; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 52 | static final int INVALID = MAX_RIGHT_SCROLL + 10000; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 53 | // A larger value is unlikely to avoid running out of space |
| 54 | final OverScroller mScroller; |
| 55 | final GestureDetector mGestureDetector; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 56 | private Evaluator mEvaluator; |
| 57 | private boolean mScrollable = false; |
| 58 | // A scrollable result is currently displayed. |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 59 | private boolean mValid = false; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 60 | // The result holds something valid; either a a number or an error |
| 61 | // message. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 62 | // A suffix of "Pos" denotes a pixel offset. Zero represents a scroll position |
| 63 | // 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] | 64 | private int mCurrentPos;// Position of right of display relative to decimal point, in pixels. |
| 65 | // Large positive values mean the decimal point is scrolled off the |
| 66 | // left of the display. Zero means decimal point is barely displayed |
| 67 | // on the right. |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 68 | private int mLastPos; // Position already reflected in display. Pixels. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 69 | 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] | 70 | private int mMaxPos; // Maximum position before we start displaying the infinite |
| 71 | // sequence of trailing zeroes on the right. Pixels. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 72 | private int mWholeLen; // Length of the whole part of current result. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 73 | // In the following, we use a suffix of Offset to denote a character position in a numeric |
| 74 | // string relative to the decimal point. Positive is to the right and negative is to |
| 75 | // the left. 1 = tenths position, -1 = units. Integer.MAX_VALUE is sometimes used |
| 76 | // for the offset of the last digit in an a nonterminating decimal expansion. |
| 77 | // We use the suffix "Index" to denote a zero-based index into a string representing a |
| 78 | // result. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 79 | private int mMaxCharOffset; // Character offset from decimal point of rightmost digit |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 80 | // that should be displayed, plus the length of any exponent |
| 81 | // needed to display that digit. |
| 82 | // Limited to MAX_RIGHT_SCROLL. Often the same as: |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 83 | private int mLsdOffset; // Position of least-significant digit in result |
| 84 | private int mLastDisplayedOffset; // Offset of last digit actually displayed after adding |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 85 | // exponent. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 86 | private boolean mWholePartFits; // Scientific notation not needed for initial display. |
| 87 | private float mNoExponentCredit; |
| 88 | // Fraction of digit width saved by avoiding scientific notation. |
| 89 | // Only accessed from UI thread. |
| 90 | private boolean mAppendExponent; |
| 91 | // The result fits entirely in the display, even with an exponent, |
| 92 | // but not with grouping separators. Since the result is not |
| 93 | // scrollable, and we do not add the exponent to max. scroll position, |
| 94 | // append an exponent insteadd of replacing trailing digits. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 95 | private final Object mWidthLock = new Object(); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 96 | // Protects the next five fields. These fields are only |
| 97 | // Updated by the UI thread, and read accesses by the UI thread |
| 98 | // sometimes do not acquire the lock. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 99 | private int mWidthConstraint = -1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 100 | // Our total width in pixels minus space for ellipsis. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 101 | private float mCharWidth = 1; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 102 | // Maximum character width. For now we pretend that all characters |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 103 | // have this width. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 104 | // TODO: We're not really using a fixed width font. But it appears |
| 105 | // to be close enough for the characters we use that the difference |
| 106 | // is not noticeable. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 107 | private float mGroupingSeparatorWidthRatio; |
| 108 | // Fraction of digit width occupied by a digit separator. |
| 109 | private float mDecimalCredit; |
| 110 | // Fraction of digit width saved by replacing digit with decimal point. |
| 111 | private float mNoEllipsisCredit; |
| 112 | // Fraction of digit width saved by both replacing ellipsis with digit |
| 113 | // and avoiding scientific notation. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 114 | private static final int MAX_WIDTH = 100; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 115 | // Maximum number of digits displayed. |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 116 | public static final int MAX_LEADING_ZEROES = 6; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 117 | // Maximum number of leading zeroes after decimal point before we |
| 118 | // switch to scientific notation with negative exponent. |
Hans Boehm | 50ed320 | 2015-06-09 14:35:49 -0700 | [diff] [blame] | 119 | public static final int MAX_TRAILING_ZEROES = 6; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 120 | // Maximum number of trailing zeroes before the decimal point before |
| 121 | // we switch to scientific notation with positive exponent. |
| 122 | private static final int SCI_NOTATION_EXTRA = 1; |
| 123 | // Extra digits for standard scientific notation. In this case we |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 124 | // have a decimal point and no ellipsis. |
| 125 | // We assume that we do not drop digits to make room for the decimal |
| 126 | // point in ordinary scientific notation. Thus >= 1. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 127 | private static final int MAX_COPY_EXTRA = 100; |
| 128 | // The number of extra digits we are willing to compute to copy |
| 129 | // a result as an exact number. |
| 130 | private static final int MAX_RECOMPUTE_DIGITS = 2000; |
| 131 | // The maximum number of digits we're willing to recompute in the UI |
| 132 | // thread. We only do this for known rational results, where we |
| 133 | // can bound the computation cost. |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 134 | private final ForegroundColorSpan mExponentColorSpan; |
| 135 | private final BackgroundColorSpan mHighlightSpan; |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 136 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 137 | private ActionMode mActionMode; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 138 | private ActionMode.Callback mCopyActionModeCallback; |
| 139 | private ContextMenu mContextMenu; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 140 | |
| 141 | public CalculatorResult(Context context, AttributeSet attrs) { |
| 142 | super(context, attrs); |
| 143 | mScroller = new OverScroller(context); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 144 | mHighlightSpan = new BackgroundColorSpan(getHighlightColor()); |
| 145 | mExponentColorSpan = new ForegroundColorSpan( |
| 146 | ContextCompat.getColor(context, R.color.display_result_exponent_text_color)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 147 | mGestureDetector = new GestureDetector(context, |
| 148 | new GestureDetector.SimpleOnGestureListener() { |
| 149 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 150 | public boolean onDown(MotionEvent e) { |
| 151 | return true; |
| 152 | } |
| 153 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 154 | public boolean onFling(MotionEvent e1, MotionEvent e2, |
| 155 | float velocityX, float velocityY) { |
| 156 | if (!mScroller.isFinished()) { |
| 157 | mCurrentPos = mScroller.getFinalX(); |
| 158 | } |
| 159 | mScroller.forceFinished(true); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 160 | stopActionModeOrContextMenu(); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 161 | CalculatorResult.this.cancelLongPress(); |
| 162 | // Ignore scrolls of error string, etc. |
| 163 | if (!mScrollable) return true; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 164 | mScroller.fling(mCurrentPos, 0, - (int) velocityX, 0 /* horizontal only */, |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 165 | mMinPos, mMaxPos, 0, 0); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 166 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | @Override |
| 170 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
| 171 | float distanceX, float distanceY) { |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 172 | int distance = (int)distanceX; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 173 | if (!mScroller.isFinished()) { |
| 174 | mCurrentPos = mScroller.getFinalX(); |
| 175 | } |
| 176 | mScroller.forceFinished(true); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 177 | stopActionModeOrContextMenu(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 178 | CalculatorResult.this.cancelLongPress(); |
| 179 | if (!mScrollable) return true; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 180 | if (mCurrentPos + distance < mMinPos) { |
| 181 | distance = mMinPos - mCurrentPos; |
| 182 | } else if (mCurrentPos + distance > mMaxPos) { |
| 183 | distance = mMaxPos - mCurrentPos; |
| 184 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 185 | int duration = (int)(e2.getEventTime() - e1.getEventTime()); |
| 186 | if (duration < 1 || duration > 100) duration = 10; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 187 | mScroller.startScroll(mCurrentPos, 0, distance, 0, (int)duration); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 188 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 189 | return true; |
| 190 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 191 | @Override |
| 192 | public void onLongPress(MotionEvent e) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 193 | if (mValid) { |
Justin Klaassen | 3a05c7e | 2016-03-04 12:40:02 -0800 | [diff] [blame] | 194 | performLongClick(); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 195 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 196 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 197 | }); |
Justin Klaassen | 3a05c7e | 2016-03-04 12:40:02 -0800 | [diff] [blame] | 198 | setOnTouchListener(new View.OnTouchListener() { |
| 199 | @Override |
| 200 | public boolean onTouch(View v, MotionEvent event) { |
| 201 | return mGestureDetector.onTouchEvent(event); |
| 202 | } |
| 203 | }); |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 204 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 205 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 206 | setupActionMode(); |
| 207 | } else { |
| 208 | setupContextMenu(); |
| 209 | } |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 210 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 211 | setCursorVisible(false); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void setEvaluator(Evaluator evaluator) { |
| 215 | mEvaluator = evaluator; |
| 216 | } |
| 217 | |
Hans Boehm | cd72f7e | 2016-06-01 16:21:25 -0700 | [diff] [blame] | 218 | // Compute maximum digit width the hard way. |
| 219 | private static float getMaxDigitWidth(TextPaint paint) { |
| 220 | // Compute the maximum advance width for each digit, thus accounting for between-character |
| 221 | // spaces. If we ever support other kinds of digits, we may have to avoid kerning effects |
| 222 | // that could reduce the advance width within this particular string. |
| 223 | final String allDigits = "0123456789"; |
| 224 | final float[] widths = new float[allDigits.length()]; |
| 225 | paint.getTextWidths(allDigits, widths); |
| 226 | float maxWidth = 0; |
| 227 | for (float x : widths) { |
| 228 | maxWidth = Math.max(x, maxWidth); |
| 229 | } |
| 230 | return maxWidth; |
| 231 | } |
| 232 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 233 | @Override |
| 234 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Justin Klaassen | d06f51d | 2016-08-03 00:41:31 -0700 | [diff] [blame] | 235 | if (!isLaidOut()) { |
| 236 | // Set a minimum height so scaled error messages won't affect our layout. |
| 237 | setMinimumHeight(getLineHeight() + getCompoundPaddingBottom() |
| 238 | + getCompoundPaddingTop()); |
| 239 | } |
| 240 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 241 | final TextPaint paint = getPaint(); |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 242 | final Context context = getContext(); |
Hans Boehm | cd72f7e | 2016-06-01 16:21:25 -0700 | [diff] [blame] | 243 | final float newCharWidth = getMaxDigitWidth(paint); |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 244 | // Digits are presumed to have no more than newCharWidth. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 245 | // There are two instances when we know that the result is otherwise narrower than |
| 246 | // expected: |
| 247 | // 1. For standard scientific notation (our type 1), we know that we have a norrow decimal |
| 248 | // point and no (usually wide) ellipsis symbol. We allow one extra digit |
| 249 | // (SCI_NOTATION_EXTRA) to compensate, and consider that in determining available width. |
| 250 | // 2. If we are using digit grouping separators and a decimal point, we give ourselves |
| 251 | // a fractional extra space for those separators, the value of which depends on whether |
| 252 | // there is also an ellipsis. |
| 253 | // |
| 254 | // Maximum extra space we need in various cases: |
| 255 | // Type 1 scientific notation, assuming ellipsis, minus sign and E are wider than a digit: |
| 256 | // Two minus signs + "E" + "." - 3 digits. |
| 257 | // Type 2 scientific notation: |
| 258 | // Ellipsis + "E" + "-" - 3 digits. |
| 259 | // In the absence of scientific notation, we may need a little less space. |
| 260 | // We give ourselves a bit of extra credit towards comma insertion and give |
| 261 | // ourselves more if we have either |
| 262 | // No ellipsis, or |
| 263 | // A decimal separator. |
| 264 | |
| 265 | // Calculate extra space we need to reserve, in addition to character count. |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 266 | final float decimalSeparatorWidth = Layout.getDesiredWidth( |
| 267 | context.getString(R.string.dec_point), paint); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 268 | final float minusWidth = Layout.getDesiredWidth(context.getString(R.string.op_sub), paint); |
| 269 | final float minusExtraWidth = Math.max(minusWidth - newCharWidth, 0.0f); |
| 270 | final float ellipsisWidth = Layout.getDesiredWidth(KeyMaps.ELLIPSIS, paint); |
| 271 | final float ellipsisExtraWidth = Math.max(ellipsisWidth - newCharWidth, 0.0f); |
| 272 | final float expWidth = Layout.getDesiredWidth(KeyMaps.translateResult("e"), paint); |
| 273 | final float expExtraWidth = Math.max(expWidth - newCharWidth, 0.0f); |
| 274 | final float type1Extra = 2 * minusExtraWidth + expExtraWidth + decimalSeparatorWidth; |
| 275 | final float type2Extra = ellipsisExtraWidth + expExtraWidth + minusExtraWidth; |
| 276 | final float extraWidth = Math.max(type1Extra, type2Extra); |
| 277 | final int intExtraWidth = (int) Math.ceil(extraWidth) + 1 /* to cover rounding sins */; |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 278 | final int newWidthConstraint = MeasureSpec.getSize(widthMeasureSpec) |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 279 | - (getPaddingLeft() + getPaddingRight()) - intExtraWidth; |
| 280 | |
| 281 | // Calculate other width constants we need to handle grouping separators. |
| 282 | final float groupingSeparatorW = |
| 283 | Layout.getDesiredWidth(KeyMaps.translateResult(","), paint); |
| 284 | // Credits in the absence of any scientific notation: |
| 285 | float noExponentCredit = extraWidth - Math.max(ellipsisExtraWidth, minusExtraWidth); |
| 286 | final float noEllipsisCredit = extraWidth - minusExtraWidth; // includes noExponentCredit. |
| 287 | final float decimalCredit = Math.max(newCharWidth - decimalSeparatorWidth, 0.0f); |
| 288 | |
| 289 | mNoExponentCredit = noExponentCredit / newCharWidth; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 290 | synchronized(mWidthLock) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 291 | mWidthConstraint = newWidthConstraint; |
| 292 | mCharWidth = newCharWidth; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 293 | mNoEllipsisCredit = noEllipsisCredit / newCharWidth; |
| 294 | mDecimalCredit = decimalCredit / newCharWidth; |
| 295 | mGroupingSeparatorWidthRatio = groupingSeparatorW / newCharWidth; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 296 | } |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 297 | |
| 298 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 301 | /** |
| 302 | * Return the number of additional digit widths required to add digit separators to |
| 303 | * the supplied string prefix. |
| 304 | * The string prefix is assumed to represent a whole number, after skipping leading non-digits. |
| 305 | * Callable from non-UI thread. |
| 306 | */ |
| 307 | public float separatorChars(String s, int len) { |
| 308 | int start = 0; |
| 309 | while (start < len && !Character.isDigit(s.charAt(start))) { |
| 310 | ++start; |
| 311 | } |
| 312 | // We assume the rest consists of digits, and for consistency with the rest |
| 313 | // of the code, we assume all digits have width mCharWidth. |
| 314 | final int nDigits = len - start; |
| 315 | // We currently insert a digit separator every three digits. |
| 316 | final int nSeparators = (nDigits - 1) / 3; |
| 317 | synchronized(mWidthLock) { |
| 318 | // Always return an upper bound, even in the presence of rounding errors. |
| 319 | return nSeparators * mGroupingSeparatorWidthRatio; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Return extra width credit for absence of ellipsis, as fraction of a digit width. |
| 325 | * May be called by non-UI thread. |
| 326 | */ |
| 327 | public float getNoEllipsisCredit() { |
| 328 | synchronized(mWidthLock) { |
| 329 | return mNoEllipsisCredit; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Return extra width credit for presence of a decimal point, as fraction of a digit width. |
| 335 | * May be called by non-UI thread. |
| 336 | */ |
| 337 | public float getDecimalCredit() { |
| 338 | synchronized(mWidthLock) { |
| 339 | return mDecimalCredit; |
| 340 | } |
| 341 | } |
| 342 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 343 | // Return the length of the exponent representation for the given exponent, in |
| 344 | // characters. |
| 345 | private final int expLen(int exp) { |
| 346 | if (exp == 0) return 0; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 347 | final int abs_exp_digits = (int) Math.ceil(Math.log10(Math.abs((double)exp)) |
| 348 | + 0.0000000001d /* Round whole numbers to next integer */); |
| 349 | return abs_exp_digits + (exp >= 0 ? 1 : 2); |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Initiate display of a new result. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 354 | * Only called from UI thread. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 355 | * The parameters specify various properties of the result. |
| 356 | * @param initPrec Initial display precision computed by evaluator. (1 = tenths digit) |
| 357 | * @param msd Position of most significant digit. Offset from left of string. |
| 358 | Evaluator.INVALID_MSD if unknown. |
| 359 | * @param leastDigPos Position of least significant digit (1 = tenths digit) |
| 360 | * or Integer.MAX_VALUE. |
| 361 | * @param truncatedWholePart Result up to but not including decimal point. |
| 362 | Currently we only use the length. |
| 363 | */ |
| 364 | void displayResult(int initPrec, int msd, int leastDigPos, String truncatedWholePart) { |
| 365 | initPositions(initPrec, msd, leastDigPos, truncatedWholePart); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 366 | redisplay(); |
| 367 | } |
| 368 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 369 | /** |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 370 | * Set up scroll bounds (mMinPos, mMaxPos, etc.) and determine whether the result is |
| 371 | * scrollable, based on the supplied information about the result. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 372 | * This is unfortunately complicated because we need to predict whether trailing digits |
| 373 | * will eventually be replaced by an exponent. |
| 374 | * Just appending the exponent during formatting would be simpler, but would produce |
| 375 | * jumpier results during transitions. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 376 | * Only called from UI thread. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 377 | */ |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 378 | private void initPositions(int initPrecOffset, int msdIndex, int lsdOffset, |
| 379 | String truncatedWholePart) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 380 | int maxChars = getMaxChars(); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 381 | mWholeLen = truncatedWholePart.length(); |
| 382 | // Allow a tiny amount of slop for associativity/rounding differences in length |
| 383 | // calculation. If getPreferredPrec() decided it should fit, we want to make it fit, too. |
| 384 | // We reserved one extra pixel, so the extra length is OK. |
| 385 | final int nSeparatorChars = (int) Math.ceil( |
| 386 | separatorChars(truncatedWholePart, truncatedWholePart.length()) |
| 387 | - getNoEllipsisCredit() - 0.0001f); |
| 388 | mWholePartFits = mWholeLen + nSeparatorChars <= maxChars; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 389 | mLastPos = INVALID; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 390 | mLsdOffset = lsdOffset; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 391 | mAppendExponent = false; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 392 | // Prevent scrolling past initial position, which is calculated to show leading digits. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 393 | mCurrentPos = mMinPos = (int) Math.round(initPrecOffset * mCharWidth); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 394 | if (msdIndex == Evaluator.INVALID_MSD) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 395 | // Possible zero value |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 396 | if (lsdOffset == Integer.MIN_VALUE) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 397 | // Definite zero value. |
| 398 | mMaxPos = mMinPos; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 399 | mMaxCharOffset = (int) Math.round(mMaxPos/mCharWidth); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 400 | mScrollable = false; |
| 401 | } else { |
| 402 | // May be very small nonzero value. Allow user to find out. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 403 | mMaxPos = mMaxCharOffset = MAX_RIGHT_SCROLL; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 404 | mMinPos -= mCharWidth; // Allow for future minus sign. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 405 | mScrollable = true; |
| 406 | } |
| 407 | return; |
| 408 | } |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 409 | int negative = truncatedWholePart.charAt(0) == '-' ? 1 : 0; |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 410 | if (msdIndex > mWholeLen && msdIndex <= mWholeLen + 3) { |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 411 | // 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] | 412 | msdIndex = mWholeLen - 1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 413 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 414 | // Set to position of leftmost significant digit relative to dec. point. Usually negative. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 415 | int minCharOffset = msdIndex - mWholeLen; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 416 | if (minCharOffset > -1 && minCharOffset < MAX_LEADING_ZEROES + 2) { |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 417 | // Small number of leading zeroes, avoid scientific notation. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 418 | minCharOffset = -1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 419 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 420 | if (lsdOffset < MAX_RIGHT_SCROLL) { |
| 421 | mMaxCharOffset = lsdOffset; |
| 422 | if (mMaxCharOffset < -1 && mMaxCharOffset > -(MAX_TRAILING_ZEROES + 2)) { |
| 423 | mMaxCharOffset = -1; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 424 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 425 | // lsdOffset is positive or negative, never 0. |
| 426 | int currentExpLen = 0; // Length of required standard scientific notation exponent. |
| 427 | if (mMaxCharOffset < -1) { |
| 428 | currentExpLen = expLen(-minCharOffset - 1); |
| 429 | } else if (minCharOffset > -1 || mMaxCharOffset >= maxChars) { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 430 | // Number is either entirely to the right of decimal point, or decimal point is |
| 431 | // not visible when scrolled to the right. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 432 | currentExpLen = expLen(-minCharOffset); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 433 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 434 | // Exponent length does not included added decimal point. But whenever we add a |
| 435 | // decimal point, we allow an extra character (SCI_NOTATION_EXTRA). |
| 436 | final int separatorLength = mWholePartFits && minCharOffset < -3 ? nSeparatorChars : 0; |
| 437 | mScrollable = (mMaxCharOffset + currentExpLen + separatorLength - minCharOffset |
| 438 | + negative >= maxChars); |
| 439 | // Now adjust mMaxCharOffset for any required exponent. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 440 | int newMaxCharOffset; |
| 441 | if (currentExpLen > 0) { |
| 442 | if (mScrollable) { |
| 443 | // We'll use exponent corresponding to leastDigPos when scrolled to right. |
| 444 | newMaxCharOffset = mMaxCharOffset + expLen(-lsdOffset); |
| 445 | } else { |
| 446 | newMaxCharOffset = mMaxCharOffset + currentExpLen; |
| 447 | } |
| 448 | if (mMaxCharOffset <= -1 && newMaxCharOffset > -1) { |
| 449 | // Very unlikely; just drop exponent. |
| 450 | mMaxCharOffset = -1; |
| 451 | } else { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 452 | mMaxCharOffset = Math.min(newMaxCharOffset, MAX_RIGHT_SCROLL); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 453 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 454 | mMaxPos = Math.min((int) Math.round(mMaxCharOffset * mCharWidth), |
| 455 | MAX_RIGHT_SCROLL); |
| 456 | } else if (!mWholePartFits && !mScrollable) { |
| 457 | // Corner case in which entire number fits, but not with grouping separators. We |
| 458 | // will use an exponent in un-scrolled position, which may hide digits. Scrolling |
| 459 | // by one character will remove the exponent and reveal the last digits. Note |
| 460 | // that in the forced scientific notation case, the exponent length is not |
| 461 | // factored into mMaxCharOffset, since we do not want such an increase to impact |
| 462 | // scrolling behavior. In the unscrollable case, we thus have to append the |
| 463 | // exponent at the end using the forcePrecision argument to formatResult, in order |
| 464 | // to ensure that we get the entire result. |
| 465 | mScrollable = (mMaxCharOffset + expLen(-minCharOffset - 1) - minCharOffset |
| 466 | + negative >= maxChars); |
| 467 | if (mScrollable) { |
| 468 | mMaxPos = (int) Math.ceil(mMinPos + mCharWidth); |
| 469 | // Single character scroll will remove exponent and show remaining piece. |
| 470 | } else { |
| 471 | mMaxPos = mMinPos; |
| 472 | mAppendExponent = true; |
| 473 | } |
| 474 | } else { |
| 475 | mMaxPos = Math.min((int) Math.round(mMaxCharOffset * mCharWidth), |
| 476 | MAX_RIGHT_SCROLL); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 477 | } |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 478 | if (!mScrollable) { |
| 479 | // Position the number consistently with our assumptions to make sure it |
| 480 | // actually fits. |
| 481 | mCurrentPos = mMaxPos; |
| 482 | } |
| 483 | } else { |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 484 | mMaxPos = mMaxCharOffset = MAX_RIGHT_SCROLL; |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 485 | mScrollable = true; |
| 486 | } |
| 487 | } |
| 488 | |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Display error message indicated by resourceId. |
| 491 | * UI thread only. |
| 492 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 493 | void displayError(int resourceId) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 494 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 495 | mScrollable = false; |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 496 | final String msg = getContext().getString(resourceId); |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 497 | final float measuredWidth = Layout.getDesiredWidth(msg, getPaint()); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 498 | if (measuredWidth > mWidthConstraint) { |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 499 | // Multiply by .99 to avoid rounding effects. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 500 | final float scaleFactor = 0.99f * mWidthConstraint / measuredWidth; |
Hans Boehm | 14344ff | 2016-06-08 13:01:51 -0700 | [diff] [blame] | 501 | final RelativeSizeSpan smallTextSpan = new RelativeSizeSpan(scaleFactor); |
| 502 | final SpannableString scaledMsg = new SpannableString(msg); |
| 503 | scaledMsg.setSpan(smallTextSpan, 0, msg.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 504 | setText(scaledMsg); |
| 505 | } else { |
| 506 | setText(msg); |
| 507 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 510 | private final int MAX_COPY_SIZE = 1000000; |
| 511 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 512 | /* |
| 513 | * 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] | 514 | * Unlike Evaluator.getMsdIndexOf, we treat a final 1 as significant. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 515 | * Pure function; callable from anywhere. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 516 | */ |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 517 | public static int getNaiveMsdIndexOf(String s) { |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 518 | final int len = s.length(); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 519 | for (int i = 0; i < len; ++i) { |
| 520 | char c = s.charAt(i); |
| 521 | if (c != '-' && c != '.' && c != '0') { |
| 522 | return i; |
| 523 | } |
| 524 | } |
| 525 | return Evaluator.INVALID_MSD; |
| 526 | } |
| 527 | |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 528 | /** |
| 529 | * Format a result returned by Evaluator.getString() into a single line containing ellipses |
| 530 | * (if appropriate) and an exponent (if appropriate). |
| 531 | * We add two distinct kinds of exponents: |
| 532 | * (1) If the final result contains the leading digit we use standard scientific notation. |
| 533 | * (2) If not, we add an exponent corresponding to an interpretation of the final result as |
| 534 | * an integer. |
| 535 | * We add an ellipsis on the left if the result was truncated. |
| 536 | * We add ellipses and exponents in a way that leaves most digits in the position they |
| 537 | * would have been in had we not done so. This minimizes jumps as a result of scrolling. |
| 538 | * Result is NOT internationalized, uses "E" for exponent. |
| 539 | * Called only from UI thread; We sometimes omit locking for fields. |
| 540 | * @param precOffset The value that was passed to getString. Identifies the significance of |
| 541 | the rightmost digit. A value of 1 means the rightmost digits corresponds to tenths. |
| 542 | * @param maxDigs The maximum number of characters in the result |
| 543 | * @param truncated The in parameter was already truncated, beyond possibly removing the |
| 544 | minus sign. |
| 545 | * @param negative The in parameter represents a negative result. (Minus sign may be removed |
| 546 | without setting truncated.) |
| 547 | * @param lastDisplayedOffset If not null, we set lastDisplayedOffset[0] to the offset of |
| 548 | the last digit actually appearing in the display. |
| 549 | * @param forcePrecision If true, we make sure that the last displayed digit corresponds to |
| 550 | precOffset, and allow maxDigs to be exceeded in adding the exponent and commas. |
| 551 | * @param forceSciNotation Force scientific notation. May be set because we don't have |
| 552 | space for grouping separators, but whole number otherwise fits. |
| 553 | * @param insertCommas Insert commas (literally, not internationalized) as digit separators. |
| 554 | We only ever do this for the integral part of a number, and only when no |
| 555 | exponent is displayed in the initial position. The combination of which means |
| 556 | that we only do it when no exponent is displayed. |
| 557 | We insert commas in a way that does consider the width of the actual localized digit |
| 558 | separator. Commas count towards maxDigs as the appropriate fraction of a digit. |
| 559 | */ |
| 560 | private String formatResult(String in, int precOffset, int maxDigs, boolean truncated, |
| 561 | boolean negative, int lastDisplayedOffset[], boolean forcePrecision, |
| 562 | boolean forceSciNotation, boolean insertCommas) { |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 563 | final int minusSpace = negative ? 1 : 0; |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 564 | final int msdIndex = truncated ? -1 : getNaiveMsdIndexOf(in); // INVALID_MSD is OK. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 565 | String result = in; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 566 | boolean needEllipsis = false; |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 567 | if (truncated || (negative && result.charAt(0) != '-')) { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 568 | needEllipsis = true; |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 569 | result = KeyMaps.ELLIPSIS + result.substring(1, result.length()); |
| 570 | // Ellipsis may be removed again in the type(1) scientific notation case. |
| 571 | } |
| 572 | final int decIndex = result.indexOf('.'); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 573 | if (lastDisplayedOffset != null) { |
| 574 | lastDisplayedOffset[0] = precOffset; |
| 575 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 576 | if (forceSciNotation || (decIndex == -1 || msdIndex != Evaluator.INVALID_MSD |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 577 | && msdIndex - decIndex > MAX_LEADING_ZEROES + 1) && precOffset != -1) { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 578 | // Either: |
| 579 | // 1) No decimal point displayed, and it's not just to the right of the last digit, or |
| 580 | // 2) we are at the front of a number whos integral part is too large to allow |
| 581 | // comma insertion, or |
| 582 | // 3) we should suppress leading zeroes. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 583 | // 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] | 584 | // Start with type (2) exponent if we dropped no digits. -1 accounts for decimal point. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 585 | // We currently never show digit separators together with an exponent. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 586 | final int initExponent = precOffset > 0 ? -precOffset : -precOffset - 1; |
| 587 | int exponent = initExponent; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 588 | boolean hasPoint = false; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 589 | if (!truncated && msdIndex < maxDigs - 1 |
| 590 | && result.length() - msdIndex + 1 + minusSpace |
| 591 | <= maxDigs + SCI_NOTATION_EXTRA) { |
| 592 | // Type (1) exponent computation and transformation: |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 593 | // Leading digit is in display window. Use standard calculator scientific notation |
| 594 | // with one digit to the left of the decimal point. Insert decimal point and |
| 595 | // delete leading zeroes. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 596 | // We try to keep leading digits roughly in position, and never |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 597 | // lengthen the result by more than SCI_NOTATION_EXTRA. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 598 | if (decIndex > msdIndex) { |
| 599 | // In the forceSciNotation, we can have a decimal point in the relevant digit |
| 600 | // range. Remove it. |
| 601 | result = result.substring(0, decIndex) |
| 602 | + result.substring(decIndex + 1, result.length()); |
| 603 | // msdIndex and precOffset unaffected. |
| 604 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 605 | final int resLen = result.length(); |
| 606 | String fraction = result.substring(msdIndex + 1, resLen); |
| 607 | result = (negative ? "-" : "") + result.substring(msdIndex, msdIndex + 1) |
| 608 | + "." + fraction; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 609 | // Original exp was correct for decimal point at right of fraction. |
| 610 | // Adjust by length of fraction. |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 611 | exponent = initExponent + resLen - msdIndex - 1; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 612 | hasPoint = true; |
| 613 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 614 | // Exponent can't be zero. |
| 615 | // Actually add the exponent of either type: |
| 616 | if (!forcePrecision) { |
| 617 | int dropDigits; // Digits to drop to make room for exponent. |
| 618 | if (hasPoint) { |
| 619 | // Type (1) exponent. |
| 620 | // Drop digits even if there is room. Otherwise the scrolling gets jumpy. |
| 621 | dropDigits = expLen(exponent); |
| 622 | if (dropDigits >= result.length() - 1) { |
| 623 | // Jumpy is better than no mantissa. Probably impossible anyway. |
| 624 | dropDigits = Math.max(result.length() - 2, 0); |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 625 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 626 | } else { |
| 627 | // Type (2) exponent. |
| 628 | // Exponent depends on the number of digits we drop, which depends on |
| 629 | // exponent ... |
| 630 | for (dropDigits = 2; expLen(initExponent + dropDigits) > dropDigits; |
| 631 | ++dropDigits) {} |
| 632 | exponent = initExponent + dropDigits; |
| 633 | if (precOffset - dropDigits > mLsdOffset) { |
| 634 | // This can happen if e.g. result = 10^40 + 10^10 |
| 635 | // It turns out we would otherwise display ...10e9 because it takes |
| 636 | // the same amount of space as ...1e10 but shows one more digit. |
| 637 | // But we don't want to display a trailing zero, even if it's free. |
| 638 | ++dropDigits; |
| 639 | ++exponent; |
| 640 | } |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 641 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 642 | result = result.substring(0, result.length() - dropDigits); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 643 | if (lastDisplayedOffset != null) { |
| 644 | lastDisplayedOffset[0] -= dropDigits; |
| 645 | } |
Hans Boehm | 73ecff2 | 2015-09-03 16:04:50 -0700 | [diff] [blame] | 646 | } |
| 647 | result = result + "E" + Integer.toString(exponent); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 648 | } else if (insertCommas) { |
| 649 | // Add commas to the whole number section, and then truncate on left to fit, |
| 650 | // counting commas as a fractional digit. |
| 651 | final int wholeStart = needEllipsis ? 1 : 0; |
| 652 | int orig_length = result.length(); |
| 653 | final float nCommaChars; |
| 654 | if (decIndex != -1) { |
| 655 | nCommaChars = separatorChars(result, decIndex); |
| 656 | result = StringUtils.addCommas(result, wholeStart, decIndex) |
| 657 | + result.substring(decIndex, orig_length); |
| 658 | } else { |
| 659 | nCommaChars = separatorChars(result, orig_length); |
| 660 | result = StringUtils.addCommas(result, wholeStart, orig_length); |
| 661 | } |
| 662 | if (needEllipsis) { |
| 663 | orig_length -= 1; // Exclude ellipsis. |
| 664 | } |
| 665 | final float len = orig_length + nCommaChars; |
| 666 | int deletedChars = 0; |
| 667 | final float ellipsisCredit = getNoEllipsisCredit(); |
| 668 | final float decimalCredit = getDecimalCredit(); |
| 669 | final float effectiveLen = len - (decIndex == -1 ? 0 : getDecimalCredit()); |
| 670 | final float ellipsisAdjustment = |
| 671 | needEllipsis ? mNoExponentCredit : getNoEllipsisCredit(); |
| 672 | // As above, we allow for a tiny amount of extra length here, for consistency with |
| 673 | // getPreferredPrec(). |
| 674 | if (effectiveLen - ellipsisAdjustment > (float) (maxDigs - wholeStart) + 0.0001f |
| 675 | && !forcePrecision) { |
| 676 | float deletedWidth = 0.0f; |
| 677 | while (effectiveLen - mNoExponentCredit - deletedWidth |
| 678 | > (float) (maxDigs - 1 /* for ellipsis */)) { |
| 679 | if (result.charAt(deletedChars) == ',') { |
| 680 | deletedWidth += mGroupingSeparatorWidthRatio; |
| 681 | } else { |
| 682 | deletedWidth += 1.0f; |
| 683 | } |
| 684 | deletedChars++; |
| 685 | } |
| 686 | } |
| 687 | if (deletedChars > 0) { |
| 688 | result = KeyMaps.ELLIPSIS + result.substring(deletedChars, result.length()); |
| 689 | } else if (needEllipsis) { |
| 690 | result = KeyMaps.ELLIPSIS + result; |
| 691 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 692 | } |
| 693 | return result; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 696 | /** |
| 697 | * Get formatted, but not internationalized, result from mEvaluator. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 698 | * @param precOffset requested position (1 = tenths) of last included digit |
| 699 | * @param maxSize maximum number of characters (more or less) in result |
| 700 | * @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] | 701 | * after adjusting for exponent, etc. May be null. |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 702 | * @param forcePrecision Ensure that last included digit is at pos, at the expense |
| 703 | * of treating maxSize as a soft limit. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 704 | * @param forceSciNotation Force scientific notation, even if not required by maxSize. |
| 705 | * @param insertCommas Insert commas as digit separators. |
Hans Boehm | f6dae11 | 2015-06-18 17:57:50 -0700 | [diff] [blame] | 706 | */ |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 707 | private String getFormattedResult(int precOffset, int maxSize, int lastDisplayedOffset[], |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 708 | boolean forcePrecision, boolean forceSciNotation, boolean insertCommas) { |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 709 | final boolean truncated[] = new boolean[1]; |
| 710 | final boolean negative[] = new boolean[1]; |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 711 | final int requestedPrecOffset[] = {precOffset}; |
| 712 | final String rawResult = mEvaluator.getString(requestedPrecOffset, mMaxCharOffset, |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 713 | maxSize, truncated, negative); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 714 | return formatResult(rawResult, requestedPrecOffset[0], maxSize, truncated[0], negative[0], |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 715 | lastDisplayedOffset, forcePrecision, forceSciNotation, insertCommas); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 718 | /** |
| 719 | * Return entire result (within reason) up to current displayed precision. |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 720 | * @param withSeparators Add digit separators |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 721 | */ |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 722 | public String getFullText(boolean withSeparators) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 723 | if (!mValid) return ""; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 724 | if (!mScrollable) return getText().toString(); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 725 | return KeyMaps.translateResult(getFormattedResult(mLastDisplayedOffset, MAX_COPY_SIZE, |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 726 | null, true /* forcePrecision */, false /* forceSciNotation */, withSeparators)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 729 | /** |
| 730 | * Did the above produce a correct result? |
| 731 | * UI thread only. |
| 732 | */ |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 733 | public boolean fullTextIsExact() { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 734 | return !mScrollable || (mMaxCharOffset == getCharOffset(mCurrentPos) |
| 735 | && mMaxCharOffset != MAX_RIGHT_SCROLL); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 738 | /** |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 739 | * Get entire result up to current displayed precision, or up to MAX_COPY_EXTRA additional |
| 740 | * digits, if it will lead to an exact result. |
| 741 | */ |
| 742 | public String getFullCopyText() { |
| 743 | if (!mValid |
| 744 | || mLsdOffset == Integer.MAX_VALUE |
| 745 | || fullTextIsExact() |
| 746 | || mWholeLen > MAX_RECOMPUTE_DIGITS |
| 747 | || mWholeLen + mLsdOffset > MAX_RECOMPUTE_DIGITS |
| 748 | || mLsdOffset - mLastDisplayedOffset > MAX_COPY_EXTRA) { |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 749 | return getFullText(false /* withSeparators */); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 750 | } |
| 751 | // It's reasonable to compute and copy the exact result instead. |
| 752 | final int nonNegLsdOffset = Math.max(0, mLsdOffset); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 753 | final String rawResult = mEvaluator.getResult().toStringTruncated(nonNegLsdOffset); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 754 | final String formattedResult = formatResult(rawResult, nonNegLsdOffset, MAX_COPY_SIZE, |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 755 | false, rawResult.charAt(0) == '-', null, true /* forcePrecision */, |
| 756 | false /* forceSciNotation */, false /* insertCommas */); |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 757 | return KeyMaps.translateResult(formattedResult); |
| 758 | } |
| 759 | |
| 760 | /** |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 761 | * Return the maximum number of characters that will fit in the result display. |
| 762 | * May be called asynchronously from non-UI thread. |
| 763 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 764 | int getMaxChars() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 765 | int result; |
| 766 | synchronized(mWidthLock) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 767 | result = (int) Math.floor(mWidthConstraint / mCharWidth); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 768 | // We can apparently finish evaluating before onMeasure in CalculatorText has been |
| 769 | // 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] | 770 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 771 | if (result <= 0) { |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 772 | // Return something conservatively big, to force sufficient evaluation. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 773 | return MAX_WIDTH; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 774 | } else { |
Hans Boehm | 80018c8 | 2015-08-02 16:59:07 -0700 | [diff] [blame] | 775 | return result; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 779 | /** |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 780 | * @return {@code true} if the currently displayed result is scrollable |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 781 | */ |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 782 | public boolean isScrollable() { |
| 783 | return mScrollable; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 786 | /** |
| 787 | * Map pixel position to digit offset. |
| 788 | * UI thread only. |
| 789 | */ |
| 790 | int getCharOffset(int pos) { |
| 791 | return (int) Math.round(pos / mCharWidth); // Lock not needed. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 794 | void clear() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 795 | mValid = false; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 796 | mScrollable = false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 797 | setText(""); |
| 798 | } |
| 799 | |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 800 | /** |
| 801 | * Refresh display. |
| 802 | * Only called in UI thread. |
| 803 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 804 | void redisplay() { |
Christine Franks | d21205c | 2016-08-04 10:06:15 -0700 | [diff] [blame^] | 805 | if (mScroller.isFinished()) { |
| 806 | setAccessibilityLiveRegion(ACCESSIBILITY_LIVE_REGION_POLITE); |
| 807 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 808 | int currentCharOffset = getCharOffset(mCurrentPos); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 809 | int maxChars = getMaxChars(); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 810 | int lastDisplayedOffset[] = new int[1]; |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 811 | String result = getFormattedResult(currentCharOffset, maxChars, lastDisplayedOffset, |
| 812 | mAppendExponent /* forcePrecision; preserve entire result */, |
| 813 | !mWholePartFits |
| 814 | && currentCharOffset == getCharOffset(mMinPos) /* forceSciNotation */, |
| 815 | mWholePartFits /* insertCommas */ ); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 816 | int expIndex = result.indexOf('E'); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 817 | result = KeyMaps.translateResult(result); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 818 | if (expIndex > 0 && result.indexOf('.') == -1) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 819 | // Gray out exponent if used as position indicator |
| 820 | SpannableString formattedResult = new SpannableString(result); |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 821 | formattedResult.setSpan(mExponentColorSpan, expIndex, result.length(), |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 822 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 823 | setText(formattedResult); |
| 824 | } else { |
| 825 | setText(result); |
| 826 | } |
Hans Boehm | 5e802f3 | 2015-06-22 17:18:52 -0700 | [diff] [blame] | 827 | mLastDisplayedOffset = lastDisplayedOffset[0]; |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 828 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | @Override |
| 832 | public void computeScroll() { |
| 833 | if (!mScrollable) return; |
| 834 | if (mScroller.computeScrollOffset()) { |
| 835 | mCurrentPos = mScroller.getCurrX(); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 836 | if (getCharOffset(mCurrentPos) != getCharOffset(mLastPos)) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 837 | mLastPos = mCurrentPos; |
| 838 | redisplay(); |
| 839 | } |
| 840 | if (!mScroller.isFinished()) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 841 | postInvalidateOnAnimation(); |
Christine Franks | d21205c | 2016-08-04 10:06:15 -0700 | [diff] [blame^] | 842 | setAccessibilityLiveRegion(ACCESSIBILITY_LIVE_REGION_NONE); |
| 843 | } else { |
| 844 | setAccessibilityLiveRegion(ACCESSIBILITY_LIVE_REGION_POLITE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 845 | } |
Christine Franks | d21205c | 2016-08-04 10:06:15 -0700 | [diff] [blame^] | 846 | } else { |
| 847 | // Finished scrolling. |
| 848 | setAccessibilityLiveRegion(ACCESSIBILITY_LIVE_REGION_POLITE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 852 | /** |
| 853 | * Use ActionMode for copy support on M and higher. |
| 854 | */ |
| 855 | @TargetApi(Build.VERSION_CODES.M) |
| 856 | private void setupActionMode() { |
| 857 | mCopyActionModeCallback = new ActionMode.Callback2() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 858 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 859 | @Override |
| 860 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 861 | final MenuInflater inflater = mode.getMenuInflater(); |
| 862 | return createCopyMenu(inflater, menu); |
| 863 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 864 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 865 | @Override |
| 866 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 867 | return false; // Return false if nothing is done |
| 868 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 869 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 870 | @Override |
| 871 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 872 | if (onMenuItemClick(item)) { |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 873 | mode.finish(); |
| 874 | return true; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 875 | } else { |
| 876 | return false; |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 877 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | @Override |
| 881 | public void onDestroyActionMode(ActionMode mode) { |
| 882 | unhighlightResult(); |
| 883 | mActionMode = null; |
| 884 | } |
| 885 | |
| 886 | @Override |
| 887 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 888 | super.onGetContentRect(mode, view, outRect); |
| 889 | |
| 890 | outRect.left += view.getPaddingLeft(); |
| 891 | outRect.top += view.getPaddingTop(); |
| 892 | outRect.right -= view.getPaddingRight(); |
| 893 | outRect.bottom -= view.getPaddingBottom(); |
| 894 | final int width = (int) Layout.getDesiredWidth(getText(), getPaint()); |
| 895 | if (width < outRect.width()) { |
| 896 | outRect.left = outRect.right - width; |
| 897 | } |
| 898 | |
| 899 | if (!BuildCompat.isAtLeastN()) { |
| 900 | // The CAB (prior to N) only takes the translation of a view into account, so |
| 901 | // if a scale is applied to the view then the offset outRect will end up being |
| 902 | // positioned incorrectly. We workaround that limitation by manually applying |
| 903 | // the scale to the outRect, which the CAB will then offset to the correct |
| 904 | // position. |
| 905 | final float scaleX = view.getScaleX(); |
| 906 | final float scaleY = view.getScaleY(); |
| 907 | outRect.left *= scaleX; |
| 908 | outRect.right *= scaleX; |
| 909 | outRect.top *= scaleY; |
| 910 | outRect.bottom *= scaleY; |
| 911 | } |
| 912 | } |
| 913 | }; |
| 914 | setOnLongClickListener(new View.OnLongClickListener() { |
| 915 | @Override |
| 916 | public boolean onLongClick(View v) { |
| 917 | if (mValid) { |
| 918 | mActionMode = startActionMode(mCopyActionModeCallback, |
| 919 | ActionMode.TYPE_FLOATING); |
| 920 | return true; |
| 921 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 922 | return false; |
| 923 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 924 | }); |
| 925 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 926 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 927 | /** |
| 928 | * Use ContextMenu for copy support on L and lower. |
| 929 | */ |
| 930 | private void setupContextMenu() { |
| 931 | setOnCreateContextMenuListener(new OnCreateContextMenuListener() { |
| 932 | @Override |
| 933 | public void onCreateContextMenu(ContextMenu contextMenu, View view, |
| 934 | ContextMenu.ContextMenuInfo contextMenuInfo) { |
| 935 | final MenuInflater inflater = new MenuInflater(getContext()); |
| 936 | createCopyMenu(inflater, contextMenu); |
| 937 | mContextMenu = contextMenu; |
| 938 | for(int i = 0; i < contextMenu.size(); i ++) { |
| 939 | contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorResult.this); |
| 940 | } |
Hans Boehm | 7f83e36 | 2015-06-10 15:41:04 -0700 | [diff] [blame] | 941 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 942 | }); |
| 943 | setOnLongClickListener(new View.OnLongClickListener() { |
| 944 | @Override |
| 945 | public boolean onLongClick(View v) { |
| 946 | if (mValid) { |
| 947 | return showContextMenu(); |
| 948 | } |
| 949 | return false; |
Justin Klaassen | f1b61f4 | 2016-04-27 16:00:11 -0700 | [diff] [blame] | 950 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 951 | }); |
| 952 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 953 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 954 | private boolean createCopyMenu(MenuInflater inflater, Menu menu) { |
| 955 | inflater.inflate(R.menu.copy, menu); |
| 956 | highlightResult(); |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | public boolean stopActionModeOrContextMenu() { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 961 | if (mActionMode != null) { |
| 962 | mActionMode.finish(); |
| 963 | return true; |
| 964 | } |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 965 | if (mContextMenu != null) { |
| 966 | unhighlightResult(); |
| 967 | mContextMenu.close(); |
| 968 | return true; |
| 969 | } |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 970 | return false; |
| 971 | } |
| 972 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 973 | private void highlightResult() { |
| 974 | final Spannable text = (Spannable) getText(); |
| 975 | text.setSpan(mHighlightSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 976 | } |
| 977 | |
| 978 | private void unhighlightResult() { |
| 979 | final Spannable text = (Spannable) getText(); |
| 980 | text.removeSpan(mHighlightSpan); |
| 981 | } |
| 982 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 983 | private void setPrimaryClip(ClipData clip) { |
| 984 | ClipboardManager clipboard = (ClipboardManager) getContext(). |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 985 | getSystemService(Context.CLIPBOARD_SERVICE); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 986 | clipboard.setPrimaryClip(clip); |
| 987 | } |
| 988 | |
| 989 | private void copyContent() { |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 990 | final CharSequence text = getFullCopyText(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 991 | ClipboardManager clipboard = |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 992 | (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); |
| 993 | // We include a tag URI, to allow us to recognize our own results and handle them |
| 994 | // specially. |
| 995 | ClipData.Item newItem = new ClipData.Item(text, null, mEvaluator.capture()); |
| 996 | String[] mimeTypes = new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}; |
| 997 | ClipData cd = new ClipData("calculator result", mimeTypes, newItem); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 998 | clipboard.setPrimaryClip(cd); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 999 | Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 1002 | @Override |
| 1003 | public boolean onMenuItemClick(MenuItem item) { |
| 1004 | switch (item.getItemId()) { |
| 1005 | case R.id.menu_copy: |
| 1006 | if (mEvaluator.reevaluationInProgress()) { |
| 1007 | // Refuse to copy placeholder characters. |
| 1008 | return false; |
| 1009 | } else { |
| 1010 | copyContent(); |
| 1011 | unhighlightResult(); |
| 1012 | return true; |
| 1013 | } |
| 1014 | default: |
| 1015 | return false; |
| 1016 | } |
| 1017 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1018 | } |