-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Missing files (SocketCommunicator)
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Built application files | ||
/app/* | ||
!/app/src | ||
/build/* | ||
|
||
# Crashlytics configuations | ||
|
24 changes: 24 additions & 0 deletions
24
Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/Settings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/SocketCommunicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |