Browse Source

removed old http server stuff

master
Robin Thoni 9 years ago
parent
commit
8eb83cde89

+ 1
- 11
SMSServer/src/main/AndroidManifest.xml View File

@@ -31,21 +31,11 @@
31 31
             </intent-filter>
32 32
         </activity>
33 33
 
34
-        <service
35
-            android:name="SMSServerService"
36
-            android:label="SMS Server Service"
37
-            android:permission="android.permission.WRITE_SMS">
38
-        </service>
39
-        <receiver android:name=".Receiver" android:exported="true" > 
34
+        <receiver android:name=".SMSReceiver" android:exported="true" >
40 35
 			<intent-filter android:priority="999">
41 36
 			  <action android:name="android.provider.Telephony.SMS_RECEIVED" />
42 37
 			</intent-filter>
43 38
 		</receiver>
44
-        <receiver android:name=".BootReceiver" android:exported="true" > 
45
-			<intent-filter>
46
-			  <action android:name="android.intent.action.BOOT_COMPLETED"/>
47
-			</intent-filter>
48
-		</receiver>
49 39
     </application>
50 40
 
51 41
 </manifest>

+ 0
- 19
SMSServer/src/main/java/com/rob57530/smsserver/BootReceiver.java View File

@@ -1,19 +0,0 @@
1
-package com.rob57530.smsserver;
2
-
3
-import android.content.BroadcastReceiver;
4
-import android.content.Context;
5
-import android.content.Intent;
6
-import android.content.SharedPreferences;
7
-import android.preference.PreferenceManager;
8
-
9
-public class BootReceiver extends BroadcastReceiver
10
-{
11
-	@Override
12
-	public void onReceive(Context context, Intent intent)
13
-	{
14
-		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
15
-		if(p.getBoolean("boot", true))
16
-			context.startService(new Intent(context, SMSServerService.class));
17
-	}
18
-
19
-}

+ 9
- 73
SMSServer/src/main/java/com/rob57530/smsserver/MainActivity.java View File

@@ -23,10 +23,7 @@ public class MainActivity extends Activity
23 23
 {
24 24
 	private Button m_btnStart;
25 25
 	private Button m_btnStop;
26
-	private EditText m_textPort;
27 26
 	private EditText m_textHttp;
28
-	private EditText m_textName;
29
-	private EditText m_textNumber;
30 27
 
31 28
 	@Override
32 29
 	protected void onCreate(Bundle savedInstanceState)
@@ -35,10 +32,7 @@ public class MainActivity extends Activity
35 32
 		setContentView(R.layout.activity_main);
36 33
 		m_btnStart = (Button) findViewById(R.id.btnStart);
37 34
 		m_btnStop = (Button) findViewById(R.id.btnStop);
38
-		m_textPort = (EditText) findViewById(R.id.editTextPort);
39 35
 		m_textHttp = (EditText) findViewById(R.id.editTextHttp);
40
-		m_textName = (EditText) findViewById(R.id.editTextName);
41
-		m_textNumber = (EditText) findViewById(R.id.editTextNumber);
42 36
 		m_btnStart.setOnClickListener(new View.OnClickListener()
43 37
 		{
44 38
 			@Override
@@ -55,97 +49,50 @@ public class MainActivity extends Activity
55 49
 				stopServer();
56 50
 			}
57 51
 		});
58
-		m_textName.setHint(Build.MODEL);
59
-		try
60
-		{
61
-			m_textNumber.setHint(((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number());
62
-		}
63
-		catch (Exception e)
64
-		{
65
-
66
-		}
67 52
 
68 53
 		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
69 54
 		m_textHttp.setText(p.getString("http", m_textHttp.getHint().toString()));
70
-		m_textName.setText(p.getString("name", m_textName.getHint().toString()));
71
-		m_textPort.setText("" + p.getInt("port", 8080));
72
-		m_textNumber.setText(p.getString("number", m_textNumber.getHint().toString()));
73
-
74
-		CheckBox checkBoot = ((CheckBox) findViewById(R.id.checkBoxBoot));
75
-		checkBoot.setChecked(p.getBoolean("boot", true));
76
-		checkBoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
77
-		{
78
-			@Override
79
-			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
80
-			{
81
-				Editor e = p.edit();
82
-				e.putBoolean("boot", isChecked);
83
-				e.commit();
84
-			}
85
-		});
86 55
 
87 56
 		updateButtons();
88
-		startServer();
89 57
 	}
