Skip to content

Commit

Permalink
Added Missing files (SocketCommunicator)
Browse files Browse the repository at this point in the history
  • Loading branch information
km2442 committed May 12, 2019
1 parent 996f10c commit 31a94cd
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions Android/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Built application files
/app/*
!/app/src
/build/*

# Crashlytics configuations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.kamilklecha.autoswitchmobile;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class Settings extends AppCompatActivity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
b = findViewById(R.id.Settings_btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.this.finish();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package dev.kamilklecha.autoswitchmobile;

import android.os.Handler;
import android.os.NetworkOnMainThreadException;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketCommunicator {

private static final String TAG = "SocketCommunicator";
Socket s;

private TextView tv;

private OutputStream out;
private PrintWriter output;
final Handler handler = new Handler();

public SocketCommunicator(TextView textView, final String SERVER_IP, final int SERVERPORT) {
tv = textView;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
s = new Socket(SERVER_IP, SERVERPORT);
out = s.getOutputStream();
output = new PrintWriter(out);

startReceiver();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}

public void sendMessage(final String msg) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
output.println(msg);
output.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}

public void startReceiver() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
boolean exception = false;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (!exception) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
final String st = input.readLine();

handler.post(new Runnable() {
@Override
public void run() {

if (st.trim().length() != 0)
tv.setText("From Server: " + st);
}
});
} catch (IOException e) {
e.printStackTrace();
exception = true;
} catch (NetworkOnMainThreadException e) {
Log.e(TAG, "startReceiver: NetworkOnMainThreadException");
e.printStackTrace();
exception = true;
} catch (Exception e) {
e.printStackTrace();
exception = true;
}
}
}
});
thread.start();
}
}
22 changes: 22 additions & 0 deletions Android/app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Settings" >

<Button
android:id="@+id/Settings_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Wróć"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

0 comments on commit 31a94cd

Please sign in to comment.