Browse Source

add notifsound

Evgeny Polivanov 2 years ago
parent
commit
bc0e39e651

+ 0 - 17
.idea/deploymentTargetDropDown.xml

@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="deploymentTargetDropDown">
-    <runningDeviceTargetSelectedWithDropDown>
-      <Target>
-        <type value="RUNNING_DEVICE_TARGET" />
-        <deviceKey>
-          <Key>
-            <type value="VIRTUAL_DEVICE_PATH" />
-            <value value="C:\Users\poliv\.android\avd\of_R_of_Pixel_API_31.avd" />
-          </Key>
-        </deviceKey>
-      </Target>
-    </runningDeviceTargetSelectedWithDropDown>
-    <timeTargetWasSelectedWithDropDown value="2022-07-22T13:44:26.567036600Z" />
-  </component>
-</project>

+ 5 - 3
.idea/misc.xml

@@ -4,15 +4,17 @@
     <option name="filePathToZoomLevelMap">
       <map>
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/drawable-v24/ic_launcher_foreground.xml" value="0.2025" />
+        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/drawable/cornerchat.xml" value="0.27" />
+        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/drawable/cornerradius.xml" value="0.234" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/drawable/ic_launcher_background.xml" value="0.2025" />
-        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_chat.xml" value="0.22" />
+        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_chat.xml" value="0.25" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_chats_list.xml" value="0.3546875" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_chats_rooms.xml" value="0.3546875" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_main.xml" value="0.33" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_main2.xml" value="0.3546875" />
-        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_room.xml" value="0.246875" />
+        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_room.xml" value="0.33" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_rooms.xml" value="0.1" />
-        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/fragment_blank.xml" value="0.3546875" />
+        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/fragment_blank.xml" value="0.25" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/fragment_chats.xml" value="0.23802083333333332" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/fragment_chats_rooms.xml" value="0.340625" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/fragment_rooms.xml" value="0.3546875" />

+ 7 - 1
app/src/main/AndroidManifest.xml

@@ -6,10 +6,16 @@
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
-    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
+        tools:ignore="ScopedStorage" />
+    <uses-permission android:name="android.permission.INSTALL_PACKAGES"
+        tools:ignore="ProtectedPermissions" />
     <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
+    <uses-permission android:name="android.permission.BROADCAST_WAP_PUSH"
+        tools:ignore="ProtectedPermissions" />
+    <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
 
     <meta-data
         android:name="com.drinkertea.test2smack.default_notification_channel_id"

+ 193 - 105
app/src/main/java/com/drinkertea/test2smack/BlankFragment.java

@@ -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());
+        }
+
     }
 
 }

+ 95 - 46
app/src/main/java/com/drinkertea/test2smack/ChatActivity.java

@@ -5,9 +5,12 @@ import androidx.appcompat.app.AppCompatActivity;
 import android.graphics.Color;
 import android.graphics.Typeface;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.SystemClock;
 import android.view.View;
+import android.view.inputmethod.InputMethodManager;
 import android.widget.LinearLayout;
+import android.widget.ScrollView;
 import android.widget.TextView;
 
 import org.jivesoftware.smack.AbstractXMPPConnection;
@@ -21,41 +24,56 @@ import org.jxmpp.jid.impl.JidCreate;
 import org.jxmpp.stringprep.XmppStringprepException;
 
 import java.util.List;
+import java.util.Timer;
+import java.util.TimerTask;
 
 public class ChatActivity extends AppCompatActivity {
 
     TextView tView;
-    AbstractXMPPConnection connect = XMPPClass.connection;
-    ChatManager chatManager = XMPPClass.chatManager;
+    AbstractXMPPConnection connect = XMPPClass.connection;;
+    ChatManager chatManager = XMPPClass.chatManager;;
     LinearLayout chatLayout;
+    ScrollView mainScroll;
     EntityBareJid jid;
 
     {
         try {
             jid = XMPPClass.chatJid;
+
         } catch (Exception e) {
-            e.printStackTrace();
+            System.out.println(e.getMessage());
         }
     }
-
     Chat chat = chatManager.chatWith(jid);
 
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_chat);
-        tView = (TextView) findViewById(R.id.testView);
-        chatLayout = (LinearLayout) findViewById(R.id.chatLayout);
-        createMessageForm();
-
+        try {
+            mainScroll = (ScrollView) findViewById(R.id.chatScroll);
+            tView = (TextView) findViewById(R.id.testView);
+            chatLayout = (LinearLayout) findViewById(R.id.chatLayout);
+            createMessageForm();
+            mainScroll.fullScroll(ScrollView.FOCUS_DOWN);
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
     }
 
