Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #30 from adroitandroid/master
Browse files Browse the repository at this point in the history
Removing dependency from activity, any context can be passed now which means even within services, Salut can be used
SalutDevice is Parcelable now, so discovered devices can be passed in bundles for use elsewhere
Serializer abstract method gives flexibility to use any serialization library now, dependency on LoganSquare removed
Dependency on Apache IOUtils removed, replaced with better performing native java API (check SO comment)
-> Salut.java hasn't been changed much apart from the abstract method for serialization, it shows the entire file changed since file encoding has been changed
  • Loading branch information
Mark Raymond Jr authored Jun 24, 2017
2 parents 969649d + 59475fc commit b3bc168
Show file tree
Hide file tree
Showing 9 changed files with 986 additions and 911 deletions.
14 changes: 5 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
targetSdkVersion 25
versionCode 3
versionName '0.3'
version 0.3
Expand All @@ -26,8 +25,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.android.tools.build:gradle:2.0.0-alpha9'
classpath 'com.android.tools.build:gradle:2.2.0'
}
}

Expand All @@ -37,9 +35,7 @@ repositories {
}

dependencies {
compile 'com.android.support:support-annotations:23.1.1'
compile 'com.android.support:support-annotations:25.3.1'
compile 'com.arasthel:asyncjob-library:1.0.3'
compile 'commons-io:commons-io:2.4'
apt 'com.bluelinelabs:logansquare-compiler:1.0.6'
compile 'com.bluelinelabs:logansquare:1.0.6'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.peak.salut;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.arasthel.asyncjob.AsyncJob;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void doOnBackground() {
Log.d(Salut.TAG, "Registered Host | " + salutInstance.registeredHost.deviceName);

salutInstance.thisDevice.isRegistered = true;
salutInstance.dataReceiver.activity.runOnUiThread(new Runnable() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (onRegistered != null)
Expand All @@ -82,9 +84,8 @@ public void run() {
salutInstance.closeDataSocket();
salutInstance.disconnectFromDevice();

if (onUnregisterSuccess != null) //Success Callback.
{
salutInstance.dataReceiver.activity.runOnUiThread(new Runnable() {
if (onUnregisterSuccess != null) { //Success Callback.
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
onUnregisterSuccess.call();
Expand All @@ -103,26 +104,24 @@ public void run() {
ex.printStackTrace();

Log.e(Salut.TAG, "An error occurred while attempting to register or unregister.");
salutInstance.dataReceiver.activity.runOnUiThread(new Runnable() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (onRegistrationFail != null && !salutInstance.thisDevice.isRegistered) //Prevents both callbacks from being called.
onRegistrationFail.call();
if (onUnregisterFailure != null)
onUnregisterFailure.call();

}
});


if (salutInstance.thisDevice.isRegistered && salutInstance.isConnectedToAnotherDevice) {
//Failed to unregister so an outright disconnect is necessary.
salutInstance.disconnectFromDevice();
}
} finally {

if (disableWiFiOnUnregister) {
Salut.disableWiFi(salutInstance.dataReceiver.activity);
Salut.disableWiFi(salutInstance.dataReceiver.context);
}
try {
registrationSocket.close();
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/com/peak/salut/BackgroundDataJob.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.peak.salut;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.arasthel.asyncjob.AsyncJob;

import org.apache.commons.io.IOUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.Socket;


Expand All @@ -29,18 +30,29 @@ public void doOnBackground() {
Log.v(Salut.TAG, "A device is sending data...");

BufferedInputStream dataStreamFromOtherDevice = new BufferedInputStream(clientSocket.getInputStream());
data = new String(IOUtils.toByteArray(dataStreamFromOtherDevice));

// http://stackoverflow.com/a/35446009/4411645
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataStreamFromOtherDevice.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
data = result.toString("UTF-8");

dataStreamFromOtherDevice.close();

Log.d(Salut.TAG, "\nSuccessfully received data.\n");

if (!data.isEmpty()) {
salutInstance.dataReceiver.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
salutInstance.dataReceiver.dataCallback.onDataReceived(data);
}
});
new Handler(Looper.getMainLooper()).post(
new Runnable() {
@Override
public void run() {
salutInstance.dataReceiver.dataCallback.onDataReceived(data);
}
});
}
} catch (Exception ex) {
Log.e(Salut.TAG, "An error occurred while trying to receive data.");
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/peak/salut/BackgroundDataSendJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import android.util.Log;

import com.arasthel.asyncjob.AsyncJob;
import com.bluelinelabs.logansquare.LoganSquare;
import com.peak.salut.Callbacks.SalutCallback;

import org.apache.commons.io.Charsets;

import java.io.BufferedOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;

public class BackgroundDataSendJob implements AsyncJob.OnBackgroundJob {

Expand Down Expand Up @@ -42,9 +40,9 @@ public void doOnBackground() {
Log.d(Salut.TAG, "Connected, transferring data...");
BufferedOutputStream dataStreamToOtherDevice = new BufferedOutputStream(dataSocket.getOutputStream());

String dataToSend = LoganSquare.serialize(data);
String dataToSend = salutInstance.serialize(data);

dataStreamToOtherDevice.write(dataToSend.getBytes(Charsets.UTF_8));
dataStreamToOtherDevice.write(dataToSend.getBytes(Charset.forName("UTF-8")));
dataStreamToOtherDevice.flush();
dataStreamToOtherDevice.close();

Expand Down
31 changes: 18 additions & 13 deletions src/main/java/com/peak/salut/BackgroundServerRegistrationJob.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.peak.salut;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.arasthel.asyncjob.AsyncJob;
Expand Down Expand Up @@ -50,12 +52,13 @@ public void doOnBackground() {
salutInstance.registeredClients.add(clientDevice);

if (salutInstance.onDeviceRegisteredWithHost != null) {
salutInstance.dataReceiver.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
salutInstance.onDeviceRegisteredWithHost.call(finalDevice);
}
});
new Handler(Looper.getMainLooper()).post(
new Runnable() {
@Override
public void run() {
salutInstance.onDeviceRegisteredWithHost.call(finalDevice);
}
});
}

} else {
Expand All @@ -66,15 +69,17 @@ public void run() {
toClient.flush();

for (final SalutDevice registered : salutInstance.registeredClients) {
if (registered.serviceAddress.equals(clientSocket.getInetAddress().toString().replace("/", ""))) {
if (registered.serviceAddress.equals(clientSocket.getInetAddress()
.toString().replace("/", ""))) {
salutInstance.registeredClients.remove(registered);
if (salutInstance.onDeviceUnregistered != null) {
salutInstance.dataReceiver.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
salutInstance.onDeviceUnregistered.call(registered);
}
});
new Handler(Looper.getMainLooper()).post(
new Runnable() {
@Override
public void run() {
salutInstance.onDeviceUnregistered.call(registered);
}
});
}
Log.d(Salut.TAG, "\nSuccesfully unregistered device.\n");
}
Expand Down
Loading

0 comments on commit b3bc168

Please sign in to comment.