Browse Source

rooms and notification

Evgeny Polivanov 2 years ago
parent
commit
2c9d567381

+ 3 - 1
.idea/misc.xml

@@ -3,9 +3,11 @@
   <component name="DesignSurface">
     <option name="filePathToZoomLevelMap">
       <map>
-        <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_chat.xml" value="0.3546875" />
+        <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_chats_list.xml" value="0.3546875" />
         <entry key="..\:/Users/poliv/AndroidStudioProjects/test2Smack/app/src/main/res/layout/activity_main.xml" value="0.340625" />
+        <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_rooms.xml" value="0.33" />
       </map>
     </option>
   </component>

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

@@ -5,8 +5,12 @@
 
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-    <meta-data android:name="com.drinkertea.test2smack.default_notification_channel_id"
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
+
+    <meta-data
+        android:name="com.drinkertea.test2smack.default_notification_channel_id"
         android:value="@string/default_notification_channel_id" />
+
     <application
         android:allowBackup="true"
         android:dataExtractionRules="@xml/data_extraction_rules"
@@ -17,6 +21,12 @@
         android:supportsRtl="true"
         android:theme="@style/Theme.Test2Smack"
         tools:targetApi="31">
+        <activity
+            android:name=".Room"
+            android:exported="false" />
+        <activity
+            android:name=".rooms"
+            android:exported="false" />
         <activity
             android:name=".ChatsList"
             android:exported="false" />
@@ -32,6 +42,8 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <service android:name=".NotificationService" />
     </application>
 
 </manifest>

+ 13 - 22
app/src/main/java/com/drinkertea/test2smack/ChatActivity.java

