TBR Adding advertise neighborhood menu option

This menu option is shown to the user as "Share Location".

Rebased and taken over by alexfandrianto

Change-Id: I14fd3e5b5136273712978aecd00b9f84a9207a31
diff --git a/app/build.gradle b/app/build.gradle
index e0ce402..80c1446 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -50,28 +50,28 @@
     testCompile 'junit:junit:4.12'
 
     // Additional testing libraries (mockito and espresso) for the mock flavor.
-    androidTestMockCompile (
+    androidTestMockCompile(
             'org.mockito:mockito-core:1.10.19',
             'com.google.dexmaker:dexmaker:1.2',
             'com.google.dexmaker:dexmaker-mockito:1.2'
     )
 
-    androidTestMockCompile ('com.android.support.test.espresso:espresso-intents:2.2') {
-            // Necessary to avoid version conflicts
-            exclude group: 'com.android.support', module: 'appcompat'
-            exclude group: 'com.android.support', module: 'support-v4'
-            exclude group: 'com.android.support', module: 'support-annotations'
-            exclude module: 'recyclerview-v7'
+    androidTestMockCompile('com.android.support.test.espresso:espresso-intents:2.2') {
+        // Necessary to avoid version conflicts
+        exclude group: 'com.android.support', module: 'appcompat'
+        exclude group: 'com.android.support', module: 'support-v4'
+        exclude group: 'com.android.support', module: 'support-annotations'
+        exclude module: 'recyclerview-v7'
     }
     androidTestMockCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
-            // Necessary to avoid version conflicts
-            exclude group: 'com.android.support', module: 'appcompat'
-            exclude group: 'com.android.support', module: 'support-v4'
-            exclude group: 'com.android.support', module: 'support-annotations'
-            exclude module: 'recyclerview-v7'
+        // Necessary to avoid version conflicts
+        exclude group: 'com.android.support', module: 'appcompat'
+        exclude group: 'com.android.support', module: 'support-v4'
+        exclude group: 'com.android.support', module: 'support-annotations'
+        exclude module: 'recyclerview-v7'
     }
 
-    compile (
+    compile(
             'com.android.support:appcompat-v7:23.1.1',
             'com.android.support:design:23.1.1',
             'com.android.support:cardview-v7:23.1.1',
diff --git a/app/src/main/java/io/v/todos/MainActivity.java b/app/src/main/java/io/v/todos/MainActivity.java
index 8a92762..127d1dc 100644
--- a/app/src/main/java/io/v/todos/MainActivity.java
+++ b/app/src/main/java/io/v/todos/MainActivity.java
@@ -13,8 +13,6 @@
 import android.view.MenuItem;
 import android.view.View;
 
-import java.util.List;
-
 import io.v.todos.model.DataList;
 import io.v.todos.model.ListMetadata;
 import io.v.todos.model.ListSpec;
@@ -163,22 +161,14 @@
     // The following methods are boilerplate for handling the Menu in the top right corner.
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
-        // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true;
     }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
-        // Handle action bar item clicks here. The action bar will
-        // automatically handle clicks on the Home/Up button, so long
-        // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
 
-        //noinspection SimplifiableIfStatement
-        if (id == R.id.action_settings) {
-            return true;
-        }
         if (id == R.id.action_debug) {
             sharePersistenceDebugDetails();
             return true;
@@ -186,4 +176,4 @@
 
         return super.onOptionsItemSelected(item);
     }
-}
\ No newline at end of file
+}
diff --git a/app/src/main/java/io/v/todos/TodoListActivity.java b/app/src/main/java/io/v/todos/TodoListActivity.java
index 092e00a..01392fa 100644
--- a/app/src/main/java/io/v/todos/TodoListActivity.java
+++ b/app/src/main/java/io/v/todos/TodoListActivity.java
@@ -222,8 +222,6 @@
             case R.id.show_done:
                 mPersistence.setShowDone(!item.isChecked());
                 return true;
-            case R.id.action_settings:
-                return true;
             case R.id.action_edit:
                 initiateTodoListEdit();
                 return true;
diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml
index 0be017d..ec59dc4 100644
--- a/app/src/main/res/menu/menu_main.xml
+++ b/app/src/main/res/menu/menu_main.xml
@@ -3,11 +3,6 @@
     xmlns:tools="http://schemas.android.com/tools"
     tools:context="io.v.todos.MainActivity">
     <item
-        android:id="@+id/action_settings"
-        android:orderInCategory="100"
-        android:title="@string/action_settings"
-        app:showAsAction="never" />
-    <item
         android:id="@+id/action_debug"
         android:orderInCategory="102"
         android:title="@string/action_debug"
diff --git a/app/src/main/res/menu/menu_task.xml b/app/src/main/res/menu/menu_task.xml
index 8540c76..c405b5b 100644
--- a/app/src/main/res/menu/menu_task.xml
+++ b/app/src/main/res/menu/menu_task.xml
@@ -3,11 +3,6 @@
     xmlns:tools="http://schemas.android.com/tools"
     tools:context="io.v.todos.TodoListActivity">
     <item
