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