Skip to content

Commit a624b61

Browse files
committed
Added Push API functionality, refactored OmaApiService
1 parent 4f1ac61 commit a624b61

File tree

6 files changed

+203
-71
lines changed

6 files changed

+203
-71
lines changed

OmaApiServer/AndroidManifest.xml

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,48 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.example.omaapi"
44
android:versionCode="1"
5-
android:versionName="1.0" >
5+
android:versionName="1.0"
6+
android:alwaysRetainTaskState="true">
67

78
<uses-sdk
89
android:minSdkVersion="8"
910
android:targetSdkVersion="17" />
1011

1112
<uses-permission android:name="android.permission.INTERNET" />
1213
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
13-
14+
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
15+
<uses-feature android:name="android.hardware.telephony"></uses-feature>
16+
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"></uses-permission>
17+
1418
<application
1519
android:allowBackup="true"
1620
android:icon="@drawable/ic_launcher"
1721
android:label="@string/app_name"
1822
android:theme="@style/AppTheme" >
1923
<activity
20-
android:name="com.example.omaapi.MainActivity"
24+
android:name=".MainActivity"
2125
android:label="@string/app_name" >
2226
<intent-filter>
2327
<action android:name="android.intent.action.MAIN" />
24-
2528
<category android:name="android.intent.category.LAUNCHER" />
2629
</intent-filter>
2730
</activity>
2831
<activity
29-
android:name="com.example.omaapi.ScanCodeActivity"
32+
android:name=".ScanCodeActivity"
3033
android:label="@string/app_name" >
3134
<intent-filter>
3235
<action android:name="android.intent.action.MAIN" />
3336
</intent-filter>
3437
</activity>
35-
<service android:name="com.example.omaapi.OmaApiService">
38+
<service android:name=".OmaApiService">
3639
</service>
37-
</application>
40+
<receiver android:name=".PushReceiver" android:priority="999">
41+
<intent-filter>
42+
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
43+
</intent-filter>
44+
<intent-filter>
45+
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
46+
</intent-filter>
47+
</receiver>
48+
</application>
3849
</manifest>

OmaApiServer/src/com/example/omaapi/MainActivity.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.app.Activity;
2222
import android.content.Context;
2323
import android.content.Intent;
24+
import android.telephony.SmsMessage;
2425
import android.view.Menu;
2526
import android.view.View;
2627
import android.widget.Toast;

OmaApiServer/src/com/example/omaapi/OmaApiService.java

+82-57
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.os.Binder;
2525
import android.os.IBinder;
2626
import android.view.View;
27+
import android.widget.Toast;
2728

