Skip to content

Commit 5b016e6

Browse files
committed
Added username support
1 parent 441a2f0 commit 5b016e6

File tree

7 files changed

+86
-35
lines changed

7 files changed

+86
-35
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ dependencies {
5656
implementation 'com.github.kenglxn.QRGen:android:2.6.0'
5757

5858
implementation 'com.github.QuadFlask:colorpicker:0.0.13'
59+
60+
implementation 'com.google.code.gson:gson:2.8.6'
5961
}

app/src/main/java/com/example/chatfull/ChatActivity.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.io.FileOutputStream;
4444
import java.io.IOException;
4545
import java.io.InputStream;
46+
import java.util.ArrayList;
4647
import java.util.Calendar;
4748

4849
public class ChatActivity extends AppCompatActivity
@@ -59,25 +60,25 @@ public class ChatActivity extends AppCompatActivity
5960

6061
MessagesList messagesList;
6162
protected final String senderId = "1";
62-
private final User me = new User("1", "SELF"); // Assign Self Username
63+
6364
MessagesListAdapter<Message> adapter;
6465
int cnt = 0;
6566

6667
Button btnSend;
67-
ImageButton btnAttachement, btnImage;
68+
ImageButton btnAttachment, btnImage;
6869
EditText input;
6970

7071
RelativeLayout back_view;
7172
int[] colors;
7273

