|
@@ -8,10 +8,15 @@ import android.os.Bundle;
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
|
|
import android.os.Handler;
|
|
|
+import android.text.Editable;
|
|
|
+import android.text.TextWatcher;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
+import android.widget.ArrayAdapter;
|
|
|
+import android.widget.EditText;
|
|
|
import android.widget.LinearLayout;
|
|
|
+import android.widget.ScrollView;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import org.jivesoftware.smack.chat2.Chat;
|
|
@@ -22,10 +27,14 @@ import org.jivesoftware.smack.roster.RosterListener;
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
+import org.minidns.record.A;
|
|
|
+import org.pgpainless.key.selection.keyring.impl.XMPP;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
import java.util.Timer;
|
|
|
import java.util.TimerTask;
|
|
|
|
|
@@ -38,6 +47,9 @@ class FishNameComparator implements Comparator<ChatItem>
|
|
|
public class BlankFragment extends Fragment {
|
|
|
|
|
|
LinearLayout linearLayout;
|
|
|
+ private EditText searchET;
|
|
|
+ private ArrayList<ChatItem> ALLUserChats = new ArrayList<>();
|
|
|
+ ArrayList<ChatItem> sortUsers = new ArrayList<>();
|
|
|
final Handler handler = new Handler();
|
|
|
Timer timer = new Timer();
|
|
|
TimerTask timerTask = new TimerTask() {
|
|
@@ -59,138 +71,214 @@ public class BlankFragment extends Fragment {
|
|
|
@Override
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
Bundle savedInstanceState) {
|
|
|
- view = inflater.inflate(R.layout.fragment_blank, container, false);
|
|
|
- init();
|
|
|
+ try {
|
|
|
+ view = inflater.inflate(R.layout.fragment_blank, container, false);
|
|
|
+ init();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
return view;
|
|
|
|
|
|
}
|
|
|
public void init(){
|
|
|
- linearLayout = (LinearLayout) view.findViewById(R.id.chatsListLayout);
|
|
|
- XMPPClass.GenerateChatList();
|
|
|
- generateChatsList();
|
|
|
- runMessageListener();
|
|
|
+
|
|
|
+ try {
|
|
|
+ linearLayout = (LinearLayout) view.findViewById(R.id.chatsListLayout);
|
|
|
+ searchET = (EditText) view.findViewById(R.id.search_edit_text);
|
|
|
+ ScrollView scrollView = (ScrollView) view.findViewById(R.id.chatsListScroll);
|
|
|
+ XMPPClass.GenerateChatList();
|
|
|
+
|
|
|
+ generateChatsList();
|
|
|
+ ALLUserChats.addAll(XMPPClass.chatItems);
|
|
|
+ runMessageListener();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
public void generateChatsList(){
|
|
|
- linearLayout.removeAllViews();
|
|
|
- Collections.sort(XMPPClass.chatItems, new FishNameComparator());
|
|
|
- for (ChatItem chatItem : XMPPClass.chatItems){
|
|
|
-
|
|
|
- TextView Name = new TextView(view.getContext());
|
|
|
- TextView Status = new TextView(view.getContext());
|
|
|
- TextView NewMessage = new TextView(view.getContext());
|
|
|
- LinearLayout Container = new LinearLayout(view.getContext());
|
|
|
-
|
|
|
- Name.setTextColor(Color.parseColor("#000000"));
|
|
|
- Name.setTextSize(16);
|
|
|
-
|
|
|
- if (chatItem.name != null){
|
|
|
- Name.setText(chatItem.name);
|
|
|
- }else {
|
|
|
- Name.setText(chatItem.jid);
|
|
|
- }
|
|
|
- if(chatItem.message != null){
|
|
|
- NewMessage.setText("New message: " + chatItem.message);
|
|
|
- NewMessage.setTextColor(Color.parseColor("#0000ff"));
|
|
|
- }
|
|
|
- else {
|
|
|
- NewMessage.setText("No new message");
|
|
|
- }
|
|
|
+ try {
|
|
|
+ if (!XMPPClass.chatItems.equals(ALLUserChats)){
|
|
|
+ linearLayout.removeAllViews();
|
|
|
+ Collections.sort(XMPPClass.chatItems, new FishNameComparator());
|
|
|
+ for (ChatItem chatItem : XMPPClass.chatItems){
|
|
|
|
|
|
- if (chatItem.status.equals("Offline")){
|
|
|
- Status.setTextColor(Color.parseColor("#a51b0b"));
|
|
|
- }
|
|
|
- else {
|
|
|
- Status.setTextColor(Color.parseColor("#006000"));
|
|
|
- }
|
|
|
- Status.setText(chatItem.status);
|
|
|
- Status.setTextSize(12);
|
|
|
-
|
|
|
- Container.setOrientation(LinearLayout.VERTICAL);
|
|
|
-
|
|
|
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
|
|
|
- LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
|
-
|
|
|
- layoutParams.setMargins(55, 15, 55, 15);
|
|
|
- layoutParams.height = 195;
|
|
|
- Container.setLayoutParams(layoutParams);
|
|
|
- GradientDrawable border = new GradientDrawable();
|
|
|
- border.setColor(Color.parseColor("#ffffff"));
|
|
|
- border.setStroke(5, Color.parseColor("#0081FF"));
|
|
|
- Container.setBackgroundDrawable(border);
|
|
|
- LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
|
|
|
- LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
|
-
|
|
|
- textParams.setMargins(20, 4, 20, 9);
|
|
|
- Name.setLayoutParams(textParams);
|
|
|
- Status.setLayoutParams(textParams);
|
|
|
- NewMessage.setLayoutParams(textParams);
|
|
|
- //Container.setBackgroundColor(Color.parseColor("#C0C0C0"));
|
|
|
- Container.addView(Name);
|
|
|
- Container.addView(Status);
|
|
|
- Container.addView(NewMessage);
|
|
|
- Container.setOnClickListener(new View.OnClickListener() {
|
|
|
- @Override
|
|
|
- public void onClick(View view) {
|
|
|
- try {
|
|
|
- XMPPClass.chatJid = JidCreate.entityBareFrom(chatItem.jid);
|
|
|
- chatItem.message = null;
|
|
|
- Intent intent = new Intent(view.getContext(), ChatActivity.class);
|
|
|
- startActivity(intent);
|
|
|
- }catch (Exception ex){
|
|
|
+ TextView Name = new TextView(view.getContext());
|
|
|
+ TextView Status = new TextView(view.getContext());
|
|
|
+ TextView NewMessage = new TextView(view.getContext());
|
|
|
+ LinearLayout Container = new LinearLayout(view.getContext());
|
|
|
+
|
|
|
+ Name.setTextColor(Color.parseColor("#000000"));
|
|
|
+ Name.setTextSize(16);
|
|
|
|
|
|
+ if (chatItem.name != null){
|
|
|
+ Name.setText(chatItem.name);
|
|
|
+ }else {
|
|
|
+ Name.setText(chatItem.jid);
|
|
|
+ }
|
|
|
+ if(chatItem.message != null){
|
|
|
+ NewMessage.setText("New message: " + chatItem.message);
|
|
|
+ NewMessage.setTextColor(Color.parseColor("#0000ff"));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NewMessage.setText("No new message");
|
|
|
}
|
|
|
|
|
|
+ if (chatItem.status.equals("Offline")){
|
|
|
+ Status.setTextColor(Color.parseColor("#a51b0b"));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Status.setTextColor(Color.parseColor("#006000"));
|
|
|
+ }
|
|
|
+ Status.setText(chatItem.status);
|
|
|
+ Status.setTextSize(12);
|
|
|
+
|
|
|
+ Container.setOrientation(LinearLayout.VERTICAL);
|
|
|
+
|
|
|
+ LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
|
|
|
+ LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
|
+
|
|
|
+ layoutParams.setMargins(55, 15, 55, 15);
|
|
|
+ layoutParams.height = 195;
|
|
|
+ Container.setLayoutParams(layoutParams);
|
|
|
+ GradientDrawable border = new GradientDrawable();
|
|
|
+ border.setColor(Color.parseColor("#ffffff"));
|
|
|
+ border.setStroke(5, Color.parseColor("#0081FF"));
|
|
|
+ Container.setBackgroundDrawable(border);
|
|
|
+ LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
|
|
|
+ LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
|
+
|
|
|
+ textParams.setMargins(20, 4, 20, 9);
|
|
|
+ Name.setLayoutParams(textParams);
|
|
|
+ Status.setLayoutParams(textParams);
|
|
|
+ NewMessage.setLayoutParams(textParams);
|
|
|
+ //Container.setBackgroundColor(Color.parseColor("#C0C0C0"));
|
|
|
+ Container.addView(Name);
|
|
|
+ Container.addView(Status);
|
|
|
+ Container.addView(NewMessage);
|
|
|
+ Container.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ try {
|
|
|
+ XMPPClass.chatJid = JidCreate.entityBareFrom(chatItem.jid);
|
|
|
+ chatItem.message = null;
|
|
|
+ Intent intent = new Intent(view.getContext(), ChatActivity.class);
|
|
|
+ startActivity(intent);
|
|
|
+ }catch (Exception ex){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ linearLayout.addView(Container);
|
|
|
}
|
|
|
- });
|
|
|
- linearLayout.addView(Container);
|
|
|
+ }
|
|
|
+ runTimerSchedule();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
}
|
|
|
- timer.schedule(timerTask, 0, 15000);
|
|
|
+
|
|
|
}
|
|
|
+ private void runTimerSchedule(){
|
|
|
+ try {
|
|
|
+ timer.schedule(timerTask, 0, 5000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
public void runMessageListener(){
|
|
|
- XMPPClass.chatManager.addIncomingListener(new IncomingChatMessageListener() {
|
|
|
- @Override
|
|
|
- public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
|
|
|
- System.out.println("New message from:" + from + ": " + message.getBody());
|
|
|
- for (ChatItem chatItem : XMPPClass.chatItems){
|
|
|
- if (chatItem.jid.trim().equals(from.asBareJid().toString().trim())){
|
|
|
+ try {
|
|
|
+ XMPPClass.chatManager.addIncomingListener(new IncomingChatMessageListener() {
|
|
|
+ @Override
|
|
|
+ public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
|
|
|
+ try {
|
|
|
+ System.out.println("New message from:" + from + ": " + message.getBody());
|
|
|
+ for (ChatItem chatItem : XMPPClass.chatItems){
|
|
|
+ if (chatItem.jid.trim().equals(from.asBareJid().toString().trim())){
|
|
|
|
|
|
- chatItem.message = message.getBody();
|
|
|
+ chatItem.message = message.getBody();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
- XMPPClass.roster.addRosterListener(new RosterListener() {
|
|
|
- @Override
|
|
|
- public void entriesAdded(Collection<Jid> addresses) {
|
|
|
- System.out.println(addresses);
|
|
|
- }
|
|
|
+ });
|
|
|
+ XMPPClass.roster.addRosterListener(new RosterListener() {
|
|
|
+ @Override
|
|
|
+ public void entriesAdded(Collection<Jid> addresses) {
|
|
|
+ System.out.println(addresses);
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void entriesUpdated(Collection<Jid> addresses) {
|
|
|
- System.out.println(addresses);
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void entriesUpdated(Collection<Jid> addresses) {
|
|
|
+ System.out.println(addresses);
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void entriesDeleted(Collection<Jid> addresses) {
|
|
|
- System.out.println(addresses);
|
|
|
- }
|
|
|
- @Override
|
|
|
- public void presenceChanged(Presence presence) {
|
|
|
- for (ChatItem chatItem : XMPPClass.chatItems){
|
|
|
- if (chatItem.jid.trim().equals(presence.getFrom().asBareJid().toString().trim())){
|
|
|
- if (presence.getType().toString().trim().equals("unavailable")){
|
|
|
- chatItem.status = "Offline";
|
|
|
+ @Override
|
|
|
+ public void entriesDeleted(Collection<Jid> addresses) {
|
|
|
+ System.out.println(addresses);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void presenceChanged(Presence presence) {
|
|
|
+ try {
|
|
|
+ for (ChatItem chatItem : XMPPClass.chatItems){
|
|
|
+ if (chatItem.jid.trim().equals(presence.getFrom().asBareJid().toString().trim())){
|
|
|
+ if (presence.getType().toString().trim().equals("unavailable")){
|
|
|
+ chatItem.status = "Offline";
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ chatItem.status = "Online";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ searchET.addTextChangedListener(new TextWatcher() {
|
|
|
+ @Override
|
|
|
+ public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
+ try {
|
|
|
+ sortUsers = new ArrayList<>();
|
|
|
+ System.out.println(charSequence.toString() + i2);
|
|
|
+ if (charSequence.length() == 0) {
|
|
|
+ XMPPClass.GenerateChatList();
|
|
|
+ sortUsers.clear();
|
|
|
}
|
|
|
else {
|
|
|
- chatItem.status = "Online";
|
|
|
+ for (ChatItem chatItem : ALLUserChats){
|
|
|
+ if (chatItem.jid.contains(charSequence)){
|
|
|
+ sortUsers.add(chatItem);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ XMPPClass.chatItems.clear();
|
|
|
+ XMPPClass.chatItems.addAll(sortUsers);
|
|
|
+ sortUsers.clear();
|
|
|
}
|
|
|
+ }catch (Exception ex){
|
|
|
+ System.out.println(ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterTextChanged(Editable editable) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|