-    public void sendMessage(View view) throws Exception{
-        String text = tView.getText().toString();
+    public void sendMessage(View view){
+        try {
+            String text = tView.getText().toString();
+            chat.send(text);
+            addMessage(connect.getUser().asBareJid().toString(), text, false);
+            tView.setText("");
+            mainScroll.fullScroll(ScrollView.FOCUS_DOWN);
+
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
 
-        chat.send(text);
-        addMessage(connect.getUser().asBareJid().toString(), text, false);
-        tView.setText("");
     }
 
     public void createMessageForm() {
@@ -71,49 +89,80 @@ public class ChatActivity extends AppCompatActivity {
                 }
             }
             runMessageListener();
-        }catch (Exception ex){
-
+        }catch (Exception e){
+            System.out.println(e.getMessage());
         }
 
     }
 
     public void runMessageListener(){
-        chatManager.addIncomingListener(new IncomingChatMessageListener() {
-            @Override
-            public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
-                runOnUiThread(new Runnable() {
-                    @Override
-                    public void run() {
-                        addMessage(from.asBareJid().toString(), message.getBody(), true);
-                        System.out.println("New message from:" + from + ": " + message.toXML().toString());
-                    }
-                });
+        try {
+            chatManager.addIncomingListener(new IncomingChatMessageListener() {
+                @Override
+                public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
+                    runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+
+                            try {
+                                addMessage(from.asBareJid().toString(), message.getBody(), true);
+                                timer.schedule(timerTask, 1, 500);
+                                System.out.println("New message from:" + from + ": " + message.toXML().toString());
+                            } catch (Exception e) {
+                                System.out.println(e.getMessage());
+                            }
+                        }
+                    });
 
-            }
-        });
+                }
+            });
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
     }
 
     public void addMessage(String from, String body, boolean black){
-        TextView fromName = new TextView(this);
-        TextView bodyView = new TextView(this);
-        LinearLayout linearLayout = new LinearLayout(this);
-
-        fromName.setText(from + ": ");
-        fromName.setTextSize(16);
-        fromName.setTypeface(null, Typeface.BOLD);
-        if (black){
-            fromName.setTextColor(Color.parseColor("#000000"));
-        } else {
-            fromName.setTextColor(Color.parseColor("#006000"));
-        }
-
-        bodyView.setText(body);
-        bodyView.setTextSize(14);
-        bodyView.setTextColor(Color.parseColor("#000000"));
+        try {
+            TextView fromName = new TextView(this);
+            TextView bodyView = new TextView(this);
+            LinearLayout linearLayout = new LinearLayout(this);
+
+            fromName.setText(from.split("@")[0] + ": ");
+            fromName.setTextSize(16);
+            fromName.setTypeface(null, Typeface.BOLD);
+            if (black){
+                fromName.setTextColor(Color.parseColor("#000000"));
+            } else {
+                fromName.setTextColor(Color.parseColor("#006000"));
+            }
 
-        linearLayout.addView(fromName);
-        linearLayout.addView(bodyView);
+            bodyView.setText(body);
+            bodyView.setTextSize(14);
+            bodyView.setTextColor(Color.parseColor("#000000"));
+            linearLayout.setOrientation(LinearLayout.VERTICAL);
+            linearLayout.addView(fromName);
+            linearLayout.addView(bodyView);
 
-        chatLayout.addView(linearLayout);
+            chatLayout.addView(linearLayout);
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
     }
+    final Handler handler = new Handler();
+    Timer timer = new Timer();
+    TimerTask timerTask = new TimerTask() {
+        @Override
+        public void run() {
+            handler.post(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        mainScroll.fullScroll(ScrollView.FOCUS_DOWN);
+                    } catch (Exception e) {
+                        System.out.println(e.getMessage());
+                    }
+                }
+            });
+        }
+    };
 }

+ 12 - 8
app/src/main/java/com/drinkertea/test2smack/ChatsRooms.java

@@ -19,14 +19,18 @@ public class ChatsRooms extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        try {
+            binding = ActivityChatsRoomsBinding.inflate(getLayoutInflater());
+            setContentView(binding.getRoot());
+
+            SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
+            ViewPager viewPager = binding.viewPager;
+            viewPager.setAdapter(sectionsPagerAdapter);
+            TabLayout tabs = binding.tabs;
+            tabs.setupWithViewPager(viewPager);
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
 
-        binding = ActivityChatsRoomsBinding.inflate(getLayoutInflater());
-        setContentView(binding.getRoot());
-
-        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
-        ViewPager viewPager = binding.viewPager;
-        viewPager.setAdapter(sectionsPagerAdapter);
-        TabLayout tabs = binding.tabs;
-        tabs.setupWithViewPager(viewPager);
     }
 }

+ 93 - 37
app/src/main/java/com/drinkertea/test2smack/FragmentRooms.java

@@ -9,6 +9,7 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
+import android.widget.Switch;
 import android.widget.TextView;
 
 import androidx.fragment.app.Fragment;
@@ -18,12 +19,15 @@ import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
 import org.jivesoftware.smack.packet.Message;
 import org.jivesoftware.smack.packet.Presence;
 import org.jivesoftware.smack.roster.RosterListener;
+import org.jivesoftware.smackx.muc.MultiUserChat;
 import org.jxmpp.jid.EntityBareJid;
 import org.jxmpp.jid.Jid;
 import org.jxmpp.jid.impl.JidCreate;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 
