24
24
import android .os .Binder ;
25
25
import android .os .IBinder ;
26
26
import android .view .View ;
27
+ import android .widget .Toast ;
27
28
28
29
import com .example .omaapi .SampleHttpd .Response .Status ;
29
30
import com .example .omaapi .R ;
@@ -34,16 +35,27 @@ public class OmaApiService extends Service {
34
35
35
36
@ Override
36
37
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();
37
40
context = getApplicationContext ();
41
+
38
42
if (httpd == null ) {
39
43
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
+ }
40
50
}
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 ();
43
57
}
44
- catch (IOException ioe ) {
45
- // error handling
46
- }
58
+
47
59
return Service .START_NOT_STICKY ;
48
60
}
49
61
@@ -75,8 +87,6 @@ public IBinder onBind(Intent intent) {
75
87
76
88
public class HttpListener extends SampleHttpd {
77
89
private int http_port ;
78
- private boolean mc_async_response = false ;
79
- private String mc_result = "This is supposed to be mc scanning result" ;
80
90
81
91
public HttpListener (Service service , int port ) {
82
92
super (port );
@@ -96,64 +106,79 @@ public Response serve(String uri, Method method, Map<String, String> header, Map
96
106
if (choice .equalsIgnoreCase ("mc" )) mc = 0 ;
97
107
if (choice .equalsIgnoreCase ("cmapi" )) cmapi = 0 ;
98
108
}
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\n id: 1\n data: 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 " ;
117
116
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 );
124
136
}
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
+ }
149
152
150
153
public void scanResult (String result ) {
151
154
mc_result = result ;
152
155
mc_async_response = true ;
153
156
}
154
157
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 ;
157
182
}
158
183
}
159
184
}
0 commit comments