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