example/ivy/android: fix demo to handle comments & 'quit' correctly

Demo feature is implemented by reading the demo.ivy file included
as an asset, read lines, and feed each line to mobile.Eval if it
is not a comment line. Comments can start from the middle of a line
so previously, the code simply looked for '#' in each line and
treated the substring starting from it as a comment. This assumption
is not correct - see ")format '%#x'". Without proper parsing, it is
hard to do it correctly. This CL passes the entire line to Ivy as
it is - the backend correctly parses the line with comments.
One drawback of this change is, unlike the line comments, we don't
apply the style for comments (gray font). But correctness is more
important. This also simplifies the code.

This CL implements the 'quit' command that terminates a demo
session (as mentioned in the demo.ivy script).

Change-Id: Ib0801338e0dd34c52c6c6209fe47a1846e71f0c8
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/356732
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
diff --git a/example/ivy/android/app/src/main/java/org/golang/ivy/MainActivity.java b/example/ivy/android/app/src/main/java/org/golang/ivy/MainActivity.java
index 7cbf0b4..d58d9c4 100644
--- a/example/ivy/android/app/src/main/java/org/golang/ivy/MainActivity.java
+++ b/example/ivy/android/app/src/main/java/org/golang/ivy/MainActivity.java
@@ -244,6 +244,10 @@
         if (s != null && !s.isEmpty()) {
             appendShowText(PROMPT + s, "expr");
         }
+        if (mDemo != null && s.trim().equals("quit")) {
+            unloadDemo();
+            s = " ";  // this will clear the text box.
+        }
         new IvyCallTask().execute(s);  // where call to Ivy backend occurs.
     }
 
@@ -293,7 +297,7 @@
     private class IvyCallTask extends AsyncTask<String, Void, Pair<String, String> > {
         private String ivyEval(final String expr) {
             try {
-                // org.golang.ivy.Mobile was generated using
+                // mobile.Mobile was generated using
                 // gomobile bind -javapkg=org.golang.ivy robpike.io/ivy/mobile
                 return Mobile.eval(expr);  // Gobind-generated method.
             } catch (Exception e) {
@@ -318,22 +322,15 @@
             String showText = null;
             while (true) {
                 String s = readDemo();
-                if (s == null) { return Pair.create(showText, null); }
-
-                int sharp = s.indexOf("#");
-                if (sharp < 0) {
-                    return Pair.create(showText, s);
+                if (s == null) {
+                    break;
                 }
-                s += "\n";
-                if (showText == null) {
-                    showText = s.substring(sharp, s.length());
-                } else {
-                    showText += s.substring(sharp, s.length());
+                if (s.startsWith("# ")) {
+                    return Pair.create(s, null);
                 }
-                if (sharp > 0) {
-                    return Pair.create(s.substring(sharp, s.length()), s.substring(0, sharp));
-                }
+                return Pair.create(null, s);
             }
+            return null;
         }
 
         @Override
@@ -355,7 +352,6 @@
                     }
                 }
             });
-
         }
     }
 }