@@ -25,14 +25,14 @@ import java.util.List;
 public class ChatActivity extends AppCompatActivity {
 
     TextView tView;
-    AbstractXMPPConnection connect = MainActivity.connection;
-    ChatManager chatManager = MainActivity.chatManager;
+    AbstractXMPPConnection connect = XMPPClass.connection;
+    ChatManager chatManager = XMPPClass.chatManager;
     LinearLayout chatLayout;
     EntityBareJid jid;
 
     {
         try {
-            jid = ChatsList.jid;
+            jid = XMPPClass.chatJid;
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -49,6 +49,7 @@ public class ChatActivity extends AppCompatActivity {
         createMessageForm();
 
     }
+
     public void sendMessage(View view) throws Exception{
         String text = tView.getText().toString();
 
@@ -59,27 +60,17 @@ public class ChatActivity extends AppCompatActivity {
 
     public void createMessageForm() {
         try {
-            MamManager mamManager = MamManager.getInstanceFor(connect);
-            //EntityBareJid jid = JidCreate.entityBareFrom("evgeny.polivanov@msg.sharix-app.org");
-            MamManager.MamQueryArgs mamQueryArgs = MamManager.MamQueryArgs.builder()
-                    .limitResultsToJid(jid)
-                    .setResultPageSizeTo(10)
-                    .queryLastPage()
-                    .build();
-            MamManager.MamQuery mamQuery = mamManager.queryArchive(mamQueryArgs);
-            List<Message> messages = mamQuery.getMessages();
-            for(Message message : messages){
+            XMPPClass.CreateChatListMessage(XMPPClass.chatJid, 15);
+            for(Message message : XMPPClass.messages){
                 String from = message.getFrom().asBareJid().toString().trim();
-                if (from.equals(jid.asEntityBareJidString().trim())){
-                    addMessage(message.getFrom().asBareJid().toString(), message.getBody(), true);
+                if (from.equals(XMPPClass.myJid.asEntityBareJidString().trim())){
+                    addMessage(message.getFrom().asBareJid().toString(), message.getBody(), false);
                 }
                 else {
-                    addMessage(message.getFrom().asBareJid().toString(), message.getBody(), false);
+                    addMessage(message.getFrom().asBareJid().toString(), message.getBody(), true);
                 }
-
             }
             runMessageListener();
-
         }catch (Exception ex){
 
         }
@@ -101,7 +92,8 @@ public class ChatActivity extends AppCompatActivity {
             }
         });
     }
-    public void addMessage(String from, String body, boolean red){
+
+    public void addMessage(String from, String body, boolean black){
         TextView fromName = new TextView(this);
         TextView bodyView = new TextView(this);
         LinearLayout linearLayout = new LinearLayout(this);
@@ -109,13 +101,12 @@ public class ChatActivity extends AppCompatActivity {
         fromName.setText(from + ": ");
         fromName.setTextSize(16);
         fromName.setTypeface(null, Typeface.BOLD);
-        if (red){
-            fromName.setTextColor(Color.parseColor("#a51b0b"));
+        if (black){
+            fromName.setTextColor(Color.parseColor("#000000"));
         } else {
             fromName.setTextColor(Color.parseColor("#006000"));
         }
 
-
         bodyView.setText(body);
         bodyView.setTextSize(14);
         bodyView.setTextColor(Color.parseColor("#000000"));

+ 19 - 82
app/src/main/java/com/drinkertea/test2smack/ChatsList.java

@@ -49,66 +49,37 @@ public class ChatsList extends AppCompatActivity {
     LinearLayout linearLayout;
     final Handler handler = new Handler();
     Timer timer = new Timer();
-    public static EntityBareJid jid;
-    PendingIntent resultPendingIntent;
     TimerTask timerTask = new TimerTask() {
         @Override
         public void run() {
-            handler.post(new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        generateChatsList();
-                    }catch (Exception ex){
-                        //System.out.println(ex.getMessage());
-                    }
+        handler.post(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    generateChatsList();
+                }catch (Exception ex){
+                    System.out.println(ex.getMessage());
                 }
-            });
+            }
+        });
         }
     };
-    ArrayList<ChatItem> chatItems = new ArrayList<ChatItem>();
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_chats_list);
-        linearLayout = (LinearLayout) findViewById(R.id.chatsListLayout);
-
-        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);
-        }
-
-        Collection<RosterEntry> entries = MainActivity.roster.getEntries();
-        Presence presence;
-        for (RosterEntry entry : entries){
-            presence = MainActivity.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;
-            }
 
-            if (presence.getType().toString().trim().equals("unavailable")){
-                chatItem.status = "Offline";
-            }
-            else chatItem.status = "Online";
+        linearLayout = (LinearLayout) findViewById(R.id.chatsListLayout);
 
-            chatItem.message = null;
-            chatItems.add(chatItem);
-        }
+        XMPPClass.GenerateChatList();
         generateChatsList();
     }
 
     public void generateChatsList(){
         linearLayout.removeAllViews();
-        Collections.sort(chatItems, new FishNameComparator());
-        for (ChatItem chatItem : chatItems){
+        Collections.sort(XMPPClass.chatItems, new FishNameComparator());
+        for (ChatItem chatItem : XMPPClass.chatItems){
             TextView Name = new TextView(this);
             TextView Status = new TextView(this);
             TextView NewMessage = new TextView(this);
@@ -130,7 +101,7 @@ public class ChatsList extends AppCompatActivity {
                 NewMessage.setText("No new message");
             }
 
-            if (chatItem.status == "Offline"){
+            if (chatItem.status.equals("Offline")){
                 Status.setTextColor(Color.parseColor("#a51b0b"));
             }
             else {
@@ -162,7 +133,7 @@ public class ChatsList extends AppCompatActivity {
                 @Override
                 public void onClick(View view) {
                     try {
-                        jid = JidCreate.entityBareFrom(chatItem.jid);
+                        XMPPClass.chatJid = JidCreate.entityBareFrom(chatItem.jid);
                         chatItem.message = null;
                         Intent intent = new Intent(ChatsList.this, ChatActivity.class);
                         startActivity(intent);
@@ -178,46 +149,12 @@ public class ChatsList extends AppCompatActivity {
         timer.schedule(timerTask, 0, 10000);
     }
 
-    private void createNotificationChannel(EntityBareJid from, String message) {
-        try {
-            jid = JidCreate.entityBareFrom(from);
-        } catch (XmppStringprepException e) {
-            e.printStackTrace();
-        }
-        // Create an Intent for the activity you want to start
-        Intent resultIntent = new Intent(this, ChatActivity.class);
-// Create the TaskStackBuilder and add the intent, which inflates the back stack
-        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
-        stackBuilder.addNextIntentWithParentStack(resultIntent);
-// Get the PendingIntent containing the entire back stack
-        PendingIntent resultPendingIntent =
-                stackBuilder.getPendingIntent(0,
-                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
-        //NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
-        NotificationCompat.Builder builder = new NotificationCompat.Builder(ChatsList.this, "My channel");
-        builder.setContentTitle(from.asBareJid().toString());
-        builder.setContentText(message);
-        builder.setSmallIcon(R.drawable.ic_launcher_background);
-        builder.setAutoCancel(true);
-        builder.setContentIntent(resultPendingIntent);
-
-        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(ChatsList.this);
-        managerCompat.notify(1, builder.build());
-    }
-
     public void runMessageListener(){
-        MainActivity.chatManager.addIncomingListener(new IncomingChatMessageListener() {
+        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());
-                }
-
-
-                for (ChatItem chatItem : chatItems){
+                for (ChatItem chatItem : XMPPClass.chatItems){
                     if (chatItem.jid.trim().equals(from.asBareJid().toString().trim())){
 
                         chatItem.message = message.getBody();
@@ -225,7 +162,7 @@ public class ChatsList extends AppCompatActivity {
                 }
             }
         });
-        MainActivity.roster.addRosterListener(new RosterListener() {
+        XMPPClass.roster.addRosterListener(new RosterListener() {
             @Override
             public void entriesAdded(Collection<Jid> addresses) {
                 System.out.println(addresses);
@@ -242,7 +179,7 @@ public class ChatsList extends AppCompatActivity {
             }
             @Override
             public void presenceChanged(Presence presence) {
-                for (ChatItem chatItem : chatItems){
+                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";

+ 56 - 66
app/src/main/java/com/drinkertea/test2smack/MainActivity.java

@@ -1,103 +1,93 @@
 package com.drinkertea.test2smack;
 
 import androidx.appcompat.app.AppCompatActivity;
-import androidx.core.app.NotificationCompat;
-import androidx.core.app.NotificationManagerCompat;
 
-import android.app.Notification;
-import android.app.NotificationChannel;
-import android.app.NotificationManager;
 import android.content.Intent;
-import android.os.AsyncTask;
-import android.os.Build;
 import android.os.Bundle;
-import android.os.Handler;
-import android.provider.Settings;
 import android.view.View;
-import android.widget.Button;
-import android.widget.TextView;
+import android.widget.EditText;
 
 import org.jivesoftware.smack.AbstractXMPPConnection;
-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.smack.roster.RosterListener;
-import org.jivesoftware.smack.tcp.XMPPTCPConnection;
-import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
 import org.jivesoftware.smackx.mam.MamManager;
-import org.jivesoftware.smackx.search.ReportedData;
-import org.jivesoftware.smackx.search.UserSearchManager;
-import org.jivesoftware.smackx.xdata.form.Form;
+import org.jivesoftware.smackx.muc.MultiUserChat;
+import org.jivesoftware.smackx.muc.MultiUserChatManager;
 import org.jxmpp.jid.EntityBareJid;
-import org.jxmpp.jid.Jid;
+import org.jxmpp.jid.EntityFullJid;
 import org.jxmpp.jid.impl.JidCreate;
+import org.jxmpp.jid.parts.Resourcepart;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Timer;
-import java.util.TimerTask;
 
-public class MainActivity extends AppCompatActivity {
-
-    TextView textView;
-    public static ChatManager chatManager;
-    public static AbstractXMPPConnection connection;
-    public static Roster roster;
 
+public class MainActivity extends AppCompatActivity {
 
+    private EditText ETemail;
+    private EditText ETpassword;
+    String email;
+    String password;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
-        textView = (TextView) findViewById(R.id.textView);
 
+        ETemail = (EditText) findViewById(R.id.ETEmail);
+        ETpassword = (EditText) findViewById(R.id.ETPassword);
 
     }
 
-    public void goToPageWithChats(View view) throws Exception{
-        System.out.println("Start");
-        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
-                .setXmppDomain("msg.sharix-app.org")
-                .setHost("46.138.247.90")
-                .build();
-        System.out.println("Success config");
-
-        connection = new XMPPTCPConnection(config);
-        connection.connect();
-        System.out.println("Success connection!");
-
-        connection.login("test", "test1234@");
-        chatManager = ChatManager.getInstanceFor(connection);
+    public void RunService(){
+        if(!NotificationService.serverIsWorked){
+            startService(new Intent(this, NotificationService.class));
+        }
+        else {
+            System.out.println("Server work!");
+        }
+    }
 
-        System.out.println(connection.getUser());
-        roster = Roster.getInstanceFor(connection);
-        /*Collection<RosterEntry> entries = roster.getEntries();
-        Presence presence;
-        for (RosterEntry entry : entries){
-            presence = roster.getPresence(entry.getJid());
-            if (presence.getType().toString() == "available"){
-                System.out.println(entry.getJid());
-                System.out.println(presence.getType());
+    public void goToPageWithChats(View view){
+        try {
+            email = ETemail.getText().toString();
+            password = ETpassword.getText().toString();
+            //Авторизация
+            boolean isLogin = XMPPClass.XMPP_Connection(email, password);
+            //Переход и заупск сервера
+            if (isLogin && XMPPClass.connection.getUser() != null){
+                RunService();
+                Intent intent = new Intent(this, ChatsList.class);
+                startActivity(intent);
+            }
+            else {
+                System.out.println("Error connect!");
             }
+        }catch (Exception ex){
+            System.out.println(ex.getMessage());
         }
-        runMessageListener();
-        */
-        System.out.println("Success chat connection!");
-        Intent intent = new Intent(this, ChatsList.class);
-        startActivity(intent);
     }
 
-    public void testMethod(View view) {
-        //createNotificationChannel();
+    public void testMethod(View view) throws Exception{
+        email = ETemail.getText().toString();
+        password = ETpassword.getText().toString();
+        boolean isLogin = XMPPClass.XMPP_Connection("evgeny.polivanov", "Anthem3dense8Occult");
+
+        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());
+            }
+        }
+        else {
+            System.out.println("Error connect!");
+        }
     }
 
-
+    public void stopNotificationClick(View view) {
+        stopService(new Intent(this, NotificationService.class));
+    }
 }

+ 117 - 0
app/src/main/java/com/drinkertea/test2smack/NotificationService.java

@@ -0,0 +1,117 @@
+package com.drinkertea.test2smack;
+
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.app.Service;
+import android.content.Intent;
+import android.os.Build;
+import android.os.IBinder;
+
+import androidx.core.app.NotificationCompat;
+import androidx.core.app.NotificationManagerCompat;
+import androidx.core.app.TaskStackBuilder;
+
+import org.jivesoftware.smack.AbstractXMPPConnection;
+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.jxmpp.jid.EntityBareJid;
+import org.jxmpp.jid.impl.JidCreate;
+import org.jxmpp.stringprep.XmppStringprepException;
+
+
+public class NotificationService extends Service {
+
+    public static boolean serverIsWorked = false;
+    NotificationCompat.Builder builder;
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        try {
+            if(!serverIsWorked){
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            task();
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }).start();
+                serverIsWorked = true;
+                builder = new NotificationCompat.Builder(this, "My channel")
+                        .setContentText("Notification server was running!")
+                        .setContentTitle("Assist chat")
+                        .setSmallIcon(R.drawable.ic_launcher_background);
+                startForeground(1001, builder.build());
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return super.onStartCommand(intent, flags, startId);
+    }
+
+    @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);
+        }
+        System.out.println("Server started!");
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        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 createNotificationChannel(EntityBareJid from, String message) {
+        try {
+            XMPPClass.chatJid = JidCreate.entityBareFrom(from);
+        } catch (XmppStringprepException e) {
+            e.printStackTrace();
+        }
+        Intent resultIntent = new Intent(this, ChatActivity.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.drawable.ic_launcher_background);
+        builder.setAutoCancel(true);
+        builder.setContentIntent(resultPendingIntent);
+
+        startForeground(1001, builder.build());
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        serverIsWorked = false;
+        System.out.println("Server destroy!");
+    }
+}

+ 103 - 0
app/src/main/java/com/drinkertea/test2smack/Room.java

@@ -0,0 +1,103 @@
+package com.drinkertea.test2smack;
+
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.recyclerview.widget.RecyclerView;
+
+import android.graphics.Color;
+import android.graphics.Typeface;
+import android.graphics.drawable.GradientDrawable;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+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.jxmpp.jid.EntityBareJid;
+
+public class Room extends AppCompatActivity {
+
+    public LinearLayout roomLayout;
+    public EditText roomMessageET;
+    ChatManager chatManager = XMPPClass.chatManager;
+    EntityBareJid jid;
+    {
+        try {
+            jid = XMPPClass.chatJid;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+    @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);
+        XMPPClass.CreateRoomListMessage(10);
+        for(Message message : XMPPClass.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);
+            }
+        }
+        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);
+
+        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);
+
+        roomLayout.addView(linearLayout);
+    }
+    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());
+                    }
+                });
+
+            }
+        });
+    }
+
+    public void sendMessage(View view) {
+        try {
+            String text = roomMessageET.getText().toString();
+            XMPPClass.muc.sendMessage(text);
+            addMessage(XMPPClass.myJid.asBareJid().toString(), text, false);
+            roomMessageET.setText("");
+        }catch (Exception e){
+            System.out.println(e.getMessage());
+        }
+
+    }
+}

+ 122 - 0
app/src/main/java/com/drinkertea/test2smack/XMPPClass.java

@@ -0,0 +1,122 @@
+package com.drinkertea.test2smack;
+
+import org.jivesoftware.smack.AbstractXMPPConnection;
+import org.jivesoftware.smack.chat2.ChatManager;
+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.smack.tcp.XMPPTCPConnection;
+import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
+import org.jivesoftware.smackx.mam.MamManager;
+import org.jivesoftware.smackx.muc.MultiUserChat;
+import org.jivesoftware.smackx.muc.MultiUserChatManager;
+import org.jxmpp.jid.EntityBareJid;
+import org.jxmpp.jid.EntityFullJid;
+import org.jxmpp.jid.impl.JidCreate;
+import org.jxmpp.jid.parts.Resourcepart;
+import org.jxmpp.stringprep.XmppStringprepException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class XMPPClass {
+
+    public static ChatManager chatManager;
+    public static AbstractXMPPConnection connection;
+    public static Roster roster;
+    public static EntityBareJid chatJid;
+    public static EntityBareJid myJid;
+    public static ArrayList<ChatItem> chatItems = new ArrayList<ChatItem>();
+    public static List<Message> messages;
+    public static MultiUserChat muc;
+
+    public static void XMPP_JoinRoom(String room_name, String nickname){
+        try {
+            EntityBareJid jid = JidCreate.entityBareFrom(room_name);
+            MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(XMPPClass.connection);
+            muc = manager.getMultiUserChat(jid);
+            Resourcepart room  = Resourcepart.from(nickname);
+            muc.join(room);
+            for(EntityFullJid jid1 : muc.getOccupants()){
+                System.out.println(jid1);
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+
+    }
+
+    public static boolean XMPP_Connection(String login, String password) throws Exception{
+        try {
+            myJid = JidCreate.entityBareFrom(login + "@msg.sharix-app.org");
+            XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
+                    .setXmppDomain("msg.sharix-app.org")
+                    .setHost("46.138.247.90")
+                    .build();
+            connection = new XMPPTCPConnection(config);
+            connection.connect();
+            connection.login(login, password);
+            chatManager = ChatManager.getInstanceFor(connection);
+            roster = Roster.getInstanceFor(connection);
+            return true;
+        }catch (Exception ex){
+            System.out.println(ex.getMessage());
+            return false;
+        }
+    }
+
+    public static void CreateChatListMessage(EntityBareJid jid, int message_count){
+        try {
+            MamManager mamManager = MamManager.getInstanceFor(connection);
+            MamManager.MamQueryArgs mamQueryArgs = MamManager.MamQueryArgs.builder()
+                    .limitResultsToJid(jid)
+                    .setResultPageSizeTo(message_count)
+                    .queryLastPage()
+                    .build();
+            MamManager.MamQuery mamQuery = mamManager.queryArchive(mamQueryArgs);
+            messages = mamQuery.getMessages();
+        }catch (Exception ex){
+        }
+    }
+
+    public static void CreateRoomListMessage(int message_count){
+        try {
+            MamManager mamManager = MamManager.getInstanceFor(muc);
+            MamManager.MamQueryArgs mamQueryArgs = MamManager.MamQueryArgs.builder()
+                    .setResultPageSizeTo(message_count)
+                    .queryLastPage()
+                    .build();
+            MamManager.MamQuery mamQuery = mamManager.queryArchive(mamQueryArgs);
+            messages = mamQuery.getMessages();
+        }catch (Exception ex){
+        }
+    }
+
+    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;
+            }
+
+            if (presence.getType().toString().trim().equals("unavailable")){
+                chatItem.status = "Offline";
+            }
+            else chatItem.status = "Online";
+
+            chatItem.message = null;
+            chatItems.add(chatItem);
+        }
+    }
+
+}

+ 29 - 0
app/src/main/java/com/drinkertea/test2smack/rooms.java

@@ -0,0 +1,29 @@
+package com.drinkertea.test2smack;
+
+import androidx.appcompat.app.AppCompatActivity;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import org.jivesoftware.smack.packet.Message;
+
+public class rooms extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_rooms);
+    }
+
+    public void goToRoom(View view) {
+        LinearLayout linearLayout = (LinearLayout) view;
+        TextView textView = (TextView) linearLayout.getChildAt(1);
+        String room_name = textView.getText().toString();
+        XMPPClass.XMPP_JoinRoom(room_name, "Evgen Polivanov");
+        Intent intent = new Intent(this, Room.class);
+        startActivity(intent);
+    }
+}

+ 22 - 21
app/src/main/res/layout/activity_chat.xml

@@ -6,30 +6,32 @@
     android:layout_height="match_parent"
     tools:context=".ChatActivity">
 
-    <EditText
-        android:id="@+id/testView"
-        android:layout_width="300dp"
-
-        android:layout_height="40dp"
-        android:text="hello world"
-        app:layout_constraintBottom_toTopOf="@+id/btnSendMessage"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent" />
-
-    <Button
-        android:id="@+id/btnSendMessage"
-        android:layout_width="200dp"
-        android:layout_height="40dp"
-        android:onClick="sendMessage"
-        android:text="Send message"
+    <LinearLayout
+        android:id="@+id/linearLayout"
+        android:layout_width="wrap_content"
+        android:layout_height="60dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent" />
+        app:layout_constraintStart_toStartOf="parent">
+
+        <EditText
+            android:id="@+id/testView"
+            android:layout_width="300dp"
+            android:layout_height="40dp"
+            android:text="hello world" />
+
+        <Button
+            android:id="@+id/btnSendMessage"
+            android:layout_width="wrap_content"
+            android:layout_height="40dp"
+            android:onClick="sendMessage"
+            android:text="Send" />
+    </LinearLayout>
 
     <ScrollView
         android:id="@+id/chatScroll"
