|
@@ -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";
|