Handle requests with // at the beginning
[louyapi.git] / src / main / java / de / cweiske / ouya / louyapi / HttpService.java
1 package de.cweiske.ouya.louyapi;
2
3 import android.app.Service;
4 import android.content.Intent;
5 import android.os.IBinder;
6 import android.util.Log;
7
8 import java.io.IOException;
9
10 import fi.iki.elonen.NanoHTTPD;
11
12 public class HttpService extends Service {
13     NanoHTTPD server;
14
15     static String TAG = "HttpService";
16
17     @Override
18     public void onCreate() {
19         Log.i("service", "start service");
20         super.onCreate();
21
22         server = new HttpServer(8080, getAssets());
23         try {
24             server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
25         } catch (IOException ioe) {
26             //FIXME
27             Log.e(TAG, "Couldn't start server:\n" + ioe);
28             System.exit(-1);
29         }
30     }
31
32     @Override
33     public void onDestroy() {
34         Log.i(TAG, "stop service");
35         super.onDestroy();
36         server.stop();
37
38         //restart the service
39         sendBroadcast(new Intent(this, Autostart.class));
40     }
41
42     @Override
43     public IBinder onBind(Intent intent) {
44         return null;
45     }
46
47     @Override
48     public int onStartCommand(Intent intent, int flags, int startId) {
49         //android shall start the service again if killed
50         return START_STICKY;
51     }
52
53     @Override
54     public void onTaskRemoved(Intent rootIntent) {
55         Log.i(TAG, "Task removed");
56         super.onTaskRemoved(rootIntent);
57     }
58 }