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