90 58
 
91 59
 	public void startServer()
92 60
 	{
93
-		int p = 0;
94
-		try
95
-		{
96
-			p = Integer.parseInt(m_textPort.getText().toString());
97
-		}
98
-		catch (Exception e)
99
-		{
100
-
101
-		}
102 61
 		String http = m_textHttp.getText().toString();
103
-		String name = m_textName.getText().toString();
104
-		String number = m_textNumber.getText().toString();
105 62
 
106 63
 		SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(this);
107 64
 		Editor edit = pr.edit();
108 65
 		edit.putString("http", http);
109
-		edit.putString("name", name);
110
-		edit.putInt("port", p);
111
-		edit.putString("number", number);
112
-		edit.commit();
113 66
 
114 67
 		try
115 68
 		{
116
-
117 69
 			if (http.length() == 0)
118 70
 				throw new Exception("Empty SMS Server");
119
-			if (name.length() == 0)
120
-				throw new Exception("Empty Name");
121
-			if (number.length() == 0)
122
-				throw new Exception("Empty Number");
123
-			if (p < 1 || p > 65535)
124
-				throw new Exception("Invalid Port (1 <= port <= 65535)");
125 71
 
126
-			startService(new Intent(MainActivity.this, SMSServerService.class));
127
-			updateButtons();
72
+            edit.putBoolean("running", true);
128 73
 
129 74
 		}
130 75
 		catch (Exception e)
131 76
 		{
132 77
 			Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
133 78
 		}
79
+        edit.commit();
80
+        updateButtons();
134 81
 	}
135 82
 
136 83
 	public void stopServer()
137 84
 	{
138
-		stopService(new Intent(MainActivity.this, SMSServerService.class));
85
+        SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(this);
86
+        Editor edit = pr.edit();
87
+        edit.putBoolean("running", false);
88
+        edit.commit();
139 89
 		updateButtons();
140 90
 	}
141 91
 
142 92
 	private boolean isServiceRunning()
143 93
 	{
144
-		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
145
-		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
146
-			if (SMSServerService.class.getName().equals(service.service.getClassName()))
147
-				return true;
148
-		return false;
94
+        SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(this);
95
+        return pr.getBoolean("running", false);
149 96
 	}
150 97
 
151 98
 	private void updateButtons()
@@ -153,17 +100,6 @@ public class MainActivity extends Activity
153 100
 		boolean is = isServiceRunning();
154 101
 		m_btnStart.setEnabled(!is);
155 102
 		m_btnStop.setEnabled(is);
156
-		m_textPort.setEnabled(!is);
157 103
 		m_textHttp.setEnabled(!is);
158
-		m_textName.setEnabled(!is);
159
-		m_textNumber.setEnabled(!is);
160
-	}
161
-
162
-	@Override
163
-	public boolean onCreateOptionsMenu(Menu menu)
164
-	{
165
-		getMenuInflater().inflate(R.menu.main, menu);
166
-		return true;
167 104
 	}
168
-
169 105
 }

SMSServer/src/main/java/com/rob57530/smsserver/Receiver.java → SMSServer/src/main/java/com/rob57530/smsserver/SMSReceiver.java View File

@@ -25,7 +25,7 @@ import android.telephony.SmsMessage;
25 25
 import android.util.Base64;
26 26
 import android.util.Log;
27 27
 
28
-public class Receiver extends BroadcastReceiver
28
+public class SMSReceiver extends BroadcastReceiver
29 29
 {
30 30
 
31 31
 	class RequestTask extends AsyncTask<String, String, String>
@@ -80,16 +80,10 @@ public class Receiver extends BroadcastReceiver
80 80
 	public void onReceive(Context context, Intent intent)
81 81
 	{
82 82
 		Log.d("SMSServerReceiver", "sms");
83
-		boolean running = false;
84
-		ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
85
-		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
86
-			if (SMSServerService.class.getName().equals(service.service.getClassName()))
87
-			{
88
-				running = true;
89
-				break;
90
-			}
91
-		if(!running)
92
-			return;
83
+        SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(context);
84
+        if (!pr.getBoolean("running", false))
85
+            return;
86
+
93 87
 		Log.d("SMSServerReceiver", "sms (running)");
94 88
 		
95 89
 		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);

+ 0
- 228
SMSServer/src/main/java/com/rob57530/smsserver/SMSServerService.java View File

@@ -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
-}

