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