Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1 | /* |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.calculator2; |
| 18 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 19 | import android.content.ClipData; |
| 20 | import android.content.ClipDescription; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 21 | import android.content.ClipboardManager; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 23 | import android.text.Layout; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 24 | import android.text.SpannableString; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 25 | import android.text.Spanned; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 26 | import android.text.TextPaint; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 27 | import android.text.style.ForegroundColorSpan; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 29 | import android.view.ActionMode; |
| 30 | import android.view.GestureDetector; |
| 31 | import android.view.Menu; |
| 32 | import android.view.MenuInflater; |
| 33 | import android.view.MenuItem; |
| 34 | import android.view.MotionEvent; |
| 35 | import android.view.View; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 36 | import android.widget.OverScroller; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 37 | import android.widget.Toast; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 38 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 39 | // A text widget that is "infinitely" scrollable to the right, |
| 40 | // and obtains the text to display via a callback to Logic. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 41 | public class CalculatorResult extends AlignedTextView { |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 42 | static final int MAX_RIGHT_SCROLL = 10000000; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 43 | static final int INVALID = MAX_RIGHT_SCROLL + 10000; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 44 | // A larger value is unlikely to avoid running out of space |
| 45 | final OverScroller mScroller; |
| 46 | final GestureDetector mGestureDetector; |
| 47 | class MyTouchListener implements View.OnTouchListener { |
| 48 | @Override |
| 49 | public boolean onTouch(View v, MotionEvent event) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 50 | return mGestureDetector.onTouchEvent(event); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | final MyTouchListener mTouchListener = new MyTouchListener(); |
| 54 | private Evaluator mEvaluator; |
| 55 | private boolean mScrollable = false; |
| 56 | // A scrollable result is currently displayed. |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 57 | private boolean mValid = false; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 58 | // The result holds something valid; either a a number or an error |
| 59 | // message. |
| 60 | private int mCurrentPos;// Position of right of display relative to decimal point, in pixels. |
| 61 | // Large positive values mean the decimal point is scrolled off the |
| 62 | // left of the display. Zero means decimal point is barely displayed |
| 63 | // on the right. |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 64 | private int mLastPos; // Position already reflected in display. Pixels. |
| 65 | private int mMinPos; // Minimum position before all digits disappear off the right. Pixels. |
| 66 | private int mMaxPos; // Maximum position before we start displaying the infinite |
| 67 | // sequence of trailing zeroes on the right. Pixels. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 68 | private final Object mWidthLock = new Object(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 69 | // Protects the next two fields. |
| 70 | private int mWidthConstraint = -1; |
| 71 | // Our total width in pixels. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 72 | private float mCharWidth = 1; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 73 | // Maximum character width. For now we pretend that all characters |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 74 | // have this width. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 75 | // TODO: We're not really using a fixed width font. But it appears |
| 76 | // to be close enough for the characters we use that the difference |
| 77 | // is not noticeable. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 78 | private static final int MAX_WIDTH = 100; |
| 79 | // Maximum number of digits displayed |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 80 | private ActionMode mActionMode; |
| 81 | private final ForegroundColorSpan mExponentColorSpan; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 82 | |
| 83 | public CalculatorResult(Context context, AttributeSet attrs) { |
| 84 | super(context, attrs); |
| 85 | mScroller = new OverScroller(context); |
| 86 | mGestureDetector = new GestureDetector(context, |
| 87 | new GestureDetector.SimpleOnGestureListener() { |
| 88 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 89 | public boolean onDown(MotionEvent e) { |
| 90 | return true; |
| 91 | } |
| 92 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 93 | public boolean onFling(MotionEvent e1, MotionEvent e2, |
| 94 | float velocityX, float velocityY) { |
| 95 | if (!mScroller.isFinished()) { |
| 96 | mCurrentPos = mScroller.getFinalX(); |
| 97 | } |
| 98 | mScroller.forceFinished(true); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 99 | stopActionMode(); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 100 | CalculatorResult.this.cancelLongPress(); |
| 101 | // Ignore scrolls of error string, etc. |
| 102 | if (!mScrollable) return true; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 103 | mScroller.fling(mCurrentPos, 0, - (int) velocityX, 0 /* horizontal only */, |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 104 | mMinPos, mMaxPos, 0, 0); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 105 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | @Override |
| 109 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
| 110 | float distanceX, float distanceY) { |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 111 | int distance = (int)distanceX; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 112 | if (!mScroller.isFinished()) { |
| 113 | mCurrentPos = mScroller.getFinalX(); |
| 114 | } |
| 115 | mScroller.forceFinished(true); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 116 | stopActionMode(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 117 | CalculatorResult.this.cancelLongPress(); |
| 118 | if (!mScrollable) return true; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 119 | if (mCurrentPos + distance < mMinPos) { |
| 120 | distance = mMinPos - mCurrentPos; |
| 121 | } else if (mCurrentPos + distance > mMaxPos) { |
| 122 | distance = mMaxPos - mCurrentPos; |
| 123 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 124 | int duration = (int)(e2.getEventTime() - e1.getEventTime()); |
| 125 | if (duration < 1 || duration > 100) duration = 10; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 126 | mScroller.startScroll(mCurrentPos, 0, distance, 0, (int)duration); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 127 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 128 | return true; |
| 129 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void onLongPress(MotionEvent e) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 132 | if (mValid) { |
| 133 | mActionMode = startActionMode(mCopyActionModeCallback, |
| 134 | ActionMode.TYPE_FLOATING); |
| 135 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 136 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 137 | }); |
| 138 | setOnTouchListener(mTouchListener); |
| 139 | setHorizontallyScrolling(false); // do it ourselves |
| 140 | setCursorVisible(false); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 141 | mExponentColorSpan = new ForegroundColorSpan( |
| 142 | context.getColor(R.color.display_result_exponent_text_color)); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 143 | |
| 144 | // Copy ActionMode is triggered explicitly, not through |
| 145 | // setCustomSelectionActionModeCallback. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void setEvaluator(Evaluator evaluator) { |
| 149 | mEvaluator = evaluator; |
| 150 | } |
| 151 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 152 | @Override |
| 153 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 154 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 155 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 156 | final TextPaint paint = getPaint(); |
| 157 | final int newWidthConstraint = MeasureSpec.getSize(widthMeasureSpec) |
| 158 | - (getPaddingLeft() + getPaddingRight()) |
| 159 | - (int) Math.ceil(Layout.getDesiredWidth(KeyMaps.ELLIPSIS, paint)); |
| 160 | final float newCharWidth = Layout.getDesiredWidth("\u2007", paint); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 161 | synchronized(mWidthLock) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 162 | mWidthConstraint = newWidthConstraint; |
| 163 | mCharWidth = newCharWidth; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 167 | // Given that the last non-zero digit is at pos, compute the precision we have to ask |
| 168 | // ask for to actually get the digit at pos displayed. This is not an identity |
| 169 | // function, since we may need to drop digits to the right to make room for the exponent. |
| 170 | private int addExpSpace(int lastDigit) { |
| 171 | if (lastDigit < getMaxChars() - 1) { |
| 172 | // The decimal point will be in view when displaying the rightmost digit. |
| 173 | // no exponent needed. |
| 174 | // TODO: This will change if we stop scrolling to the left of the decimal |
| 175 | // point, which might be desirable in the traditional scientific notation case. |
| 176 | return lastDigit; |
| 177 | } |
| 178 | // When the last digit is displayed, the exponent will look like "e-<lastDigit>". |
| 179 | // The length of that string is the extra precision we need. |
| 180 | return lastDigit + (int)Math.ceil(Math.log10((double)lastDigit)) + 2; |
| 181 | } |
| 182 | |
| 183 | // Display a new result, given initial displayed precision, position of the rightmost |
| 184 | // nonzero digit (or Integer.MAX_VALUE if non-terminating), and the string representing |
| 185 | // the whole part of the number to be displayed. |
| 186 | // We pass the string, instead of just the length, so we have one less place to fix in case |
| 187 | // we ever decide to fully handle a variable width font. |
| 188 | void displayResult(int initPrec, int leastDigPos, String truncatedWholePart) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 189 | mLastPos = INVALID; |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 190 | synchronized(mWidthLock) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 191 | mCurrentPos = (int) Math.ceil(initPrec * mCharWidth); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 192 | } |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 193 | // Should logically be |
| 194 | // mMinPos = - (int) Math.ceil(getPaint().measureText(truncatedWholePart)), but |
| 195 | // we eventually transalate to a character position by dividing by mCharWidth. |
| 196 | // To avoid rounding issues, we use the analogous computation here. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 197 | mMinPos = - (int) Math.ceil(truncatedWholePart.length() * mCharWidth); |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 198 | if (leastDigPos < MAX_RIGHT_SCROLL) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 199 | mMaxPos = Math.min((int) Math.ceil(addExpSpace(leastDigPos) * mCharWidth), |
| 200 | MAX_RIGHT_SCROLL); |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 201 | } else { |
| 202 | mMaxPos = MAX_RIGHT_SCROLL; |
| 203 | } |
| 204 | mScrollable = (leastDigPos != (initPrec == -1 ? 0 : initPrec)); |
| 205 | // We assume that initPrec allows most significant digit to be displayed. |
| 206 | // If there is nothing to the right of initPrec, there is no point in scrolling. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 207 | redisplay(); |
| 208 | } |
| 209 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 210 | void displayError(int resourceId) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 211 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 212 | mScrollable = false; |
| 213 | setText(resourceId); |
| 214 | } |
| 215 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 216 | private final int MAX_COPY_SIZE = 1000000; |
| 217 | |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 218 | // Format a result returned by Evaluator.getString() into a single line containing ellipses |
| 219 | // (if appropriate) and an exponent (if appropriate). digs is the value that was passed to |
| 220 | // getString and thus identifies the significance of the rightmost digit. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 221 | // We add two distinct kinds of exponents: |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 222 | // 1) If the final result contains the leading digit we use standard scientific notation. |
| 223 | // 2) If not, we add an exponent corresponding to an interpretation of the final result as |
| 224 | // an integer. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 225 | // We add an ellipsis on the left if the result was truncated. |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 226 | // We add ellipses and exponents in a way that leaves most digits in the position they |
| 227 | // would have been in had we not done so. |
| 228 | // This minimizes jumps as a result of scrolling. Result is NOT internationalized, |
| 229 | // uses "e" for exponent. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 230 | public String formatResult(String res, int digs, |
| 231 | int maxDigs, boolean truncated, |
| 232 | boolean negative) { |
| 233 | if (truncated) { |
| 234 | res = KeyMaps.ELLIPSIS + res.substring(1, res.length()); |
| 235 | } |
| 236 | int decIndex = res.indexOf('.'); |
| 237 | int resLen = res.length(); |
| 238 | if (decIndex == -1 && digs != -1) { |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 239 | // No decimal point displayed, and it's not just to the right of the last digit. |
| 240 | // Add an exponent to let the user track which digits are currently displayed. |
| 241 | // This is a bit tricky, since the number of displayed digits affects the displayed |
| 242 | // exponent, which can affect the room we have for mantissa digits. We occasionally |
| 243 | // display one digit too few. This is sometimes unavoidable, but we could |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 244 | // avoid it in more cases. |
| 245 | int exp = digs > 0 ? -digs : -digs - 1; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 246 | // Can be used as TYPE (2) EXPONENT. -1 accounts for decimal point. |
| 247 | int msd; // Position of most significant digit in res or indication its outside res. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 248 | boolean hasPoint = false; |
| 249 | if (truncated) { |
| 250 | msd = -1; |
| 251 | } else { |
| 252 | msd = Evaluator.getMsdPos(res); // INVALID_MSD is OK |
| 253 | } |
| 254 | if (msd < maxDigs - 1 && msd >= 0) { |
| 255 | // TYPE (1) EXPONENT computation and transformation: |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 256 | // Leading digit is in display window. Use standard calculator scientific notation |
| 257 | // with one digit to the left of the decimal point. Insert decimal point and |
| 258 | // delete leading zeroes. |
| 259 | String fraction = res.substring(msd + 1, resLen); |
| 260 | res = (negative ? "-" : "") + res.substring(msd, msd+1) + "." + fraction; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 261 | exp += resLen - msd - 1; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 262 | // Original exp was correct for decimal point at right of fraction. |
| 263 | // Adjust by length of fraction. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 264 | resLen = res.length(); |
| 265 | hasPoint = true; |
| 266 | } |
| 267 | if (exp != 0 || truncated) { |
| 268 | // Actually add the exponent of either type: |
| 269 | String expAsString = Integer.toString(exp); |
| 270 | int expDigits = expAsString.length(); |
| 271 | int dropDigits = expDigits + 1; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 272 | // Drop digits even if there is room. Otherwise the scrolling gets jumpy. |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 273 | if (dropDigits >= resLen - 1) { |
| 274 | dropDigits = Math.max(resLen - 2, 0); |
| 275 | // Jumpy is better than no mantissa. |
| 276 | } |
| 277 | if (!hasPoint) { |
| 278 | // Special handling for TYPE(2) EXPONENT: |
| 279 | exp += dropDigits; |
| 280 | expAsString = Integer.toString(exp); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 281 | // Adjust for digits we are about to drop to drop to make room for exponent. |
| 282 | // This can affect the room we have for the mantissa. We adjust only for |
| 283 | // positive exponents, when it could otherwise result in a truncated |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 284 | // displayed result. |
| 285 | if (exp > 0 && expAsString.length() > expDigits) { |
| 286 | // ++expDigits; (dead code) |
| 287 | ++dropDigits; |
| 288 | ++exp; |
| 289 | // This cannot increase the length a second time. |
| 290 | } |
| 291 | } |
| 292 | res = res.substring(0, resLen - dropDigits); |
| 293 | res = res + "e" + expAsString; |
| 294 | } // else don't add zero exponent |
| 295 | } |
| 296 | return res; |
| 297 | } |
| 298 | |
| 299 | // Get formatted, but not internationalized, result from |
| 300 | // mEvaluator. |
| 301 | private String getFormattedResult(int pos, int maxSize) { |
| 302 | final boolean truncated[] = new boolean[1]; |
| 303 | final boolean negative[] = new boolean[1]; |
| 304 | final int requested_prec[] = {pos}; |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 305 | final String raw_res = mEvaluator.getString(requested_prec, maxSize, truncated, negative); |
| 306 | return formatResult(raw_res, requested_prec[0], maxSize, truncated[0], negative[0]); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 309 | // Return entire result (within reason) up to current displayed precision. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 310 | public String getFullText() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 311 | if (!mValid) return ""; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 312 | if (!mScrollable) return getText().toString(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 313 | int currentCharPos = getCurrentCharPos(); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 314 | return KeyMaps.translateResult(getFormattedResult(currentCharPos, MAX_COPY_SIZE)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 317 | public boolean fullTextIsExact() { |
| 318 | BoundedRational rat = mEvaluator.getRational(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 319 | int currentCharPos = getCurrentCharPos(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 320 | if (currentCharPos == -1) { |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 321 | // Suppressing decimal point; still showing all integral digits. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 322 | currentCharPos = 0; |
| 323 | } |
| 324 | // TODO: Could handle scientific notation cases better; |
| 325 | // We currently treat those conservatively as approximate. |
| 326 | return (currentCharPos >= BoundedRational.digitsRequired(rat)); |
| 327 | } |
| 328 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Return the maximum number of characters that will fit in the result display. |
| 331 | * May be called asynchronously from non-UI thread. |
| 332 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 333 | int getMaxChars() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 334 | int result; |
| 335 | synchronized(mWidthLock) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 336 | result = (int) Math.floor(mWidthConstraint / mCharWidth); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 337 | // We can apparently finish evaluating before onMeasure in CalculatorText has been |
| 338 | // called, in which case we get 0 or -1 as the width constraint. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 339 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 340 | if (result <= 0) { |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 341 | // Return something conservatively big, to force sufficient evaluation. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 342 | return MAX_WIDTH; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 343 | } else { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 344 | // Always allow for the ellipsis character which already accounted for in the width |
| 345 | // constraint. |
| 346 | return result + 1; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 350 | /** |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 351 | * @return {@code true} if the currently displayed result is scrollable |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 352 | */ |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 353 | public boolean isScrollable() { |
| 354 | return mScrollable; |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 357 | int getCurrentCharPos() { |
| 358 | synchronized(mWidthLock) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 359 | return (int) Math.ceil(mCurrentPos / mCharWidth); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 363 | void clear() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 364 | mValid = false; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 365 | mScrollable = false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 366 | setText(""); |
| 367 | } |
| 368 | |
| 369 | void redisplay() { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 370 | int currentCharPos = getCurrentCharPos(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 371 | int maxChars = getMaxChars(); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 372 | String result = getFormattedResult(currentCharPos, maxChars); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 373 | int epos = result.indexOf('e'); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 374 | result = KeyMaps.translateResult(result); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 375 | if (epos > 0 && result.indexOf('.') == -1) { |
| 376 | // Gray out exponent if used as position indicator |
| 377 | SpannableString formattedResult = new SpannableString(result); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 378 | formattedResult.setSpan(mExponentColorSpan, epos, result.length(), |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 379 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 380 | setText(formattedResult); |
| 381 | } else { |
| 382 | setText(result); |
| 383 | } |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 384 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public void computeScroll() { |
| 389 | if (!mScrollable) return; |
| 390 | if (mScroller.computeScrollOffset()) { |
| 391 | mCurrentPos = mScroller.getCurrX(); |
| 392 | if (mCurrentPos != mLastPos) { |
| 393 | mLastPos = mCurrentPos; |
| 394 | redisplay(); |
| 395 | } |
| 396 | if (!mScroller.isFinished()) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 397 | postInvalidateOnAnimation(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 402 | // Copy support: |
| 403 | |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 404 | private ActionMode.Callback mCopyActionModeCallback = new ActionMode.Callback() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 405 | @Override |
| 406 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 407 | MenuInflater inflater = mode.getMenuInflater(); |
| 408 | inflater.inflate(R.menu.copy, menu); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 414 | return false; // Return false if nothing is done |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 419 | switch (item.getItemId()) { |
| 420 | case R.id.menu_copy: |
| 421 | copyContent(); |
| 422 | mode.finish(); |
| 423 | return true; |
| 424 | default: |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void onDestroyActionMode(ActionMode mode) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 431 | mActionMode = null; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 432 | } |
| 433 | }; |
| 434 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 435 | public boolean stopActionMode() { |
| 436 | if (mActionMode != null) { |
| 437 | mActionMode.finish(); |
| 438 | return true; |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 443 | private void setPrimaryClip(ClipData clip) { |
| 444 | ClipboardManager clipboard = (ClipboardManager) getContext(). |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 445 | getSystemService(Context.CLIPBOARD_SERVICE); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 446 | clipboard.setPrimaryClip(clip); |
| 447 | } |
| 448 | |
| 449 | private void copyContent() { |
| 450 | final CharSequence text = getFullText(); |
| 451 | ClipboardManager clipboard = |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 452 | (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); |
| 453 | // We include a tag URI, to allow us to recognize our own results and handle them |
| 454 | // specially. |
| 455 | ClipData.Item newItem = new ClipData.Item(text, null, mEvaluator.capture()); |
| 456 | String[] mimeTypes = new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}; |
| 457 | ClipData cd = new ClipData("calculator result", mimeTypes, newItem); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 458 | clipboard.setPrimaryClip(cd); |
Hans Boehm | c01cd7f | 2015-05-12 18:32:19 -0700 | [diff] [blame] | 459 | Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 462 | } |