diff --git a/Android/.gitignore b/Android/.gitignore index 39dab4f..1e5821a 100644 --- a/Android/.gitignore +++ b/Android/.gitignore @@ -1,5 +1,6 @@ # Built application files /app/* +!/app/src /build/* # Crashlytics configuations diff --git a/Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/Settings.java b/Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/Settings.java new file mode 100644 index 0000000..87577e8 --- /dev/null +++ b/Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/Settings.java @@ -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(); + } + }); + } +} diff --git a/Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/SocketCommunicator.java b/Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/SocketCommunicator.java new file mode 100644 index 0000000..ff9a39a --- /dev/null +++ b/Android/app/src/main/java/dev/kamilklecha/autoswitchmobile/SocketCommunicator.java @@ -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(); + } +} diff --git a/Android/app/src/main/res/layout/activity_settings.xml b/Android/app/src/main/res/layout/activity_settings.xml new file mode 100644 index 0000000..0f25c24 --- /dev/null +++ b/Android/app/src/main/res/layout/activity_settings.xml @@ -0,0 +1,22 @@ + + + +