-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add second layer of communication with HTTP server
- Loading branch information
Showing
10 changed files
with
215 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
SleepGarmin-android/app/src/main/java/com/urbandroid/sleep/garmin/HttpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.urbandroid.sleep.garmin; | ||
|
||
import android.content.Context; | ||
|
||
import com.urbandroid.common.logging.Logger; | ||
|
||
import org.json.JSONException; | ||
|
||
import java.util.Map; | ||
|
||
import fi.iki.elonen.NanoHTTPD; | ||
|
||
public class HttpServer extends NanoHTTPD { | ||
|
||
private static final String TAG = "HttpServer#"; | ||
Context context; | ||
|
||
public HttpServer(int port, Context context) { | ||
super(port); | ||
this.context = context; | ||
} | ||
|
||
private final QueueToWatch queueToWatch = QueueToWatch.getInstance(); | ||
|
||
@Override | ||
public Response serve(IHTTPSession session) { | ||
Map<String, String> params = session.getParms(); | ||
|
||
for (Map.Entry<String,String> msg : params.entrySet()) { | ||
// Get all messages from watch and send them to sleep | ||
MessageHandler.getInstance().handleMessageFromWatchUsingHTTP(msg.getKey(), msg.getValue(), context); | ||
} | ||
|
||
// Serve all messages enqueued to watch | ||
return newFixedLengthResponse(serveQueue()); | ||
} | ||
|
||
private String serveQueue() { | ||
String jsonQueue = null; | ||
try { | ||
jsonQueue = queueToWatch.getQueueAsJsonArray(); | ||
queueToWatch.emptyQueue(); | ||
} catch (JSONException e) { | ||
Logger.logSevere(TAG + "serveQueue", e); | ||
} | ||
Logger.logDebug(TAG + "serveQueue: " + jsonQueue); | ||
return jsonQueue; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
SleepGarmin-android/app/src/main/java/com/urbandroid/sleep/garmin/HttpServerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.urbandroid.sleep.garmin; | ||
|
||
import static com.urbandroid.sleep.garmin.Constants.ACTION_STOP_SELF; | ||
import static com.urbandroid.sleep.garmin.Notifications.NOTIFICATION_CHANNEL_ID_TRACKING; | ||
|
||
import android.app.PendingIntent; | ||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.Handler; | ||
import android.os.IBinder; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.core.app.NotificationCompat; | ||
|
||
import com.urbandroid.common.logging.Logger; | ||
|
||
import java.io.IOException; | ||
|
||
public class HttpServerService extends Service { | ||
private static final String TAG = "HttpServerService"; | ||
private static final int PORT_DEFAULT = 1765; | ||
|
||
private boolean running = false; | ||
private HttpServer server; | ||
|
||
@Nullable | ||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
|
||
if (!running) { | ||
running = true; | ||
server = new HttpServer(PORT_DEFAULT, this); | ||
try { | ||
server.start(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
Logger.logSevere(TAG + ": IOException when starting HttpServer", e); | ||
} | ||
Handler h = new Handler(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
super.onDestroy(); | ||
server.stop(); | ||
} | ||
|
||
@Override | ||
public int onStartCommand(final Intent intent, int flags, int startId) { | ||
Logger.logDebug(TAG + "onStartCommand, intent " + ((intent != null && intent.getAction() != null) ? intent.getAction() : "null")); | ||
|
||
startForeground(); | ||
running = true; | ||
|
||
// ciqManager.init(this, intent); | ||
|
||
return START_STICKY; | ||
} | ||
|
||
private void startForeground() { | ||
final Intent stopIntent = new Intent(this, SleepAsAndroidProviderService.class); | ||
stopIntent.setAction(ACTION_STOP_SELF); | ||
|
||
PendingIntent pendingIntent = PendingIntent.getService(this, 151, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
|
||
if (Build.VERSION.SDK_INT >= 26) { | ||
pendingIntent = PendingIntent.getForegroundService(this, 151, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
} | ||
|
||
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_TRACKING) | ||
.setContentIntent(pendingIntent) | ||
.setColor(getResources().getColor(R.color.tint_dark)) | ||
.addAction(R.drawable.ic_action_stop, getResources().getString(R.string.stop), pendingIntent) | ||
.setContentText(getString(R.string.running_server)); | ||
|
||
if (Build.VERSION.SDK_INT < 24) { | ||
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name_long)); | ||
} | ||
|
||
notificationBuilder.setSmallIcon(R.drawable.ic_action_track); | ||
|
||
startForeground(1350, notificationBuilder.build()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters