ChatFragmet
hacker98
plain_text
a year ago
11 kB
5
Indexable
main
package com.example.chatappdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.chatappdemo.DAO.ConversationDAO;
import com.example.chatappdemo.DAO.MessDAO;
import com.example.chatappdemo.model.Conversation;
import com.example.chatappdemo.model.Mess;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class ChatFragment extends Fragment {
private static final String TAG = "SocketServerClient";
private LinearLayout messageContainer;
private EditText inputMess;
private Button sendButton;
private TextView textViewIP;
private FloatingActionButton fab;
private BroadcastReceiver newMessageReceiver;
private String yourIP,MyIP;
private Conversation conversation;
private ConversationDAO conversationDAO;
private MessDAO messDAO;
private int Conver_id;
private javaExternal func= new javaExternal();
IntentFilter filter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chat, container, false);
messageContainer = view.findViewById(R.id.messageContainer);
inputMess = view.findViewById(R.id.inputMess);
sendButton = view.findViewById(R.id.sendButton);
textViewIP = view.findViewById(R.id.textViewIP);
fab = view.findViewById(R.id.fab);
// ScrollView scrollView=view.findViewById(R.id.scrollView);
Bundle bundle = getArguments();
if(bundle!=null) {
yourIP = bundle.getString("conversation_IP");
MyIP= bundle.getString("MyIP");
}
textViewIP.setText(yourIP);
conversationDAO = new ConversationDAO(requireContext());
messDAO = new MessDAO(requireContext());
conversation =conversationDAO.getConversationByIPReceive(yourIP);
Conver_id=conversation.getConver_id();
List<Mess> MessList = messDAO.getMessagesByConversationId(Conver_id);
Log.i("Socket","cuoc hoi thoai giua "+ MyIP +" va "+ yourIP);
Log.i("Socket","khoa ma hoa 128: "+ conversation.getKeyEncrypt128() + "khoa ma hoa 256 la:"+conversation.getKeyEncrypt256()+ "dang su dng thuat toan ma hoa: "+ conversation.getAlgorithm());
// lich su tro chuyen
if(MessList!=null){
for (Mess mess : MessList){
String algorithm = mess.getAlgorithm();
String messOut="giai ma loi";
if(algorithm.equals("AES128")){
messOut=func.decryptAES128(mess.getMess_text(),conversation.getKeyEncrypt128());
}else if(algorithm.equals("AES256")){
messOut=func.decryptAES256(mess.getMess_text(),conversation.getKeyEncrypt256());
}else if(algorithm.equals("SNOWV")){
messOut = func.decryptSNOWV(mess.getMess_text(),conversation.getKeyEncrypt256());
}
if(mess.getSender_ip().equals(MyIP)){
String finalMessOut = messOut;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
addMessageToUI(finalMessOut,true);
}
});
}else{
String finalMessOut1 = messOut;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
addMessageToUI(finalMessOut1,false);
}
});
}
}
}
newMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String ClientIP = intent.getStringExtra("ClientIP");
String message = intent.getStringExtra("message");
addMessageToUI(message, false);
}
};
filter = new IntentFilter("com.example.chatappdemo.NEW_MESSAGE");
requireActivity().registerReceiver(newMessageReceiver, filter);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(view);
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = inputMess.getText().toString();
if (!message.isEmpty()) {
try {
// ma hoa tin nhan
String cipher="ma hoa loi";
String algorithm = conversation.getAlgorithm();
if(algorithm.equals("AES128")){
cipher= func.encryptAES128(message,conversation.getKeyEncrypt128());
}else if(algorithm.equals("AES256")){
cipher = func.encryptAES256(message,conversation.getKeyEncrypt256());
}else if(algorithm.equals("SNOWV")){
cipher = func.encryptSNOWV(message,conversation.getKeyEncrypt256());
}
// tao json gui tin nhan
JSONObject messageJson = new JSONObject();
JSONObject messageContentJson = new JSONObject();
messageJson.put("type", "MESSAGE");
messageContentJson.put("mess_text", cipher);
messageContentJson.put("algorithm",algorithm);
messageJson.put("content", messageContentJson);
ChatClient chatClient = new ChatClient(yourIP, 1234, messageJson.toString());
chatClient.start();
addMessageToUI(message, true);
Mess MessSave = new Mess(conversation.getConver_id(),MyIP,cipher,getCurrentTime(),algorithm);
conversation.setLast_mess(message);
conversation.setLast_mess_time(getCurrentTime());
conversationDAO.updateConversation(conversation);
messDAO.insertMess(MessSave);
inputMess.setText("");
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Lỗi khi chuyển đổi JSON: " + e.getMessage());
}
}
}
});
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
// Hủy đăng ký BroadcastReceiver khi Fragment bị hủy
Log.i("Socket","fragment bi huy");
if (newMessageReceiver != null) {
requireActivity().unregisterReceiver(newMessageReceiver);
}
}
public String getCurrentTime(){
SimpleDateFormat sdf= new SimpleDateFormat("hh:mm a", Locale.getDefault());
return sdf.format(new Date());
}
public void showPopupMenu(View view){
PopupMenu popupMenu = new PopupMenu(getContext(),view);
popupMenu.getMenu().add("AES 128");
popupMenu.getMenu().add("AES 256");
popupMenu.getMenu().add("SNOWV");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String title= menuItem.getTitle().toString();
switch (title){
case "AES 128":
conversation.setKeyEncrypt128("AES128");
conversationDAO.updateConversation(conversation);
Toast.makeText(getContext(),"Da chuyen sang che do ma hoa AES 128",Toast.LENGTH_SHORT).show();
return true;
case "AES 256":
conversation.setAlgorithm("AES256");
conversationDAO.updateConversation(conversation);
Toast.makeText(getContext(),"Da chuyen sang che do ma hoa AES 256",Toast.LENGTH_SHORT).show();
return true;
case "SNOWV":
conversation.setAlgorithm("SNOWV");
conversationDAO.updateConversation(conversation);
Toast.makeText(getContext(),"Da chuyen sang che do ma hoa SNOWV",Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
popupMenu.show();
}
public void addMessageToUI(String message, boolean isSent) {
TextView textView = new TextView(getContext());
textView.setText(message);
textView.setTextSize(16);
textView.setPadding(10, 10, 10, 10);
textView.setTextColor(getResources().getColor(android.R.color.white));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
(int) (getResources().getDisplayMetrics().widthPixels * 0.6),
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);
if (isSent) {
textView.setBackgroundResource(R.color.purple_200); // Blue for sent messages
params.gravity = Gravity.END;
} else {
textView.setBackgroundResource(R.color.teal_700); // Color for received messages
params.gravity = Gravity.START;
}
textView.setLayoutParams(params);
messageContainer.addView(textView);
// Scroll to the bottom of the scroll view
final ScrollView scrollView = getActivity().findViewById(R.id.scrollView);
if(scrollView != null){
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}else {
Log.d("Socket","Scroll View is null");
}
}
}
Editor is loading...
Leave a Comment