+ 4
- 72
SMSServer/src/main/res/layout/activity_main.xml View File

@@ -26,26 +26,14 @@
26 26
         android:layout_below="@+id/btnStart"
27 27
         android:text="@string/stop" />
28 28
 
29
-    <EditText
30
-        android:id="@+id/editTextPort"
31
-        android:layout_width="wrap_content"
32
-        android:layout_height="wrap_content"
33
-        android:layout_alignLeft="@+id/textViewPort"
34
-        android:layout_alignRight="@+id/textViewPort"
35
-        android:layout_below="@+id/textViewPort"
36
-        android:text="8080"
37
-        android:hint="8080"
38
-        android:ems="10"
39
-        android:inputType="number" />
40
-
41 29
     <TextView
42 30
         android:id="@+id/textViewHttp"
43 31
         android:layout_width="wrap_content"
44 32
         android:layout_height="wrap_content"
45
-        android:layout_alignLeft="@+id/editTextPort"
46
-        android:layout_alignRight="@+id/editTextPort"
47
-        android:layout_below="@+id/editTextPort"
48
-        android:text="SMS Server:" />
33
+        android:text="SMS Server:"
34
+        android:layout_alignLeft="@+id/btnStop"
35
+        android:layout_alignRight="@+id/btnStop"
36
+        android:layout_below="@+id/btnStop" />
49 37
 
50 38
     <EditText
51 39
         android:id="@+id/editTextHttp"
@@ -61,60 +49,4 @@
61 49
         <requestFocus />
62 50
     </EditText>
63 51
 
64
-    <TextView
65
-        android:id="@+id/textViewName"
66
-        android:layout_width="wrap_content"
67
-        android:layout_height="wrap_content"
68
-        android:layout_alignLeft="@+id/editTextHttp"
69
-        android:layout_alignRight="@+id/editTextHttp"
70
-        android:layout_below="@+id/editTextHttp"
71
-        android:text="Name:" />
72
-
73
-    <EditText
74
-        android:id="@+id/editTextName"
75
-        android:layout_width="wrap_content"
76
-        android:layout_height="wrap_content"
77
-        android:layout_alignLeft="@+id/textViewName"
78
-        android:layout_alignRight="@+id/textViewName"
79
-        android:layout_below="@+id/textViewName"
80
-        android:ems="10" />
81
-
82
-    <TextView
83
-        android:id="@+id/textViewNumber"
84
-        android:layout_width="wrap_content"
85
-        android:layout_height="wrap_content"
86
-        android:layout_alignLeft="@+id/editTextName"
87
-        android:layout_alignRight="@+id/editTextName"
88
-        android:layout_below="@+id/editTextName"
89
-        android:text="Phone Number:" />
90
-
91
-    <EditText
92
-        android:id="@+id/editTextNumber"
93
-        android:layout_width="wrap_content"
94
-        android:layout_height="wrap_content"
95
-        android:layout_alignLeft="@+id/textViewNumber"
96
-        android:layout_alignRight="@+id/textViewNumber"
97
-        android:layout_below="@+id/textViewNumber"
98
-        android:ems="10"
99
-        android:inputType="phone" />
100
-
101
-    <CheckBox
102
-        android:id="@+id/checkBoxBoot"
103
-        android:layout_width="wrap_content"
104
-        android:layout_height="wrap_content"
105
-        android:layout_alignLeft="@+id/btnStop"
106
-        android:layout_alignRight="@+id/btnStop"
107
-        android:layout_below="@+id/btnStop"
108
-        android:checked="true"
109
-        android:text="Start at Boot" />
110
-
111
-    <TextView
112
-        android:id="@+id/textViewPort"
113
-        android:layout_width="wrap_content"
114
-        android:layout_height="wrap_content"
115
-        android:layout_alignLeft="@+id/checkBoxBoot"
116
-        android:layout_alignRight="@+id/checkBoxBoot"
117
-        android:layout_below="@+id/checkBoxBoot"
118
-        android:text="Port:" />
119
-
120 52
 </RelativeLayout>

+ 0
- 3
SMSServer/src/main/res/menu/main.xml View File

@@ -1,3 +0,0 @@
1
-<menu xmlns:android="http://schemas.android.com/apk/res/android" >
2
-
3
-</menu>

Loading…
Cancel
Save