-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatActivity.java
More file actions
423 lines (351 loc) · 15.8 KB
/
Copy pathChatActivity.java
File metadata and controls
423 lines (351 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package com.codewithharry.firebase;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.codewithharry.firebase.adapters.AdapterChat;
import com.codewithharry.firebase.models.ModelChat;
import com.codewithharry.firebase.models.ModelUser;
import com.codewithharry.firebase.notifications.APIService;
import com.codewithharry.firebase.notifications.Client;
import com.codewithharry.firebase.notifications.Data;
import com.codewithharry.firebase.notifications.Response;
import com.codewithharry.firebase.notifications.Sender;
import com.codewithharry.firebase.notifications.Token;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
public class ChatActivity extends AppCompatActivity {
//views from xml
Toolbar toolbar;
RecyclerView recyclerView;
ImageView profileIv;
TextView nameTv, userStatusTv, isSeenTv;
EditText messageEt;
ImageButton sendBtn;
//Firebase Auth
FirebaseAuth firebaseAuth;
FirebaseDatabase firebaseDatabase;
DatabaseReference usersDbRef;
//for checking if user has seen message or not
ValueEventListener seenListener;
DatabaseReference userRefForSeen;
ArrayList<ModelChat> chatList;
AdapterChat adapterChat;
String hisUid;
String myUid;
String hisImage;
APIService apiService;
boolean notify = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//init views
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("");
recyclerView = findViewById(R.id.chat_recyclerView);
profileIv = findViewById(R.id.profileIv);
nameTv = findViewById(R.id.nameTv);
userStatusTv = findViewById(R.id.userStatusTv);
messageEt = findViewById(R.id.messageEt);
sendBtn = findViewById(R.id.sendBtn);
isSeenTv = findViewById(R.id.isSeenTv);
//Layout LinearLayout for RecyclerView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
//recyclerview properties
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
//create API service
apiService = Client.getRetrofit("https://fcm.googleapis.com/").create(APIService.class);
/*On clicking user from list we have passed that user's UID using intent
* So get that UID here to get the profile picture, name and start and chat with that user */
hisUid = getIntent().getStringExtra("hisUID");
//firebase auth instance
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
usersDbRef = firebaseDatabase.getReference("Users");
//search user's to get user info
Query userQuery = usersDbRef.orderByChild("uid").equalTo(hisUid);
//get user's picture and name
userQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
//get data
String name = "" + ds.child("name").getValue();
hisImage = "" + ds.child("image").getValue();
String typingStatus = "" + ds.child("typingTo").getValue();
//get value of online status
//check typing status
if (typingStatus.equals(myUid)) {
userStatusTv.setText("Typing...");
} else {
String onlineStatus = "" + ds.child("onlineStatus").getValue();
if (onlineStatus.equals("Online")) {
userStatusTv.setText(onlineStatus);
} else {
//convert timestamp to proper time and date
//convert time stamp to dd/mm/yyyy format hh:mm am/pm
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(Long.parseLong(onlineStatus));
String dateTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();
userStatusTv.setText("Last seen at " + dateTime);
}
}
//set data
nameTv.setText(name);
try {
//image received, set it to imageview in toolbar
Picasso.get().load(hisImage).placeholder(R.drawable.ic_default_image_white).into(profileIv);
} catch (Exception e) {
//there is an exception in getting pictures, set default picture
Picasso.get().load(R.drawable.ic_default_img_white).into(profileIv);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//click button to send message
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notify = true;
//get text from edit text
String message = messageEt.getText().toString().trim();
//check if text is empty or not
if (TextUtils.isEmpty(message)) {
//text empty
Toast.makeText(ChatActivity.this, "Cannot send an empty message..", Toast.LENGTH_SHORT).show();
} else {
//text not empty
sendMessage(message);
}
//reset edit text after sending message
messageEt.setText("");
}
});
//check edit text change listener
messageEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().length() == 0) {
checkTypingStatus("noOne");
} else {
checkTypingStatus(hisUid);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
readMessages();
seenMessage();
}
private void seenMessage() {
userRefForSeen = FirebaseDatabase.getInstance().getReference("Chats");
seenListener = userRefForSeen.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ModelChat chat = ds.getValue(ModelChat.class);
if (chat.getReceiver().equals(myUid) && chat.getSender().equals(hisUid)) {
HashMap<String, Object> hasSeenHashMap = new HashMap<>();
hasSeenHashMap.put("isSeen", "true");
ds.getRef().updateChildren(hasSeenHashMap);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readMessages() {
chatList = new ArrayList<>();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Chats");
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
chatList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ModelChat chat = ds.getValue(ModelChat.class);
if (chat.getReceiver().equals(myUid) && chat.getSender().equals(hisUid) ||
chat.getReceiver().equals(hisUid) && chat.getSender().equals(myUid)) {
chatList.add(chat);
}
//adapter
adapterChat = new AdapterChat(ChatActivity.this, chatList, hisImage);
//set adapter to recycler view
recyclerView.setAdapter(adapterChat);
adapterChat.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void sendMessage(String message) {
/*"chats" node will be created that will contain all chats
* Whenever user sends message, it will create new child in "Chats" node and that child will contain
* the following key values
* Sender : UID of sender
* Receiver : UID of receiver
* message : the actual message*/
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
String timestamp = String.valueOf(System.currentTimeMillis());
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("sender", myUid);
hashMap.put("receiver", hisUid);
hashMap.put("message", message);
hashMap.put("timestamp", timestamp);
hashMap.put("isSeen", "false");
//Toast.makeText(this, "" + hashMap, Toast.LENGTH_LONG).show();
databaseReference.child("Chats").push().setValue(hashMap);
String msg = message;
DatabaseReference database = FirebaseDatabase.getInstance().getReference("Users").child(myUid);
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ModelUser user = dataSnapshot.getValue(ModelUser.class);
if (notify) {
sendNotification(hisUid, user.getName(), message);
}
notify = false;
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void sendNotification(final String hisUid, final String name, final String message) {
DatabaseReference allTokens = FirebaseDatabase.getInstance().getReference("Tokens");
Query query = allTokens.orderByKey().equalTo(hisUid);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Token token = ds.getValue(Token.class);
Data data = new Data(myUid, name + " : " + message, "New Message", hisUid, R.drawable.ic_default_image_blue);
Sender sender = new Sender(data, token.getToken());
apiService.sendNotification(sender)
.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
Toast.makeText(ChatActivity.this, "" + response.message(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void checkUserStatus() {
//Get current user
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
//User is signed in stay here
//set email of logged in user
//mProfileTv.setText(user.getEmail());
myUid = user.getUid(); //currently signed in user
} else {
//User not signed in go to main activity
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
private void checkOnlineStatus(String status) {
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Users").child(myUid);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("onlineStatus", status);
//update value of online status of current user
dbRef.updateChildren(hashMap);
}
private void checkTypingStatus(String typing) {
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Users").child(myUid);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("typingTo", typing);
//update value of online status of current user
dbRef.updateChildren(hashMap);
}
@Override
protected void onStart() {
checkUserStatus();
//set Online
checkOnlineStatus("Online");
super.onStart();
}
@Override
protected void onPause() {
super.onPause();
//get timestamp
String timestamp = String.valueOf(System.currentTimeMillis());
//set offline with last seem time stamp
checkOnlineStatus(timestamp);
checkTypingStatus("noOne");
userRefForSeen.removeEventListener(seenListener);
}
@Override
protected void onResume() {
//set online
checkOnlineStatus("Online");
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
//hide search view, add-posts, as we don't need that here
menu.findItem(R.id.action_search).setVisible(false);
menu.findItem(R.id.action_add_post).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
firebaseAuth.signOut();
checkUserStatus();
}
return super.onOptionsItemSelected(item);
}
}