2829
import com.example.omaapi.SampleHttpd.Response.Status;
2930
import com.example.omaapi.R;
@@ -34,16 +35,27 @@ public class OmaApiService extends Service {
3435

3536
@Override
3637
public int onStartCommand(Intent intent, int flags, int startId) {
38+
// Toast t = Toast.makeText(getBaseContext(), "OmaApiService (re)started\nIntent Category: "+intent.getAction(), Toast.LENGTH_LONG);
39+
// t.show();
3740
context = getApplicationContext();
41+
3842
if (httpd == null) {
3943
httpd = new HttpListener(this, Integer.parseInt(this.getString(R.string.port)));
44+
try {
45+
httpd.start();
46+
}
47+
catch (IOException ioe) {
48+
// error handling
49+
}
4050
}
41-
try {
42-
httpd.start();
51+
52+
if (intent.getAction() == "pushevent") {
53+
SampleHttpd.pushEvent = intent.getStringExtra("data");
54+
Toast toast = Toast.makeText(getBaseContext(),
55+
"OmaApiService event:\n"+SampleHttpd.pushEvent, Toast.LENGTH_LONG);
56+
toast.show();
4357
}
44-
catch (IOException ioe) {
45-
// error handling
46-
}
58+
4759
return Service.START_NOT_STICKY;
4860
}
4961

@@ -75,8 +87,6 @@ public IBinder onBind(Intent intent) {
7587

7688
public class HttpListener extends SampleHttpd {
7789
private int http_port;
78-
private boolean mc_async_response = false;
79-
private String mc_result = "This is supposed to be mc scanning result";
8090

8191
public HttpListener(Service service, int port) {
8292
super(port);
@@ -96,64 +106,79 @@ public Response serve(String uri, Method method, Map<String, String> header, Map
96106
if (choice.equalsIgnoreCase("mc")) mc = 0;
97107
if (choice.equalsIgnoreCase("cmapi")) cmapi = 0;
98108
}
99-
100-
// Stub for WRAPI Push API: currently only verifies events can be delivered...
101-
// the connection is closed immediately which causes an error after the first event
102-
// TODO: add SSE support to SampleHttpd.java
103-
if (push==0) {
104-
SampleHttpd.Response resp = new SampleHttpd.Response(Status.OK,"text/event-stream",
105-
"event: message\nid: 1\ndata: hello world\n\n");
106-
resp.addHeader("Access-Control-Allow-Origin","*");
107-
return resp;
108-
}
109-
else if (mc==0) {
110-
mc_async_response = false;
111-
Intent intent = new Intent(context, ScanCodeActivity.class);
112-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
113-
context.startActivity(intent);
114-
while (!mc_async_response) {}
115-
SampleHttpd.Response resp = new SampleHttpd.Response(Status.OK,"text/plain", mc_result);
116-
resp.addHeader("Access-Control-Allow-Origin","*");
109+
Response resp;
110+
if (push==0) resp = handlePush(uri, parms);
111+
else if (mc==0) resp = handleMC();
112+
else if (cmapi == 0) resp = handleCMAPI();
113+
else {
114+
String msg = "<html><body><h1>Request to "+method + " '" + uri + "'\n"+
115+
"Hello from AT&T HTTP Listener at port " + http_port + "</h1>\n";
117116

118-
return resp;
119-
}
120-
else if (cmapi == 0) {
121-
SampleHttpd.Response resp = new SampleHttpd.Response(Status.OK,"text/html", handleCMAPI());
122-
resp.addHeader("Access-Control-Allow-Origin","*");
123-
return resp;
117+
msg += "<form action='?' method='get'>\n" +
118+
"<h1>Please select:</h1>\n" +
119+
"<input type='radio' name='choice' value='mc' checked='yes'/>Mobile Code<br/>\n" +
120+
"<input type='radio' name='choice' value='cmapi'/>OpenCMAPI<br/>\n" +
121+
"<input type='submit' value='SUBMIT'/>" +
122+
"</form>\n";
123+
/*
124+
if (parms.get("username") == null) {
125+
msg += "<form action='?' method='get'>\n" +
126+
"<p>Your name: <input type='text' name='username'></p>\n" +
127+
"</form>\n";
128+
}
129+
else {
130+
msg += "<p>How are you, " + parms.get("username") + "?</p>";
131+
}
132+
*/
133+
msg += "</body></html>\n";
134+
135+
resp = new Response(msg);
124136
}
125-
126-
String msg = "<html><body><h1>Request to "+method + " '" + uri + "'\n"+
127-
"Hello from AT&T HTTP Listener at port " + http_port + "</h1>\n";
128-
129-
msg += "<form action='?' method='get'>\n" +
130-
"<h1>Please select:</h1>\n" +
131-
"<input type='radio' name='choice' value='mc' checked='yes'/>Mobile Code<br/>\n" +
132-
"<input type='radio' name='choice' value='cmapi'/>OpenCMAPI<br/>\n" +
133-
"<input type='submit' value='SUBMIT'/>" +
134-
"</form>\n";
135-
/*
136-
if (parms.get("username") == null) {
137-
msg += "<form action='?' method='get'>\n" +
138-
"<p>Your name: <input type='text' name='username'></p>\n" +
139-
"</form>\n";
140-
}
141-
else {
142-
msg += "<p>How are you, " + parms.get("username") + "?</p>";
143-
}
144-
*/
145-
msg += "</body></html>\n";
146-
147-
return new SampleHttpd.Response(msg);
148-
}
137+
return(resp);
138+
}
139+
140+
public boolean mc_async_response = false;
141+
private String mc_result = "This is supposed to be mc scanning result";
142+
private Response handleMC() {
143+
mc_async_response = false;
144+
Intent intent = new Intent(context, ScanCodeActivity.class);
145+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
146+
context.startActivity(intent);
147+
while (!mc_async_response) {}
148+
SampleHttpd.Response resp = new SampleHttpd.Response(Status.OK,"text/plain", mc_result);
149+
resp.addHeader("Access-Control-Allow-Origin","*");
150+
return resp;
151+
}
149152

150153
public void scanResult(String result) {
151154
mc_result = result;
152155
mc_async_response = true;
153156
}
154157

155-
private String handleCMAPI() {
156-
return "<html><body>\n<h1>You have chosen CMAPI</h1>\n</body></html>\n";
158+
private Response handlePush(String uri, Map<String, String> parms) {
159+
// WRAPI Push API
160+
// Set the accepted sources filter to be used by the Push API server. See where this
161+
// is used in SampleHttpd.
162+
if (parms.get("push-accept-source")!= null) {
163+
pushAcceptSource = parms.get("push-accept-source");
164+
if (pushAcceptSource.indexOf(' ')==0) pushAcceptSource = pushAcceptSource.substring(1);
165+
}
166+
else {
167+
pushAcceptSource = "any";
168+
}
169+
// Log the event
170+
System.out.println("OMA API server received Push API request for source: "+pushAcceptSource);
171+
//
172+
Response resp = new Response(Status.OK,"text/event-stream","");
173+
resp.addHeader("Access-Control-Allow-Origin","*");
174+
resp.eventsource = true; // This is a flag to to the server to not close the connection...
175+
return resp;
176+
}
177+
178+
private Response handleCMAPI() {
179+
Response resp = new Response(Status.OK,"text/html","<html><body><h1>You have chosen CMAPI</h1></body></html>");
180+
resp.addHeader("Access-Control-Allow-Origin","*");
181+
return resp;
157182
}
158183
}
159184
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.omaapi;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.telephony.SmsMessage;
8+
import android.util.Log;
9+
import android.widget.Toast;
10+
11+
public class PushReceiver extends BroadcastReceiver {
12+
@Override
13+
public void onReceive(Context context, Intent intent) {
14+
Bundle bundle = intent.getExtras();
15+
String str = intent.getAction()+":";
16+
String data = "";
17+
if (bundle != null) {
18+
if (intent.getAction().equals("android.provider.Telephony.WAP_PUSH_RECEIVED")) {
19+
String mimeType = intent.getType();
20+
str = "mimeType:"+mimeType;
21+
String headers = intent.getStringExtra("header");
22+
data = intent.getStringExtra("data");
23+
str += "/n" + data;
24+
String contentTypeParameters = intent.getStringExtra("contentTypeParameters");
25+
// TODO: Filter based upon
26+
// pushSource.acceptSource
27+
// pushSource.acceptContentType
28+
// pushSource.acceptApplicationId
29+
// pushSource.onmessage(headers+"\n"+str);
30+
}
31+
else {
32+
int i = 0;
33+
SmsMessage[ ] msgs = null;
34+
Object[ ] pdus = (Object[ ]) bundle.get("pdus");
35+
msgs = new SmsMessage[pdus.length];
36+
String source;
37+
for (i=0; i<msgs.length; i++) {
38+
msgs[i] = SmsMessage.createFromPdu((byte[ ])pdus[i]);
39+
source = msgs[i].getOriginatingAddress();
40+
str += "Message from sms:" + source;
41+
str += " :";
42+
data = msgs[i].getMessageBody().toString();
43+
str += data;
44+
// TODO: Need to deliver to all PushSource objects created (there could be more than one)
45+
}
46+
// Toast toast = Toast.makeText(context,
47+
// "PushReceiver event:\n"+str, Toast.LENGTH_LONG);
48+
// toast.show();
49+
Intent pushevent = new Intent(context, OmaApiService.class);
50+
pushevent.setAction("pushevent");
51+
pushevent.putExtra("data",str);
52+
context.startService(pushevent);
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)