-        android:layout_width="409dp"
-        android:layout_height="600dp"
+        android:layout_width="match_parent"
+        android:layout_height="500dp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent">
@@ -38,8 +40,7 @@
             android:id="@+id/chatLayout"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:orientation="vertical">
-        </LinearLayout>
+            android:orientation="vertical"></LinearLayout>
     </ScrollView>
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 60 - 24
app/src/main/res/layout/activity_main.xml

@@ -1,41 +1,77 @@
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">
 
-    <TextView
-        android:id="@+id/textView"
+    <LinearLayout
+        android:id="@+id/linearLayout3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="Hello World!"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
-    <Button
-        android:id="@+id/BtnConnect"
-        android:layout_width="200dp"
-        android:layout_height="40dp"
-        android:text="Connect"
-        android:onClick="goToPageWithChats"
-        app:layout_constraintBottom_toBottomOf="parent"
+        android:orientation="vertical"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/textView" />
-    <Button
-        android:id="@+id/test"
-        android:layout_width="200dp"
-        android:layout_height="40dp"
-        android:text="Test"
-        android:onClick="testMethod"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintBottom_toTopOf="@+id/linearLayout2">
+
+        <EditText
+            android:id="@+id/ETEmail"
+            android:layout_width="250dp"
+            android:layout_height="wrap_content"
+            android:hint="Email"
+            app:layout_constraintBottom_toTopOf="@+id/ETPassword" />
+
+        <EditText
+            android:id="@+id/ETPassword"
+            android:layout_width="250dp"
+            android:layout_height="wrap_content"
+            android:hint="Password"
+            android:inputType="textPassword"
+            app:layout_constraintTop_toBottomOf="@+id/ETEmail"
+            tools:ignore="MissingConstraints" />
+    </LinearLayout>
+
+
+    <LinearLayout
+        android:id="@+id/linearLayout2"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/BtnConnect" />
+        app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
+        tools:ignore="MissingConstraints">
+
+        <Button
+            android:id="@+id/BtnConnect"
+            android:layout_width="200dp"
+            android:layout_height="40dp"
+            android:onClick="goToPageWithChats"
+            android:text="Chats"
+            app:layout_constraintBottom_toTopOf="@+id/test"
+            app:layout_constraintTop_toBottomOf="@+id/ETPassword" />
+
+        <Button
+            android:id="@+id/test"
+            android:layout_width="200dp"
+            android:layout_height="40dp"
+            android:onClick="testMethod"
+            android:text="Rooms"
+            app:layout_constraintBottom_toTopOf="@+id/button"
+            app:layout_constraintTop_toBottomOf="@+id/BtnConnect" />
+
+        <Button
+            android:id="@+id/button"
+            android:layout_width="200dp"
+            android:layout_height="40dp"
+            android:onClick="stopNotificationClick"
+            android:text="Stop notification"
+            app:layout_constraintTop_toBottomOf="@+id/test" />
+    </LinearLayout>
+
 
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 45 - 0
app/src/main/res/layout/activity_room.xml

