|
|
@@ -3,6 +3,7 @@ package homespeakers.knacki.info.homespeakersandroid;
|
|
|
import android.app.Notification;
|
|
|
import android.app.PendingIntent;
|
|
|
import android.content.Intent;
|
|
|
+import android.os.Build;
|
|
|
import android.os.IBinder;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
@@ -15,6 +16,8 @@ import java.io.StringReader;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.InetSocketAddress;
|
|
|
import java.net.Socket;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayDeque;
|
|
|
import java.util.Collection;
|
|
|
import java.util.logging.Level;
|
|
|
@@ -23,14 +26,18 @@ import java.util.logging.Logger;
|
|
|
import homespeakers.knacki.info.homespeakersandroid.radio.Input;
|
|
|
import homespeakers.knacki.info.homespeakersandroid.radio.InputPlayer;
|
|
|
import homespeakers.knacki.info.homespeakersandroid.radio.InputReader;
|
|
|
+import homespeakers.knacki.info.homespeakersandroid.system.BatteryController;
|
|
|
+import homespeakers.knacki.info.homespeakersandroid.system.VolumeController;
|
|
|
|
|
|
public class Service extends android.app.Service {
|
|
|
public final static String NOTIFICATION_CHANNEL_ID = "info.knacki.homespeakers.service";
|
|
|
public final static int NOTIFICATION_ID = NOTIFICATION_CHANNEL_ID.hashCode();
|
|
|
- public final static long RECONNECT_DELAY = 60000; // 1 minute
|
|
|
+ public final static long RECONNECT_DELAY = 6000; // 1 minute
|
|
|
public final static String ADDRESS = "192.168.0.5";
|
|
|
public final static short PORT = 9001;
|
|
|
|
|
|
+ protected static final Charset charset = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? StandardCharsets.UTF_8 : Charset.forName("utf-8");
|
|
|
+
|
|
|
private final static Logger log = Logger.getLogger(Service.class.getName());
|
|
|
private Collection<InputPlayer> playingInputs = new ArrayDeque<>();
|
|
|
|
|
|
@@ -94,7 +101,7 @@ public class Service extends android.app.Service {
|
|
|
continue newLoop;
|
|
|
// New source, add it
|
|
|
InputPlayer player = new InputPlayer(i);
|
|
|
- player.start();
|
|
|
+ player.start(this);
|
|
|
playingInputs.add(player);
|
|
|
}
|
|
|
}
|
|
|
@@ -105,8 +112,18 @@ public class Service extends android.app.Service {
|
|
|
String config = stream.readLine();
|
|
|
if (config == null)
|
|
|
return;
|
|
|
- Collection<Input> inputs = InputReader.Read(new JsonReader(new StringReader(config)));
|
|
|
- setInputs(inputs);
|
|
|
+ if (config.startsWith("SETVOL")) {
|
|
|
+ short vol = Short.parseShort(config.substring("SETVOL".length()));
|
|
|
+ VolumeController.SetVolume(this, vol);
|
|
|
+ sendStatus();
|
|
|
+ } else if (config.startsWith("SET")) {
|
|
|
+ Collection<Input> inputs = InputReader.Read(new JsonReader(new StringReader(config.substring("SET".length()))));
|
|
|
+ setInputs(inputs);
|
|
|
+ } else if (config.startsWith("PING")) {
|
|
|
+ log.severe("Send pong");
|
|
|
+ fSock.getOutputStream().write("PONG\n".getBytes(charset));
|
|
|
+ sendStatus();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -115,11 +132,24 @@ public class Service extends android.app.Service {
|
|
|
private static void sendName(String name) {
|
|
|
if (fSock == null) return;
|
|
|
try {
|
|
|
- fSock.getOutputStream().write(("HELO " +name + "\n").getBytes("utf-8"));
|
|
|
+ fSock.getOutputStream().write(("HELO " +name + "\n").getBytes(charset));
|
|
|
}
|
|
|
catch (IOException e) {}
|
|
|
}
|
|
|
|
|
|
+ public void sendStatus() {
|
|
|
+ final short volume = VolumeController.GetVolume(this);
|
|
|
+ final boolean plugged = BatteryController.IsPlugged(this);
|
|
|
+ final short battery = BatteryController.GetLevel(this);
|
|
|
+
|
|
|
+ try {
|
|
|
+ fSock.getOutputStream().write(("STATUS{\"battery\":" +battery +",\"plugged\":" +(plugged ? "true" : "false") +",\"volume\":" +volume +"}").getBytes(charset));
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot send status to server", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void createSocket() {
|
|
|
fSock = null;
|
|
|
while (true) {
|