74+
ArrayList<Message> messageArrayList;
75+
7376
@Override
7477
protected void onCreate(Bundle savedInstanceState) {
7578
super.onCreate(savedInstanceState);
7679
setContentView(R.layout.activity_chat_alternate);
7780

7881
user = (User) getIntent().getSerializableExtra("user");
79-
user.setId("0");
80-
user.setName("ABAL"); // Assign user username
8182

8283
messageReceiveServer = new MessageReceiveServer(ShowInfoActivity.getSelfIpAddress(), ShowInfoActivity.getSelfPort(), this);
8384

@@ -116,11 +117,12 @@ public void loadImage(ImageView imageView, @Nullable String url, @Nullable Objec
116117

117118
input = findViewById(R.id.et_message);
118119
btnSend = findViewById(R.id.bt_send);
119-
btnAttachement = findViewById(R.id.bt_attachment);
120+
btnAttachment = findViewById(R.id.bt_attachment);
120121
btnImage = findViewById(R.id.bt_image);
121122

122123
//Initialize color picker
123124
back_view = findViewById(R.id.background_view);
125+
124126
TypedArray ta = getApplicationContext().getResources().obtainTypedArray(R.array.colors);
125127
colors = new int[ta.length()];
126128
for (int i = 0; i < ta.length(); i++) {
@@ -165,13 +167,13 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
165167
.setOnColorSelectedListener(new OnColorSelectedListener() {
166168
@Override
167169
public void onColorSelected(int selectedColor) {
168-
Toast.makeText(getApplicationContext(),"onColorSelected: 0x" + Integer.toHexString(selectedColor),Toast.LENGTH_SHORT).show();
170+
Toast.makeText(getApplicationContext(), "onColorSelected: 0x" + Integer.toHexString(selectedColor), Toast.LENGTH_SHORT).show();
169171
}
170172
})
171173
.setPositiveButton("ok", new ColorPickerClickListener() {
172174
@Override
173175
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
174-
Message message = new Message(Integer.toString(++cnt), me, null, Calendar.getInstance().getTime());
176+
Message message = new Message(Integer.toString(++cnt), MainActivity.me, null, Calendar.getInstance().getTime());
175177
message.setColor(selectedColor);
176178
message.setIsColor(true);
177179

@@ -195,7 +197,7 @@ public void onClick(DialogInterface dialog, int which) {
195197
public void onBtnSendClick(View view) {
196198
if (input.getText().toString() == null) return;
197199

198-
Message message = new Message(Integer.toString(++cnt), me, input.getText().toString(), Calendar.getInstance().getTime());
200+
Message message = new Message(Integer.toString(++cnt), MainActivity.me, input.getText().toString(), Calendar.getInstance().getTime());
199201
message.setIsImage(false);
200202
message.setFilename(null);
201203
adapter.addToStart(message, true);
@@ -228,7 +230,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
228230
if (requestCode == PICK_FILE_REQUEST && data != null) {
229231
if (resultCode == RESULT_OK) {
230232
Uri file = data.getData();
231-
Message message = new Message(Integer.toString(++cnt), me, null, Calendar.getInstance().getTime());
233+
Message message = new Message(Integer.toString(++cnt), MainActivity.me, null, Calendar.getInstance().getTime());
232234
message.setFilename(getFileName(file));
233235
try {
234236
message.setFile(getBytes(this, file));
@@ -246,7 +248,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
246248
} else if (requestCode == PICK_IMAGE_REQUEST && data != null) {
247249
if (resultCode == RESULT_OK) {
248250
Uri file = data.getData();
249-
Message message = new Message(Integer.toString(++cnt), me, null, Calendar.getInstance().getTime());
251+
Message message = new Message(Integer.toString(++cnt), MainActivity.me, null, Calendar.getInstance().getTime());
250252
message.setFilename(getFileName(file));
251253
try {
252254
message.setFile(getBytes(this, file));
@@ -382,7 +384,7 @@ protected void onDestroy() {
382384
@Override
383385
public void onBackPressed() {
384386
Log.e("CHAT_ACTIVITY", "PAUSE");
385-
Message message = new Message(Integer.toString(++cnt), me, null);
387+
Message message = new Message(Integer.toString(++cnt), MainActivity.me, null);
386388
message.setOffline(true);
387389
sender = new SendMessage(user.getIpAddress(), user.getPort(), message, this);
388390
sender.execute();

app/src/main/java/com/example/chatfull/Client.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,26 @@ protected String doInBackground(Void... arg0) {
3333
if (clientSocket != null) {
3434

3535
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
36-
out.println(ShowInfoActivity.getSelfIpAddress() + ":" + ShowInfoActivity.getSelfPort());
36+
out.println(ShowInfoActivity.getSelfIpAddress() + ":" + ShowInfoActivity.getSelfPort() + "_" + MainActivity.me.getName());
3737

3838
Log.e("CLIENT", "After Connection");
3939
user = new User(dstAddress, dstPort);
40-
activity.setUser(user);
40+
4141
//MainActivity.userArrayList.add(user);
4242
}
4343

4444
try {
4545
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
4646
serverResponse = input.readLine();
47+
48+
String connected_name = serverResponse.substring(serverResponse.indexOf('_')+1);
49+
user.setName(connected_name);
50+
user.setId(serverResponse);
4751
} catch (IOException e) {
4852
e.printStackTrace();
4953
Log.e("CLIENT", "Could not read socket");
5054
}
51-
55+
activity.setUser(user);
5256
} catch (Exception e) {
5357
e.printStackTrace();
5458
serverResponse = e.getCause().toString();

app/src/main/java/com/example/chatfull/MainActivity.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
package com.example.chatfull;
22

3-
import androidx.appcompat.app.AppCompatActivity;
4-
53
import android.content.Intent;
64
import android.os.Bundle;
75
import android.view.View;
86
import android.widget.Button;
7+
import android.widget.EditText;
8+
9+
import androidx.appcompat.app.AppCompatActivity;
910

1011
public class MainActivity extends AppCompatActivity {
1112

1213
Button showInfoBtn, enterInfoBtn;
13-
14+
EditText nameInput;
15+
static User me; // Assign Self Username
1416
@Override
1517
protected void onCreate(Bundle savedInstanceState) {
1618
super.onCreate(savedInstanceState);
1719
setContentView(R.layout.activity_main);
1820

1921
showInfoBtn = findViewById(R.id.showInfo);
2022
enterInfoBtn = findViewById(R.id.enterInfo);
23+
nameInput = findViewById(R.id.nameInput);
24+
2125

2226
showInfoBtn.setOnClickListener(new View.OnClickListener() {
2327
@Override
2428
public void onClick(View view) {
29+
me = new User("1", nameInput.getText().toString());
2530
Intent intent = new Intent(getApplicationContext(), ShowInfoActivity.class);
2631
startActivity(intent);
2732
}
@@ -30,6 +35,7 @@ public void onClick(View view) {
3035
enterInfoBtn.setOnClickListener(new View.OnClickListener() {
3136
@Override
3237
public void onClick(View view) {
38+
me = new User("1", nameInput.getText().toString());
3339
Intent intent = new Intent(getApplicationContext(), ConnectToUserActivity.class);
3440
startActivity(intent);
3541
}

app/src/main/java/com/example/chatfull/Message.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public Message(String id, User user, String text, Date createdAt) {
3939
public boolean isOffline() {
4040
return offline;
4141
}
42-
4342
public void setOffline(boolean offline) {
4443
this.offline = offline;
4544
}
@@ -50,7 +49,6 @@ public void setOffline(boolean offline) {
5049
public boolean isFile() {
5150
return isFile;
5251
}
53-
5452
public void setIsFile(boolean file) {
5553
isFile = file;
5654
}

app/src/main/java/com/example/chatfull/Server.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ public void run() {
4848
continue;
4949

5050
String client_ip = client_cred.substring(0, client_cred.indexOf(':'));
51-
String client_port = client_cred.substring(client_cred.indexOf(':') + 1);
51+
String client_port = client_cred.substring(client_cred.indexOf(':') + 1, client_cred.indexOf('_'));
52+
String client_name = client_cred.substring(client_cred.indexOf('_')+1);
5253

5354
user = new User(client_ip, Integer.parseInt(client_port));
55+
user.setName(client_name);
56+
user.setId(client_cred);
5457
//MainActivity.userArrayList.add(user);
5558

56-
//---Testing Connection with response. Optional---
59+
//---Sending self name---
5760
try {
58-
String response_message = self_ip_address + ":" + self_port;
61+
String response_message = self_ip_address + ":" + self_port + "_" + MainActivity.me.getName();
5962
PrintWriter out = new PrintWriter(received_userSocket.getOutputStream(), true);
6063
out.println(response_message);
6164

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
@@ -8,26 +8,62 @@
88
android:gravity="center"
99
android:orientation="vertical">
1010

11-
1211
<LinearLayout
12+
android:id="@+id/linearLayout"
1313
android:layout_width="match_parent"
1414
android:layout_height="wrap_content"
15-
android:orientation="vertical"
16-
android:gravity="center">
15+
android:layout_marginStart="8dp"
16+
android:layout_marginTop="8dp"
17+
android:layout_marginEnd="8dp"
18+
android:layout_marginBottom="8dp"
19+
app:layout_constraintBottom_toTopOf="@+id/showInfo"
20+
app:layout_constraintEnd_toEndOf="parent"
21+
app:layout_constraintHorizontal_bias="0.0"
22+
app:layout_constraintStart_toStartOf="parent"
23+
app:layout_constraintTop_toTopOf="parent"
24+
app:layout_constraintVertical_bias="0.613">
1725

18-
<Button
19-
android:id="@+id/showInfo"
26+
<TextView
27+
android:id="@+id/textView"
2028
android:layout_width="wrap_content"
2129
android:layout_height="wrap_content"
22-
android:text="Show information"/>
30+
android:text="Name : "
31+
android:textSize="20sp"
32+
android:textStyle="bold"
33+
android:layout_marginLeft="30dp"/>
2334

24-
<Button
25-
android:id="@+id/enterInfo"
26-
android:layout_width="wrap_content"
35+
<EditText
36+
android:id="@+id/nameInput"
37+
android:layout_width="match_parent"
2738
android:layout_height="wrap_content"
28-
android:text="Enter Ip : port"/>
29-
39+
android:layout_marginLeft="20dp"
40+
android:layout_marginRight="20dp"
41+
android:paddingLeft="10dp" />
3042
</LinearLayout>
3143

44+
<Button
45+
android:id="@+id/showInfo"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:layout_marginStart="8dp"
49+
android:layout_marginEnd="8dp"
50+
android:layout_marginBottom="16dp"
51+
android:text="Show information"
52+
app:layout_constraintBottom_toTopOf="@+id/enterInfo"
53+
app:layout_constraintEnd_toEndOf="parent"
54+
app:layout_constraintStart_toStartOf="parent" />
55+
56+
<Button
57+
android:id="@+id/enterInfo"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:layout_marginStart="8dp"
61+
android:layout_marginEnd="8dp"
62+
android:layout_marginBottom="332dp"
63+
android:text="Enter Ip : port"
64+
app:layout_constraintBottom_toBottomOf="parent"
65+
app:layout_constraintEnd_toEndOf="parent"
66+
app:layout_constraintStart_toStartOf="parent" />
67+
3268

33-
</LinearLayout>
69+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)