-        android:id="@+id/action_settings"
-        android:orderInCategory="100"
-        android:title="@string/action_settings"
-        app:showAsAction="never" />
-    <item
         android:id="@+id/show_done"
         android:orderInCategory="101"
         android:checkable="true"
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 6c7ef76..5c7280c 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,5 +1,4 @@
 <resources>
-    <string name="action_settings">Settings</string>
     <string name="action_debug">Debug DB</string>
     <string name="action_edit">Edit</string>
     <string name="action_share">Share</string>
diff --git a/app/src/syncbase/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java b/app/src/syncbase/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java
index 4e0d795..df27fea 100644
--- a/app/src/syncbase/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java
+++ b/app/src/syncbase/java/io/v/todos/persistence/syncbase/SyncbasePersistence.java
@@ -5,6 +5,7 @@
 package io.v.todos.persistence.syncbase;
 
 import android.app.Activity;
+import android.app.FragmentTransaction;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.Handler;
@@ -41,6 +42,7 @@
 import io.v.impl.google.services.syncbase.SyncbaseServer;
 import io.v.todos.R;
 import io.v.todos.persistence.Persistence;
+import io.v.todos.sharing.NeighborhoodFragment;
 import io.v.v23.OptionDefs;
 import io.v.v23.Options;
 import io.v.v23.VFutures;
@@ -429,6 +431,10 @@
         Futures.addCallback(future, new TrappingCallback<>(getErrorReporter()));
     }
 
+    protected void addFeatureFragments(FragmentTransaction fragments) {
+        fragments.add(new NeighborhoodFragment(), NeighborhoodFragment.FRAGMENT_TAG);
+    }
+
     /**
      * This constructor is blocking for simplicity.
      */
@@ -436,6 +442,12 @@
             throws VException, SyncbaseServer.StartException {
         mVAndroidContext = VAndroidContexts.withDefaults(activity, savedInstanceState);
 
+        if (savedInstanceState == null) {
+            FragmentTransaction fragments = activity.getFragmentManager().beginTransaction();
+            addFeatureFragments(fragments);
+            fragments.commit();
+        }
+
         // We might not actually have to seek blessings each time, but getBlessings does not
         // block if we already have blessings and this has better-behaved lifecycle
         // implications than trying to seek blessings in the static code.
diff --git a/app/src/syncbase/java/io/v/todos/sharing/NeighborhoodFragment.java b/app/src/syncbase/java/io/v/todos/sharing/NeighborhoodFragment.java
new file mode 100644
index 0000000..c7ab727
--- /dev/null
+++ b/app/src/syncbase/java/io/v/todos/sharing/NeighborhoodFragment.java
@@ -0,0 +1,62 @@
+package io.v.todos.sharing;
+
+
+import android.app.Fragment;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.support.annotation.Nullable;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+
+import io.v.todos.R;
+
+/**
+ * A fragment encapsulating menu options and functionality related to list sharing.
+ */
+public class NeighborhoodFragment extends Fragment {
+    public static final String FRAGMENT_TAG = NeighborhoodFragment.class.getSimpleName();
+
+    private static final String PREF_ADVERTISE_NEIGHBORHOOD = "advertise neighborhood";
+
+    private SharedPreferences mPrefs;
+
+    private boolean isAdvertising() {
+        return mPrefs.getBoolean(PREF_ADVERTISE_NEIGHBORHOOD, false);
+    }
+
+    @Override
+    public void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setHasOptionsMenu(true);
+
+        mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        inflater.inflate(R.menu.sharing, menu);
+        setAdvertiseNeighborhoodChecked(menu.findItem(R.id.advertise_neighborhood), isAdvertising());
+    }
+
+    private void setAdvertiseNeighborhoodChecked(MenuItem menuItem, boolean value) {
+        menuItem.setChecked(value);
+        menuItem.setIcon(value ? R.drawable.ic_advertise_neighborhood_on_white_24dp :
+                R.drawable.ic_advertise_neighborhood_off_white_24dp);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        if (item.getItemId() == R.id.advertise_neighborhood) {
+            boolean advertiseNeighborhood = !item.isChecked();
+            mPrefs.edit()
+                    .putBoolean(PREF_ADVERTISE_NEIGHBORHOOD, advertiseNeighborhood)
+                    .apply();
+            setAdvertiseNeighborhoodChecked(item, advertiseNeighborhood);
+            return true;
+        } else {
+            return super.onOptionsItemSelected(item);
+        }
+    }
+}
diff --git a/app/src/syncbase/res/drawable-hdpi/ic_advertise_neighborhood_off_white_24dp.png b/app/src/syncbase/res/drawable-hdpi/ic_advertise_neighborhood_off_white_24dp.png
new file mode 100644
index 0000000..6e61f95
--- /dev/null
+++ b/app/src/syncbase/res/drawable-hdpi/ic_advertise_neighborhood_off_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-hdpi/ic_advertise_neighborhood_on_white_24dp.png b/app/src/syncbase/res/drawable-hdpi/ic_advertise_neighborhood_on_white_24dp.png
new file mode 100644
index 0000000..6c0314a
--- /dev/null
+++ b/app/src/syncbase/res/drawable-hdpi/ic_advertise_neighborhood_on_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-hdpi/ic_gps_fixed_white_24dp.png b/app/src/syncbase/res/drawable-hdpi/ic_gps_fixed_white_24dp.png
new file mode 100644
index 0000000..745db48
--- /dev/null
+++ b/app/src/syncbase/res/drawable-hdpi/ic_gps_fixed_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-mdpi/ic_advertise_neighborhood_off_white_24dp.png b/app/src/syncbase/res/drawable-mdpi/ic_advertise_neighborhood_off_white_24dp.png
new file mode 100644
index 0000000..ab03ab5
--- /dev/null
+++ b/app/src/syncbase/res/drawable-mdpi/ic_advertise_neighborhood_off_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-mdpi/ic_advertise_neighborhood_on_white_24dp.png b/app/src/syncbase/res/drawable-mdpi/ic_advertise_neighborhood_on_white_24dp.png
new file mode 100644
index 0000000..8dec6e0
--- /dev/null
+++ b/app/src/syncbase/res/drawable-mdpi/ic_advertise_neighborhood_on_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-mdpi/ic_gps_fixed_white_24dp.png b/app/src/syncbase/res/drawable-mdpi/ic_gps_fixed_white_24dp.png
new file mode 100644
index 0000000..d1c563c
--- /dev/null
+++ b/app/src/syncbase/res/drawable-mdpi/ic_gps_fixed_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xhdpi/ic_advertise_neighborhood_off_white_24dp.png b/app/src/syncbase/res/drawable-xhdpi/ic_advertise_neighborhood_off_white_24dp.png
new file mode 100644
index 0000000..e21ea70
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xhdpi/ic_advertise_neighborhood_off_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xhdpi/ic_advertise_neighborhood_on_white_24dp.png b/app/src/syncbase/res/drawable-xhdpi/ic_advertise_neighborhood_on_white_24dp.png
new file mode 100644
index 0000000..cb07640
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xhdpi/ic_advertise_neighborhood_on_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xhdpi/ic_gps_fixed_white_24dp.png b/app/src/syncbase/res/drawable-xhdpi/ic_gps_fixed_white_24dp.png
new file mode 100644
index 0000000..ffab865
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xhdpi/ic_gps_fixed_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xxhdpi/ic_advertise_neighborhood_off_white_24dp.png b/app/src/syncbase/res/drawable-xxhdpi/ic_advertise_neighborhood_off_white_24dp.png
new file mode 100644
index 0000000..8735faa
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xxhdpi/ic_advertise_neighborhood_off_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xxhdpi/ic_advertise_neighborhood_on_white_24dp.png b/app/src/syncbase/res/drawable-xxhdpi/ic_advertise_neighborhood_on_white_24dp.png
new file mode 100644
index 0000000..9e9560c
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xxhdpi/ic_advertise_neighborhood_on_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xxhdpi/ic_gps_fixed_white_24dp.png b/app/src/syncbase/res/drawable-xxhdpi/ic_gps_fixed_white_24dp.png
new file mode 100644
index 0000000..387ecdf
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xxhdpi/ic_gps_fixed_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xxxhdpi/ic_advertise_neighborhood_off_white_24dp.png b/app/src/syncbase/res/drawable-xxxhdpi/ic_advertise_neighborhood_off_white_24dp.png
new file mode 100644
index 0000000..e28418e
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xxxhdpi/ic_advertise_neighborhood_off_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xxxhdpi/ic_advertise_neighborhood_on_white_24dp.png b/app/src/syncbase/res/drawable-xxxhdpi/ic_advertise_neighborhood_on_white_24dp.png
new file mode 100644
index 0000000..f992b16
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xxxhdpi/ic_advertise_neighborhood_on_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/drawable-xxxhdpi/ic_gps_fixed_white_24dp.png b/app/src/syncbase/res/drawable-xxxhdpi/ic_gps_fixed_white_24dp.png
new file mode 100644
index 0000000..c55220a
--- /dev/null
+++ b/app/src/syncbase/res/drawable-xxxhdpi/ic_gps_fixed_white_24dp.png
Binary files differ
diff --git a/app/src/syncbase/res/menu/sharing.xml b/app/src/syncbase/res/menu/sharing.xml
new file mode 100644
index 0000000..622ff1d
--- /dev/null
+++ b/app/src/syncbase/res/menu/sharing.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+      xmlns:app="http://schemas.android.com/apk/res-auto">
+    <item android:id="@+id/advertise_neighborhood"
+          android:checkable="true"
+          android:icon="@drawable/ic_advertise_neighborhood_off_white_24dp"
+          android:title="@string/share_location"
+          android:showAsAction="always"/>
+</menu>
\ No newline at end of file
diff --git a/app/src/syncbase/res/values/strings.xml b/app/src/syncbase/res/values/strings.xml
index 5088ba7..d6d2a51 100644
--- a/app/src/syncbase/res/values/strings.xml
+++ b/app/src/syncbase/res/values/strings.xml
@@ -1,3 +1,4 @@
 <resources>
     <string name="app_name">Syncbase Todos</string>
+    <string name="share_location">Share Location</string>
 </resources>