discovery: simplify discovery/plugin init in android

  Currently discovery factory is using its own context which was
  given during runtime initialization. This makes discovery init
  in Android a bit dirty since we need to keep the android context
  in the initial context and also lock the reference to it.

  This change gets rid of this hack by using the context that is
  given to v23.NewDiscovery() to create a new discovery instance
  if not yet exists.

Change-Id: I00177b90ded8cc2cfe71efeeb7aaff2feae6a49c
MultiPart: 2/3
diff --git a/impl/google/discovery/plugins/jni.go b/impl/google/discovery/plugins/jni.go
index d397ca4..85706ab 100644
--- a/impl/google/discovery/plugins/jni.go
+++ b/impl/google/discovery/plugins/jni.go
@@ -26,7 +26,7 @@
 	jNativeScanHandlerClass jutil.Class // io.v.android.impl.google.discovery.plugins.NativeScanHandler
 )
 
-func Init(env jutil.Env, jCtx jutil.Object) error {
+func Init(env jutil.Env) error {
 	var err error
 	jAdInfoClass, err = jutil.JFindClass(env, "io/v/x/ref/lib/discovery/AdInfo")
 	if err != nil {
@@ -37,7 +37,7 @@
 		return err
 	}
 
-	return initPluginFactories(env, jCtx)
+	return initPluginFactories(env)
 }
 
 //export Java_io_v_android_impl_google_discovery_plugins_NativeScanHandler_nativeHandleUpdate
diff --git a/impl/google/discovery/plugins/plugin.go b/impl/google/discovery/plugins/plugin.go
index c6b88df..ef4451e 100644
--- a/impl/google/discovery/plugins/plugin.go
+++ b/impl/google/discovery/plugins/plugin.go
@@ -15,6 +15,7 @@
 	dfactory "v.io/x/ref/lib/discovery/factory"
 
 	jutil "v.io/x/jni/util"
+	jcontext "v.io/x/jni/v23/context"
 )
 
 // #include "jni.h"
@@ -82,13 +83,17 @@
 	return nil
 }
 
-func newPluginFactory(env jutil.Env, jCtx jutil.Object, jPluginClass jutil.Class) func(*context.T, string) (idiscovery.Plugin, error) {
-	return func(_ *context.T, host string) (idiscovery.Plugin, error) {
+func newPluginFactory(env jutil.Env, jPluginClass jutil.Class) func(*context.T, string) (idiscovery.Plugin, error) {
+	return func(ctx *context.T, host string) (idiscovery.Plugin, error) {
 		env, freeFunc := jutil.GetEnv()
 		defer freeFunc()
 
 		// We pass the global context of the android vanadium runtime, since the context of the discovery
 		// factory does not have an Android context.
+		jCtx, err := jcontext.JavaContext(env, ctx, nil)
+		if err != nil {
+			return nil, err
+		}
 		jPlugin, err := jutil.NewObject(env, jPluginClass, []jutil.Sign{contextSign, jutil.StringSign}, jCtx, host)
 		if err != nil {
 			return nil, err
@@ -108,15 +113,11 @@
 	}
 }
 
-func initPluginFactories(env jutil.Env, jCtx jutil.Object) error {
-	// We need to keep the reference to the context during the lifetime
-	// of the runtime since plugin factories can be called anytime.
-	jCtx = jutil.NewGlobalRef(env, jCtx)
-
+func initPluginFactories(env jutil.Env) error {
 	jPluginClass, err := jutil.JFindClass(env, "io/v/android/impl/google/discovery/plugins/ble/BlePlugin")
 	if err != nil {
 		return err
 	}
-	dfactory.SetBlePluginFactory(newPluginFactory(env, jCtx, jPluginClass))
+	dfactory.SetPluginFactory("ble", newPluginFactory(env, jPluginClass))
 	return nil
 }
diff --git a/jni_android.go b/jni_android.go
index b0f3958..c7273c0 100644
--- a/jni_android.go
+++ b/jni_android.go
@@ -20,7 +20,7 @@
 import "C"
 
 //export Java_io_v_android_v23_V_nativeInitGlobalAndroid
-func Java_io_v_android_v23_V_nativeInitGlobalAndroid(jenv *C.JNIEnv, jVClass C.jclass, jContext C.jobject, jOptions C.jobject) {
+func Java_io_v_android_v23_V_nativeInitGlobalAndroid(jenv *C.JNIEnv, jVClass C.jclass, jOptions C.jobject) {
 	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
 	jOpts := jutil.Object(uintptr(unsafe.Pointer(jOptions)))
 
@@ -35,8 +35,7 @@
 	vlog.Log.Configure(vlog.OverridePriorConfiguration(true), vlog.LogToStderr(false), vlog.AlsoLogToStderr(false), level, vmodule)
 
 	// Setup discovery plugins.
-	jCtx := jutil.Object(uintptr(unsafe.Pointer(jContext)))
-	if err := jdplugins.Init(env, jCtx); err != nil {
+	if err := jdplugins.Init(env); err != nil {
 		jutil.JThrowV(env, err)
 		return
 	}