This commit is contained in:
844143714@qq,com 2026-04-28 21:33:15 +08:00
parent 5dc924baf3
commit 0c49fd0faa
6 changed files with 137 additions and 16 deletions

View File

@ -473,6 +473,14 @@
// 延迟检测 // 延迟检测
setTimeout(checkApp, 300); setTimeout(checkApp, 300);
// 同步 PTT 登录状态
setTimeout(function() {
if (window.android && window.android.pttGetLoginStatus) {
var status = window.android.pttGetLoginStatus();
var loginId = window.android.pttGetLoginId ? window.android.pttGetLoginId() : "";
updatePttLoginStatus(status, loginId);
}
}, 500);
// 告知 Android 页面加载完成 // 告知 Android 页面加载完成
window.onload = function() { window.onload = function() {
@ -670,22 +678,37 @@
// PTT 登录成功回调 (由 Android 调用) // PTT 登录成功回调 (由 Android 调用)
function onPttLoginSuccess() { function onPttLoginSuccess() {
pttStatusTag.innerText = "PTT 已登录"; updatePttLoginStatusUI("02");
pttStatusTag.className = "ws-status ws-connected";
// 激活按钮
pttBtn.disabled = false;
pttBtn.style.background = "#ef4444";
pttBtn.style.cursor = "pointer";
var actionBtns = document.querySelectorAll('.ptt-action-btn');
for (var i = 0; i < actionBtns.length; i++) {
actionBtns[i].disabled = false;
}
showPttResult("PTT 业务初始化成功,对讲功能已就绪"); showPttResult("PTT 业务初始化成功,对讲功能已就绪");
} }
// 更新 PTT 登录状态 (由 Android 实时推送)
window.updatePttLoginStatus = function(status, loginId) {
updatePttLoginStatusUI(status);
var statusMap = { "00": "未登录", "01": "登录中...", "02": "已登录" };
showPttResult("登录状态变更: " + (statusMap[status] || status) + " (ID:" + loginId + ")");
};
function updatePttLoginStatusUI(status) {
if (status === "02") {
pttStatusTag.innerText = "PTT 已登录";
pttStatusTag.className = "ws-status ws-connected";
pttBtn.disabled = false;
pttBtn.style.background = "#ef4444";
pttBtn.style.cursor = "pointer";
var actionBtns = document.querySelectorAll('.ptt-action-btn');
for (var i = 0; i < actionBtns.length; i++) {
actionBtns[i].disabled = false;
}
} else if (status === "01") {
pttStatusTag.innerText = "PTT 登录中...";
pttStatusTag.className = "ws-status ws-disconnected";
} else {
pttStatusTag.innerText = "PTT 未登录";
pttStatusTag.className = "ws-status ws-disconnected";
}
}
// 修复 PTT 按键逻辑:支持 TouchEvents 并处理异常中断 // 修复 PTT 按键逻辑:支持 TouchEvents 并处理异常中断
if (pttBtn) { if (pttBtn) {
var isDown = false; var isDown = false;

View File

@ -18,6 +18,21 @@ public class CallBackResolution {
private static int logNum = 0; private static int logNum = 0;
private static long offlineTime = 0; private static long offlineTime = 0;
private static boolean loginState = false; private static boolean loginState = false;
private static String pttLoginStatus = "00"; // 00-未登录 01-登录中 02-已登录
private static String pttLoginId = "";
public static String getPttLoginStatus() {
return pttLoginStatus;
}
public static String getPttLoginId() {
return pttLoginId;
}
public static void resetPttLoginStatus() {
pttLoginStatus = "00";
pttLoginId = "";
}
public static void at_OEM_AT_cb(List<String> lisHex){ public static void at_OEM_AT_cb(List<String> lisHex){
if (lisHex.size() == 0) { if (lisHex.size() == 0) {
@ -33,14 +48,23 @@ public class CallBackResolution {
switch (firstHex){ switch (firstHex){
case "82"://登录状态通知指令 case "82"://登录状态通知指令
String loginId = lisHex.get(2) + "" + lisHex.get(3) + "" + lisHex.get(4) + "" + lisHex.get(5); String loginId = lisHex.get(2) + "" + lisHex.get(3) + "" + lisHex.get(4) + "" + lisHex.get(5);
pttLoginStatus = errorNum;
pttLoginId = loginId;
Timber.tag(TAG).i("PTT login status: %s, id: %s", pttLoginStatus, pttLoginId);
// 实时推送状态到前端
com.stand.standapp.MainActivity.executeJs("updatePttLoginStatus('" + pttLoginStatus + "','" + pttLoginId + "')");
CallBackUtil.callBackLoinState(errorNum, loginId); CallBackUtil.callBackLoinState(errorNum, loginId);
loginState = isLoginConflict(errorNum); loginState = isLoginConflict(errorNum);
CallBackUtil.callBackLoginConflict(loginState); CallBackUtil.callBackLoginConflict(loginState);
if (loginState) {
com.stand.standapp.MainActivity.showPttConflictDialog();
}
if ("02".equals(errorNum)) { if ("02".equals(errorNum)) {
com.stand.standapp.MainActivity.onPttLoginSuccess(); com.stand.standapp.MainActivity.onPttLoginSuccess();
} }
break; break;
case "a2"://账号有视频功能 case "a2"://账号有视频功能
CallBackUtil.callBackHaveVideo(); CallBackUtil.callBackHaveVideo();
break; break;

View File

@ -46,4 +46,14 @@ public class MyApplication {
Log.e("PTT_INIT", "Failed to init PTT: " + e.getMessage()); Log.e("PTT_INIT", "Failed to init PTT: " + e.getMessage());
} }
} }
public static void destroy() {
if (pNative != null) {
pNative.destory();
pNative = null;
}
mContext = null;
mAppInstance = new MyApplication();
CallBackResolution.resetPttLoginStatus();
}
} }

View File

@ -35,9 +35,26 @@ class AndroidInterface(private val activity: MainActivity) {
} }
/** /**
* PTT 停止说话 * 获取 PTT 登录状态: "00"-未登录 "01"-登录中 "02"-已登录
*/ */
@JavascriptInterface @JavascriptInterface
fun pttGetLoginStatus(): String {
return com.example.kingway.ptt.CallBackResolution.getPttLoginStatus()
}
/**
* 获取 PTT 登录 ID
*/
@JavascriptInterface
fun pttGetLoginId(): String {
return com.example.kingway.ptt.CallBackResolution.getPttLoginId()
}
/**
* PTT 登录
*/
@JavascriptInterface
fun pttSpeakRelease() { fun pttSpeakRelease() {
if (isSpeaking) { if (isSpeaking) {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()

View File

@ -57,6 +57,7 @@ class LoginActivity : AppCompatActivity() {
// 4. 退出按钮 // 4. 退出按钮
btnExit.setOnClickListener { btnExit.setOnClickListener {
pttLogoutAndDestroy()
finish() finish()
} }
@ -231,8 +232,26 @@ class LoginActivity : AppCompatActivity() {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle("提示") .setTitle("提示")
.setMessage("确定要退出应用吗?") .setMessage("确定要退出应用吗?")
.setPositiveButton("确定") { _, _ -> finish() } .setPositiveButton("确定") { _, _ ->
pttLogoutAndDestroy()
finish()
}
.setNegativeButton("取消", null) .setNegativeButton("取消", null)
.show() .show()
} }
private fun pttLogoutAndDestroy() {
try {
com.example.kingway.ptt.SendAtUtil.toLogout()
Timber.tag("LoginActivity").i("PTT logout sent")
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "PTT logout failed")
}
try {
com.example.kingway.ptt.MyApplication.destroy()
Timber.tag("LoginActivity").i("PTT native destroyed")
} catch (e: Exception) {
Timber.tag("LoginActivity").e(e, "PTT destroy failed")
}
}
} }

View File

@ -69,6 +69,11 @@ class MainActivity : AppCompatActivity() {
.setMessage("您的 PTT 账号已在其他设备登录,当前设备已被迫下线。\n请点击确认退出并重新登录。") .setMessage("您的 PTT 账号已在其他设备登录,当前设备已被迫下线。\n请点击确认退出并重新登录。")
.setCancelable(false) .setCancelable(false)
.setPositiveButton("确认") { _, _ -> .setPositiveButton("确认") { _, _ ->
try {
com.example.kingway.ptt.SendAtUtil.toLogout()
com.example.kingway.ptt.MyApplication.destroy()
com.stand.standapp.utils.GpioManager.stopListening()
} catch (_: Exception) {}
val pm = ctx.packageManager val pm = ctx.packageManager
val intent = pm.getLaunchIntentForPackage(ctx.packageName) val intent = pm.getLaunchIntentForPackage(ctx.packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
@ -171,11 +176,34 @@ class MainActivity : AppCompatActivity() {
androidx.appcompat.app.AlertDialog.Builder(this) androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("提示") .setTitle("提示")
.setMessage("确定要退出应用吗?") .setMessage("确定要退出应用吗?")
.setPositiveButton("确定") { _, _ -> finish() } .setPositiveButton("确定") { _, _ ->
pttLogoutAndDestroy()
finish()
}
.setNegativeButton("取消", null) .setNegativeButton("取消", null)
.show() .show()
} }
private fun pttLogoutAndDestroy() {
try {
com.example.kingway.ptt.SendAtUtil.toLogout()
Timber.tag("MainActivity").i("PTT logout sent")
} catch (e: Exception) {
Timber.tag("MainActivity").e(e, "PTT logout failed")
}
try {
com.stand.standapp.utils.GpioManager.stopListening()
} catch (e: Exception) {
Timber.tag("MainActivity").e(e, "GPIO stop failed")
}
try {
com.example.kingway.ptt.MyApplication.destroy()
Timber.tag("MainActivity").i("PTT native destroyed")
} catch (e: Exception) {
Timber.tag("MainActivity").e(e, "PTT destroy failed")
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (mAgentWeb?.handleKeyEvent(keyCode, event) == true) { if (mAgentWeb?.handleKeyEvent(keyCode, event) == true) {
return true return true