Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.34 KB | None | 0 0
  1. public class GroupFragment extends Fragment {
  2.     // TODO: Rename parameter arguments, choose names that match
  3.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  4.     private static final String ARG_PARAM1 = "param1";
  5.     private static final String ARG_PARAM2 = "param2";
  6.  
  7.     // TODO: Rename and change types of parameters
  8.     private String mParam1;
  9.     private String mParam2;
  10.  
  11.     static ArrayList<Group> groupNameList = new ArrayList<Group>();
  12.     static ArrayList<String> groupKeyValues = new ArrayList<String>();
  13.     GroupAdapter groupAdapter;
  14.     ListView lstViewGroup;
  15.  
  16.     private Button RegisterNewGroup;
  17.  
  18.     private OnFragmentInteractionListener mListener;
  19.  
  20.     /**
  21.      * Use this factory method to create a new instance of
  22.      * this fragment using the provided parameters.
  23.      *
  24.      * @param param1 Parameter 1.
  25.      * @param param2 Parameter 2.
  26.      * @return A new instance of fragment GroupFragment.
  27.      */
  28.     // TODO: Rename and change types and number of parameters
  29.     public static GroupFragment newInstance(String param1, String param2) {
  30.         GroupFragment fragment = new GroupFragment();
  31.         Bundle args = new Bundle();
  32.         args.putString(ARG_PARAM1, param1);
  33.         args.putString(ARG_PARAM2, param2);
  34.         fragment.setArguments(args);
  35.         return fragment;
  36.     }
  37.  
  38.     public GroupFragment() {
  39.         // Required empty public constructor
  40.     }
  41.  
  42.     @Override
  43.     public void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         if (getArguments() != null) {
  46.             mParam1 = getArguments().getString(ARG_PARAM1);
  47.             mParam2 = getArguments().getString(ARG_PARAM2);
  48.         }
  49.  
  50.         groupKeyValues.clear();
  51.         ReadGroupData();
  52.     }
  53.  
  54.     @Override
  55.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  56.                              Bundle savedInstanceState) {
  57.         // Inflate the layout for this fragment
  58.         View view = inflater.inflate(R.layout.fragment_group, container, false);
  59.  
  60.         RegisterNewGroup = (Button)view.findViewById(R.id.btn_reg_new_group);
  61.         RegisterNewGroup.setOnClickListener(new View.OnClickListener() {
  62.             @Override
  63.             public void onClick(View v) {
  64.                     EditText editRegNewGr = (EditText) getView().findViewById(R.id.txt_group_name);
  65.  
  66.                     String groupName = editRegNewGr.getText().toString();
  67.                     if (groupName != "") {
  68.                         CreateNewGroup(groupName);
  69.                     }
  70.                 }
  71.             });
  72.  
  73.         ReadGroupData();
  74.  
  75.         return view;
  76.     }
  77.  
  78.     // TODO: Rename method, update argument and hook method into UI event
  79.     public void onButtonPressed(Uri uri) {
  80.         if (mListener != null) {
  81.             mListener.onFragmentInteraction(uri);
  82.         }
  83.     }
  84.     // TODO, Skapa ett testmeddelande!
  85.  
  86.     public void CreateNewGroup(String groupName) {
  87.  
  88.         Map<String, Group> newGroup = new HashMap<>();
  89.  
  90.         Firebase  firebaserootRef = new Firebase("https://luminous-heat-420.firebaseio.com");
  91.         Firebase firebaseGroup = firebaserootRef.child("").push();
  92.  
  93.         Group group = new Group();
  94.  
  95.         group.SetID(firebaseGroup.getKey());
  96.         group.SetName(groupName);
  97.  
  98.         String id = firebaseGroup.getKey();
  99.         firebaseGroup.child("id").setValue(group.GetId());
  100.         firebaseGroup.child("name").setValue(group.GetName());
  101.  
  102.         newGroup.put(id, group);
  103.  
  104.         ChangeToChatFragment(group.GetId());
  105.     }
  106.  
  107.     public void ReadGroupData() {
  108.         Firebase  firebaserootRef = new Firebase("https://luminous-heat-000.firebaseio.com");
  109.         firebaserootRef.addChildEventListener(new ChildEventListener() {
  110.             @Override
  111.             public void onChildAdded(DataSnapshot snapshot, String s) {
  112.                 if (snapshot.getValue() != null) {
  113.                     Group newGroup = new Group((String) snapshot.child("name").getValue(), (String) snapshot.child("id").getValue());
  114.  
  115.                     if(!groupKeyValues.contains(newGroup.GetId())) {
  116.                         groupKeyValues.add(newGroup.GetId());
  117.  
  118.                         AddToLstViewGroup(newGroup);
  119.                         System.out.println("Read group data from firebase and inserted in listView");
  120.                     }
  121.                 }
  122.             }
  123.  
  124.             @Override
  125.             public void onChildChanged(DataSnapshot snapshot, String s) {
  126.             }
  127.             @Override
  128.             public void onChildRemoved(DataSnapshot snapshot) {
  129.             }
  130.             @Override
  131.             public void onChildMoved(DataSnapshot snapshot, String s) {
  132.             }
  133.             @Override
  134.             public void onCancelled(FirebaseError firebaseError) {
  135.             }
  136.         });
  137.     }
  138.  
  139.     public void AddToLstViewGroup(Group newGroup) {
  140.         groupNameList.add(newGroup);
  141.  
  142.         if(groupAdapter == null) {
  143.             groupAdapter = new GroupAdapter(getActivity(), groupNameList);
  144.         }
  145.  
  146.             groupAdapter.notifyDataSetChanged();
  147.  
  148.  
  149.         if (lstViewGroup == null) {
  150.             lstViewGroup = (ListView) getView().findViewById(R.id.listView_group);
  151.         }
  152.  
  153.         lstViewGroup.setOnItemClickListener(onItemClickListener);
  154.         lstViewGroup.setOnItemLongClickListener(onItemLongClickListener);
  155.  
  156.         groupAdapter.notifyDataSetChanged();
  157.         lstViewGroup.setAdapter(groupAdapter);
  158.     }
  159.  
  160.     @Override
  161.     public void onAttach(Activity activity) {
  162.         super.onAttach(activity);
  163.         try {
  164.             mListener = (OnFragmentInteractionListener) activity;
  165.         } catch (ClassCastException e) {
  166.             throw new ClassCastException(activity.toString()
  167.                     + " must implement OnFragmentInteractionListener");
  168.         }
  169.     }
  170.  
  171.     @Override
  172.     public void onDetach() {
  173.         super.onDetach();
  174.         mListener = null;
  175.     }
  176.  
  177.     public void ChangeToChatFragment(String groupId) {
  178.         ChatFragment groupFragment = ChatFragment.newInstance("", "");
  179.         groupFragment.SetGroupID(groupId);
  180.         FragmentManager fM = getFragmentManager();
  181.         FragmentTransaction fT = fM.beginTransaction();
  182.         fT.replace(R.id.container_chat, groupFragment, null);
  183.         fT.addToBackStack("go to chat fragmement");
  184.         fT.commit();
  185.     }
  186.  
  187.     private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
  188.         @Override
  189.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  190.             Log.d("Short clicked on group item in listView!", "GOTO Chatfragment");
  191.             Group group = groupAdapter.getItem(position);
  192.             ChangeToChatFragment(group.GetId());
  193.         }
  194.     };
  195.  
  196.     private AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() {
  197.         @Override
  198.         public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  199.             Log.d("Long clicked on group item in listView!", "GOTO Chatfragment");
  200.             Group group = groupAdapter.getItem(position);
  201.             ChangeToChatFragment(group.GetId());
  202.             return true;
  203.         }
  204.     };
  205.  
  206.     public interface OnFragmentInteractionListener {
  207.         // TODO: Update argument type and name
  208.         public void onFragmentInteraction(Uri uri);
  209.     }
  210.  
  211. }
  212.  
  213.  
  214. public class ChatFragment extends Fragment implements
  215.         View.OnClickListener {
  216.  
  217.     // TODO: Rename parameter arguments, choose names that match
  218.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  219.     private static final String ARG_PARAM1 = "param1";
  220.     private static final String ARG_PARAM2 = "param2";
  221.  
  222.     // TODO: Rename and change types of parameters
  223.     private String mParam1;
  224.     private String mParam2;
  225.  
  226.     private OnFragmentInteractionListener mListener;
  227.  
  228.     static ArrayList<Message> chatMsgList = new ArrayList<>();
  229.     static ArrayList<String> msgKeyValues = new ArrayList<>();
  230.     ChatAdapter chatAdapter;
  231.     ListView lstViewChat = null;
  232.  
  233.     private String groupId;
  234.  
  235.     Firebase firebaserootRef = new Firebase("https://luminous-heat-000.firebaseio.com");
  236.     static String username = "Me";
  237.     EditText editMsg;
  238.     Button btnSend;
  239.  
  240.     /**
  241.      * Use this factory method to create a new instance of
  242.      * this fragment using the provided parameters.
  243.      *
  244.      * @param param1 Parameter 1.
  245.      * @param param2 Parameter 2.
  246.      * @return A new instance of fragment ChatFragment.
  247.      */
  248.     // TODO: Rename and change types and number of parameters
  249.     public static ChatFragment newInstance(String param1, String param2) {
  250.         ChatFragment fragment = new ChatFragment();
  251.         Bundle args = new Bundle();
  252.         args.putString(ARG_PARAM1, param1);
  253.         args.putString(ARG_PARAM2, param2);
  254.         fragment.setArguments(args);
  255.         return fragment;
  256.     }
  257.  
  258.     public ChatFragment() {
  259.         // Required empty public constructor
  260.     }
  261.  
  262.     @Override
  263.     public void onCreate(Bundle savedInstanceState) {
  264.         super.onCreate(savedInstanceState);
  265.         if (getArguments() != null) {
  266.             mParam1 = getArguments().getString(ARG_PARAM1);
  267.             mParam2 = getArguments().getString(ARG_PARAM2);
  268.         }
  269.  
  270.         msgKeyValues.clear();
  271.         ReadChatMessages(firebaserootRef);
  272.     }
  273.  
  274.     @Override
  275.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  276.                              Bundle savedInstanceState) {
  277.         // Inflate the layout for this fragment
  278.         View view = inflater.inflate(R.layout.fragment_chat, container, false);
  279.  
  280.         editMsg = (EditText)  view.findViewById(R.id.txt_message_input);
  281.         btnSend = (Button) view.findViewById(R.id.btnSend);
  282.         btnSend.setOnClickListener(this);
  283.  
  284.         return view;
  285.     }
  286.  
  287.     @Override
  288.     public void onClick(View v) {
  289.         String txtMsg = editMsg.getText().toString();
  290.         if (txtMsg != "") {
  291.             System.out.println("GOTO CreateNewMessage!");
  292.             CreateNewMessage(firebaserootRef, txtMsg);
  293.         }
  294.     }
  295.  
  296.     public void  CreateNewMessage(Firebase firebaserootRef, String textMsg) {
  297.         Map<String, Message> chatMessages = new HashMap<>();
  298.  
  299.         String msg = textMsg;
  300.         String from = username;
  301.         String time = TimeStamp();
  302.         String id = "";
  303.  
  304.         //Message
  305.         if (groupId != "") {
  306.             Firebase firebaseParentMsg = firebaserootRef.child(GetGroupId()).child("messages");
  307.             Firebase firebaseMsg = firebaseParentMsg.push();
  308.  
  309.             Message cm = new Message(id, from, msg, time);
  310.  
  311.             id = firebaseMsg.getKey();
  312.             firebaseMsg.child("from").setValue(cm.GetFrom());
  313.             firebaseMsg.child("message").setValue(cm.GetMsg());
  314.             firebaseMsg.child("time").setValue(cm.GetTime());
  315.  
  316.             chatMessages.put(id,cm);
  317.             System.out.println("Succesfully created a new message.");
  318.         }
  319.  
  320.         ReadChatMessages(firebaserootRef);
  321.     }
  322.  
  323.     public void ReadChatMessages(Firebase firebaseRootRef) {
  324.         firebaseRootRef.addChildEventListener(new ChildEventListener() {
  325.             @Override
  326.             public void onChildAdded(DataSnapshot snapshot, String s) {
  327.                 if (snapshot.child(GetGroupId()).child("messages").getChildren() != null) {
  328.                     for (DataSnapshot c : snapshot.child(GetGroupId()).child("messages").getChildren()) {
  329.                         String key = c.getKey();
  330.  
  331.                         Message newMessage = new Message();
  332.                         newMessage.SetFrom((String) c.child("from").getValue());
  333.                         newMessage.SetMsg((String) c.child("message").getValue());
  334.                         newMessage.SetTime((String) c.child("time").getValue());
  335.                         newMessage.SetId((String) c.child("id").getValue());
  336.  
  337.                         if ((!msgKeyValues.contains(key)) || newMessage.GetFrom() != "") {
  338.                             msgKeyValues.add(key);
  339.  
  340.                             AddToLstViewChat(newMessage);
  341.  
  342.                             //Automatic scrolls to last line in listView.
  343.                             lstViewChat.setSelection(chatAdapter.getCount() -1);
  344.                        }
  345.                     }
  346.                 }
  347.             }
  348.  
  349.             @Override
  350.             public void onChildChanged(DataSnapshot snapshot, String s) {
  351.             }
  352.  
  353.             @Override
  354.             public void onChildRemoved(DataSnapshot snapshot) {
  355.             }
  356.  
  357.             @Override
  358.             public void onChildMoved(DataSnapshot snapshot, String s) {
  359.             }
  360.  
  361.             @Override
  362.             public void onCancelled(FirebaseError firebaseError) {
  363.             }
  364.         });
  365.     }
  366.  
  367.     public boolean IsMsgFromMe(Message message) {
  368.         boolean isSenderMe = username.equals((CharSequence) message.GetFrom());
  369.         return isSenderMe;
  370.     }
  371.  
  372.     public void AddToLstViewChat(Message newMessage) {
  373.         chatMsgList.add(newMessage);
  374.  
  375.         if (chatAdapter == null) {
  376.             chatAdapter = new ChatAdapter(getActivity(), chatMsgList);
  377.         }
  378.  
  379.         if(IsMsgFromMe(newMessage)) {
  380.             lstViewChat = (ListView) getView().findViewById(R.id.listView_chat_message_me);
  381.         } else {
  382.             lstViewChat = (ListView)getView().findViewById(R.id.listView_chat_message_others);
  383.         }
  384.  
  385.         chatAdapter.notifyDataSetChanged();
  386.         lstViewChat.setAdapter(chatAdapter);
  387.     }
  388.  
  389.     // TODO: Rename method, update argument and hook method into UI event
  390.     public void onButtonPressed(Uri uri) {
  391.         if (mListener != null) {
  392.             mListener.onFragmentInteraction(uri);
  393.         }
  394.     }
  395.  
  396.     @Override
  397.     public void onAttach(Activity activity) {
  398.         super.onAttach(activity);
  399.         try {
  400.             mListener = (OnFragmentInteractionListener) activity;
  401.         } catch (ClassCastException e) {
  402.             throw new ClassCastException(activity.toString()
  403.                     + " must implement OnFragmentInteractionListener");
  404.         }
  405.     }
  406.  
  407.     @Override
  408.     public void onDetach() {
  409.         super.onDetach();
  410.         mListener = null;
  411.     }
  412.  
  413.     public String GetGroupId() {
  414.         System.out.println(groupId);
  415.         return groupId;
  416.     }
  417.  
  418.     public void SetGroupID(String groupId) {
  419.         this.groupId = groupId;
  420.     }
  421.  
  422.     public interface OnFragmentInteractionListener {
  423.         // TODO: Update argument type and name
  424.         public void onFragmentInteraction(Uri uri);
  425.     }
  426.  
  427.     public String TimeStamp() {
  428.         String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
  429.         return timeStamp;
  430.     }
  431. }
  432.  
  433.  
  434. public class ChatActivity extends ActionBarActivity implements
  435.         GroupFragment.OnFragmentInteractionListener,
  436.         ChatFragment.OnFragmentInteractionListener {
  437.  
  438.     @Override
  439.     protected void onCreate(Bundle savedInstanceState) {
  440.         super.onCreate(savedInstanceState);
  441.         setContentView(R.layout.activity_chat);
  442.  
  443.         Firebase.setAndroidContext(this);
  444.  
  445.         GroupFragment groupFragment = GroupFragment.newInstance("", "");
  446.         FragmentManager fM = getFragmentManager();
  447.         FragmentTransaction fT = fM.beginTransaction();
  448.         fT.replace(R.id.container_chat, groupFragment, null);
  449.         fT.addToBackStack("go to group fragmement");
  450.         fT.commit();
  451.     }
  452.  
  453.     @Override
  454.     public boolean onCreateOptionsMenu(Menu menu) {
  455.         // Inflate the menu; this adds items to the action bar if it is present.
  456.         getMenuInflater().inflate(R.menu.menu_chat, menu);
  457.         return true;
  458.     }
  459.  
  460.     @Override
  461.     public boolean onOptionsItemSelected(MenuItem item) {
  462.         // Handle action bar item clicks here. The action bar will
  463.         // automatically handle clicks on the Home/Up button, so long
  464.         // as you specify a parent activity in AndroidManifest.xml.
  465.         int id = item.getItemId();
  466.  
  467.         //noinspection SimplifiableIfStatement
  468.         if (id == R.id.action_settings) {
  469.             return true;
  470.         }
  471.  
  472.         return super.onOptionsItemSelected(item);
  473.     }
  474.  
  475.     @Override
  476.     public void onFragmentInteraction(Uri uri) {
  477.  
  478.     }
  479.  
  480.     /**
  481.      * A placeholder fragment containing a simple view.
  482.      */
  483.     public static class PlaceholderFragment extends Fragment {
  484.  
  485.         public PlaceholderFragment() {
  486.         }
  487.  
  488.         @Override
  489.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  490.                                  Bundle savedInstanceState) {
  491.             View rootView = inflater.inflate(R.layout.activity_chat, container, false);
  492.  
  493.             return rootView;
  494.         }
  495.     }
  496.  
  497.     @Override
  498.     public void onBackPressed() {
  499.         if (getFragmentManager().getBackStackEntryCount() > 0) {
  500.             getFragmentManager().popBackStack();
  501.         } else {
  502.             finish();
  503.         }
  504.     }
  505. }
  506.  
  507. public class Message {
  508.     String from = "";
  509.     String message = "";
  510.     String time = "";
  511.     String id = "";
  512.  
  513.     public Message() {}
  514.  
  515.     public Message(String id, String from, String message, String time) {
  516.         this.from = from;
  517.         this.time = time;
  518.         this.message = message;
  519.         this.id = id;
  520.  
  521.     }
  522.  
  523.     public String GetMsg() {
  524.         return this.message;
  525.     }
  526.  
  527.     public String SetMsg(String message) {
  528.         return this.message = message;
  529.     }
  530.  
  531.     public String GetFrom() {
  532.         return this.from;
  533.     }
  534.  
  535.     public String SetFrom(String from) {
  536.         return this.from = from;
  537.     }
  538.  
  539.     public String GetTime() {
  540.         return this.time;
  541.     }
  542.  
  543.     public String SetTime(String time) {
  544.         return this.time = time;
  545.     }
  546.  
  547.     public String GetId() {
  548.         return id;
  549.     }
  550.  
  551.     public String SetId(String id) {
  552.         return this.id = id;
  553.     }
  554.  
  555. }
  556.  
  557.  
  558. public class Group {
  559.     String name = "";
  560.     String id = "";
  561.  
  562.     public Group() {}
  563.  
  564.     public Group(String name, String id) {
  565.         this.name = name;
  566.         this.id = id;
  567.     }
  568.  
  569.     public String GetName() {
  570.         return this.name;
  571.     }
  572.  
  573.     public String SetName(String name) {
  574.         return this.name = name;
  575.     }
  576.  
  577.     public String GetId() {
  578.         return this.id;
  579.     }
  580.  
  581.     public String SetID(String id) {
  582.         return this.id = id;
  583.     }
  584. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement