From 119252cb4b091a578aadd5067cd57b2553624a8e Mon Sep 17 00:00:00 2001 From: "844143714@qq,com" <844143714@qq,com> Date: Sat, 16 May 2026 01:23:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/stand/standapp/LoginActivity.kt | 92 +++++++++++++++---- .../java/com/stand/standapp/MyApplication.kt | 21 ----- .../com/stand/standapp/TempCallActivity.kt | 3 +- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/stand/standapp/LoginActivity.kt b/app/src/main/java/com/stand/standapp/LoginActivity.kt index f6e65f5..fa049b5 100644 --- a/app/src/main/java/com/stand/standapp/LoginActivity.kt +++ b/app/src/main/java/com/stand/standapp/LoginActivity.kt @@ -154,27 +154,12 @@ class LoginActivity : AppCompatActivity() { // 提取登录数据 val data = json.optJSONObject("data") val loginData = data?.toString() ?: "{}" + val accessToken = data?.optString("access_token", "") ?: "" Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show() - // 1. PTT 引擎强制重置后登录 (销毁旧 session,确保干净状态) - try { - com.example.kingway.ptt.SendAtUtil.toCancelLogin() - com.example.kingway.ptt.SendAtUtil.toLogout() - } catch (e: Exception) { - Timber.tag("PTT").e(e, "PTT init error") - } - - Toast.makeText(this@LoginActivity, "PTT初始化中...", Toast.LENGTH_SHORT).show() - - // 使用 Handler 替代硬编码 Thread.sleep,避免阻塞或随意开辟线程 - android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({ - try { - com.example.kingway.ptt.SendAtUtil.toLogin(username, "123456", "220.154.131.231", 1, 1) - } catch (e: Exception) { - Timber.tag("PTT").e(e, "PTT login error") - } - }, 1500) + // 获取独立的 PTT 账号并初始化 + initPttWithAccount(username, accessToken) // 2. 启动硬件 GPIO 监听 try { @@ -302,8 +287,9 @@ class LoginActivity : AppCompatActivity() { val gname = group.optString("gname", "临时群组") Timber.tag("TempGroup").d("发现临时群组: $gname") // 打开临时呼叫页面 + val appCtx = com.stand.standapp.MyApplication.getAppInstance() val intent = android.content.Intent( - this@LoginActivity, + appCtx, TempCallActivity::class.java ) intent.putExtra("group_name", gname) @@ -312,7 +298,7 @@ class LoginActivity : AppCompatActivity() { android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK or android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS ) - startActivity(intent) + appCtx.startActivity(intent) break } } @@ -322,4 +308,70 @@ class LoginActivity : AppCompatActivity() { } }) } + + private fun initPttWithAccount(username: String, accessToken: String) { + if (accessToken.isEmpty()) { + doPttInit(username, "123456") + return + } + + val serverUrl = AppConfig.getServerUrl(this) + val url = "$serverUrl/api/poc-card/account/$username" + + val request = Request.Builder() + .url(url) + .addHeader("Authorization", "Bearer $accessToken") + .get() + .build() + + client.newCall(request).enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + Timber.tag("PTT").w(e, "获取 PTT 账号失败,使用默认账号") + doPttInit(username, "123456") + } + + override fun onResponse(call: Call, response: Response) { + try { + val body = response.body?.string() ?: "" + val json = JSONObject(body) + if (json.optInt("code") == 0) { + val data = json.optJSONObject("data") + val pttUsername = data?.optString("cardNumber", username) ?: username + val pttPassword = data?.optString("pocPassword", "123456") ?: "123456" + doPttInit(pttUsername, pttPassword) + } else { + doPttInit(username, "123456") + } + } catch (e: Exception) { + Timber.tag("PTT").e(e, "解析 PTT 账号失败") + doPttInit(username, "123456") + } + } + }) + } + + private fun doPttInit(pttUsername: String, pttPassword: String) { + runOnUiThread { + if (!isFinishing) { + Toast.makeText(this@LoginActivity, "PTT初始化中...", Toast.LENGTH_SHORT).show() + } + } + + // 1. PTT 引擎强制重置后登录 (销毁旧 session,确保干净状态) + try { + com.example.kingway.ptt.SendAtUtil.toCancelLogin() + com.example.kingway.ptt.SendAtUtil.toLogout() + } catch (e: Exception) { + Timber.tag("PTT").e(e, "PTT init error") + } + + // 使用 Handler 替代硬编码 Thread.sleep,避免阻塞或随意开辟线程 + android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({ + try { + com.example.kingway.ptt.SendAtUtil.toLogin(pttUsername, pttPassword, "220.154.131.231", 1, 1) + } catch (e: Exception) { + Timber.tag("PTT").e(e, "PTT login error") + } + }, 1500) + } } diff --git a/app/src/main/java/com/stand/standapp/MyApplication.kt b/app/src/main/java/com/stand/standapp/MyApplication.kt index ea03c70..4c78f98 100644 --- a/app/src/main/java/com/stand/standapp/MyApplication.kt +++ b/app/src/main/java/com/stand/standapp/MyApplication.kt @@ -68,27 +68,6 @@ class MyApplication : Application() { } } - /** - * 登录成功后将真实账号信息覆盖写入 cfg.dat - */ - @JvmStatic - fun writeCfgDat(ip: String, account: String, password: String) { - try { - val cfgFile = File("/data/data/$pageName/cfg.dat") - val data = ByteArray(240) - ip.toByteArray().copyInto(data, 0) - account.toByteArray().copyInto(data, 0x10) - password.toByteArray().copyInto(data, 0x20) - "GPSON".toByteArray().copyInto(data, 0x30) - data[0xE1] = 0x01 - data[0xEA] = 0x3F - data[0xEB] = 0x3F - cfgFile.writeBytes(data) - Timber.tag("PTT_INIT").i("writeCfgDat done: ip=$ip, account=$account") - } catch (e: Exception) { - Timber.tag("PTT_INIT").w(e, "writeCfgDat failed") - } - } } /** diff --git a/app/src/main/java/com/stand/standapp/TempCallActivity.kt b/app/src/main/java/com/stand/standapp/TempCallActivity.kt index 46b3eac..7242251 100644 --- a/app/src/main/java/com/stand/standapp/TempCallActivity.kt +++ b/app/src/main/java/com/stand/standapp/TempCallActivity.kt @@ -62,8 +62,9 @@ class TempCallActivity : AppCompatActivity() { @JvmStatic fun closeWithMessage(message: String) { + val appCtx = com.stand.standapp.MyApplication.getAppInstance() instance?.runOnUiThread { - Toast.makeText(instance, message, Toast.LENGTH_SHORT).show() + Toast.makeText(appCtx, message, Toast.LENGTH_SHORT).show() instance?.finish() } ?: run { // Activity 已销毁但状态还在,清除状态