@@ -35,56 +39,108 @@ public class FragmentRooms extends Fragment {
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
-        view = inflater.inflate(R.layout.fragment_rooms, container, false);
-        init();
+
+        try {
+            view = inflater.inflate(R.layout.fragment_rooms, container, false);
+            init();
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
         return view;
 
     }
     public void init(){
-        mainListRooms = (LinearLayout) view.findViewById(R.id.roomsListLayout);
         try {
+            mainListRooms = (LinearLayout) view.findViewById(R.id.roomsListLayout);
             GenerateRoomsList();
+            XMPPClass.listForMessageListenerMuc.clear();
+            XMPPClass.listForMessageListenerMuc.addAll(listForMessageListener());
+            getActivity().startService(new Intent(getActivity(), NotificationService.class));
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
-
-    public void GenerateRoomsList() throws Exception{
-        for(EntityBareJid entity : XMPPClass.GenerateRoomList()){
-            LinearLayout Container = new LinearLayout(view.getContext());
-            TextView NameRoom = new TextView(view.getContext());
-            TextView JIDRoom = new TextView(view.getContext());
-
-            Container.setOrientation(LinearLayout.VERTICAL);
-
-            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
-                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
-            layoutParams.height = 195;
-            GradientDrawable border = new GradientDrawable();
-            border.setColor(Color.parseColor("#ffffff"));
-            border.setStroke(5, Color.parseColor("#0081FF"));
-            layoutParams.setMargins(55, 15, 55, 15);
-            Container.setPadding(15, 15, 15, 15);
-            Container.setLayoutParams(layoutParams);
-            Container.setBackgroundDrawable(border);
-            NameRoom.setTextColor(Color.parseColor("#000000"));
-            NameRoom.setText(entity.asBareJid().toString().split("@")[0].toUpperCase());
-
-            JIDRoom.setText(entity.asBareJid().toString());
-
-            Container.addView(NameRoom);
-            Container.addView(JIDRoom);
-            Container.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View view) {
-                    XMPPClass.XMPP_JoinRoom(entity.asEntityBareJidString(), "Test Room User");
-                    Intent intent = new Intent(view.getContext(), Room.class);
-                    startActivity(intent);
+    public List<MultiUserChat> mySortMethod(){
+        List<MultiUserChat> sortMuc = new ArrayList<>();
+        try {
+            for (MultiUserChat sort : XMPPClass.GenerateRoomList()){
+                if (sort.isJoined()){
+                    sortMuc.add(sort);
+                }
+            }
+            for (MultiUserChat sort : XMPPClass.GenerateRoomList()){
+                if (!sort.isJoined()){
+                    sortMuc.add(sort);
+                }
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+        return sortMuc;
+    }
+    public List<MultiUserChat> listForMessageListener(){
+        List<MultiUserChat> sortMuc = new ArrayList<>();
+        try {
+            for (MultiUserChat sort : XMPPClass.GenerateRoomList()){
+                if (sort.isJoined()){
+                    sortMuc.add(sort);
+                }
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+        return sortMuc;
+    }
+    public void GenerateRoomsList(){
+        try {
+            for (MultiUserChat muc : mySortMethod()){
+                LinearLayout Container = new LinearLayout(view.getContext());
+                TextView NameRoom = new TextView(view.getContext());
+                TextView JIDRoom = new TextView(view.getContext());
+                TextView isJoinRoom = new TextView(view.getContext());
+                if(muc.isJoined()){
+                    isJoinRoom.setText("Connected");
+                    isJoinRoom.setTextColor(Color.parseColor("#0081FF"));
+                }
+                else {
+                    isJoinRoom.setText("No access");
                 }
-            });
 
-            mainListRooms.addView(Container);
+                Container.setOrientation(LinearLayout.VERTICAL);
+
+                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
+                layoutParams.height = 195;
+                GradientDrawable border = new GradientDrawable();
+                border.setColor(Color.parseColor("#ffffff"));
+                border.setStroke(5, Color.parseColor("#0081FF"));
+                layoutParams.setMargins(55, 15, 55, 15);
+                Container.setPadding(15, 15, 15, 15);
+                Container.setLayoutParams(layoutParams);
+                Container.setBackgroundDrawable(border);
+                NameRoom.setTextColor(Color.parseColor("#000000"));
+                NameRoom.setText(muc.getRoom().toString().split("@")[0].toUpperCase());
+
+                JIDRoom.setText(muc.getRoom().toString());
+
+                Container.addView(NameRoom);
+                Container.addView(JIDRoom);
+                Container.addView(isJoinRoom);
+                Container.setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View view) {
+                        XMPPClass.XMPP_JoinRoom(muc.getRoom().asEntityBareJidString(), "Test Room User");
+                        Intent intent = new Intent(view.getContext(), Room.class);
+                        startActivity(intent);
+                    }
+                });
+
+                mainListRooms.addView(Container);
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
+
     }
 
 }

+ 76 - 87
app/src/main/java/com/drinkertea/test2smack/MainActivity.java

@@ -1,56 +1,25 @@
 package com.drinkertea.test2smack;
 
-import androidx.annotation.NonNull;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.core.app.ActivityCompat;
 import androidx.core.content.ContextCompat;
 
 import android.Manifest;
-import android.app.Activity;
-import android.app.Dialog;
-import android.app.ProgressDialog;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
-import android.os.Handler;
 import android.text.method.HideReturnsTransformationMethod;
 import android.text.method.PasswordTransformationMethod;
 import android.view.View;
-import android.widget.CheckBox;
 import android.widget.EditText;
+import android.widget.ImageButton;
 import android.widget.Switch;
 
 import com.dcastalia.localappupdate.DownloadApk;
 
-import org.jivesoftware.smack.AbstractXMPPConnection;
-import org.jivesoftware.smack.MessageListener;
-import org.jivesoftware.smack.XMPPConnection;
-import org.jivesoftware.smack.chat2.Chat;
-import org.jivesoftware.smack.chat2.ChatManager;
-import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
-import org.jivesoftware.smack.packet.Message;
-import org.jivesoftware.smack.packet.Presence;
-import org.jivesoftware.smack.roster.Roster;
-import org.jivesoftware.smack.roster.RosterEntry;
-import org.jivesoftware.smackx.mam.MamManager;
-import org.jivesoftware.smackx.muc.HostedRoom;
-import org.jivesoftware.smackx.muc.InvitationListener;
-import org.jivesoftware.smackx.muc.MultiUserChat;
-import org.jivesoftware.smackx.muc.MultiUserChatManager;
-import org.jivesoftware.smackx.muc.packet.MUCUser;
-import org.jxmpp.jid.DomainBareJid;
-import org.jxmpp.jid.EntityBareJid;
-import org.jxmpp.jid.EntityFullJid;
-import org.jxmpp.jid.EntityJid;
-import org.jxmpp.jid.impl.JidCreate;
-import org.jxmpp.jid.parts.Resourcepart;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Timer;
-import java.util.TimerTask;
 
 
 public class MainActivity extends AppCompatActivity {
@@ -59,6 +28,7 @@ public class MainActivity extends AppCompatActivity {
     private EditText ETpassword;
     private SharedPreferences pref;
     private Switch switchPass;
+    private ImageButton imgBtn;
     String email;
     String password;
 
@@ -66,23 +36,25 @@ public class MainActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
-        pref = getSharedPreferences("login_pass_pref", MODE_PRIVATE);
-        ETemail = (EditText) findViewById(R.id.ETEmail);
-        ETpassword = (EditText) findViewById(R.id.ETPassword);
-        switchPass = (Switch) findViewById(R.id.SwitchBoxPassword);
-        ETemail.setText(pref.getString("email", ""));
-        ETpassword.setText(pref.getString("pass", ""));
+        init();
     }
-
-    public void RunService(){
-        if(!NotificationService.serverIsWorked){
-            startService(new Intent(this, NotificationService.class));
-        }
-        else {
-            System.out.println("Server work!");
+    private void init(){
+        try {
+            imgBtn = (ImageButton) findViewById(R.id.notifButtonSound);
+            pref = getSharedPreferences("login_pass_pref", MODE_PRIVATE);
+            ETemail = (EditText) findViewById(R.id.ETEmail);
+            ETpassword = (EditText) findViewById(R.id.ETPassword);
+            switchPass = (Switch) findViewById(R.id.SwitchBoxPassword);
+            ETemail.setText(pref.getString("email", ""));
+            ETpassword.setText(pref.getString("pass", ""));
+            if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_WAP_PUSH) ==
+                    PackageManager.PERMISSION_GRANTED){
+                imgBtn.setVisibility(View.GONE);
+            }
+        }catch (Exception ex){
+            System.out.println(ex.getMessage());
         }
     }
-
     public void goToPageWithChats(View view){
         try {
             SharedPreferences.Editor editor = pref.edit();
@@ -96,9 +68,6 @@ public class MainActivity extends AppCompatActivity {
                 editor.putString("email", email);
                 editor.putString("pass", password);
                 editor.apply();
-                //RunService();
-                //Intent intent = new Intent(this, ChatsList.class);
-                //startActivity(intent);
                 Intent intent = new Intent(this, ChatsRooms.class);
                 startActivity(intent);
             }
@@ -110,51 +79,71 @@ public class MainActivity extends AppCompatActivity {
         }
     }
 
-    public void testMethod(View view) throws Exception{
-        /*email = ETemail.getText().toString();
-        password = ETpassword.getText().toString();
-        boolean isLogin = XMPPClass.XMPP_Connection(email, password);
-
-        if (isLogin && XMPPClass.connection.getUser() != null){
-            try {
-                RunService();
-                Intent intent = new Intent(this, rooms.class);
-                startActivity(intent);
-            }catch (Exception ex){
-                System.out.println(ex.getMessage());
+    public void updateApp(View view){
+        try {
+            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
+                    Manifest.permission.INSTALL_PACKAGES,
+                    Manifest.permission.REQUEST_INSTALL_PACKAGES};
+            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
+                    PackageManager.PERMISSION_GRANTED){
+                String BASEURL = "https://github.com/WorldOfPets/test_apk_file/raw/master/app-debug.apk";
+                DownloadApk downloadApk = new DownloadApk(MainActivity.this);
+                downloadApk.startDownloadingApk(BASEURL);
+            } else {
+                ActivityCompat.requestPermissions(
+                        this,
+                        permissions,
+                        1
+                );
             }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
-        else {
-            System.out.println("Error connect!");
-        }*/
     }
 
-    public void stopNotificationClick(View view) throws Exception{
-        stopService(new Intent(this, NotificationService.class));
+    public void showPass(View view) {
+        try {
+            if(switchPass.isChecked()){
+                ETpassword.setTransformationMethod(new HideReturnsTransformationMethod());
+            }
+            else {
+                ETpassword.setTransformationMethod(new PasswordTransformationMethod());
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
     }
 
-    public void updateApp(View view) throws Exception{
-        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.REQUEST_INSTALL_PACKAGES};
-        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
-                PackageManager.PERMISSION_GRANTED){
-            String BASEURL = "https://github.com/WorldOfPets/test_apk_file/raw/master/app-debug.apk";
-            DownloadApk downloadApk = new DownloadApk(MainActivity.this);
-            downloadApk.startDownloadingApk(BASEURL);
-        } else {
-            ActivityCompat.requestPermissions(
-                this,
-                permissions,
-                1
-            );
+    public void testMethod(View view) {
+        try {
+            stopService(new Intent(this, NotificationService.class));
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
     }
 
-    public void showPass(View view) {
-        if(switchPass.isChecked()){
-            ETpassword.setTransformationMethod(new HideReturnsTransformationMethod());
-        }
-        else {
-            ETpassword.setTransformationMethod(new PasswordTransformationMethod());
+    public void notifSound(View view) {
+
+        try {
+            if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_WAP_PUSH) ==
+            PackageManager.PERMISSION_DENIED){
+                String[] permissions  = new String[]{
+                        Manifest.permission.BROADCAST_WAP_PUSH,
+                        Manifest.permission.RECEIVE_WAP_PUSH,
+                };
+                ActivityCompat.requestPermissions(
+                        this,
+                        permissions,
+                        4
+                );
+            }
+            else{
+                imgBtn.setVisibility(View.GONE);
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
     }
+
+
 }

+ 71 - 37
app/src/main/java/com/drinkertea/test2smack/NotificationService.java

@@ -13,16 +13,20 @@ import androidx.core.app.NotificationManagerCompat;
 import androidx.core.app.TaskStackBuilder;
 
 import org.jivesoftware.smack.AbstractXMPPConnection;
+import org.jivesoftware.smack.MessageListener;
 import org.jivesoftware.smack.chat2.Chat;
 import org.jivesoftware.smack.chat2.ChatManager;
 import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
 import org.jivesoftware.smack.packet.Message;
 import org.jivesoftware.smack.tcp.XMPPTCPConnection;
 import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
+import org.jivesoftware.smackx.muc.MultiUserChat;
 import org.jxmpp.jid.EntityBareJid;
 import org.jxmpp.jid.impl.JidCreate;
 import org.jxmpp.stringprep.XmppStringprepException;
 
+import java.util.Locale;
+
 
 public class NotificationService extends Service {
 
@@ -49,9 +53,8 @@ public class NotificationService extends Service {
                         .setSmallIcon(R.mipmap.ic_launcher_round);
                 startForeground(1001, builder.build());
             }
-
         } catch (Exception e) {
-            e.printStackTrace();
+            System.out.println(e.getMessage());
         }
         return super.onStartCommand(intent, flags, startId);
     }
@@ -59,12 +62,16 @@ public class NotificationService extends Service {
     @Override
     public void onCreate() {
         super.onCreate();
-        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
-            NotificationChannel channel = new NotificationChannel("My channel", "My notification", NotificationManager.IMPORTANCE_DEFAULT);
-            NotificationManager manager = getSystemService(NotificationManager.class);
-            manager.createNotificationChannel(channel);
+        try {
+            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
+                NotificationChannel channel = new NotificationChannel("My channel", "My notification", NotificationManager.IMPORTANCE_DEFAULT);
+                NotificationManager manager = getSystemService(NotificationManager.class);
+                manager.createNotificationChannel(channel);
+            }
+            System.out.println("Server started!");
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
-        System.out.println("Server started!");
     }
 
     @Override
@@ -72,46 +79,73 @@ public class NotificationService extends Service {
         return null;
     }
 
-    private void task() throws Exception{
-        XMPPClass.chatManager.addIncomingListener(new IncomingChatMessageListener() {
-            @Override
-            public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
-                System.out.println("New message from:" + from + ": " + message.getBody());
-                try {
-                    createNotificationChannel(from, message.getBody());
-                }catch (Exception ex){
-                    System.out.println(ex.getMessage());
+    private void task(){
+        try {
+            XMPPClass.chatManager.addIncomingListener(new IncomingChatMessageListener() {
+                @Override
+                public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
+                    System.out.println("New message from:" + from + ": " + message.getBody());
+                    try {
+                        createNotificationChannel(from.asBareJid().toString(), message.getBody());
+                    }catch (Exception ex){
+                        System.out.println(ex.getMessage());
+                    }
                 }
+            });
+            for (MultiUserChat muc : XMPPClass.listForMessageListenerMuc){
+                muc.addMessageListener(new MessageListener() {
+                    @Override
+                    public void processMessage(Message message) {
+                        try {
+                            if (message.getBody() != null){
+                                createNotificationChannel(message.getFrom().toString().split("@")[0].toUpperCase(Locale.ROOT) + "/" + message.getFrom().toString().split("/")[1], message.getBody());
+                            }
+
+                        }catch (Exception ex){
+                            System.out.println(ex.getMessage());
+                        }
+                    }
+                });
             }
-        });
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
     }
-    private void createNotificationChannel(EntityBareJid from, String message) {
+
+
+    private void createNotificationChannel(String from, String message) {
         try {
             XMPPClass.chatJid = JidCreate.entityBareFrom(from);
-        } catch (XmppStringprepException e) {
-            e.printStackTrace();
+            Intent resultIntent = new Intent(this, ChatsRooms.class);
+            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
+            stackBuilder.addNextIntentWithParentStack(resultIntent);
+            PendingIntent resultPendingIntent =
+                    stackBuilder.getPendingIntent(0,
+                            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
+
+            NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this, "My channel");
+            builder.setContentTitle(from);
+            builder.setContentText(message);
+            builder.setSmallIcon(R.mipmap.ic_launcher);
+            builder.setAutoCancel(true);
+            builder.setContentIntent(resultPendingIntent);
+
+            startForeground(1001, builder.build());
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
-        Intent resultIntent = new Intent(this, MainActivity.class);
-        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
-        stackBuilder.addNextIntentWithParentStack(resultIntent);
-        PendingIntent resultPendingIntent =
-                stackBuilder.getPendingIntent(0,
-                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
-
-        NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this, "My channel");
-        builder.setContentTitle(from.asBareJid().toString());
-        builder.setContentText(message);
-        builder.setSmallIcon(R.mipmap.ic_launcher);
-        builder.setAutoCancel(true);
-        builder.setContentIntent(resultPendingIntent);
-
-        startForeground(1001, builder.build());
+
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
-        serverIsWorked = false;
-        System.out.println("Server destroy!");
+        try {
+            serverIsWorked = false;
+            System.out.println("Server destroy!");
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+
     }
 }

+ 81 - 44
app/src/main/java/com/drinkertea/test2smack/Room.java

@@ -7,9 +7,11 @@ import android.graphics.Color;
 import android.graphics.Typeface;
 import android.graphics.drawable.GradientDrawable;
 import android.os.Bundle;
+import android.os.Handler;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.LinearLayout;
+import android.widget.ScrollView;
 import android.widget.TextView;
 
 import org.jivesoftware.smack.MessageListener;
@@ -21,71 +23,89 @@ import org.jivesoftware.smackx.muc.MultiUserChat;
 import org.jxmpp.jid.EntityBareJid;
 
 import java.util.List;
+import java.util.Timer;
+import java.util.TimerTask;
 
 public class Room extends AppCompatActivity {
 
     public LinearLayout roomLayout;
     public EditText roomMessageET;
     public MultiUserChat thisMuc = XMPPClass.activeMuc;
+    private ScrollView mainScroll;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        setContentView(R.layout.activity_room);
-        roomMessageET = (EditText) findViewById(R.id.roomMessageView);
-        roomLayout = (LinearLayout) findViewById(R.id.roomLayout);
-        List<Message> messages = XMPPClass.CreateRoomListMessage(10, thisMuc);
-        for(Message message : messages){
-            String from = message.getFrom().asBareJid().toString().trim();
-            if (from.equals(XMPPClass.myJid.asEntityBareJidString().trim())){
-                addMessage(message.getFrom().toString().split("/")[1], message.getBody(), false);
-            }
-            else {
-                addMessage(message.getFrom().toString().split("/")[1], message.getBody(), true);
+        try {
+            setContentView(R.layout.activity_room);
+            roomMessageET = (EditText) findViewById(R.id.roomMessageView);
+            roomLayout = (LinearLayout) findViewById(R.id.roomLayout);
+            mainScroll = (ScrollView) findViewById(R.id.roomScroll);
+            List<Message> messages = XMPPClass.CreateRoomListMessage(10, thisMuc);
+            for(Message message : messages){
+                String from = message.getFrom().asBareJid().toString().trim();
+                if (from.equals(XMPPClass.myJid.asEntityBareJidString().trim())){
+                    addMessage(message.getFrom().toString().split("/")[1], message.getBody(), false);
+                }
+                else {
+                    addMessage(message.getFrom().toString().split("/")[1], message.getBody(), true);
+                }
             }
+            mainScroll.fullScroll(View.FOCUS_DOWN);
+            runMessageListener();
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
-        runMessageListener();
+
     }
 
     public void addMessage(String from, String body, boolean black){
-        TextView fromName = new TextView(this);
-        TextView bodyView = new TextView(this);
-        LinearLayout linearLayout = new LinearLayout(this);
+        try {
+            TextView fromName = new TextView(this);
+            TextView bodyView = new TextView(this);
+            LinearLayout linearLayout = new LinearLayout(this);
 
-        fromName.setText(from + ": ");
-        fromName.setTextSize(16);
-        fromName.setTypeface(null, Typeface.BOLD);
-        if (black){
-            fromName.setTextColor(Color.parseColor("#000000"));
-        } else {
-            fromName.setTextColor(Color.parseColor("#006000"));
-        }
+            fromName.setText(from + ": ");
+            fromName.setTextSize(16);
+            fromName.setTypeface(null, Typeface.BOLD);
+            if (black){
+                fromName.setTextColor(Color.parseColor("#000000"));
+            } else {
+                fromName.setTextColor(Color.parseColor("#006000"));
+            }
 
-        bodyView.setText(body);
-        bodyView.setTextSize(14);
-        bodyView.setTextColor(Color.parseColor("#000000"));
-        linearLayout.setOrientation(LinearLayout.VERTICAL);
-        linearLayout.addView(fromName);
-        linearLayout.addView(bodyView);
+            bodyView.setText(body);
+            bodyView.setTextSize(14);
+            bodyView.setTextColor(Color.parseColor("#000000"));
+            linearLayout.setOrientation(LinearLayout.VERTICAL);
+            linearLayout.addView(fromName);
+            linearLayout.addView(bodyView);
+
+            roomLayout.addView(linearLayout);
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
 
-        roomLayout.addView(linearLayout);
     }
     public void runMessageListener(){
-        thisMuc.addMessageListener(new MessageListener() {
-            @Override
-            public void processMessage(Message message) {
-                runOnUiThread(new Runnable() {
-                    @Override
-                    public void run() {
-                        if (message.getBody() != null && message.getBody() != ""){
-                            addMessage(message.getFrom().toString().split("/")[1], message.getBody(), true);
+        try {
+            thisMuc.addMessageListener(new MessageListener() {
+                @Override
+                public void processMessage(Message message) {
+                    runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            if (message.getBody() != null && message.getBody() != ""){
+                                addMessage(message.getFrom().toString().split("/")[1], message.getBody(), true);
+                                timer.schedule(timerTask, 1, 500);
+                            }
                         }
-
-                        //System.out.println("New message from:" + from + ": " + message.toXML().toString());
-                    }
-                });
-            }
-        });
+                    });
+                }
+            });
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
     }
 
     public void sendMessage(View view) {
@@ -94,9 +114,26 @@ public class Room extends AppCompatActivity {
             thisMuc.sendMessage(text);
             addMessage(XMPPClass.myJid.asBareJid().toString(), text, false);
             roomMessageET.setText("");
+            mainScroll.fullScroll(ScrollView.FOCUS_DOWN);
         }catch (Exception e){
             System.out.println(e.getMessage());
         }
 
     }
+    final Handler handler = new Handler();
+    Timer timer = new Timer();
+    TimerTask timerTask = new TimerTask() {
+        @Override
+        public void run() {
+            handler.post(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        mainScroll.fullScroll(ScrollView.FOCUS_DOWN);
+                    }catch (Exception ex){
+                    }
+                }
+            });
+        }
+    };
 }

+ 59 - 33
app/src/main/java/com/drinkertea/test2smack/XMPPClass.java

@@ -33,24 +33,26 @@ public class XMPPClass {
     public static Roster roster;
     public static EntityBareJid chatJid;
     public static EntityBareJid myJid;
-    public static ArrayList<ChatItem> chatItems = new ArrayList<ChatItem>();
+    public static ArrayList<ChatItem> chatItems = new ArrayList<>();
     public static List<Message> messages;
     public static MultiUserChatManager manager;
     public static List<MultiUserChat> listMuc = new ArrayList<>();
     public static MultiUserChat activeMuc;
+    public static String nickname_local = "Test Room User";
+    public static List<MultiUserChat> listForMessageListenerMuc = new ArrayList<>();
 
     public static void XMPP_JoinRoom(String room_name, String nickname){
         try {
             EntityBareJid JIDRoom = JidCreate.entityBareFrom(room_name);
             MultiUserChat muc = manager.getMultiUserChat(JIDRoom);
-            listMuc.add(muc);
+            //listMuc.add(muc);
             Resourcepart room  = Resourcepart.from(nickname);
             if(!muc.isJoined()){
                 muc.join(room);
             }
             activeMuc = muc;
             for(EntityFullJid jid1 : muc.getOccupants()){
-                System.out.println(jid1);
+                //System.out.println(jid1);
             }
         } catch (Exception e) {
             System.out.println(e.getMessage());
@@ -58,9 +60,10 @@ public class XMPPClass {
 
     }
 
-    public static boolean XMPP_Connection(String login, String password) throws Exception{
+    public static boolean XMPP_Connection(String login, String password){
         try {
             myJid = JidCreate.entityBareFrom(login + "@msg.sharix-app.org");
+            nickname_local = login;
             XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                     .setXmppDomain("msg.sharix-app.org")
                     .setHost("46.138.247.90")
@@ -70,11 +73,13 @@ public class XMPPClass {
             connection.login(login, password);
             chatManager = ChatManager.getInstanceFor(connection);
             roster = Roster.getInstanceFor(connection);
+            manager = MultiUserChatManager.getInstanceFor(XMPPClass.connection);
             return true;
         }catch (Exception ex){
             System.out.println(ex.getMessage());
             return false;
         }
+
     }
 
     public static void CreateChatListMessage(EntityBareJid jid, int message_count){
@@ -88,6 +93,7 @@ public class XMPPClass {
             MamManager.MamQuery mamQuery = mamManager.queryArchive(mamQueryArgs);
             messages = mamQuery.getMessages();
         }catch (Exception ex){
+            System.out.println(ex.getMessage());
         }
     }
 
@@ -103,44 +109,64 @@ public class XMPPClass {
         }catch (Exception ex){
             return null;
         }
+
     }
 
-    public static List<EntityBareJid> GenerateRoomList() throws Exception{
-        List<EntityBareJid> listRoom = new ArrayList<>();
-        manager = MultiUserChatManager.getInstanceFor(XMPPClass.connection);
-        List<DomainBareJid> domains = manager.getMucServiceDomains();
-        for (DomainBareJid domain : domains){
-            Map<EntityBareJid, HostedRoom> map = manager.getRoomsHostedBy(domain);
-            for(EntityBareJid ent : map.keySet()){
-                listRoom.add(ent);
-                System.out.println(ent);
+    public static List<MultiUserChat> GenerateRoomList() {
+        try {
+            List<DomainBareJid> domains = manager.getMucServiceDomains();
+            for (DomainBareJid domain : domains){
+                Map<EntityBareJid, HostedRoom> map = manager.getRoomsHostedBy(domain);
+                listMuc.clear();
+                for(EntityBareJid ent : map.keySet()){
+                    //listRoom.add(ent);
+                    try {
+                        EntityBareJid JIDRoom = JidCreate.entityBareFrom(ent);
+                        MultiUserChat muc =  manager.getMultiUserChat(JIDRoom);
+                        listMuc.add(muc);
+                        Resourcepart room  = Resourcepart.from(nickname_local);
+                        if(!muc.isJoined()){
+                            muc.join(room);
+                        }
+                    }catch (Exception ex){
+                        System.out.println(ex);
+                    }
+                    //System.out.println(ent);
+                }
             }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
-        return listRoom;
+        return listMuc;
     }
 
     public static void GenerateChatList(){
-        Collection<RosterEntry> entries = roster.getEntries();
-        Presence presence;
-        for (RosterEntry entry : entries){
-            presence = roster.getPresence(entry.getJid());
-            ChatItem chatItem = new ChatItem();
-            chatItem.jid = presence.getFrom().asBareJid().toString().trim();
-
-            if(entry.getName() != null){
-                chatItem.name = entry.getName().trim();
-            }
-            else {
-                chatItem.name = null;
-            }
+        try {
+            Collection<RosterEntry> entries = roster.getEntries();
+            Presence presence;
+            chatItems.clear();
+            for (RosterEntry entry : entries){
+                presence = roster.getPresence(entry.getJid());
+                ChatItem chatItem = new ChatItem();
+                chatItem.jid = presence.getFrom().asBareJid().toString().trim();
 
-            if (presence.getType().toString().trim().equals("unavailable")){
-                chatItem.status = "Offline";
-            }
-            else chatItem.status = "Online";
+                if(entry.getName() != null){
+                    chatItem.name = entry.getName().trim();
+                }
+                else {
+                    chatItem.name = null;
+                }
+
+                if (presence.getType().toString().trim().equals("unavailable")){
+                    chatItem.status = "Offline";
+                }
+                else chatItem.status = "Online";
 
-            chatItem.message = null;
-            chatItems.add(chatItem);
+                chatItem.message = null;
+                chatItems.add(chatItem);
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
         }
     }
 

+ 7 - 0
app/src/main/res/drawable/cornerchat.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid android:color="#ffffff" />
+    <corners android:radius="5dp" />
+    <stroke android:width="5dp"/>
+    <stroke android:color="#0081FF"/>
+</shape>

+ 5 - 0
app/src/main/res/drawable/cornerradius.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid android:color="#0081FF" />
+    <corners android:radius="10dp" />
+</shape>

+ 32 - 15
app/src/main/res/layout/activity_chat.xml

@@ -8,30 +8,44 @@
 
     <LinearLayout
         android:id="@+id/linearLayout"
-        android:layout_width="wrap_content"
-        android:layout_height="60dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="@drawable/cornerchat"
+        android:layout_margin="5dp"
+        android:padding="15dp"
+        android:paddingBottom="0dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent">
 
-        <EditText
-            android:id="@+id/testView"
-            android:layout_width="300dp"
-            android:layout_height="40dp"
-            android:text="hello world" />
+        <LinearLayout
+            android:orientation="horizontal"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent">
+            <EditText
+                android:id="@+id/testView"
+                android:inputType="textMultiLine"
+                android:layout_width="wrap_content"
+                android:maxWidth="270dp"
+                android:minWidth="260dp"
+                android:layout_height="60dp"
+                android:text="hello world" />
 
-        <Button
-            android:id="@+id/btnSendMessage"
-            android:layout_width="wrap_content"
-            android:layout_height="40dp"
-            android:onClick="sendMessage"
-            android:text="Send" />
+            <ImageButton
+                android:id="@+id/btnSendMessage"
+                android:src="@android:drawable/ic_menu_send"
+                android:background="@drawable/cornerradius"
+                android:layout_width="50dp"
+                android:layout_height="50dp"
+                android:onClick="sendMessage" />
+        </LinearLayout>
     </LinearLayout>
 
     <ScrollView
         android:id="@+id/chatScroll"
         android:layout_width="match_parent"
-        android:layout_height="500dp"
+        android:layout_height="0dp"
+        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent">
@@ -40,7 +54,10 @@
             android:id="@+id/chatLayout"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:orientation="vertical"></LinearLayout>
+            android:padding="15dp"
+            android:orientation="vertical">
+
+        </LinearLayout>
     </ScrollView>
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 23 - 0
app/src/main/res/layout/activity_main.xml

@@ -108,8 +108,31 @@
             android:textColor="#0081FF"
             android:background="@android:color/transparent"
             app:layout_constraintTop_toBottomOf="@+id/test" />
+        <Button
+            android:id="@+id/testButtttttton"
+            android:layout_width="200dp"
+            android:layout_height="40dp"
+            android:textSize="10dp"
+            android:text="Stop Notification"
+            android:onClick="testMethod"
+            android:textColor="#c0c0c0"
+            android:background="@android:color/transparent"
+            app:layout_constraintTop_toBottomOf="@+id/test" />
     </LinearLayout>
 
+    <ImageButton
+        android:id="@+id/notifButtonSound"
+        android:onClick="notifSound"
+        android:layout_width="50dp"
+        android:layout_margin="30dp"
+
+        android:layout_height="50dp"
+        android:background="@android:color/transparent"
+        android:src="@android:drawable/stat_notify_chat"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:tint="#0081FF"
+        tools:ignore="MissingConstraints" />
 
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 20 - 10
app/src/main/res/layout/activity_room.xml

@@ -5,10 +5,12 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".Room">
+
     <ScrollView
         android:id="@+id/roomScroll"
         android:layout_width="match_parent"
-        android:layout_height="500dp"
+        android:layout_height="0dp"
+        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent">
@@ -17,6 +19,7 @@
             android:id="@+id/roomLayout"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
+            android:padding="15dp"
             android:orientation="vertical">
 
         </LinearLayout>
@@ -24,23 +27,30 @@
     <LinearLayout
         android:id="@+id/linearLayout"
         android:layout_width="match_parent"
-        android:layout_height="60dp"
-        android:background="@color/white"
+        android:layout_height="wrap_content"
+        android:padding="15dp"
+        android:paddingBottom="0dp"
+        android:layout_margin="5dp"
+        android:background="@drawable/cornerchat"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent">
 
         <EditText
             android:id="@+id/roomMessageView"
-            android:layout_width="300dp"
-            android:layout_height="40dp"
+            android:layout_width="wrap_content"
+            android:maxWidth="270dp"
+            android:minWidth="260dp"
+            android:layout_height="60dp"
+            android:inputType="textMultiLine"
             android:text="hello world" />
 
-        <Button
+        <ImageButton
             android:id="@+id/btnSendMessage"
-            android:layout_width="wrap_content"
-            android:layout_height="40dp"
-            android:onClick="sendMessage"
-            android:text="Send" />
+            android:src="@android:drawable/ic_menu_send"
+            android:background="@drawable/cornerradius"
+            android:layout_width="50dp"
+            android:layout_height="50dp"
+            android:onClick="sendMessage" />
     </LinearLayout>
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 18 - 1
app/src/main/res/layout/fragment_blank.xml

@@ -9,11 +9,27 @@
         android:id="@+id/chatsListScroll"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
+        <LinearLayout
+            android:id="@+id/secondLayout"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:orientation="vertical">
 
+
+        <EditText
+            android:id="@+id/search_edit_text"
+            android:layout_marginLeft="18dp"
+            android:layout_marginRight="18dp"
+            android:layout_marginBottom="10dp"
+            android:layout_marginTop="10dp"
+            android:backgroundTint="#0081FF"
+            android:hint="Search by JID"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"/>
         <LinearLayout
             android:id="@+id/chatsListLayout"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
+            android:layout_height="match_parent"
             android:orientation="vertical">
             <LinearLayout
                 android:layout_margin="15dp"
@@ -35,6 +51,7 @@
                     android:layout_height="match_parent"/>
             </LinearLayout>
         </LinearLayout>
+        </LinearLayout>
     </ScrollView>
 
 </FrameLayout>