Advertisement
Pavle_nis

Chat

Aug 26th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.87 KB | None | 0 0
  1. package audiophileradio.example.com.audiophileradio;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.media.MediaPlayer;
  9. import android.media.RingtoneManager;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.os.CountDownTimer;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.support.v7.app.NotificationCompat;
  15. import android.util.Log;
  16. import android.view.Gravity;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.EditText;
  22. import android.widget.ImageView;
  23. import android.widget.LinearLayout;
  24. import android.widget.RelativeLayout;
  25. import android.widget.RemoteViews;
  26. import android.widget.ScrollView;
  27. import android.widget.TextView;
  28.  
  29. import com.firebase.client.ChildEventListener;
  30. import com.firebase.client.DataSnapshot;
  31. import com.firebase.client.Firebase;
  32. import com.firebase.client.FirebaseError;
  33. import com.firebase.client.realtime.util.StringListReader;
  34.  
  35. import java.text.DateFormat;
  36. import java.text.SimpleDateFormat;
  37. import java.util.Calendar;
  38. import java.util.HashMap;
  39. import java.util.Map;
  40.  
  41.  
  42. public class Chat extends AppCompatActivity
  43. {
  44.     LinearLayout layout;
  45.     ImageView sendButton;
  46.     EditText messageArea;
  47.     ScrollView scrollView;
  48.     Firebase reference1, reference2;
  49.     boolean timerProcessing = false;
  50.     boolean timerStarts = false;
  51.     String date = "";
  52.     String message;
  53.     String userName;
  54.     int value = 0;
  55.  
  56.     @Override
  57.     protected void onCreate(Bundle savedInstanceState)
  58.     {
  59.         super.onCreate(savedInstanceState);
  60.         setContentView(R.layout.activity_chat);
  61.  
  62.         layout = (LinearLayout) findViewById(R.id.layout1);
  63.         layout_2 = (RelativeLayout)findViewById(R.id.layout2);
  64.         sendButton = (ImageView)findViewById(R.id.sendButton);
  65.         messageArea = (EditText)findViewById(R.id.messageArea);
  66.         scrollView = (ScrollView)findViewById(R.id.scrollView);
  67.  
  68.         Firebase.setAndroidContext(this);
  69.         reference1 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith);
  70.         reference2 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username);
  71.  
  72.         sendButton.setOnClickListener(new View.OnClickListener() {
  73.             @Override
  74.             public void onClick(View v) {
  75.                 String messageText = messageArea.getText().toString();
  76.  
  77.                 if(!messageText.equals(""))
  78.                 {
  79.                     Map<String, String> map = new HashMap<String, String>();
  80.                     map.put("message", messageText);
  81.  
  82.  
  83.                     map.put("user", UserDetails.username);
  84.  
  85.                     reference1.push().setValue(map);
  86.                     reference2.push().setValue(map);
  87.                     messageArea.setText("");
  88.                 }
  89.             }
  90.         });
  91.  
  92.         reference1.addChildEventListener(new ChildEventListener()
  93.         {
  94.             @Override
  95.             public void onChildAdded(DataSnapshot dataSnapshot, String s)
  96.             {
  97.                 Map map = (Map) dataSnapshot.getValue();
  98.  
  99.                 message = map.get("message").toString();
  100.                 userName = map.get("user").toString();
  101.  
  102.                 if (userName.equals(UserDetails.username))
  103.                 {
  104.                     addMessageBox(message, 1);
  105.                 }
  106.                 else
  107.                 {
  108.                     addMessageBox(message, 2);
  109.                     sendNotification(lastMessage);
  110.                 }
  111.             }
  112.  
  113.             @Override
  114.             public void onChildChanged(final DataSnapshot dataSnapshot, String s)
  115.             {
  116.  
  117.             }
  118.  
  119.             @Override
  120.             public void onChildRemoved(DataSnapshot dataSnapshot) {
  121.  
  122.             }
  123.  
  124.             @Override
  125.             public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  126.  
  127.             }
  128.  
  129.             @Override
  130.             public void onCancelled(FirebaseError firebaseError) {
  131.  
  132.             }
  133.         });
  134.  
  135.         timer.cancel();
  136.  
  137.         scrollView.post(new Runnable() {
  138.             @Override
  139.             public void run() {
  140.                 scrollView.fullScroll(View.FOCUS_DOWN);
  141.             }
  142.         });
  143.     }
  144.  
  145.     public void addMessageBox(String message, int type)
  146.     {
  147.         DateFormat df = new SimpleDateFormat("h:mm a");
  148.         date = df.format(Calendar.getInstance().getTime());
  149.  
  150.         TextView textView1 = new TextView(Chat.this);
  151.         textView1.setText(date);
  152.         textView1.setTextSize(10);
  153.  
  154.         textView = new TextView(Chat.this);
  155.         textView.setText(message);
  156.  
  157.         LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  158.         lp2.weight = 1.0f;
  159.  
  160.         if(type == 1) {
  161.             lp2.gravity = Gravity.RIGHT;
  162.             textView.setBackgroundResource(R.drawable.left_bubble);
  163.         }
  164.         else{
  165.             lp2.gravity = Gravity.LEFT;
  166.             textView.setBackgroundResource(R.drawable.right_bubble);
  167.         }
  168.         textView.setLayoutParams(lp2);
  169.         layout.addView(textView);
  170.  
  171.         textView1.setLayoutParams(lp2);
  172.         layout.addView(textView1);
  173.  
  174.         scrollView.post(new Runnable() {
  175.             @Override
  176.             public void run() {
  177.                 scrollView.fullScroll(View.FOCUS_DOWN);
  178.             }
  179.         });
  180.     }
  181.  
  182.     private void sendNotification(String message)
  183.     {
  184.         Intent intent = new Intent(this,Chat.class);
  185.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  186.  
  187.         PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
  188.  
  189.         Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  190.  
  191.         NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  192.  
  193.         NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
  194.                 .setVisibility(Notification.VISIBILITY_PUBLIC)
  195.                 .setSmallIcon(R.mipmap.ic_launcher)
  196.                 .setContentTitle(userName)
  197.                 .setContentText(message)
  198.                 .setSound(notificationSound)
  199.                 .setContentIntent(pendingIntent);
  200.  
  201.         NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  202.         mNotificationManager.notify(0, builder.build());
  203.     }
  204.  
  205.     @Override
  206.     public boolean onCreateOptionsMenu(Menu menu)
  207.     {
  208.         // Inflate the menu; this adds items to the action bar if it is present.
  209.         getMenuInflater().inflate(R.menu.menu_main, menu);
  210.         return true;
  211.     }
  212.  
  213.     @Override
  214.     public boolean onOptionsItemSelected(MenuItem item)
  215.     {
  216.         // Handle action bar item clicks here. The action bar will
  217.         // automatically handle clicks on the Home/Up button, so long
  218.         // as you specify a parent activity in AndroidManifest.xml.
  219.         int id = item.getItemId();
  220.  
  221.         //noinspection SimplifiableIfStatement
  222.         if (id == R.id.action_settings)
  223.         {
  224.             //startActivityForResult(new Intent(this,ChatSettings.class),SETTINGS_ACTION);
  225.             Intent intent = new Intent(this, ChatSettings.class);
  226.             startActivity(intent);
  227.             finish();
  228.         }
  229.  
  230.         return super.onOptionsItemSelected(item);
  231.     }
  232.  
  233.     @Override
  234.     public void onDestroy()
  235.     {
  236.         super.onDestroy();
  237.  
  238.         NotificationManager mNotificationManager =
  239.                 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  240.         mNotificationManager.cancel(0);
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement