Skip to content

Commit

Permalink
Rewritten socket code
Browse files Browse the repository at this point in the history
  • Loading branch information
km2442 committed Jun 22, 2019
1 parent ff7b3a1 commit ac26f9f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
Binary file modified Android/.idea/caches/gradle_models.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ else if(CertainRGCustom.isChecked()) {
amount = Integer.parseInt(CertainCustomAmount.getText().toString());
}
catch (Exception e) {
Toast.makeText(AddCustomTask.this, "Custom time value cannot be empty!", Toast.LENGTH_LONG);
e.printStackTrace();
}
int index = CustomUnitspinner.getSelectedItemPosition();
switch(index) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kamilklecha.autoswitchmobile;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.NetworkOnMainThreadException;
import android.util.Log;
Expand All @@ -10,10 +9,12 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class SocketCommunicator implements Serializable {
public class SocketCommunicator {

private static final String TAG = "SocketCommunicator";
public Socket socket;
Expand All @@ -22,42 +23,30 @@ public class SocketCommunicator implements Serializable {
private PrintWriter output;
final Handler handler = new Handler();

ConnectWait cw;

public SocketCommunicator(final String SERVER_IP, final ConnectWait cw) {
this.cw = cw;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(SERVER_IP, 1234);
InetSocketAddress sockAdr = new InetSocketAddress(SERVER_IP, 1234);
int timeout = 10000;
socket = new Socket();
socket.connect(sockAdr, timeout);

out = socket.getOutputStream();
output = new PrintWriter(out);

startReceiver();

AsyncTask.execute(new Runnable() {
@Override
public void run() {
int i = 0;
if(socket.isConnected()) {
cw.connected();
} else {
if(i == 20) {
cw.finish();
}
i++;
try {
i++;
Thread.sleep(250);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});

} catch (IOException e) {
cw.connected();
}
catch(SocketTimeoutException e) {
//TODO info
Log.e(TAG, "SocketCommunicator: SocketTimeOutException");
e.printStackTrace();
} catch (Exception e) {
cw.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
Expand All @@ -81,7 +70,7 @@ public void run() {
}

public void startReceiver() {
final Thread thread = new Thread(new Runnable() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
boolean exception = false;
Expand All @@ -104,14 +93,17 @@ public void run() {
// tv.setText("From Server: " + st);
}
});
} catch (SocketException e) {
//TODO info
cw.finish();
} catch (IOException e) {
e.printStackTrace();
exception = true;
} catch (NetworkOnMainThreadException e) {
Log.e(TAG, "startReceiver: NetworkOnMainThreadException");
e.printStackTrace();
exception = true;
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
exception = true;
}
Expand Down
1 change: 1 addition & 0 deletions Android/app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:saveEnabled="false"
android:text="@string/home_next_screen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down

0 comments on commit ac26f9f

Please sign in to comment.