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