@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    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"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent">
+
+        <LinearLayout
+            android:id="@+id/roomLayout"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical">
+
+        </LinearLayout>
+    </ScrollView>
+    <LinearLayout
+        android:id="@+id/linearLayout"
+        android:layout_width="wrap_content"
+        android:layout_height="60dp"
+        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:text="hello world" />
+
+        <Button
+            android:id="@+id/btnSendMessage"
+            android:layout_width="wrap_content"
+            android:layout_height="40dp"
+            android:onClick="sendMessage"
+            android:text="Send" />
+    </LinearLayout>
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 63 - 0
app/src/main/res/layout/activity_rooms.xml

@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".rooms">
+    <ScrollView
+        android:id="@+id/roomsListScroll"
+        android:layout_width="409dp"
+        android:layout_height="600dp"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent">
+
+        <LinearLayout
+            android:id="@+id/roomsListLayout"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical">
+            <LinearLayout
+                android:layout_margin="8dp"
+                android:padding="5dp"
+                android:onClick="goToRoom"
+                android:background="@color/cardview_shadow_start_color"
+                android:orientation="vertical"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content">
+                <TextView
+                    android:text="ShariX Backend"
+                    android:textColor="@color/black"
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"/>
+                <TextView
+                    android:text="backend@chat.msg.sharix-app.org"
+                    android:textSize="12dp"
+
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"/>
+            </LinearLayout>
+            <LinearLayout
+                android:layout_margin="8dp"
+                android:padding="5dp"
+                android:background="@color/cardview_shadow_start_color"
+                android:orientation="vertical"
+                android:onClick="goToRoom"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content">
+                <TextView
+                    android:text="ShariX TEAM"
+                    android:textColor="@color/black"
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"/>
+                <TextView
+
+                    android:text="team@chat.msg.sharix-app.org"
+                    android:textSize="12dp"
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"/>
+            </LinearLayout>
+        </LinearLayout>
+    </ScrollView>
+</androidx.constraintlayout.widget.ConstraintLayout>