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; |
| 31 | import android.text.Spanned; |
| 32 | import android.text.SpannableString; |
| 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 | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 51 | final static int MAX_RIGHT_SCROLL = 100000000; |
| 52 | final static int INVALID = MAX_RIGHT_SCROLL + 10000; |
| 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; |
| 68 | // The result holds something valid; either a |
| 69 | // a number or an error message. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 70 | private int mCurrentPos;// Position of right of display relative |
| 71 | // to decimal point, in pixels. |
| 72 | // Large positive values mean the decimal |
| 73 | // point is scrolled off the left of the |
| 74 | // display. Zero means decimal point is |
| 75 | // barely displayed on the right. |
| 76 | private int mLastPos; // Position already reflected in display. |
| 77 | private int mMinPos; // Maximum position before all digits |
| 78 | // digits disappear of the right. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 79 | private Object mWidthLock = new Object(); |
| 80 | // Protects the next two fields. |
| 81 | private int mWidthConstraint = -1; |
| 82 | // Our total width in pixels. |
| 83 | private int mCharWidth = 1; |
| 84 | // Maximum character width. |
| 85 | // For now we pretend that all characters |
| 86 | // have this width. |
| 87 | // TODO: We're not really using a fixed |
| 88 | // width font. But it appears to be close |
| 89 | // enough for the characters we use that |
| 90 | // the difference is not noticeable. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 91 | private Paint mPaint; // Paint object matching display. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 92 | private static final int MAX_WIDTH = 100; |
| 93 | // Maximum number of digits displayed |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 94 | |
| 95 | public CalculatorResult(Context context, AttributeSet attrs) { |
| 96 | super(context, attrs); |
| 97 | mScroller = new OverScroller(context); |
| 98 | mGestureDetector = new GestureDetector(context, |
| 99 | new GestureDetector.SimpleOnGestureListener() { |
| 100 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 101 | public boolean onDown(MotionEvent e) { |
| 102 | return true; |
| 103 | } |
| 104 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 105 | public boolean onFling(MotionEvent e1, MotionEvent e2, |
| 106 | float velocityX, float velocityY) { |
| 107 | if (!mScroller.isFinished()) { |
| 108 | mCurrentPos = mScroller.getFinalX(); |
| 109 | } |
| 110 | mScroller.forceFinished(true); |
| 111 | CalculatorResult.this.cancelLongPress(); // Ignore scrolls of error string, etc. |
| 112 | if (!mScrollable) return true; |
| 113 | mScroller.fling(mCurrentPos, 0, - (int) velocityX, |
| 114 | 0 /* horizontal only */, mMinPos, |
| 115 | MAX_RIGHT_SCROLL, 0, 0); |
| 116 | ViewCompat.postInvalidateOnAnimation(CalculatorResult.this); |
| 117 | return true; |
| 118 | } |
| 119 | @Override |
| 120 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
| 121 | float distanceX, float distanceY) { |
| 122 | // TODO: Should we be dealing with any edge effects here? |
| 123 | if (!mScroller.isFinished()) { |
| 124 | mCurrentPos = mScroller.getFinalX(); |
| 125 | } |
| 126 | mScroller.forceFinished(true); |
| 127 | CalculatorResult.this.cancelLongPress(); |
| 128 | if (!mScrollable) return true; |
| 129 | int duration = (int)(e2.getEventTime() - e1.getEventTime()); |
| 130 | if (duration < 1 || duration > 100) duration = 10; |
| 131 | mScroller.startScroll(mCurrentPos, 0, (int)distanceX, 0, |
| 132 | (int)duration); |
| 133 | ViewCompat.postInvalidateOnAnimation(CalculatorResult.this); |
| 134 | return true; |
| 135 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void onLongPress(MotionEvent e) { |
| 138 | startActionMode(mCopyActionModeCallback); |
| 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 | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 144 | |
| 145 | // Copy ActionMode is triggered explicitly, not through |
| 146 | // setCustomSelectionActionModeCallback. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void setEvaluator(Evaluator evaluator) { |
| 150 | mEvaluator = evaluator; |
| 151 | } |
| 152 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 153 | @Override |
| 154 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 155 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 156 | |
| 157 | mPaint = getPaint(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 158 | char testChar = KeyMaps.translateResult("5").charAt(0); |
| 159 | // TODO: Redo on Locale change? Doesn't seem to matter? |
| 160 | // We try to determine the maximal size of a digit plus |
| 161 | // corresponding inter-character space. |
| 162 | // We assume that "5" has maximal width. Since any |
| 163 | // string includes one fewer inter-character space than |
| 164 | // characters, me measure one that's longer than any real |
| 165 | // display string, and then divide by the number of characters. |
| 166 | // This should bound the per-character space we need for any |
| 167 | // real string. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 168 | StringBuilder sb = new StringBuilder(MAX_WIDTH); |
| 169 | for (int i = 0; i < MAX_WIDTH; ++i) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 170 | sb.append(testChar); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 171 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 172 | final int newWidthConstraint = |
| 173 | MeasureSpec.getSize(widthMeasureSpec) |
| 174 | - getPaddingLeft() - getPaddingRight(); |
| 175 | final int newCharWidth = |
| 176 | (int)Math.ceil(mPaint.measureText(sb.toString()) / MAX_WIDTH); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 177 | synchronized(mWidthLock) { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 178 | mWidthConstraint = newWidthConstraint; |
| 179 | mCharWidth = newCharWidth; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 183 | // Display a new result, given initial displayed |
| 184 | // precision and the string representing the whole part of |
| 185 | // the number to be displayed. |
| 186 | // We pass the string, instead of just the length, so we have |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 187 | // one less place to fix in case we ever decide to |
| 188 | // correctly use a variable width font. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 189 | void displayResult(int initPrec, String truncatedWholePart) { |
| 190 | mLastPos = INVALID; |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 191 | synchronized(mWidthLock) { |
| 192 | mCurrentPos = initPrec * mCharWidth; |
| 193 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 194 | mMinPos = - (int) Math.ceil(mPaint.measureText(truncatedWholePart)); |
| 195 | redisplay(); |
| 196 | } |
| 197 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 198 | void displayError(int resourceId) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame^] | 199 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 200 | mScrollable = false; |
| 201 | setText(resourceId); |
| 202 | } |
| 203 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 204 | private final int MAX_COPY_SIZE = 1000000; |
| 205 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 206 | // Return entire result (within reason) up to current displayed precision. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 207 | public String getFullText() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame^] | 208 | if (!mValid) return ""; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 209 | if (!mScrollable) return getText().toString(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 210 | int currentCharPos = getCurrentCharPos(); |
| 211 | return KeyMaps.translateResult( |
| 212 | mEvaluator.getString(currentCharPos, MAX_COPY_SIZE)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 215 | public boolean fullTextIsExact() { |
| 216 | BoundedRational rat = mEvaluator.getRational(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 217 | int currentCharPos = getCurrentCharPos(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 218 | if (currentCharPos == -1) { |
| 219 | // Suppressing decimal point; still showing all |
| 220 | // integral digits. |
| 221 | currentCharPos = 0; |
| 222 | } |
| 223 | // TODO: Could handle scientific notation cases better; |
| 224 | // We currently treat those conservatively as approximate. |
| 225 | return (currentCharPos >= BoundedRational.digitsRequired(rat)); |
| 226 | } |
| 227 | |
| 228 | // May be called asynchronously from non-UI thread. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 229 | int getMaxChars() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 230 | // We only use 2/3 of the available space, since the |
| 231 | // left 1/3 of the result is not visible when it is shown |
| 232 | // in large size. |
| 233 | int result; |
| 234 | synchronized(mWidthLock) { |
| 235 | result = 2 * mWidthConstraint / (3 * mCharWidth); |
| 236 | // We can apparently finish evaluating before |
| 237 | // onMeasure in CalculatorEditText has been called, in |
| 238 | // which case we get 0 or -1 as the width constraint. |
| 239 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 240 | if (result <= 0) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 241 | // Return something conservatively big, to force sufficient |
| 242 | // evaluation. |
| 243 | return MAX_WIDTH; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 244 | } else { |
| 245 | return result; |
| 246 | } |
| 247 | } |
| 248 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 249 | int getCurrentCharPos() { |
| 250 | synchronized(mWidthLock) { |
| 251 | return mCurrentPos/mCharWidth; |
| 252 | } |
| 253 | } |
| 254 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 255 | void clear() { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame^] | 256 | mValid = false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 257 | setText(""); |
| 258 | } |
| 259 | |
| 260 | void redisplay() { |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 261 | int currentCharPos = getCurrentCharPos(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 262 | int maxChars = getMaxChars(); |
| 263 | String result = mEvaluator.getString(currentCharPos, maxChars); |
| 264 | int epos = result.indexOf('e'); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 265 | result = KeyMaps.translateResult(result); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 266 | if (epos > 0 && result.indexOf('.') == -1) { |
| 267 | // Gray out exponent if used as position indicator |
| 268 | SpannableString formattedResult = new SpannableString(result); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 269 | formattedResult.setSpan(new ForegroundColorSpan(Color.LTGRAY), |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 270 | epos, result.length(), |
| 271 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 272 | setText(formattedResult); |
| 273 | } else { |
| 274 | setText(result); |
| 275 | } |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame^] | 276 | mValid = true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 277 | mScrollable = true; |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void computeScroll() { |
| 282 | if (!mScrollable) return; |
| 283 | if (mScroller.computeScrollOffset()) { |
| 284 | mCurrentPos = mScroller.getCurrX(); |
| 285 | if (mCurrentPos != mLastPos) { |
| 286 | mLastPos = mCurrentPos; |
| 287 | redisplay(); |
| 288 | } |
| 289 | if (!mScroller.isFinished()) { |
| 290 | ViewCompat.postInvalidateOnAnimation(this); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 295 | // Copy support: |
| 296 | |
| 297 | private ActionMode.Callback mCopyActionModeCallback = |
| 298 | new ActionMode.Callback() { |
| 299 | @Override |
| 300 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 301 | MenuInflater inflater = mode.getMenuInflater(); |
| 302 | inflater.inflate(R.menu.copy, menu); |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 308 | return false; // Return false if nothing is done |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 313 | switch (item.getItemId()) { |
| 314 | case R.id.menu_copy: |
| 315 | copyContent(); |
| 316 | mode.finish(); |
| 317 | return true; |
| 318 | default: |
| 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void onDestroyActionMode(ActionMode mode) { |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | private void setPrimaryClip(ClipData clip) { |
| 329 | ClipboardManager clipboard = (ClipboardManager) getContext(). |
| 330 | getSystemService(Context.CLIPBOARD_SERVICE); |
| 331 | clipboard.setPrimaryClip(clip); |
| 332 | } |
| 333 | |
| 334 | private void copyContent() { |
| 335 | final CharSequence text = getFullText(); |
| 336 | ClipboardManager clipboard = |
| 337 | (ClipboardManager) getContext().getSystemService( |
| 338 | Context.CLIPBOARD_SERVICE); |
| 339 | // We include a tag URI, to allow us to recognize our |
| 340 | // own results and handle them specially. |
| 341 | ClipData.Item newItem = new ClipData.Item(text, null, |
| 342 | mEvaluator.capture()); |
| 343 | String[] mimeTypes = |
| 344 | new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}; |
| 345 | ClipData cd = new ClipData("calculator result", |
| 346 | mimeTypes, newItem); |
| 347 | clipboard.setPrimaryClip(cd); |
| 348 | Toast.makeText(getContext(), R.string.text_copied_toast, |
| 349 | Toast.LENGTH_SHORT).show(); |
| 350 | } |
| 351 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 352 | } |