Commit 483c055b authored by akwizgran's avatar akwizgran

Added network client and server.

parent a942d4f4
......@@ -27,4 +27,5 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'org.jetbrains:annotations-java5:15.0'
}
......@@ -2,7 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.briarproject.snooze">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name=".SnoozeApplication"
......
......@@ -15,4 +15,8 @@ interface Constants {
String EXTRA_ALARM_TYPE = "org.briarproject.snooze.EXTRA_ALARM_TYPE";
int ALARM_INTERVAL_MS = 5 * 1000;
int SOCKET_TIMEOUT_MS = 5 * 60 * 1000;
int CONNECT_TIMEOUT_MS = 30 * 1000;
}
package org.briarproject.snooze;
import android.content.Context;
import android.os.PowerManager;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import static android.content.Context.POWER_SERVICE;
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
import static org.briarproject.snooze.Constants.CONNECT_TIMEOUT_MS;
import static org.briarproject.snooze.Constants.SOCKET_TIMEOUT_MS;
class NetworkThread extends Thread {
private static final String TAG = NetworkThread.class.getName();
private final String host;
private final int port;
private final Logger log;
private final PowerManager.WakeLock wakeLock;
NetworkThread(String host, int port, Context appContext) {
this.host = host;
this.port = port;
log = ((SnoozeApplication) appContext).getLogger();
PowerManager pm = (PowerManager) appContext.getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, TAG);
wakeLock.setReferenceCounted(false);
setDaemon(true);
}
@Override
public void run() {
byte[] buf = new byte[4096];
try {
Socket s = new Socket();
s.setSoTimeout(SOCKET_TIMEOUT_MS);
log.log("Connecting to " + host + ":" + port);
s.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT_MS);
InputStream in = s.getInputStream();
log.log("Connected");
for (int read = 0; read != -1 && !isInterrupted(); read = in.read(buf)) {
wakeLock.acquire();
log.log("Read " + read + " bytes");
wakeLock.release();
}
if (isInterrupted()) log.log("Network thread interrupted");
else log.log("End of stream");
s.close();
} catch (IOException e) {
log.log(e.toString());
}
}
}
package org.briarproject.snooze;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
......@@ -11,10 +13,12 @@ import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import org.briarproject.snooze.Logger.LogListener;
import static android.view.View.FOCUS_DOWN;
import static android.widget.Toast.LENGTH_SHORT;
import static org.briarproject.snooze.AlarmType.TIMER;
import static org.briarproject.snooze.Constants.ACTION_START;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
......@@ -66,9 +70,9 @@ public class SnoozeActivity extends AppCompatActivity implements LogListener,
});
}
public void onClickStartServiceButton(View v) {
public void onClickApplySettingsButton(View v) {
boolean foreground = ((Switch) findViewById(R.id.foregroundServiceSwitch)).isChecked();
boolean wakeLock = ((Switch) findViewById(R.id.permanentWakeLockSwitch)).isChecked();
boolean wakeLock = ((Switch) findViewById(R.id.wakeLockSwitch)).isChecked();
boolean wifiLock = ((Switch) findViewById(R.id.wifiLockSwitch)).isChecked();
Intent i = new Intent(this, SnoozeService.class);
i.setAction(ACTION_START);
......@@ -79,6 +83,13 @@ public class SnoozeActivity extends AppCompatActivity implements LogListener,
startService(i);
}
public void onClickCopyToClipboardButton(View v) {
ClipboardManager cp = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String text = outputTextView.getText().toString();
cp.setPrimaryClip(ClipData.newPlainText(getString(R.string.app_name), text));
Toast.makeText(this, R.string.copied_to_clipboard, LENGTH_SHORT).show();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
alarmType = AlarmType.values()[position];
......
......@@ -33,6 +33,7 @@ public class SnoozeService extends Service {
private boolean foreground = false;
private PowerManager.WakeLock wakeLock = null;
private WifiManager.WifiLock wifiLock = null;
private NetworkThread networkThread = null;
private Alarm alarm = null;
private volatile Logger log = null;
......@@ -64,12 +65,14 @@ public class SnoozeService extends Service {
else if (!newWakeLock && wakeLock != null) releaseWakeLock();
if (newWifiLock && wifiLock == null) acquireWifiLock();
else if (!newWifiLock && wifiLock != null) releaseWifiLock();
startNetworkThread();
setAlarm(newAlarmType);
} else if (ACTION_ALARM.equals(i.getAction())) {
AlarmType alarmType = AlarmType.valueOf(i.getStringExtra(EXTRA_ALARM_TYPE));
log.log("Alarm " + alarmType);
if (alarm == null) {
log.log("Service recreated by alarm");
startNetworkThread();
setAlarm(alarmType);
} else {
setAlarm();
......@@ -148,6 +151,14 @@ public class SnoozeService extends Service {
wifiLock = null;
}
private void startNetworkThread() {
if (networkThread != null) networkThread.interrupt();
String host = getString(R.string.host);
int port = getResources().getInteger(R.integer.port);
networkThread = new NetworkThread(host, port, getApplicationContext());
networkThread.start();
}
private void setAlarm(AlarmType alarmType) {
if (alarm != null) alarm.cancelAlarm();
if (alarmType != STOP) {
......
......@@ -15,7 +15,7 @@
android:text="@string/use_foreground_service"/>
<Switch
android:id="@+id/permanentWakeLockSwitch"
android:id="@+id/wakeLockSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/use_permanent_wake_lock"/>
......@@ -33,16 +33,17 @@
android:padding="8dp"/>
<Button
android:id="@+id/startServiceButton"
android:id="@+id/applySettingsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/apply_settings"
android:onClick="onClickStartServiceButton"/>
android:onClick="onClickApplySettingsButton"/>
<ScrollView
android:id="@+id/outputScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="8dp">
<TextView
......@@ -52,4 +53,11 @@
</ScrollView>
<Button
android:id="@+id/copyToClipboardButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_to_clipboard"
android:onClick="onClickCopyToClipboardButton"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="host">192.168.0.7</string>
<integer name="port">10101</integer>
</resources>
\ No newline at end of file
......@@ -6,6 +6,8 @@
<string name="use_foreground_service">Foreground service</string>
<string name="apply_settings">Apply settings</string>
<string name="use_wifi_lock">Wi-Fi lock</string>
<string name="copy_to_clipboard">Copy to clipboard</string>
<string name="copied_to_clipboard">Copied to clipboard</string>
<string-array name="alarm_types">
<item>No alarm</item>
<item>Java Timer</item>
......
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
package org.briarproject.snooze;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SnoozeServer implements Runnable {
private final int port, writeIntervalMs;
public SnoozeServer(int port, int writeIntervalMs) {
this.port = port;
this.writeIntervalMs = writeIntervalMs;
}
public static void main(String[] args) {
if (args.length != 2) printUsageAndExit();
try {
int port = Integer.parseInt(args[0]);
if (port < 1 || port > 65535) printUsageAndExit();
int writeIntervalMs = Integer.parseInt(args[1]);
if (writeIntervalMs < 1) printUsageAndExit();
new SnoozeServer(port, writeIntervalMs).run();
} catch (NumberFormatException e) {
printUsageAndExit();
}
}
private static void printUsageAndExit() {
System.err.println("Usage: SnoozeServer <port> <write_interval_ms>");
System.exit(1);
}
@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Listening: " + ss.getLocalSocketAddress());
while (true) new WriterThread(ss.accept(), writeIntervalMs).start();
} catch (IOException e) {
System.err.println(e.toString());
}
}
private static class WriterThread extends Thread {
private final Socket socket;
private final int writeIntervalMs;
private WriterThread(Socket socket, int writeIntervalMs) {
this.socket = socket;
this.writeIntervalMs = writeIntervalMs;
setDaemon(true);
}
@Override
public void run() {
try {
OutputStream out = socket.getOutputStream();
while (!socket.isClosed()) {
System.out.println("Writing: " + socket.getRemoteSocketAddress());
out.write(0);
sleep(writeIntervalMs);
}
System.out.println("Socket closed: " + socket.getRemoteSocketAddress());
} catch (IOException | InterruptedException e) {
System.err.println(e.toString());
}
}
}
}
include ':app'
include ':app', ':server'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment