server almost finished (but untested)
This commit is contained in:
@@ -4,35 +4,85 @@ import java.net.DatagramPacket;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class RemoteControlServer implements Runnable {
|
public class RemoteControlServer{
|
||||||
|
|
||||||
public static final String SERVERIP = "0.0.0.0";
|
public static final String SERVERIP = "0.0.0.0";
|
||||||
public static final int SERVERPORT = 4444;
|
public static final int SERVERPORT = 4444;
|
||||||
|
|
||||||
@Override
|
private Activity activity;
|
||||||
|
private OnCommandRecieved onCommandRecievedListener;
|
||||||
|
private String lastCommand = "";
|
||||||
|
|
||||||
|
public OnCommandRecieved getOnCommandRecievedListener() {
|
||||||
|
return onCommandRecievedListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOnCommandRecievedListener(
|
||||||
|
OnCommandRecieved onCommandRecievedListener) {
|
||||||
|
this.onCommandRecievedListener = onCommandRecievedListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteControlServer(Activity activity) {
|
||||||
|
super();
|
||||||
|
this.activity = activity;
|
||||||
|
setOnCommandRecievedListener(new OnCommandRecieved() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recieve(String command) {
|
||||||
|
// dummy implementation to be sure there is something to call
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
DatagramSocket socket = null;
|
DatagramSocket socket = null;
|
||||||
try {
|
try {
|
||||||
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
|
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
|
||||||
|
|
||||||
socket = new DatagramSocket(SERVERPORT, serverAddr);
|
socket = new DatagramSocket(SERVERPORT, serverAddr);
|
||||||
byte[] buf = new byte[17];
|
|
||||||
|
// samples of commands, UTF8 encoded:
|
||||||
for(;;) {
|
// S114A286 - surah 114, ayah 286 (nonexistent - but contains "max"
|
||||||
|
// values)
|
||||||
|
// S1A7 - surah 1, ayah 7
|
||||||
|
// F225 - font size 225
|
||||||
|
byte[] buf = new byte[20];
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
|
||||||
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
|
|
||||||
|
if (packet != null) {
|
||||||
|
lastCommand = new String(packet.getData(), "UTF-8");
|
||||||
|
activity.runOnUiThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
getOnCommandRecievedListener().recieve(lastCommand);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("SERVER", "Something is wrong with server: " + e.toString());
|
Log.e("SERVER", "Something is wrong with server: " + e.toString());
|
||||||
}
|
} finally {
|
||||||
finally {
|
if (socket != null)
|
||||||
if(socket != null) socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface OnCommandRecieved {
|
||||||
|
|
||||||
|
public void recieve(String command);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package com.mhalka.qurantranslationdisplay;
|
package com.mhalka.qurantranslationdisplay;
|
||||||
|
|
||||||
|
import com.mhalka.qurantranslationdisplay.RemoteControlServer.OnCommandRecieved;
|
||||||
import com.mhalka.qurantranslationdisplay.VerticalMarqueeTextView.ScrollingStoppedListener;
|
import com.mhalka.qurantranslationdisplay.VerticalMarqueeTextView.ScrollingStoppedListener;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.res.AssetManager;
|
import android.content.res.AssetManager;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.method.ScrollingMovementMethod;
|
import android.text.method.ScrollingMovementMethod;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
@@ -28,6 +30,10 @@ public class ShowAyah extends Activity {
|
|||||||
private TextView chapterName;
|
private TextView chapterName;
|
||||||
int currentChapter=1;
|
int currentChapter=1;
|
||||||
int currentVerse=1;
|
int currentVerse=1;
|
||||||
|
private RemoteControlServer rcs;
|
||||||
|
private ServerTask server;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -115,6 +121,8 @@ public class ShowAyah extends Activity {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
|
|
||||||
|
startServer();
|
||||||
|
|
||||||
// Start or restart the Marquee if paused.
|
// Start or restart the Marquee if paused.
|
||||||
if (ayah.isPaused()) {
|
if (ayah.isPaused()) {
|
||||||
@@ -128,6 +136,7 @@ public class ShowAyah extends Activity {
|
|||||||
|
|
||||||
// Pause the Marquee when the Activity pauses.
|
// Pause the Marquee when the Activity pauses.
|
||||||
ayah.pauseMarquee();
|
ayah.pauseMarquee();
|
||||||
|
stopServer();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,5 +146,41 @@ public class ShowAyah extends Activity {
|
|||||||
ayah.stopMarquee();
|
ayah.stopMarquee();
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void startServer() {
|
||||||
|
server = new ServerTask();
|
||||||
|
rcs = new RemoteControlServer((ShowAyah.this));
|
||||||
|
rcs.setOnCommandRecievedListener(new OnCommandRecieved() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recieve(String command) {
|
||||||
|
// TODO parse the command and do it !
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.execute("");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopServer() {
|
||||||
|
server.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private class ServerTask extends AsyncTask<String,String, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... params) {
|
||||||
|
|
||||||
|
rcs.run();
|
||||||
|
return "yes!";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user