b=790041; add mDNS broadcasts to SUTAgent; r=ctalbert

This commit is contained in:
Vladimir Vukicevic 2012-09-14 09:29:51 -04:00
parent 8e12f7baa0
commit 3a82a1c16e
5 changed files with 158 additions and 17 deletions

View File

@ -8,20 +8,29 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.util.Timer;
import java.io.IOException;
import java.net.InetAddress;
import com.mozilla.SUTAgentAndroid.SUTAgentAndroid;
import com.mozilla.SUTAgentAndroid.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
public class ASMozStub extends android.app.Service {
private final static int COMMAND_PORT = 20701;
private final static int DATA_PORT = 20700;
private ServerSocket cmdChnl = null;
private ServerSocket dataChnl = null;
@ -79,19 +88,97 @@ public class ASMozStub extends android.app.Service {
doToast("Listener Service created...");
}
WifiManager.MulticastLock multicastLock;
JmDNS jmdns;
void startZeroConf() {
if (multicastLock == null) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock("SUTAgent");
multicastLock.setReferenceCounted(true);
}
multicastLock.acquire();
try {
InetAddress inetAddress = SUTAgentAndroid.getLocalInetAddress();
if (jmdns == null) {
jmdns = JmDNS.create(inetAddress, null);
}
if (jmdns != null) {
String name = "SUTAgent";
String hwid = SUTAgentAndroid.getHWID(this);
if (hwid != null) {
name += " [hwid:" + hwid + "]";
}
// multicast reception is broken for some reason, so
// this service can't be resolved; it can only be
// broadcast. So, we cheat -- we put the IP address
// in the broadcast that we can pull out later.
// However, periods aren't legal, so replace them.
// The IP address will show up as [ip:127_0_0_1]
name += " [ip:" + inetAddress.getHostAddress().toString().replace('.', '_') + "]";
final ServiceInfo serviceInfo = ServiceInfo.create("_sutagent._tcp.local.",
name,
COMMAND_PORT,
"Android SUTAgent");
final JmDNS dns = jmdns;
// we want to call registerService on a new thread, because it can block
// for a little while.
Thread registerThread = new Thread() {
public void run() {
try {
dns.registerService(serviceInfo);
} catch (IOException e) {
Log.e("SUTAgent", "Failed to register JmDNS service!", e);
}
}
};
registerThread.setDaemon(true);
registerThread.start();
}
} catch (IOException e) {
Log.e("SUTAgent", "Failed to register JmDNS service!", e);
}
}
void stopZeroConf() {
if (jmdns != null) {
try {
jmdns.unregisterAllServices();
jmdns.close();
} catch (IOException e) {
Log.e("SUTAgent", "Failed to close JmDNS service!", e);
}
jmdns = null;
}
if (multicastLock != null) {
multicastLock.release();
multicastLock = null;
}
}
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
try {
cmdChnl = new ServerSocket(20701);
cmdChnl = new ServerSocket(COMMAND_PORT);
runCmdThrd = new RunCmdThread(cmdChnl, this, handler);
runCmdThrd.start();
doToast("Command channel port 20701 ...");
doToast(String.format("Command channel port %d ...", COMMAND_PORT));
dataChnl = new ServerSocket(20700);
dataChnl = new ServerSocket(DATA_PORT);
runDataThrd = new RunDataThread(dataChnl, this);
runDataThrd.start();
doToast("Data channel port 20700 ...");
doToast(String.format("Data channel port %d ...", DATA_PORT));
startZeroConf();
Notification notification = new Notification();
startForegroundCompat(R.string.foreground_service_started, notification);
@ -108,6 +195,8 @@ public class ASMozStub extends android.app.Service {
{
super.onDestroy();
stopZeroConf();
if (runCmdThrd.isAlive())
{
runCmdThrd.StopListening();

View File

@ -33,6 +33,7 @@
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.REBOOT"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>

View File

@ -47,7 +47,8 @@ GARBAGE += \
GARBAGE_DIRS += res classes network-libs
JAVA_CLASSPATH = $(ANDROID_SDK)/android.jar:$(srcdir)/network-libs/commons-net-2.0.jar
EXTRA_JARS = $(srcdir)/network-libs/commons-net-2.0.jar:$(srcdir)/network-libs/jmdns.jar
JAVA_CLASSPATH = $(ANDROID_SDK)/android.jar:$(EXTRA_JARS)
include $(topsrcdir)/config/rules.mk
@ -59,10 +60,10 @@ tools:: sutAgentAndroid.apk
classes.dex: $(JAVAFILES)
$(NSINSTALL) -D classes
$(JAVAC) $(JAVAC_FLAGS) -d classes $(addprefix $(srcdir)/,$(JAVAFILES))
$(DX) --dex --output=$@ classes
$(DX) --dex --output=$@ classes $(subst :, ,$(EXTRA_JARS))
sutAgentAndroid.ap_: $(srcdir)/AndroidManifest.xml
$(AAPT) package -f -M $(srcdir)/AndroidManifest.xml -I $(ANDROID_SDK)/android.jar -S res -F $@
$(AAPT) package -f -M $(srcdir)/AndroidManifest.xml -I $(ANDROID_SDK)/android.jar -S res -F $@
sutAgentAndroid-unsigned-unaligned.apk: sutAgentAndroid.ap_ classes.dex
$(APKBUILDER) $@ -v $(APKBUILDER_FLAGS) -z sutAgentAndroid.ap_ -f classes.dex

View File

@ -264,10 +264,13 @@ public class SUTAgentAndroid extends Activity
}
}
String hwid = getHWID(this);
sLocalIPAddr = getLocalIpAddress();
Toast.makeText(getApplication().getApplicationContext(), "SUTAgent [" + sLocalIPAddr + "] ...", Toast.LENGTH_LONG).show();
String sConfig = "Unique ID: " + sUniqueID + lineSep;
sConfig += "HWID: " + hwid + lineSep;
sConfig += "OS Info" + lineSep;
sConfig += "\t" + dc.GetOSInfo() + lineSep;
sConfig += "Screen Info" + lineSep;
@ -738,23 +741,70 @@ public class SUTAgentAndroid extends Activity
}
};
public String getLocalIpAddress()
static String sHWID = null;
public static String getHWID(Context cx) {
if (sHWID != null)
return sHWID;
// If we're on SDK version >= 8, use Build.SERIAL
if (android.os.Build.VERSION.SDK_INT >= 8) {
sHWID = android.os.Build.SERIAL;
}
if (sHWID != null)
return sHWID;
// Otherwise, try from the telephony manager
TelephonyManager mTelephonyMgr = (TelephonyManager) cx.getSystemService(TELEPHONY_SERVICE);
if (mTelephonyMgr != null) {
sHWID = mTelephonyMgr.getDeviceId();
}
if (sHWID != null)
return sHWID;
// Otherwise, try WIFI_SERVICE and use the wifi manager
WifiManager wifiMan = (WifiManager) cx.getSystemService(Context.WIFI_SERVICE);
if (wifiMan != null) {
WifiInfo wifi = wifiMan.getConnectionInfo();
if (wifi != null) {
sHWID = "wifimac" + wifi.getMacAddress();
}
}
if (sHWID != null)
return sHWID;
sHWID = "0011223344556677";
return sHWID;
}
public static InetAddress getLocalInetAddress() throws SocketException
{
try
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
{
return inetAddress.getHostAddress().toString();
}
return inetAddress;
}
}
}
return null;
}
public String getLocalIpAddress()
{
try {
InetAddress inetAddress = getLocalInetAddress();
if (inetAddress != null)
return inetAddress.getHostAddress().toString();
}
catch (SocketException ex)
{
Toast.makeText(getApplication().getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();

Binary file not shown.