|
@@ -0,0 +1,258 @@
|
|
|
|
+package com.drinkertea.test2smack;
|
|
|
|
+
|
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
+import androidx.core.app.NotificationCompat;
|
|
|
|
+import androidx.core.app.NotificationManagerCompat;
|
|
|
|
+import androidx.core.app.TaskStackBuilder;
|
|
|
|
+
|
|
|
|
+import android.app.Notification;
|
|
|
|
+import android.app.NotificationChannel;
|
|
|
|
+import android.app.NotificationManager;
|
|
|
|
+import android.app.PendingIntent;
|
|
|
|
+import android.content.Intent;
|
|
|
|
+import android.graphics.Color;
|
|
|
|
+import android.graphics.Typeface;
|
|
|
|
+import android.icu.text.SymbolTable;
|
|
|
|
+import android.os.Build;
|
|
|
|
+import android.os.Bundle;
|
|
|
|
+import android.os.Handler;
|
|
|
|
+import android.view.View;
|
|
|
|
+import android.widget.LinearLayout;
|
|
|
|
+import android.widget.TextView;
|
|
|
|
+
|
|
|
|
+import org.jivesoftware.smack.chat2.Chat;
|
|
|
|
+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.jxmpp.jid.EntityBareJid;
|
|
|
|
+import org.jxmpp.jid.Jid;
|
|
|
|
+import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
+import org.jxmpp.stringprep.XmppStringprepException;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Collection;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.Comparator;
|
|
|
|
+import java.util.Timer;
|
|
|
|
+import java.util.TimerTask;
|
|
|
|
+class FishNameComparator implements Comparator<ChatItem>
|
|
|
|
+{
|
|
|
|
+ public int compare(ChatItem left, ChatItem right) {
|
|
|
|
+ return right.status.compareTo(left.status);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+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());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ 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";
|
|
|
|
+
|
|
|
|
+ chatItem.message = null;
|
|
|
|
+ chatItems.add(chatItem);
|
|
|
|
+ }
|
|
|
|
+ generateChatsList();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void generateChatsList(){
|
|
|
|
+ linearLayout.removeAllViews();
|
|
|
|
+ Collections.sort(chatItems, new FishNameComparator());
|
|
|
|
+ for (ChatItem chatItem : chatItems){
|
|
|
|
+ TextView Name = new TextView(this);
|
|
|
|
+ TextView Status = new TextView(this);
|
|
|
|
+ TextView NewMessage = new TextView(this);
|
|
|
|
+ LinearLayout Container = new LinearLayout(this);
|
|
|
|
+
|
|
|
|
+ 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 == "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(15, 15, 15, 15);
|
|
|
|
+ Container.setLayoutParams(layoutParams);
|
|
|
|
+
|
|
|
|
+ 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 {
|
|
|
|
+ jid = JidCreate.entityBareFrom(chatItem.jid);
|
|
|
|
+ chatItem.message = null;
|
|
|
|
+ Intent intent = new Intent(ChatsList.this, ChatActivity.class);
|
|
|
|
+ startActivity(intent);
|
|
|
|
+ }catch (Exception ex){
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ linearLayout.addView(Container);
|
|
|
|
+ }
|
|
|
|
+ runMessageListener();
|
|
|
|
+ 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() {
|
|
|
|
+ @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){
|
|
|
|
+ if (chatItem.jid.trim().equals(from.asBareJid().toString().trim())){
|
|
|
|
+
|
|
|
|
+ chatItem.message = message.getBody();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ MainActivity.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 entriesDeleted(Collection<Jid> addresses) {
|
|
|
|
+ System.out.println(addresses);
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void presenceChanged(Presence presence) {
|
|
|
|
+ for (ChatItem chatItem : 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";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+}
|