You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SMSServerService.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.rob57530.smsserver;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLEncoder;
  6. import java.util.Calendar;
  7. import java.util.Map;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.HttpStatus;
  10. import org.apache.http.StatusLine;
  11. import org.apache.http.client.ClientProtocolException;
  12. import org.apache.http.client.HttpClient;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.eclipse.jetty.server.Server;
  16. import org.eclipse.jetty.util.ajax.JSON;
  17. import android.app.Notification;
  18. import android.app.NotificationManager;
  19. import android.app.PendingIntent;
  20. import android.app.Service;
  21. import android.content.Context;
  22. import android.content.Intent;
  23. import android.content.SharedPreferences;
  24. import android.os.AsyncTask;
  25. import android.os.Handler;
  26. import android.os.IBinder;
  27. import android.preference.PreferenceManager;
  28. import android.support.v4.app.NotificationCompat;
  29. import android.util.Base64;
  30. import android.util.Log;
  31. public class SMSServerService extends Service
  32. {
  33. private Server m_server = null;
  34. private int m_port = 0;
  35. private String m_http = null;
  36. private String m_name = null;
  37. private String m_number = null;
  38. private int m_gateTimeout = -1;
  39. private NotificationCompat.Builder m_builder = null;
  40. private NotificationManager m_notificationManager = null;
  41. private Handler m_handler = null;
  42. private Runnable m_timer = new Runnable()
  43. {
  44. @Override
  45. public void run()
  46. {
  47. register();
  48. }
  49. };
  50. class RequestTask extends AsyncTask<String, String, String>
  51. {
  52. @Override
  53. protected String doInBackground(String... uri)
  54. {
  55. Log.d("SMSServerService", "doinbackground");
  56. HttpClient httpclient = new DefaultHttpClient();
  57. HttpResponse response;
  58. String responseString = null;
  59. try
  60. {
  61. HttpGet get = new HttpGet(uri[0]);
  62. get.setHeader("Host", m_http);
  63. get.setHeader("Authorization", "Basic " + Base64.encodeToString("__gates__:gatespasswd".getBytes(), Base64.NO_WRAP));
  64. response = httpclient.execute(get);
  65. Log.d("SMSServerService", "doinbackground exec");
  66. StatusLine statusLine = response.getStatusLine();
  67. if (statusLine.getStatusCode() == HttpStatus.SC_OK)
  68. {
  69. ByteArrayOutputStream out = new ByteArrayOutputStream();
  70. response.getEntity().writeTo(out);
  71. out.close();
  72. responseString = out.toString();
  73. }
  74. else
  75. {
  76. response.getEntity().getContent().close();
  77. throw new IOException(statusLine.getReasonPhrase());
  78. }
  79. }
  80. catch (ClientProtocolException e)
  81. {
  82. Log.d("SMSServerService", "doinbackground:" + e.toString());
  83. e.printStackTrace();
  84. }
  85. catch (IOException e)
  86. {
  87. Log.d("SMSServerService", "doinbackground:" + e.toString());
  88. e.printStackTrace();
  89. }
  90. Log.d("SMSServerService", "doinbackground end");
  91. return responseString;
  92. }
  93. @Override
  94. protected void onPostExecute(String result)
  95. {
  96. super.onPostExecute(result);
  97. if (result != null)
  98. {
  99. @SuppressWarnings("unchecked")
  100. Map<String, Object> root = (Map<String, Object>) JSON.parse(result);
  101. if (root.containsKey("error"))
  102. {
  103. m_gateTimeout = 30;
  104. Log.w("SMSServer", root.get("error").toString());
  105. setText("Error: " + root.get("error").toString());
  106. }
  107. else
  108. {
  109. m_gateTimeout = ((Long) root.get("gateTimeout")).intValue();
  110. Log.d("SMSServerService", "" + m_gateTimeout);
  111. setText("Registered. id=" + ((Long) root.get("id")).intValue() + " timeout=" + m_gateTimeout);
  112. }
  113. }
  114. else
  115. {
  116. m_gateTimeout = 30;
  117. setText("Error while registering");
  118. }
  119. m_handler.postDelayed(m_timer, Math.max((m_gateTimeout - 20) * 1000, 10000));
  120. }
  121. }
  122. private void setText(String t)
  123. {
  124. m_builder.setContentText(t).setOngoing(true).setWhen(Calendar.getInstance().getTimeInMillis());
  125. Notification n = m_builder.build();
  126. m_notificationManager.notify(0, n);
  127. startForeground(0, n);
  128. }
  129. @Override
  130. public int onStartCommand(Intent intent, int flags, int startId)
  131. {
  132. if (m_server == null)
  133. {
  134. Intent i = new Intent(getApplicationContext(), MainActivity.class);
  135. i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  136. PendingIntent pi = PendingIntent.getActivity(this, 42, i, PendingIntent.FLAG_UPDATE_CURRENT);
  137. m_builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.network_wireless).setContentTitle("SMS Server").setContentIntent(pi);
  138. m_notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  139. setText("Starting...");
  140. SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
  141. m_port = p.getInt("port", 0);
  142. m_http = p.getString("http", "");
  143. m_name = p.getString("name", "");
  144. m_number = p.getString("number", "");
  145. try
  146. {
  147. if (m_name.length() == 0)
  148. throw new Exception("Empty Name");
  149. if (m_number.length() == 0)
  150. throw new Exception("Empty Number");
  151. if (m_port < 1 || m_port > 65535)
  152. throw new Exception("Invalid Port (1 <= port <= 65535)");
  153. m_handler = new Handler();
  154. m_server = new Server(m_port);
  155. HttpHandler h = new HttpHandler();
  156. h.setContext(this);
  157. h.setHttp(m_http);
  158. m_server.setHandler(h);
  159. m_server.start();
  160. if (m_http.length() != 0)
  161. register();
  162. }
  163. catch (Exception e)
  164. {
  165. e.printStackTrace();
  166. setText("Error: " + e.getMessage());
  167. }
  168. }
  169. return Service.START_REDELIVER_INTENT;
  170. }
  171. private void register()
  172. {
  173. try
  174. {
  175. setText("Registering...");
  176. RequestTask r = new RequestTask();
  177. r.execute("http://" + m_http + "/register.php?name=" + URLEncoder.encode(m_name, "utf-8") + "&number=" + URLEncoder.encode(m_number, "utf-8")
  178. + "&port=" + m_port);
  179. }
  180. catch (UnsupportedEncodingException e)
  181. {
  182. e.printStackTrace();
  183. }
  184. }
  185. @Override
  186. public void onDestroy()
  187. {
  188. m_handler.removeCallbacks(m_timer);
  189. m_builder.setOngoing(false);
  190. m_notificationManager.notify(0, m_builder.build());
  191. stopForeground(false);
  192. Log.d("SMSServerService", "stop");
  193. if (m_server != null)
  194. {
  195. try
  196. {
  197. m_server.stop();
  198. }
  199. catch (Exception e)
  200. {
  201. e.printStackTrace();
  202. }
  203. m_server = null;
  204. }
  205. super.onDestroy();
  206. }
  207. @Override
  208. public IBinder onBind(Intent arg0)
  209. {
  210. return null;
  211. }
  212. }