Merge "[NAN] Add Handler to publish/subscribe per API review"

am: 95abef9355

Change-Id: I31f34ec26776ae7168128c47cda0fc29d08c7753
This commit is contained in:
Etan Cohen
2016-09-26 00:21:46 +00:00
committed by android-build-merger
2 changed files with 25 additions and 20 deletions

View File

@@ -58,8 +58,8 @@ import java.util.Arrays;
* <li>Initialize a NAN cluster (peer-to-peer synchronization). Refer to * <li>Initialize a NAN cluster (peer-to-peer synchronization). Refer to
* {@link #attach(Handler, WifiNanAttachCallback)}. * {@link #attach(Handler, WifiNanAttachCallback)}.
* <li>Create discovery sessions (publish or subscribe sessions). Refer to * <li>Create discovery sessions (publish or subscribe sessions). Refer to
* {@link WifiNanSession#publish(PublishConfig, WifiNanDiscoverySessionCallback)} and * {@link WifiNanSession#publish(Handler, PublishConfig, WifiNanDiscoverySessionCallback)} and
* {@link WifiNanSession#subscribe(SubscribeConfig, WifiNanDiscoverySessionCallback)}. * {@link WifiNanSession#subscribe(Handler, SubscribeConfig, WifiNanDiscoverySessionCallback)}.
* <li>Create a NAN network specifier to be used with * <li>Create a NAN network specifier to be used with
* {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)} * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)}
* to set-up a NAN connection with a peer. Refer to * to set-up a NAN connection with a peer. Refer to
@@ -85,8 +85,8 @@ import java.util.Arrays;
* device will actually disable NAN once the last application detaches. * device will actually disable NAN once the last application detaches.
* <p> * <p>
* Once a NAN attach is confirmed use the * Once a NAN attach is confirmed use the
* {@link WifiNanSession#publish(PublishConfig, WifiNanDiscoverySessionCallback)} or * {@link WifiNanSession#publish(Handler, PublishConfig, WifiNanDiscoverySessionCallback)} or
* {@link WifiNanSession#subscribe(SubscribeConfig, WifiNanDiscoverySessionCallback)} to * {@link WifiNanSession#subscribe(Handler, SubscribeConfig, WifiNanDiscoverySessionCallback)} to
* create publish or subscribe NAN discovery sessions. Events are called on the provided * create publish or subscribe NAN discovery sessions. Events are called on the provided
* callback object {@link WifiNanDiscoverySessionCallback}. Specifically, the * callback object {@link WifiNanDiscoverySessionCallback}. Specifically, the
* {@link WifiNanDiscoverySessionCallback#onPublishStarted(WifiNanPublishDiscoverySession)} * {@link WifiNanDiscoverySessionCallback#onPublishStarted(WifiNanPublishDiscoverySession)}
@@ -330,9 +330,9 @@ public class WifiNanManager {
* then this function will simply indicate success immediately using the same {@code * then this function will simply indicate success immediately using the same {@code
* attachCallback}. * attachCallback}.
* *
* @param handler The Handler on whose thread to execute all callbacks related to the * @param handler The Handler on whose thread to execute the callbacks of the {@code
* attach request - including all sessions opened as part of this * attachCallback} object. If a null is provided then the application's main thread will be
* attach. If a null is provided then the application's main thread will be used. * used.
* @param attachCallback A callback for attach events, extended from * @param attachCallback A callback for attach events, extended from
* {@link WifiNanAttachCallback}. * {@link WifiNanAttachCallback}.
*/ */
@@ -360,12 +360,13 @@ public class WifiNanManager {
* requirements this listener will wake up the host at regular intervals causing higher power * requirements this listener will wake up the host at regular intervals causing higher power
* consumption, do not use it unless the information is necessary (e.g. for OOB discovery). * consumption, do not use it unless the information is necessary (e.g. for OOB discovery).
* *
* @param handler The Handler on whose thread to execute all callbacks related to the * @param handler The Handler on whose thread to execute the callbacks of the {@code
* attach request - including all sessions opened as part of this * attachCallback} and {@code identityChangedListener} objects. If a null is provided then the
* attach. If a null is provided then the application's main thread will be used. * application's main thread will be used.
* @param attachCallback A callback for attach events, extended from * @param attachCallback A callback for attach events, extended from
* {@link WifiNanAttachCallback}. * {@link WifiNanAttachCallback}.
* @param identityChangedListener A listener for changed identity. * @param identityChangedListener A listener for changed identity, extended from
* {@link WifiNanIdentityChangedListener}.
*/ */
public void attach(@Nullable Handler handler, @NonNull WifiNanAttachCallback attachCallback, public void attach(@Nullable Handler handler, @NonNull WifiNanAttachCallback attachCallback,
@NonNull WifiNanIdentityChangedListener identityChangedListener) { @NonNull WifiNanIdentityChangedListener identityChangedListener) {
@@ -695,7 +696,7 @@ public class WifiNanManager {
switch (msg.what) { switch (msg.what) {
case CALLBACK_CONNECT_SUCCESS: case CALLBACK_CONNECT_SUCCESS:
attachCallback.onAttached( attachCallback.onAttached(
new WifiNanSession(mgr, mBinder, mLooper, msg.arg1)); new WifiNanSession(mgr, mBinder, msg.arg1));
break; break;
case CALLBACK_CONNECT_FAIL: case CALLBACK_CONNECT_FAIL:
mNanManager.clear(); mNanManager.clear();

View File

@@ -41,19 +41,17 @@ public class WifiNanSession {
private final WeakReference<WifiNanManager> mMgr; private final WeakReference<WifiNanManager> mMgr;
private final Binder mBinder; private final Binder mBinder;
private final Looper mLooper;
private final int mClientId; private final int mClientId;
private boolean mTerminated = true; private boolean mTerminated = true;
private final CloseGuard mCloseGuard = CloseGuard.get(); private final CloseGuard mCloseGuard = CloseGuard.get();
/** @hide */ /** @hide */
public WifiNanSession(WifiNanManager manager, Binder binder, Looper looper, int clientId) { public WifiNanSession(WifiNanManager manager, Binder binder, int clientId) {
if (VDBG) Log.v(TAG, "New session created: manager=" + manager + ", clientId=" + clientId); if (VDBG) Log.v(TAG, "New session created: manager=" + manager + ", clientId=" + clientId);
mMgr = new WeakReference<>(manager); mMgr = new WeakReference<>(manager);
mBinder = binder; mBinder = binder;
mLooper = looper;
mClientId = clientId; mClientId = clientId;
mTerminated = false; mTerminated = false;
@@ -68,7 +66,7 @@ public class WifiNanSession {
* session-wide destroy. * session-wide destroy.
* <p> * <p>
* An application may re-attach after a destroy using * An application may re-attach after a destroy using
* {@link WifiNanManager#attach(Handler, WifiNanEventCallback)} . * {@link WifiNanManager#attach(Handler, WifiNanAttachCallback)} .
*/ */
public void destroy() { public void destroy() {
WifiNanManager mgr = mMgr.get(); WifiNanManager mgr = mMgr.get();
@@ -115,12 +113,14 @@ public class WifiNanSession {
* terminate the publish discovery session once it isn't needed. This will free * terminate the publish discovery session once it isn't needed. This will free
* resources as well terminate any on-air transmissions. * resources as well terminate any on-air transmissions.
* *
* @param handler The Handler on whose thread to execute the callbacks of the {@code
* callback} object. If a null is provided then the application's main thread will be used.
* @param publishConfig The {@link PublishConfig} specifying the * @param publishConfig The {@link PublishConfig} specifying the
* configuration of the requested publish session. * configuration of the requested publish session.
* @param callback A {@link WifiNanDiscoverySessionCallback} derived object to be used for * @param callback A {@link WifiNanDiscoverySessionCallback} derived object to be used for
* session event callbacks. * session event callbacks.
*/ */
public void publish(@NonNull PublishConfig publishConfig, public void publish(@Nullable Handler handler, @NonNull PublishConfig publishConfig,
@NonNull WifiNanDiscoverySessionCallback callback) { @NonNull WifiNanDiscoverySessionCallback callback) {
WifiNanManager mgr = mMgr.get(); WifiNanManager mgr = mMgr.get();
if (mgr == null) { if (mgr == null) {
@@ -131,7 +131,8 @@ public class WifiNanSession {
Log.e(TAG, "publish: called after termination"); Log.e(TAG, "publish: called after termination");
return; return;
} }
mgr.publish(mClientId, mLooper, publishConfig, callback); mgr.publish(mClientId, (handler == null) ? Looper.getMainLooper() : handler.getLooper(),
publishConfig, callback);
} }
/** /**
@@ -154,12 +155,14 @@ public class WifiNanSession {
* terminate the subscribe discovery session once it isn't needed. This will free * terminate the subscribe discovery session once it isn't needed. This will free
* resources as well terminate any on-air transmissions. * resources as well terminate any on-air transmissions.
* *
* @param handler The Handler on whose thread to execute the callbacks of the {@code
* callback} object. If a null is provided then the application's main thread will be used.
* @param subscribeConfig The {@link SubscribeConfig} specifying the * @param subscribeConfig The {@link SubscribeConfig} specifying the
* configuration of the requested subscribe session. * configuration of the requested subscribe session.
* @param callback A {@link WifiNanDiscoverySessionCallback} derived object to be used for * @param callback A {@link WifiNanDiscoverySessionCallback} derived object to be used for
* session event callbacks. * session event callbacks.
*/ */
public void subscribe(@NonNull SubscribeConfig subscribeConfig, public void subscribe(@Nullable Handler handler, @NonNull SubscribeConfig subscribeConfig,
@NonNull WifiNanDiscoverySessionCallback callback) { @NonNull WifiNanDiscoverySessionCallback callback) {
WifiNanManager mgr = mMgr.get(); WifiNanManager mgr = mMgr.get();
if (mgr == null) { if (mgr == null) {
@@ -170,7 +173,8 @@ public class WifiNanSession {
Log.e(TAG, "publish: called after termination"); Log.e(TAG, "publish: called after termination");
return; return;
} }
mgr.subscribe(mClientId, mLooper, subscribeConfig, callback); mgr.subscribe(mClientId, (handler == null) ? Looper.getMainLooper() : handler.getLooper(),
subscribeConfig, callback);
} }
/** /**