UI for memory and functionality for M+ and M-

Bug: 31686717
Test: manual - long-press on result and formula and proper context
menus appear, as well as on results in history.

Change-Id: I88b25ed9d0402e03b420ab984a6b086dff6859e5
diff --git a/src/com/android/calculator2/Calculator.java b/src/com/android/calculator2/Calculator.java
index f41d87e..ea1d445 100644
--- a/src/com/android/calculator2/Calculator.java
+++ b/src/com/android/calculator2/Calculator.java
@@ -80,8 +80,10 @@
 import java.io.ObjectOutput;
 import java.io.ObjectOutputStream;
 
+import static com.android.calculator2.CalculatorFormula.OnFormulaContextMenuClickListener;
+
 public class Calculator extends Activity
-        implements OnTextSizeChangeListener, OnLongClickListener, CalculatorFormula.OnPasteListener,
+        implements OnTextSizeChangeListener, OnLongClickListener,
         AlertDialogFragment.OnClickListener, Evaluator.EvaluationListener /* for main result */ {
 
     /**
@@ -158,6 +160,48 @@
         }
     };
 
+    public final OnDisplayMemoryOperationsListener mOnDisplayMemoryOperationsListener =
+            new OnDisplayMemoryOperationsListener() {
+        @Override
+        public boolean shouldDisplayMemory() {
+            return mEvaluator.getMemoryIndex() != 0;
+        }
+    };
+
+    public final OnFormulaContextMenuClickListener mOnFormulaContextMenuClickListener =
+            new OnFormulaContextMenuClickListener() {
+        @Override
+        public boolean onPaste(ClipData clip) {
+            final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0);
+            if (item == null) {
+                // nothing to paste, bail early...
+                return false;
+            }
+
+            // Check if the item is a previously copied result, otherwise paste as raw text.
+            final Uri uri = item.getUri();
+            if (uri != null && mEvaluator.isLastSaved(uri)) {
+                clearIfNotInputState();
+                mEvaluator.appendExpr(mEvaluator.getSavedIndex());
+                redisplayAfterFormulaChange();
+            } else {
+                addChars(item.coerceToText(Calculator.this).toString(), false);
+            }
+            return true;
+        }
+
+        @Override
+        public void onMemoryRecall() {
+            clearIfNotInputState();
+            long memoryIndex = mEvaluator.getMemoryIndex();
+            if (memoryIndex != 0) {
+                mEvaluator.appendExpr(mEvaluator.getMemoryIndex());
+                redisplayAfterFormulaChange();
+            }  // FIXME: Avoid the 0 case, e.g. by graying out button when memory is unavailable.
+        }
+    };
+
+
     private final TextWatcher mFormulaTextWatcher = new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
@@ -340,8 +384,10 @@
             mEvaluator.clearMain();
         }
 
+        mFormulaText.setOnContextMenuClickListener(mOnFormulaContextMenuClickListener);
+        mFormulaText.setOnDisplayMemoryOperationsListener(mOnDisplayMemoryOperationsListener);
+
         mFormulaText.setOnTextSizeChangeListener(this);
-        mFormulaText.setOnPasteListener(this);
         mFormulaText.addTextChangedListener(mFormulaTextWatcher);
         mDeleteButton.setOnLongClickListener(this);
 
@@ -765,12 +811,6 @@
                     mEvaluator.evaluateAndNotify(mEvaluator.MAIN_INDEX, this, mResultText);
                 }
                 return;
-            case R.id.memory_store:
-                mResultText.onMemoryStore();
-                return;
-            case R.id.memory_recall:
-                onMemoryRecall();
-                return;
             default:
                 cancelIfEvaluating(false);
                 if (haveUnprocessed()) {
@@ -920,15 +960,6 @@
         redisplayAfterFormulaChange();
     }
 
-    private void onMemoryRecall() {
-        clearIfNotInputState();
-        long memoryIndex = mEvaluator.getMemoryIndex();
-        if (memoryIndex != 0) {
-            mEvaluator.appendExpr(mEvaluator.getMemoryIndex());
-            redisplayAfterFormulaChange();
-        }  // FIXME: Avoid the 0 case, e.g. by graying out button when memory is unavailable.
-    }
-
     private void reveal(View sourceView, int colorRes, AnimatorListener listener) {
         final ViewGroupOverlay groupOverlay =
                 (ViewGroupOverlay) getWindow().getDecorView().getOverlay();
@@ -1319,26 +1350,6 @@
         return mHitRect.contains((int) event.getX(), (int) event.getY());
     }
 
-    @Override
-    public boolean onPaste(ClipData clip) {
-        final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0);
-        if (item == null) {
-            // nothing to paste, bail early...
-            return false;
-        }
-
-        // Check if the item is a previously copied result, otherwise paste as raw text.
-        final Uri uri = item.getUri();
-        if (uri != null && mEvaluator.isLastSaved(uri)) {
-            clearIfNotInputState();
-            mEvaluator.appendExpr(mEvaluator.getSavedIndex());
-            redisplayAfterFormulaChange();
-        } else {
-            addChars(item.coerceToText(this).toString(), false);
-        }
-        return true;
-    }
-
     /**
      * Clean up animation for context menu.
      */
@@ -1346,4 +1357,8 @@
     public void onContextMenuClosed(Menu menu) {
         stopActionModeOrContextMenu();
     }
+
+    public interface OnDisplayMemoryOperationsListener {
+        boolean shouldDisplayMemory();
+    }
 }