/build/mobile/sutagent/android/SUTAgentAndroid.java

http://github.com/zpao/v8monkey · Java · 681 lines · 524 code · 87 blank · 70 comment · 92 complexity · 890c70e514cc024e76aa3836475c4a20 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Android SUTAgent code.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Bob Moss.
  18. * Portions created by the Initial Developer are Copyright (C) 2010
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Bob Moss <bmoss@mozilla.com>
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. package com.mozilla.SUTAgentAndroid;
  38. import java.io.File;
  39. import java.io.PrintWriter;
  40. import java.net.InetAddress;
  41. import java.net.NetworkInterface;
  42. import java.net.SocketException;
  43. import java.util.Enumeration;
  44. import java.util.List;
  45. import java.util.Timer;
  46. import com.mozilla.SUTAgentAndroid.service.ASMozStub;
  47. import com.mozilla.SUTAgentAndroid.service.DoCommand;
  48. import android.app.Activity;
  49. import android.bluetooth.BluetoothAdapter;
  50. import android.content.BroadcastReceiver;
  51. import android.content.Context;
  52. import android.content.Intent;
  53. import android.content.IntentFilter;
  54. import android.content.pm.ActivityInfo;
  55. import android.content.res.Configuration;
  56. import android.net.Uri;
  57. import android.net.wifi.SupplicantState;
  58. import android.net.wifi.WifiConfiguration;
  59. import android.net.wifi.WifiInfo;
  60. import android.net.wifi.WifiManager;
  61. import android.net.wifi.WifiManager.WifiLock;
  62. import android.os.BatteryManager;
  63. import android.os.Bundle;
  64. import android.os.Handler;
  65. import android.telephony.TelephonyManager;
  66. import android.util.Log;
  67. import android.view.Menu;
  68. import android.view.MenuItem;
  69. import android.view.View;
  70. import android.view.View.OnClickListener;
  71. import android.widget.Button;
  72. import android.widget.TextView;
  73. import android.widget.Toast;
  74. public class SUTAgentAndroid extends Activity
  75. {
  76. final Handler mHandler = new Handler();
  77. public static final int START_PRG = 1959;
  78. MenuItem mExitMenuItem;
  79. Timer timer = null;
  80. public static String sUniqueID = null;
  81. public static String sLocalIPAddr = null;
  82. public static String sACStatus = null;
  83. public static String sPowerStatus = null;
  84. public static int nChargeLevel = 0;
  85. public static int nBatteryTemp = 0;
  86. String lineSep = System.getProperty("line.separator");
  87. public PrintWriter dataOut = null;
  88. private static boolean bNetworkingStarted = false;
  89. private static String RegSvrIPAddr = "";
  90. private static String RegSvrIPPort = "";
  91. private static String HardwareID = "";
  92. private static String Pool = "";
  93. private static String sRegString = "";
  94. private WifiLock wl = null;
  95. private BroadcastReceiver battReceiver = null;
  96. private TextView tv = null;
  97. public boolean onCreateOptionsMenu(Menu menu)
  98. {
  99. mExitMenuItem = menu.add("Exit");
  100. mExitMenuItem.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
  101. return super.onCreateOptionsMenu(menu);
  102. }
  103. public boolean onMenuItemSelected(int featureId, MenuItem item)
  104. {
  105. if (item == mExitMenuItem)
  106. {
  107. finish();
  108. }
  109. return super.onMenuItemSelected(featureId, item);
  110. }
  111. public static String getRegSvrIPAddr()
  112. {
  113. return(RegSvrIPAddr);
  114. }
  115. /** Called when the activity is first created. */
  116. @Override
  117. public void onCreate(Bundle savedInstanceState)
  118. {
  119. super.onCreate(savedInstanceState);
  120. setContentView(R.layout.main);
  121. fixScreenOrientation();
  122. DoCommand dc = new DoCommand(getApplication());
  123. // Get configuration settings from "ini" file
  124. File dir = getFilesDir();
  125. File iniFile = new File(dir, "SUTAgent.ini");
  126. String sIniFile = iniFile.getAbsolutePath();
  127. SUTAgentAndroid.RegSvrIPAddr = dc.GetIniData("Registration Server", "IPAddr", sIniFile);
  128. SUTAgentAndroid.RegSvrIPPort = dc.GetIniData("Registration Server", "PORT", sIniFile);
  129. SUTAgentAndroid.HardwareID = dc.GetIniData("Registration Server", "HARDWARE", sIniFile);
  130. SUTAgentAndroid.Pool = dc.GetIniData("Registration Server", "POOL", sIniFile);
  131. tv = (TextView) this.findViewById(R.id.Textview01);
  132. if (getLocalIpAddress() == null)
  133. setUpNetwork(sIniFile);
  134. WifiInfo wifi;
  135. WifiManager wifiMan = (WifiManager)getSystemService(Context.WIFI_SERVICE);
  136. String macAddress = "Unknown";
  137. if (wifiMan != null)
  138. {
  139. wifi = wifiMan.getConnectionInfo();
  140. if (wifi != null)
  141. {
  142. macAddress = wifi.getMacAddress();
  143. if (macAddress != null)
  144. sUniqueID = macAddress;
  145. }
  146. }
  147. if (sUniqueID == null)
  148. {
  149. BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
  150. if ((ba != null) && (ba.isEnabled() != true))
  151. {
  152. ba.enable();
  153. while(ba.getState() != BluetoothAdapter.STATE_ON)
  154. {
  155. try {
  156. Thread.sleep(1000);
  157. }
  158. catch (InterruptedException e)
  159. {
  160. e.printStackTrace();
  161. }
  162. }
  163. sUniqueID = ba.getAddress();
  164. ba.disable();
  165. while(ba.getState() != BluetoothAdapter.STATE_OFF)
  166. {
  167. try {
  168. Thread.sleep(1000);
  169. }
  170. catch (InterruptedException e)
  171. {
  172. e.printStackTrace();
  173. }
  174. }
  175. }
  176. else
  177. {
  178. if (ba != null)
  179. {
  180. sUniqueID = ba.getAddress();
  181. sUniqueID.toLowerCase();
  182. }
  183. }
  184. }
  185. if (sUniqueID == null)
  186. {
  187. TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
  188. if (mTelephonyMgr != null)
  189. {
  190. sUniqueID = mTelephonyMgr.getDeviceId();
  191. if (sUniqueID == null)
  192. {
  193. sUniqueID = "0011223344556677";
  194. }
  195. }
  196. }
  197. sLocalIPAddr = getLocalIpAddress();
  198. Toast.makeText(getApplication().getApplicationContext(), "SUTAgent [" + sLocalIPAddr + "] ...", Toast.LENGTH_LONG).show();
  199. String sConfig = "Unique ID: " + sUniqueID + lineSep;
  200. sConfig += "OS Info" + lineSep;
  201. sConfig += "\t" + dc.GetOSInfo() + lineSep;
  202. sConfig += "Screen Info" + lineSep;
  203. int [] xy = dc.GetScreenXY();
  204. sConfig += "\t Width: " + xy[0] + lineSep;
  205. sConfig += "\t Height: " + xy[1] + lineSep;
  206. sConfig += "Memory Info" + lineSep;
  207. sConfig += "\t" + dc.GetMemoryInfo() + lineSep;
  208. sConfig += "Network Info" + lineSep;
  209. sConfig += "\tMac Address: " + macAddress + lineSep;
  210. sConfig += "\tIP Address: " + sLocalIPAddr + lineSep;
  211. displayStatus(sConfig);
  212. sRegString = "NAME=" + sUniqueID;
  213. sRegString += "&IPADDR=" + sLocalIPAddr;
  214. sRegString += "&CMDPORT=" + 20701;
  215. sRegString += "&DATAPORT=" + 20700;
  216. sRegString += "&OS=Android-" + dc.GetOSInfo();
  217. sRegString += "&SCRNWIDTH=" + xy[0];
  218. sRegString += "&SCRNHEIGHT=" + xy[1];
  219. sRegString += "&BPP=8";
  220. sRegString += "&MEMORY=" + dc.GetMemoryConfig();
  221. sRegString += "&HARDWARE=" + HardwareID;
  222. sRegString += "&POOL=" + Pool;
  223. String sTemp = Uri.encode(sRegString,"=&");
  224. sRegString = "register " + sTemp;
  225. if (!bNetworkingStarted)
  226. {
  227. Thread thread = new Thread(null, doStartService, "StartServiceBkgnd");
  228. thread.start();
  229. bNetworkingStarted = true;
  230. Thread thread2 = new Thread(null, doRegisterDevice, "RegisterDeviceBkgnd");
  231. thread2.start();
  232. }
  233. monitorBatteryState();
  234. // If we are returning from an update let'em know we're back
  235. Thread thread3 = new Thread(null, doUpdateCallback, "UpdateCallbackBkgnd");
  236. thread3.start();
  237. final Button goButton = (Button) findViewById(R.id.Button01);
  238. goButton.setOnClickListener(new OnClickListener() {
  239. public void onClick(View v) {
  240. finish();
  241. }
  242. });
  243. }
  244. private class UpdateStatus implements Runnable {
  245. public String sText = "";
  246. UpdateStatus(String sStatus) {
  247. sText = sStatus;
  248. }
  249. public void run() {
  250. displayStatus(sText);
  251. }
  252. }
  253. public synchronized void displayStatus(String sStatus) {
  254. String sTVText = (String) tv.getText();
  255. sTVText += sStatus;
  256. tv.setText(sTVText);
  257. }
  258. public void fixScreenOrientation()
  259. {
  260. setRequestedOrientation((getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ?
  261. ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  262. }
  263. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  264. {
  265. if (requestCode == START_PRG)
  266. {
  267. Toast.makeText(getApplication().getApplicationContext(), "SUTAgent startprg finished ...", Toast.LENGTH_LONG).show();
  268. }
  269. }
  270. @Override
  271. public void onDestroy()
  272. {
  273. super.onDestroy();
  274. if (isFinishing())
  275. {
  276. Intent listenerSvc = new Intent(this, ASMozStub.class);
  277. listenerSvc.setAction("com.mozilla.SUTAgentAndroid.service.LISTENER_SERVICE");
  278. stopService(listenerSvc);
  279. bNetworkingStarted = false;
  280. unregisterReceiver(battReceiver);
  281. if (wl != null)
  282. wl.release();
  283. System.exit(0);
  284. }
  285. }
  286. private void monitorBatteryState()
  287. {
  288. battReceiver = new BroadcastReceiver()
  289. {
  290. public void onReceive(Context context, Intent intent)
  291. {
  292. StringBuilder sb = new StringBuilder();
  293. int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); // charge level from 0 to scale inclusive
  294. int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // Max value for charge level
  295. int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  296. int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
  297. boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
  298. int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); //0 if the device is not plugged in; 1 if plugged into an AC power adapter; 2 if plugged in via USB.
  299. // int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // voltage in millivolts
  300. nBatteryTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); // current battery temperature in tenths of a degree Centigrade
  301. // String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
  302. nChargeLevel = -1; // percentage, or -1 for unknown
  303. if (rawlevel >= 0 && scale > 0)
  304. {
  305. nChargeLevel = (rawlevel * 100) / scale;
  306. }
  307. if (plugged > 0)
  308. sACStatus = "ONLINE";
  309. else
  310. sACStatus = "OFFLINE";
  311. if (present == false)
  312. sb.append("NO BATTERY");
  313. else
  314. {
  315. if (nChargeLevel < 10)
  316. sb.append("Critical");
  317. else if (nChargeLevel < 33)
  318. sb.append("LOW");
  319. else if (nChargeLevel > 80)
  320. sb.append("HIGH");
  321. }
  322. if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health)
  323. {
  324. sb.append("Overheated ");
  325. sb.append((((float)(nBatteryTemp))/10));
  326. sb.append("(C)");
  327. }
  328. else
  329. {
  330. switch(status)
  331. {
  332. case BatteryManager.BATTERY_STATUS_UNKNOWN:
  333. // old emulator; maybe also when plugged in with no battery
  334. if (present == true)
  335. sb.append(" UNKNOWN");
  336. break;
  337. case BatteryManager.BATTERY_STATUS_CHARGING:
  338. sb.append(" CHARGING");
  339. break;
  340. case BatteryManager.BATTERY_STATUS_DISCHARGING:
  341. sb.append(" DISCHARGING");
  342. break;
  343. case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
  344. sb.append(" NOTCHARGING");
  345. break;
  346. case BatteryManager.BATTERY_STATUS_FULL:
  347. sb.append(" FULL");
  348. break;
  349. default:
  350. if (present == true)
  351. sb.append("Unknown");
  352. break;
  353. }
  354. }
  355. sPowerStatus = sb.toString();
  356. }
  357. };
  358. IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  359. registerReceiver(battReceiver, battFilter);
  360. }
  361. public boolean setUpNetwork(String sIniFile)
  362. {
  363. boolean bRet = false;
  364. int lcv = 0;
  365. int lcv2 = 0;
  366. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  367. WifiConfiguration wc = new WifiConfiguration();
  368. DoCommand tmpdc = new DoCommand(getApplication());
  369. String ssid = tmpdc.GetIniData("Network Settings", "SSID", sIniFile);
  370. String auth = tmpdc.GetIniData("Network Settings", "AUTH", sIniFile);
  371. String encr = tmpdc.GetIniData("Network Settings", "ENCR", sIniFile);
  372. String key = tmpdc.GetIniData("Network Settings", "KEY", sIniFile);
  373. String eap = tmpdc.GetIniData("Network Settings", "EAP", sIniFile);
  374. String adhoc = tmpdc.GetIniData("Network Settings", "ADHOC", sIniFile);
  375. Toast.makeText(getApplication().getApplicationContext(), "Starting and configuring network", Toast.LENGTH_LONG).show();
  376. /*
  377. ContentResolver cr = getContentResolver();
  378. int nRet;
  379. try {
  380. nRet = Settings.System.getInt(cr, Settings.System.WIFI_USE_STATIC_IP);
  381. String foo2 = "" + nRet;
  382. } catch (SettingNotFoundException e1) {
  383. e1.printStackTrace();
  384. }
  385. */
  386. /*
  387. wc.SSID = "\"Mozilla-Build\"";
  388. wc.preSharedKey = "\"MozillaBuildQA500\"";
  389. wc.hiddenSSID = true;
  390. wc.status = WifiConfiguration.Status.ENABLED;
  391. wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
  392. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  393. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  394. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
  395. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  396. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  397. wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
  398. */
  399. wc.SSID = "\"" + ssid + "\"";
  400. // wc.SSID = "\"Mozilla-G\"";
  401. // wc.SSID = "\"Mozilla\"";
  402. if (auth.contentEquals("wpa2"))
  403. {
  404. wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
  405. wc.preSharedKey = null;
  406. }
  407. if (encr.contentEquals("aes"))
  408. {
  409. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  410. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  411. }
  412. if (eap.contentEquals("peap"))
  413. {
  414. wc.eap.setValue("PEAP");
  415. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
  416. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
  417. }
  418. wc.hiddenSSID = false;
  419. wc.status = WifiConfiguration.Status.ENABLED;
  420. wc.password.setValue("\"password\"");
  421. wc.identity.setValue("\"bmoss@mozilla.com\"");
  422. if (!wifi.isWifiEnabled())
  423. wifi.setWifiEnabled(true);
  424. while(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
  425. {
  426. Thread.yield();
  427. if (++lcv > 10000)
  428. return(bRet);
  429. }
  430. wl = wifi.createWifiLock(WifiManager.WIFI_MODE_FULL, "SUTAgent");
  431. if (wl != null)
  432. wl.acquire();
  433. WifiConfiguration foo = null;
  434. int nNetworkID = -1;
  435. List<WifiConfiguration> connsLst = wifi.getConfiguredNetworks();
  436. int nConns = connsLst.size();
  437. for (int i = 0; i < nConns; i++)
  438. {
  439. foo = connsLst.get(i);
  440. if (foo.SSID.equalsIgnoreCase(wc.SSID))
  441. {
  442. nNetworkID = foo.networkId;
  443. wc.networkId = foo.networkId;
  444. break;
  445. }
  446. }
  447. int res;
  448. if (nNetworkID != -1)
  449. {
  450. res = wifi.updateNetwork(wc);
  451. }
  452. else
  453. {
  454. res = wifi.addNetwork(wc);
  455. }
  456. Log.d("WifiPreference", "add Network returned " + res );
  457. boolean b = wifi.enableNetwork(res, true);
  458. Log.d("WifiPreference", "enableNetwork returned " + b );
  459. wifi.saveConfiguration();
  460. WifiInfo wi = wifi.getConnectionInfo();
  461. SupplicantState ss = wi.getSupplicantState();
  462. lcv = 0;
  463. lcv2 = 0;
  464. while (ss.compareTo(SupplicantState.COMPLETED) != 0)
  465. {
  466. try {
  467. Thread.sleep(1000);
  468. }
  469. catch (InterruptedException e)
  470. {
  471. e.printStackTrace();
  472. }
  473. if (wi != null)
  474. wi = null;
  475. if (ss != null)
  476. ss = null;
  477. wi = wifi.getConnectionInfo();
  478. ss = wi.getSupplicantState();
  479. if (++lcv > 60)
  480. {
  481. if (++lcv2 > 5)
  482. {
  483. Toast.makeText(getApplication().getApplicationContext(), "Unable to start and configure network", Toast.LENGTH_LONG).show();
  484. return(bRet);
  485. }
  486. else
  487. {
  488. Toast.makeText(getApplication().getApplicationContext(), "Resetting wifi interface", Toast.LENGTH_LONG).show();
  489. if (wl != null)
  490. wl.release();
  491. wifi.setWifiEnabled(false);
  492. while(wifi.getWifiState() != WifiManager.WIFI_STATE_DISABLED)
  493. {
  494. Thread.yield();
  495. }
  496. wifi.setWifiEnabled(true);
  497. while(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
  498. {
  499. Thread.yield();
  500. }
  501. b = wifi.enableNetwork(res, true);
  502. Log.d("WifiPreference", "enableNetwork returned " + b );
  503. if (wl != null)
  504. wl.acquire();
  505. lcv = 0;
  506. }
  507. }
  508. }
  509. lcv = 0;
  510. while(getLocalIpAddress() == null)
  511. {
  512. if (++lcv > 10000)
  513. return(bRet);
  514. }
  515. Toast.makeText(getApplication().getApplicationContext(), "Network started and configured", Toast.LENGTH_LONG).show();
  516. bRet = true;
  517. return(bRet);
  518. }
  519. // If there is an update.info file callback the server and send the status
  520. private Runnable doUpdateCallback = new Runnable() {
  521. public void run() {
  522. DoCommand dc = new DoCommand(getApplication());
  523. String sRet = dc.UpdateCallBack("update.info");
  524. if (sRet.length() > 0) {
  525. if (sRet.contains("ok")) {
  526. sRet = "Callback Server contacted successfully" + lineSep;
  527. } else if (sRet.contains("Nothing to do")) {
  528. sRet = "";
  529. } else {
  530. sRet = "Callback Server NOT contacted successfully" + lineSep;
  531. }
  532. }
  533. if (sRet.length() > 0)
  534. mHandler.post(new UpdateStatus(sRet));
  535. dc = null;
  536. }
  537. };
  538. // registers with the reg server defined in the SUTAgent.ini file
  539. private Runnable doRegisterDevice = new Runnable() {
  540. public void run() {
  541. DoCommand dc = new DoCommand(getApplication());
  542. String sRet = "";
  543. if (RegSvrIPAddr.length() > 0) {
  544. String sRegRet = dc.RegisterTheDevice(RegSvrIPAddr, RegSvrIPPort, sRegString);
  545. if (sRegRet.contains("ok")) {
  546. sRet += "Registered with testserver" + lineSep;
  547. sRet += "\tIPAddress: " + RegSvrIPAddr + lineSep;
  548. if (RegSvrIPPort.length() > 0)
  549. sRet += "\tPort: " + RegSvrIPPort + lineSep;
  550. } else {
  551. sRet += "Not registered with testserver" + lineSep;
  552. }
  553. } else {
  554. sRet += "Not registered with testserver" + lineSep;
  555. }
  556. if (sRet.length() > 0)
  557. mHandler.post(new UpdateStatus(sRet));
  558. dc = null;
  559. }
  560. };
  561. // this starts the listener service for the command and data channels
  562. private Runnable doStartService = new Runnable()
  563. {
  564. public void run()
  565. {
  566. Intent listenerService = new Intent();
  567. listenerService.setAction("com.mozilla.SUTAgentAndroid.service.LISTENER_SERVICE");
  568. startService(listenerService);
  569. }
  570. };
  571. public String getLocalIpAddress()
  572. {
  573. try
  574. {
  575. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
  576. {
  577. NetworkInterface intf = en.nextElement();
  578. for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
  579. {
  580. InetAddress inetAddress = enumIpAddr.nextElement();
  581. if (!inetAddress.isLoopbackAddress())
  582. {
  583. return inetAddress.getHostAddress().toString();
  584. }
  585. }
  586. }
  587. }
  588. catch (SocketException ex)
  589. {
  590. Toast.makeText(getApplication().getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
  591. }
  592. return null;
  593. }
  594. }