Skip to content

Commit

Permalink
🎉Alpha 1.3,临时去除新UI页面(新UI页面后续将会重新发布),修复星闪网络随机无法接收聊天信息的Bug(BETA),修复消息滚动…
Browse files Browse the repository at this point in the history
…随机的Bug(消息滚动结束BETA),修复剪贴板问题(剪贴板结束BETA),新增串口日志全部获取,新增串口MAC地址打印在UI上(仅限服务板获取)
  • Loading branch information
Hny0305Lin committed Jul 4, 2024
1 parent fb9192e commit b0f1ce5
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 311 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://github.com/Hny0305Lin/LICENSE/blob/main/LICENSE */
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;

import android.util.Log;
import android.widget.TextView;

import java.util.Iterator;
import java.util.Queue;

/**
* ChatMessageQueueUpdater 类用于更新消息队列并将其内容显示在指定的 TextView 上。
*/
public class ChatMessageQueueUpdater {
private static final String TAG = "ChatMessageQueueUpdater & NLChat";

private Queue<String> messageQueue; // 消息队列
private TextView textView; // 显示消息的 TextView
private String logPrefix; // 日志前缀,用于区分不同的消息队列

/**
* 构造函数
*
* @param messageQueue 消息队列
* @param textView 显示消息的 TextView
* @param logPrefix 日志前缀
*/
public ChatMessageQueueUpdater(TextView textView, Queue<String> messageQueue, String logPrefix) {
this.messageQueue = messageQueue;
this.textView = textView;
this.logPrefix = logPrefix;
}

/**
* 更新 TextView 的内容,将消息队列中的所有消息显示在 TextView 上。
* 同时,移除消息队列中的空消息。
*/
public void updateTextView() {
StringBuilder allMessages = new StringBuilder();
Iterator<String> iterator = messageQueue.iterator();
while (iterator.hasNext()) {
String message = iterator.next();
Log.i(TAG, logPrefix + "当前队列消息内容:" + message); // 打印每个消息到日志
if (!message.trim().isEmpty()) {
allMessages.append(message);
} else {
Log.i(TAG, logPrefix + "忽略空消息,因此消息队列无改动"); // 打印忽略空消息到日志
iterator.remove(); // 从队列中移除空消息
return;
}
}
textView.setText(allMessages.toString());
Log.i(TAG, logPrefix + "消息队列有改动");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://github.com/Hny0305Lin/LICENSE/blob/main/LICENSE */
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;

import static com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUIToast.SnackBarToastForDebug;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
Expand All @@ -13,13 +15,16 @@
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatTextView;

import com.google.android.material.snackbar.Snackbar;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChatProcessor {
private static final String TAG = "ChatProcessor & NLChat";
public class ChatProcessorForExtract {
private static final String TAG = "ChatProcessorForExtract & NLChat";
private static Handler HhandlerClipBoard;

//用于存储提取到的内容
Expand All @@ -28,8 +33,7 @@ public class ChatProcessor {
private static StringBuilder extractedEmails = new StringBuilder();
private static StringBuilder extractedPhones = new StringBuilder();

//设置时间

private static StringBuilder extractedLogForNearLinkAddr = new StringBuilder();

//Handler初始化
public static void initializeHandler() {
Expand All @@ -40,17 +44,19 @@ public static void initializeHandler() {
}

//处理聊天数据
public static void processChat(Context context, String completeSecondData) {
public static void processChat(Context context, String string) {
if (ChatUtils.isClipMessages()) {
// 清空 extractedNumbers 以确保每次都是最新的提取结果
// 清空 extractedNumbers 等String以确保每次都是最新的提取结果
extractedNumbers.setLength(0);
extractedUrls.setLength(0);
extractedEmails.setLength(0);
extractedPhones.setLength(0);

extractedLogForNearLinkAddr.setLength(0);

// 提取四位和六位数字,但排除年份相关的四位数字
Pattern pattern = Pattern.compile("\\b(?!19\\d{2}|20\\d{2})\\d{4}\\b|\\b\\d{6}\\b");
Matcher matcher = pattern.matcher(completeSecondData);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
Log.v(TAG, "找到疑似验证码,提取中");
String foundNumber = matcher.group();
Expand All @@ -61,7 +67,7 @@ public static void processChat(Context context, String completeSecondData) {
Pattern urlPattern = Pattern.compile(
"(https?://(?:www\\.|(?!www))[^\\s\\.]+\\.[^\\s]{2,}|www\\.[^\\s]+\\.[^\\s]{2,})",
Pattern.CASE_INSENSITIVE);
Matcher urlMatcher = urlPattern.matcher(completeSecondData);
Matcher urlMatcher = urlPattern.matcher(string);
while (urlMatcher.find()) {
Log.v(TAG, "找到互联网链接,提取中");
String foundUrl = urlMatcher.group();
Expand All @@ -72,20 +78,31 @@ public static void processChat(Context context, String completeSecondData) {
Pattern emailPattern = Pattern.compile(
"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}",
Pattern.CASE_INSENSITIVE);
Matcher emailMatcher = emailPattern.matcher(completeSecondData);
Matcher emailMatcher = emailPattern.matcher(string);
while (emailMatcher.find()) {
String foundEmail = emailMatcher.group();
extractedEmails.append(foundEmail).append("\n");
}

// 提取符合条件的中国大陆电话号码(11位数字,第一位是1,第二位是3、5、7、8、9)
Pattern phonePattern = Pattern.compile("\\b1[35789]\\d{9}\\b");
Matcher phoneMatcher = phonePattern.matcher(completeSecondData);
Matcher phoneMatcher = phonePattern.matcher(string);
while (phoneMatcher.find()) {
String foundPhone = phoneMatcher.group();
extractedPhones.append(foundPhone).append("\n");
}

// 提取符合条件的星闪设备Mac地址(Log)
//开头是两位十六进制,接下来是三组 :**,然后是两位十六进制,最后是两到五位十六进制
//Pattern macPattern = Pattern.compile("addr:([0-9a-fA-F]{2}(:\\*{2}){3}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2,5})");
//前四组是俩位十六进制或星号,第五组是俩位十六进制,最后一组是俩到伍位十六进制
Pattern macPattern = Pattern.compile("addr:((?:[0-9a-fA-F]{2}|\\*{2})(?::(?:[0-9a-fA-F]{2}|\\*{2})){3}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2,5})");
Matcher macMatcher = macPattern.matcher(string);
while (macMatcher.find()) {
String foundMac = macMatcher.group();
extractedLogForNearLinkAddr.append(foundMac).append("\n");
}

// 将提取到的数字和链接分别复制到剪贴板
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (extractedNumbers.length() > 0) {
Expand All @@ -98,6 +115,7 @@ public static void processChat(Context context, String completeSecondData) {
HhandlerClipBoard.postDelayed(new ClipboardRunnable(context), ClipboardRunnable.DELAY_NORMAL); // 验证码可以60秒以上,到达时间后清空剪贴板
}

// 将提取到的链接复制到剪贴板
if (extractedUrls.length() > 0) {
ClipData clipUrls = ClipData.newPlainText("extractedUrls", extractedUrls.toString().trim());
clipboard.setPrimaryClip(clipUrls);
Expand Down Expand Up @@ -142,6 +160,26 @@ public void run() {
}
});
}

// 将提取到的星闪MAC地址复制到剪贴板
if (extractedLogForNearLinkAddr.length() > 0) {
((Activity) context).runOnUiThread(new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
AppCompatTextView compatTextView = ((Activity) context).findViewById(R.id.userTitle);
compatTextView.setText("User" + extractedLogForNearLinkAddr.toString().trim());
}
});

ClipData clipNearLinkAddr = ClipData.newPlainText("extractedLogForNearLinkAddr", extractedLogForNearLinkAddr.toString().trim());
clipboard.setPrimaryClip(clipNearLinkAddr);
SnackBarToastForDebug(context,"提取到星闪对方设备地址,已复制到剪贴板!", "推荐去保存", 0, Snackbar.LENGTH_LONG);

// 取消之前的清空任务并重新设置定时任务
HhandlerClipBoard.removeCallbacksAndMessages(null);
HhandlerClipBoard.postDelayed(new ClipboardRunnable(context), ClipboardRunnable.DELAY_VERY_LONG); // 星闪设备地址可以保存久点
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://github.com/Hny0305Lin/LICENSE/blob/main/LICENSE */
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;

import android.annotation.SuppressLint;
import android.content.Context;

import com.haohanyh.linmengjia.nearlink.nlchat.fun.R;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.SQLite.SQLiteDataBaseAPP;

public class ChatSaveMessageDatabaseManager {

//调用SQLite
private static SQLiteDataBaseAPP dbHelper;

private Context context;

public ChatSaveMessageDatabaseManager(Context context) {
this.context = context;
}

public void saveMessageToDatabase(String timestamp, String message, String sender) {
//检索是否有空消息,串口通讯时常有相关问题
if (message == null || message.trim().isEmpty()) {
return;
}
//如果有消息再保存,上面是没消息不予保存
dbHelper.saveMessageToDatabase(message, sender, timestamp);
dbHelper.saveVersionToDatabase(context.getString(R.string.app_version));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://github.com/Hny0305Lin/LICENSE/blob/main/LICENSE */
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;

import android.app.Activity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class ChatUtils {
//这里设置的是UI相关,是否做到消息滚动展示在UI上,而不是全部展示,全部展示会占用大量UI资源
private static boolean showUartLog = true; // 控制是否启用显示串口Log,打印在UI上(开发者使用)
private static final boolean scrollingMessages = true; // 控制是否启用滚动消息功能
private static final boolean clipMessages = true; // 控制是否启用剪贴板功能(目前仅验证码)
private static final boolean clipMessages = true; // 控制是否启用剪贴板功能
//这里设置的是数据库相关
private static boolean sqlitemanager = true; // 控制是否启用SQLite存储功能
private static boolean sqlitehistory = false; // 控制是否启用SQLite历史记录显示功能
Expand All @@ -14,6 +14,7 @@ public class ChatUtils {
private static final String PREFIX_CLIENT = " Let's start chatting, This is the content of the client:";
//这里设置的是跟C代码相关的,黑名单处理串口Log,当这些文本出现在串口通讯里面的时候,提取这String内容
private static final String PREFIX_LOG_NOT_CONNECTED_SERVER = "[sle uart server] sle client is not connected! ";
private static final String PREFIX_LOG_NEARLINK_DEVICES_ADDR = "[sle uart server] pair complete addr:";
// 控制是否启用显示串口Log,打印在UI上(开发者使用)
public static boolean isShowUartLog() { return showUartLog; }

Expand Down Expand Up @@ -47,4 +48,8 @@ public static String getPrefixClient() {
public static String getPrefixLogNotConnectedServer() {
return PREFIX_LOG_NOT_CONNECTED_SERVER;
}

public static String getPrefixLogNearlinkDevicesAddr() {
return PREFIX_LOG_NEARLINK_DEVICES_ADDR;
}
}
Loading

0 comments on commit b0f1ce5

Please sign in to comment.