Advertisement
Pavle_nis

Java Android Chat Application

Jun 17th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | None | 0 0
  1. public class Chat extends AppCompatActivity
  2. {
  3.     LinearLayout layout;
  4.     RelativeLayout layout_2;
  5.     ImageView sendButton;
  6.     EditText messageArea;
  7.     ScrollView scrollView;
  8.     Firebase reference1, reference2;
  9.     boolean timerProcessing = false;
  10.     boolean timerStarts = false;
  11.     String date = "";
  12.     String message;
  13.     String userName;
  14.     int value = 0;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState)
  18.     {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_chat);
  21.  
  22.         layout = (LinearLayout) findViewById(R.id.layout1);
  23.         layout_2 = (RelativeLayout)findViewById(R.id.layout2);
  24.         sendButton = (ImageView)findViewById(R.id.sendButton);
  25.         messageArea = (EditText)findViewById(R.id.messageArea);
  26.         scrollView = (ScrollView)findViewById(R.id.scrollView);
  27.  
  28.         Firebase.setAndroidContext(this);
  29.         reference1 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith);
  30.         reference2 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username);
  31.  
  32.         sendButton.setOnClickListener(new View.OnClickListener() {
  33.             @Override
  34.             public void onClick(View v) {
  35.                 String messageText = messageArea.getText().toString();
  36.  
  37.                 if(!messageText.equals(""))
  38.                 {
  39.                     Map<String, String> map = new HashMap<String, String>();
  40.                     map.put("message", messageText);
  41.                     map.put("user", UserDetails.username);
  42.  
  43.                     reference1.push().setValue(map);
  44.                     reference2.push().setValue(map);
  45.                     messageArea.setText("");
  46.                 }
  47.             }
  48.         });
  49.  
  50.         reference1.addChildEventListener(new ChildEventListener()
  51.         {
  52.             @Override
  53.             public void onChildAdded(DataSnapshot dataSnapshot, String s)
  54.             {
  55.                 Map map = (Map) dataSnapshot.getValue();
  56.  
  57.                 message = map.get("message").toString();
  58.                 userName = map.get("user").toString();
  59.  
  60.  
  61.                 String lastMessage = String.valueOf(map.values().toArray()[map.size() - 2]);
  62.                 Log.e("LAST MESSAGE", lastMessage);
  63.  
  64.                 if (userName.equals(UserDetails.username))
  65.                 {
  66.                     addMessageBox(message, 1);
  67.                 }
  68.                 else
  69.                 {
  70.                     addMessageBox(message, 2);
  71.                     if(message == lastMessage)
  72.                     {
  73.                         sendNotification(lastMessage);
  74.                     }
  75.                 }
  76.             }
  77.  
  78.             @Override
  79.             public void onChildChanged(final DataSnapshot dataSnapshot, String s)
  80.             {
  81.  
  82.             }
  83.  
  84.             @Override
  85.             public void onChildRemoved(DataSnapshot dataSnapshot) {
  86.  
  87.             }
  88.  
  89.             @Override
  90.             public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  91.  
  92.             }
  93.  
  94.             @Override
  95.             public void onCancelled(FirebaseError firebaseError) {
  96.  
  97.             }
  98.         });
  99.  
  100.         scrollView.post(new Runnable() {
  101.             @Override
  102.             public void run() {
  103.                 scrollView.fullScroll(View.FOCUS_DOWN);
  104.             }
  105.         });
  106.     }
  107.     TextView textView,textView2;
  108.     public void addMessageBox(String message, int type)
  109.     {
  110.         DateFormat df = new SimpleDateFormat("h:mm a");
  111.         date = df.format(Calendar.getInstance().getTime());
  112.  
  113.         TextView textView1 = new TextView(Chat.this);
  114.         textView1.setText(date);
  115.         textView1.setTextSize(10);
  116.  
  117.         textView = new TextView(Chat.this);
  118.         textView.setText(message);
  119.  
  120.         LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  121.         lp2.weight = 1.0f;
  122.  
  123.         if(type == 1) {
  124.             lp2.gravity = Gravity.RIGHT;
  125.             textView.setBackgroundResource(R.drawable.left_bubble);
  126.         }
  127.         else{
  128.             lp2.gravity = Gravity.LEFT;
  129.             textView.setBackgroundResource(R.drawable.right_bubble);
  130.         }
  131.         textView.setLayoutParams(lp2);
  132.         layout.addView(textView);
  133.  
  134.         textView1.setLayoutParams(lp2);
  135.         layout.addView(textView1);
  136.  
  137.         scrollView.post(new Runnable() {
  138.             @Override
  139.             public void run() {
  140.                 scrollView.fullScroll(View.FOCUS_DOWN);
  141.             }
  142.         });
  143.     }
  144.  
  145.     private void sendNotification(String message)
  146.     {
  147.         Intent intent = new Intent(this,Chat.class);
  148.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  149.  
  150.         PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
  151.  
  152.         Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  153.  
  154.         NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  155.  
  156.         NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
  157.                 .setVisibility(Notification.VISIBILITY_PUBLIC)
  158.                 .setSmallIcon(R.mipmap.ic_launcher)
  159.                 .setContentTitle(userName)
  160.                 .setContentText(message)
  161.                 .setSound(notificationSound)
  162.                 .setContentIntent(pendingIntent);
  163.  
  164.         NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  165.         mNotificationManager.notify(0, builder.build());
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement