TODOs: HLAPI 0.1.7 upgrade (cloud) and discovery workaround

This upgrades TODOs to use the cloud changes from 0.1.7.

It also contains a bugfix for finding peers. Restarting an ad
doesn't seem to play well with our underlying implementation,
so I'm more careful to only stop if started and start if not
started. However, I have a feeling the real bug is deeper in
our implementation.

Change-Id: I3232cca474b9aca1e1bd9bbc917bd2224d252f13
diff --git a/app/build.gradle b/app/build.gradle
index 5e9e0a6..c6b2854 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -83,5 +83,5 @@
     )
     firebaseCompile 'com.firebase:firebase-client-android:2.5.2'
     syncbaseCompile 'io.v:vanadium-android:2.2.+'
-    syncbase2Compile 'io.v:syncbase:0.1.6'
+    syncbase2Compile 'io.v:syncbase:0.1.7'
 }
diff --git a/app/src/syncbase2/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java b/app/src/syncbase2/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java
index e5d4ef8..8a1d5db 100644
--- a/app/src/syncbase2/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java
+++ b/app/src/syncbase2/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java
@@ -17,7 +17,6 @@
 import java.util.Iterator;
 import java.util.Map;
 
-import io.v.syncbase.Collection;
 import io.v.syncbase.Database;
 import io.v.syncbase.Syncbase;
 import io.v.syncbase.Id;
@@ -42,6 +41,13 @@
     protected static final String TODO_LIST_COLLECTION_PREFIX = "list";
     protected static final String TAG = "High-Level Syncbase";
 
+    // The todos app's cloud instance allocated by sb-allocator. All users share this cloud.
+    // To allocate your own cloud instance, go to https://sb-allocator.v.io/home
+    private static final String CLOUD_NAME =
+            "/(dev.v.io:r:vprod:service:mounttabled)@ns.dev.v.io:8101/sb/syncbased-e0cf21ca";
+    private static final String CLOUD_ADMIN = "dev.v.io:r:allocator:us:x:syncbased-e0cf21ca";
+    private static final String MOUNT_POINT = "/ns.dev.v.io:8101/tmp/todos/users/";
+
     protected static boolean sInitialized = false;
 
     protected static final Map<Id, ListSpec> sListSpecMap = new HashMap<>();
@@ -68,9 +74,11 @@
             if (!sInitialized) {
                 Log.d(TAG, "Initializing Syncbase Persistence...");
 
-                Syncbase.Options opts = new Syncbase.Options();
-                opts.rootDir = activity.getFilesDir().getAbsolutePath();
-                opts.disableSyncgroupPublishing = true;
+                String rootDir = activity.getFilesDir().getAbsolutePath();
+                Syncbase.Options opts =
+                        Syncbase.Options.cloudBuilder(rootDir, CLOUD_NAME, CLOUD_ADMIN)
+                                .setMountPoint(MOUNT_POINT)
+                                .build();
                 try {
                     Syncbase.init(opts);
                 } catch (SyncbaseException e) {
@@ -81,37 +89,34 @@
                 final Object initializeMutex = new Object();
 
                 Log.d(TAG, "Logging the user in!");
-                if (!Syncbase.isLoggedIn()) {
-                    Syncbase.loginAndroid(activity, new Syncbase.LoginCallback() {
-                        @Override
-                        public void onSuccess() {
-                            Log.d(TAG, "Successfully logged in!");
-                            try {
-                                sDb = Syncbase.database();
-                            } catch (SyncbaseException e) {
-                                Log.e(TAG, "Failed to create database", e);
-                                callNotify();
-                                return;
-                            }
+                Syncbase.loginAndroid(activity, new Syncbase.LoginCallback() {
+                    @Override
+                    public void onSuccess() {
+                        Log.d(TAG, "Successfully logged in!");
+                        try {
+                            sDb = Syncbase.database();
                             continueSetup();
                             sInitialized = true;
                             Log.d(TAG, "Successfully initialized!");
+                        } catch (SyncbaseException e) {
+                            Log.e(TAG, "Failed to create database", e);
+                        } finally {
                             callNotify();
                         }
+                    }
 
-                        @Override
-                        public void onError(Throwable e) {
-                            Log.e(TAG, "Failed to login. :(", e);
-                            callNotify();
-                        }
+                    @Override
+                    public void onError(Throwable e) {
+                        Log.e(TAG, "Failed to login. :(", e);
+                        callNotify();
+                    }
 
-                        private void callNotify() {
-                            synchronized (initializeMutex) {
-                                initializeMutex.notify();
-                            }
+                    private void callNotify() {
+                        synchronized (initializeMutex) {
+                            initializeMutex.notify();
                         }
-                    });
-                }
+                    }
+                });
 
                 Log.d(TAG, "Let's wait until we are logged in...");
                 synchronized (initializeMutex) {
@@ -320,7 +325,7 @@
             public void onError(Throwable e) {
                 Log.w(TAG, "error during watch", e);
             }
-        }, new Database.AddWatchChangeHandlerOptions());
+        });
 
         Log.d(TAG, "Accepting all invitations");
 
@@ -345,7 +350,7 @@
             public void onError(Throwable e) {
                 Log.w(TAG, "error while handling invitations", e);
             }
-        }, new Database.AddSyncgroupInviteHandlerOptions());
+        });
 
         // And do a background scan for peers near me.
         ShareListDialogFragment.initScan();
diff --git a/app/src/syncbase2/java/io/v/todos/sharing/NeighborhoodFragment.java b/app/src/syncbase2/java/io/v/todos/sharing/NeighborhoodFragment.java
index bb2ff3b..69537a9 100644
--- a/app/src/syncbase2/java/io/v/todos/sharing/NeighborhoodFragment.java
+++ b/app/src/syncbase2/java/io/v/todos/sharing/NeighborhoodFragment.java
@@ -17,9 +17,6 @@
 import io.v.syncbase.Syncbase;
 import io.v.syncbase.core.VError;
 import io.v.todos.R;
-import io.v.todos.persistence.syncbase.SyncbasePersistence;
-import io.v.v23.context.VContext;
-import io.v.v23.discovery.Advertisement;
 
 /**
  * A fragment encapsulating menu options and functionality related to list sharing.
@@ -61,11 +58,13 @@
     private static void updateSharePresence() {
         if (shouldAdvertise()) {
             try {
-                Syncbase.advertiseLoggedInUserInNeighborhood();
+                if (!Syncbase.isAdvertisingLoggedInUserInNeighborhood()) {
+                    Syncbase.advertiseLoggedInUserInNeighborhood();
+                }
             } catch (VError vError) {
                 Log.w(FRAGMENT_TAG, "Failed to advertise logged in user", vError);
             }
-        } else {
+        } else if (Syncbase.isAdvertisingLoggedInUserInNeighborhood()) {
             Syncbase.stopAdvertisingLoggedInUserInNeighborhood();
         }
     }