Skip to content

Commit

Permalink
Moved Sockets to another class
Browse files Browse the repository at this point in the history
  • Loading branch information
km2442 committed May 8, 2019
1 parent f0a64e3 commit 2085678
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 65 deletions.
Binary file modified Android/.idea/caches/build_file_checksums.ser
Binary file not shown.
Binary file modified Android/.idea/caches/gradle_models.ser
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.kamilklecha.autoswitchmobile;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
Expand All @@ -17,30 +17,20 @@
import android.widget.EditText;
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 MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

Button btn;
private Socket socket;

private static final int SERVERPORT = 21;
private static final String SERVER_IP = "192.168.1.160";

private TextView mTextViewReplyFromServer;
private EditText mEditTextSendMessage;
private SocketCommunicator sc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
sc = new SocketCommunicator((TextView) findViewById(R.id.tv_reply_from_server));

setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
Expand All @@ -67,15 +57,24 @@ public void onClick(View v) {
};
btn.setOnClickListener(cl);

Button buttonSend = (Button) findViewById(R.id.btn_send);
final Button buttonSend = (Button) findViewById(R.id.btn_send);
Button buttonSetup = (Button) findViewById(R.id.btn_setup);

mEditTextSendMessage = (EditText) findViewById(R.id.edt_send_message);
mTextViewReplyFromServer = (TextView) findViewById(R.id.tv_reply_from_server);

buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(mEditTextSendMessage.getText().toString());
sc.sendMessage(mEditTextSendMessage.getText().toString());
}
});

buttonSetup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sc.sendMessage("Hi, " + Build.MODEL + " here");
sc.startReceiver();
buttonSend.setEnabled(true);
}
});
}
Expand Down Expand Up @@ -110,47 +109,4 @@ public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawer(GravityCompat.START);
return true;
}

private void sendMessage(final String msg) {

final Handler handler = new Handler();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {

try {
//Replace below IP with the IP of that device in which server socket open.
//If you change port then change the port number in the server side code also.
Socket s = new Socket(SERVER_IP, SERVERPORT);

OutputStream out = s.getOutputStream();

PrintWriter output = new PrintWriter(out);

output.println(msg);
output.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
final String st = input.readLine();

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

String s = mTextViewReplyFromServer.getText().toString();
if (st.trim().length() != 0)
mTextViewReplyFromServer.setText(s + "\nFrom Server : " + st);
}
});

output.close();
out.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});

thread.start();
}
}
25 changes: 20 additions & 5 deletions Android/app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Socket"
android:enabled="false"
android:text="Send Message"
app:layout_constraintBottom_toTopOf="@+id/Main_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_reply_from_server" />
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/edt_send_message"
Expand All @@ -74,12 +73,14 @@

<TextView
android:id="@+id/tv_reply_from_server"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/btn_setup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/edt_send_message" />
Expand All @@ -90,8 +91,22 @@
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="Result:"
app:layout_constraintBottom_toTopOf="@+id/btn_setup"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_send_message" />

<Button
android:id="@+id/btn_setup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Setup Socket"
app:layout_constraintBottom_toTopOf="@+id/btn_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

0 comments on commit 2085678

Please sign in to comment.