/build/mobile/sutagent/android/RunCmdThread.java

http://github.com/zpao/v8monkey · Java · 318 lines · 236 code · 40 blank · 42 comment · 22 complexity · dc2707ac2ac615eae0b9dc8bbd6cffe0 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.service;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import java.net.ServerSocket;
  41. import java.net.Socket;
  42. import java.net.SocketTimeoutException;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. import com.mozilla.SUTAgentAndroid.R;
  46. import com.mozilla.SUTAgentAndroid.SUTAgentAndroid;
  47. import android.app.Notification;
  48. import android.app.NotificationManager;
  49. import android.app.PendingIntent;
  50. import android.content.Context;
  51. import android.content.Intent;
  52. import android.os.Handler;
  53. public class RunCmdThread extends Thread
  54. {
  55. private ServerSocket SvrSocket = null;
  56. private Socket socket = null;
  57. private Handler handler = null;
  58. boolean bListening = true;
  59. boolean bNetError = false;
  60. List<CmdWorkerThread> theWorkers = new ArrayList<CmdWorkerThread>();
  61. android.app.Service svc = null;
  62. public RunCmdThread(ServerSocket socket, android.app.Service service, Handler handler)
  63. {
  64. super("RunCmdThread");
  65. this.SvrSocket = socket;
  66. this.svc = service;
  67. this.handler = handler;
  68. }
  69. public void StopListening()
  70. {
  71. bListening = false;
  72. }
  73. public void run() {
  74. try {
  75. int nIterations = 0;
  76. SvrSocket.setSoTimeout(5000);
  77. while (bListening)
  78. {
  79. try
  80. {
  81. socket = SvrSocket.accept();
  82. CmdWorkerThread theWorker = new CmdWorkerThread(this, socket);
  83. theWorker.start();
  84. theWorkers.add(theWorker);
  85. }
  86. catch (SocketTimeoutException toe)
  87. {
  88. if (++nIterations > 60)
  89. {
  90. nIterations = 0;
  91. String sRet = SendPing("www.mozilla.org");
  92. if (sRet.contains("3 received"))
  93. handler.post(new doCancelNotification());
  94. else
  95. handler.post(new doSendNotification("SUTAgent - Network Connectivity Error", sRet));
  96. sRet = null;
  97. }
  98. continue;
  99. }
  100. catch (IOException e)
  101. {
  102. e.printStackTrace();
  103. continue;
  104. }
  105. }
  106. int nNumWorkers = theWorkers.size();
  107. for (int lcv = 0; lcv < nNumWorkers; lcv++)
  108. {
  109. if (theWorkers.get(lcv).isAlive())
  110. {
  111. theWorkers.get(lcv).StopListening();
  112. while(theWorkers.get(lcv).isAlive())
  113. ;
  114. }
  115. }
  116. theWorkers.clear();
  117. SvrSocket.close();
  118. svc.stopSelf();
  119. // SUTAgentAndroid.me.finish();
  120. }
  121. catch (IOException e)
  122. {
  123. e.printStackTrace();
  124. }
  125. return;
  126. }
  127. private String SendPing(String sIPAddr)
  128. {
  129. Process pProc;
  130. String sRet = "";
  131. String [] theArgs = new String [4];
  132. boolean bStillRunning = true;
  133. int nBytesOut = 0;
  134. int nBytesErr = 0;
  135. int nBytesRead = 0;
  136. byte[] buffer = new byte[1024];
  137. theArgs[0] = "ping";
  138. theArgs[1] = "-c";
  139. theArgs[2] = "3";
  140. theArgs[3] = sIPAddr;
  141. try
  142. {
  143. pProc = Runtime.getRuntime().exec(theArgs);
  144. InputStream sutOut = pProc.getInputStream();
  145. InputStream sutErr = pProc.getErrorStream();
  146. while (bStillRunning)
  147. {
  148. try
  149. {
  150. if ((nBytesOut = sutOut.available()) > 0)
  151. {
  152. if (nBytesOut > buffer.length)
  153. {
  154. buffer = null;
  155. System.gc();
  156. buffer = new byte[nBytesOut];
  157. }
  158. nBytesRead = sutOut.read(buffer, 0, nBytesOut);
  159. if (nBytesRead == -1)
  160. bStillRunning = false;
  161. else
  162. {
  163. String sRep = new String(buffer,0,nBytesRead).replace("\n", "\r\n");
  164. sRet += sRep;
  165. sRep = null;
  166. }
  167. }
  168. if ((nBytesErr = sutErr.available()) > 0)
  169. {
  170. if (nBytesErr > buffer.length)
  171. {
  172. buffer = null;
  173. System.gc();
  174. buffer = new byte[nBytesErr];
  175. }
  176. nBytesRead = sutErr.read(buffer, 0, nBytesErr);
  177. if (nBytesRead == -1)
  178. bStillRunning = false;
  179. else
  180. {
  181. String sRep = new String(buffer,0,nBytesRead).replace("\n", "\r\n");
  182. sRet += sRep;
  183. sRep = null;
  184. }
  185. }
  186. bStillRunning = (IsProcRunning(pProc) || (sutOut.available() > 0) || (sutErr.available() > 0));
  187. }
  188. catch (IOException e)
  189. {
  190. e.printStackTrace();
  191. }
  192. if ((bStillRunning == true) && (nBytesErr == 0) && (nBytesOut == 0))
  193. {
  194. try {
  195. sleep(2000);
  196. }
  197. catch (InterruptedException e) {
  198. e.printStackTrace();
  199. }
  200. }
  201. }
  202. pProc.destroy();
  203. pProc = null;
  204. }
  205. catch (IOException e)
  206. {
  207. sRet = e.getMessage();
  208. e.printStackTrace();
  209. }
  210. return (sRet);
  211. }
  212. private boolean IsProcRunning(Process pProc)
  213. {
  214. boolean bRet = false;
  215. @SuppressWarnings("unused")
  216. int nExitCode = 0;
  217. try
  218. {
  219. nExitCode = pProc.exitValue();
  220. }
  221. catch (IllegalThreadStateException z)
  222. {
  223. bRet = true;
  224. }
  225. catch (Exception e)
  226. {
  227. e.printStackTrace();
  228. }
  229. return(bRet);
  230. }
  231. private void SendNotification(String tickerText, String expandedText)
  232. {
  233. NotificationManager notificationManager = (NotificationManager)svc.getSystemService(Context.NOTIFICATION_SERVICE);
  234. // int icon = android.R.drawable.stat_notify_more;
  235. // int icon = R.drawable.ic_stat_first;
  236. // int icon = R.drawable.ic_stat_second;
  237. // int icon = R.drawable.ic_stat_neterror;
  238. int icon = R.drawable.ateamlogo;
  239. long when = System.currentTimeMillis();
  240. Notification notification = new Notification(icon, tickerText, when);
  241. notification.flags |= (Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL);
  242. notification.defaults |= Notification.DEFAULT_SOUND;
  243. notification.defaults |= Notification.DEFAULT_VIBRATE;
  244. notification.defaults |= Notification.DEFAULT_LIGHTS;
  245. Context context = svc.getApplicationContext();
  246. // Intent to launch an activity when the extended text is clicked
  247. Intent intent2 = new Intent(svc, SUTAgentAndroid.class);
  248. PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent2, 0);
  249. notification.setLatestEventInfo(context, tickerText, expandedText, launchIntent);
  250. notificationManager.notify(1959, notification);
  251. }
  252. private void CancelNotification()
  253. {
  254. NotificationManager notificationManager = (NotificationManager)svc.getSystemService(Context.NOTIFICATION_SERVICE);
  255. notificationManager.cancel(1959);
  256. }
  257. class doCancelNotification implements Runnable
  258. {
  259. public void run()
  260. {
  261. CancelNotification();
  262. }
  263. };
  264. class doSendNotification implements Runnable
  265. {
  266. private String sTitle = "";
  267. private String sBText = "";
  268. doSendNotification(String sTitle, String sBodyText)
  269. {
  270. this.sTitle = sTitle;
  271. this.sBText = sBodyText;
  272. }
  273. public void run()
  274. {
  275. SendNotification(sTitle, sBText);
  276. }
  277. };
  278. }