Merge "Update sample NSD app to work reliably with current Android"
This commit is contained in:
@@ -52,7 +52,9 @@ public class ChatConnection {
|
|||||||
|
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
mChatServer.tearDown();
|
mChatServer.tearDown();
|
||||||
mChatClient.tearDown();
|
if (mChatClient != null) {
|
||||||
|
mChatClient.tearDown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connectToServer(InetAddress address, int port) {
|
public void connectToServer(InetAddress address, int port) {
|
||||||
@@ -64,15 +66,15 @@ public class ChatConnection {
|
|||||||
mChatClient.sendMessage(msg);
|
mChatClient.sendMessage(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLocalPort() {
|
public int getLocalPort() {
|
||||||
return mPort;
|
return mPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocalPort(int port) {
|
public void setLocalPort(int port) {
|
||||||
mPort = port;
|
mPort = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void updateMessages(String msg, boolean local) {
|
public synchronized void updateMessages(String msg, boolean local) {
|
||||||
Log.e(TAG, "Updating message: " + msg);
|
Log.e(TAG, "Updating message: " + msg);
|
||||||
@@ -142,7 +144,7 @@ public class ChatConnection {
|
|||||||
// used. Just grab an available one and advertise it via Nsd.
|
// used. Just grab an available one and advertise it via Nsd.
|
||||||
mServerSocket = new ServerSocket(0);
|
mServerSocket = new ServerSocket(0);
|
||||||
setLocalPort(mServerSocket.getLocalPort());
|
setLocalPort(mServerSocket.getLocalPort());
|
||||||
|
|
||||||
while (!Thread.currentThread().isInterrupted()) {
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
Log.d(TAG, "ServerSocket Created, awaiting connection");
|
Log.d(TAG, "ServerSocket Created, awaiting connection");
|
||||||
setSocket(mServerSocket.accept());
|
setSocket(mServerSocket.accept());
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ public class NsdChatActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
Log.d(TAG, "Creating chat activity");
|
||||||
setContentView(R.layout.main);
|
setContentView(R.layout.main);
|
||||||
mStatusView = (TextView) findViewById(R.id.status);
|
mStatusView = (TextView) findViewById(R.id.status);
|
||||||
|
|
||||||
@@ -54,11 +55,6 @@ public class NsdChatActivity extends Activity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
mConnection = new ChatConnection(mUpdateHandler);
|
|
||||||
|
|
||||||
mNsdHelper = new NsdHelper(this);
|
|
||||||
mNsdHelper.initializeNsd();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clickAdvertise(View v) {
|
public void clickAdvertise(View v) {
|
||||||
@@ -100,26 +96,59 @@ public class NsdChatActivity extends Activity {
|
|||||||
mStatusView.append("\n" + line);
|
mStatusView.append("\n" + line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
Log.d(TAG, "Starting.");
|
||||||
|
mConnection = new ChatConnection(mUpdateHandler);
|
||||||
|
|
||||||
|
mNsdHelper = new NsdHelper(this);
|
||||||
|
mNsdHelper.initializeNsd();
|
||||||
|
super.onStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
|
Log.d(TAG, "Pausing.");
|
||||||
if (mNsdHelper != null) {
|
if (mNsdHelper != null) {
|
||||||
mNsdHelper.stopDiscovery();
|
mNsdHelper.stopDiscovery();
|
||||||
}
|
}
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
|
Log.d(TAG, "Resuming.");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if (mNsdHelper != null) {
|
if (mNsdHelper != null) {
|
||||||
mNsdHelper.discoverServices();
|
mNsdHelper.discoverServices();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// For KitKat and earlier releases, it is necessary to remove the
|
||||||
|
// service registration when the application is stopped. There's
|
||||||
|
// no guarantee that the onDestroy() method will be called (we're
|
||||||
|
// killable after onStop() returns) and the NSD service won't remove
|
||||||
|
// the registration for us if we're killed.
|
||||||
|
|
||||||
|
// In L and later, NsdService will automatically unregister us when
|
||||||
|
// our connection goes away when we're killed, so this step is
|
||||||
|
// optional (but recommended).
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onStop() {
|
||||||
|
Log.d(TAG, "Being stopped.");
|
||||||
mNsdHelper.tearDown();
|
mNsdHelper.tearDown();
|
||||||
mConnection.tearDown();
|
mConnection.tearDown();
|
||||||
|
mNsdHelper = null;
|
||||||
|
mConnection = null;
|
||||||
|
super.onStop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
Log.d(TAG, "Being destroyed.");
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,8 +44,6 @@ public class NsdHelper {
|
|||||||
|
|
||||||
public void initializeNsd() {
|
public void initializeNsd() {
|
||||||
initializeResolveListener();
|
initializeResolveListener();
|
||||||
initializeDiscoveryListener();
|
|
||||||
initializeRegistrationListener();
|
|
||||||
|
|
||||||
//mNsdManager.init(mContext.getMainLooper(), this);
|
//mNsdManager.init(mContext.getMainLooper(), this);
|
||||||
|
|
||||||
@@ -78,22 +76,20 @@ public class NsdHelper {
|
|||||||
mService = null;
|
mService = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDiscoveryStopped(String serviceType) {
|
public void onDiscoveryStopped(String serviceType) {
|
||||||
Log.i(TAG, "Discovery stopped: " + serviceType);
|
Log.i(TAG, "Discovery stopped: " + serviceType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
||||||
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
|
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
|
||||||
mNsdManager.stopServiceDiscovery(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
|
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
|
||||||
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
|
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
|
||||||
mNsdManager.stopServiceDiscovery(this);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -125,48 +121,68 @@ public class NsdHelper {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
|
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
|
||||||
mServiceName = NsdServiceInfo.getServiceName();
|
mServiceName = NsdServiceInfo.getServiceName();
|
||||||
|
Log.d(TAG, "Service registered: " + mServiceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
|
public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
|
||||||
|
Log.d(TAG, "Service registration failed: " + arg1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceUnregistered(NsdServiceInfo arg0) {
|
public void onServiceUnregistered(NsdServiceInfo arg0) {
|
||||||
|
Log.d(TAG, "Service unregistered: " + arg0.getServiceName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||||
|
Log.d(TAG, "Service unregistration failed: " + errorCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerService(int port) {
|
public void registerService(int port) {
|
||||||
|
tearDown(); // Cancel any previous registration request
|
||||||
|
initializeRegistrationListener();
|
||||||
NsdServiceInfo serviceInfo = new NsdServiceInfo();
|
NsdServiceInfo serviceInfo = new NsdServiceInfo();
|
||||||
serviceInfo.setPort(port);
|
serviceInfo.setPort(port);
|
||||||
serviceInfo.setServiceName(mServiceName);
|
serviceInfo.setServiceName(mServiceName);
|
||||||
serviceInfo.setServiceType(SERVICE_TYPE);
|
serviceInfo.setServiceType(SERVICE_TYPE);
|
||||||
|
|
||||||
mNsdManager.registerService(
|
mNsdManager.registerService(
|
||||||
serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
|
serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void discoverServices() {
|
public void discoverServices() {
|
||||||
|
stopDiscovery(); // Cancel any existing discovery request
|
||||||
|
initializeDiscoveryListener();
|
||||||
mNsdManager.discoverServices(
|
mNsdManager.discoverServices(
|
||||||
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
|
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopDiscovery() {
|
public void stopDiscovery() {
|
||||||
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
|
if (mDiscoveryListener != null) {
|
||||||
|
try {
|
||||||
|
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
|
||||||
|
} finally {
|
||||||
|
}
|
||||||
|
mDiscoveryListener = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public NsdServiceInfo getChosenServiceInfo() {
|
public NsdServiceInfo getChosenServiceInfo() {
|
||||||
return mService;
|
return mService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
mNsdManager.unregisterService(mRegistrationListener);
|
if (mRegistrationListener != null) {
|
||||||
|
try {
|
||||||
|
mNsdManager.unregisterService(mRegistrationListener);
|
||||||
|
} finally {
|
||||||
|
}
|
||||||
|
mRegistrationListener = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user