This commit is contained in:
parent
a6cd238330
commit
24c8289a0d
|
|
@ -75,6 +75,29 @@ public class AudioRecordManager {
|
||||||
return mInstance;
|
return mInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 彻底销毁单例及相关资源
|
||||||
|
*/
|
||||||
|
public static void destroy() {
|
||||||
|
if (mInstance != null) {
|
||||||
|
synchronized (AudioRecordManager.class) {
|
||||||
|
if (mInstance != null) {
|
||||||
|
try {
|
||||||
|
mInstance.destroyAudio();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Timber.tag("AudioRecordManager").e(e, "destroyAudio error");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
mInstance.destroyThread();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Timber.tag("AudioRecordManager").e(e, "destroyThread error");
|
||||||
|
}
|
||||||
|
mInstance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 销毁线程方法
|
* 销毁线程方法
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -532,18 +532,18 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
// 4. Stop and destroy AudioRecord
|
// 4. Stop and destroy AudioRecord
|
||||||
try {
|
try {
|
||||||
com.example.kingway.ptt.AudioRecordManager.getInstance().destroyAudio()
|
com.example.kingway.ptt.AudioRecordManager.destroy()
|
||||||
Timber.tag("MainActivity").i("AudioRecordManager destroyed")
|
Timber.tag("MainActivity").i("AudioRecordManager destroyed")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.tag("MainActivity").e(e, "AudioRecordManager destroy failed")
|
Timber.tag("MainActivity").e(e, "AudioRecordManager destroy failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Close printer connection
|
// 5. Close printer connection and reset
|
||||||
try {
|
try {
|
||||||
com.stand.standapp.printer.PrinterManager.getFactory()?.ClosePort()
|
com.stand.standapp.printer.PrinterManager.destroy()
|
||||||
Timber.tag("MainActivity").i("Printer port closed")
|
Timber.tag("MainActivity").i("Printer destroyed")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.tag("MainActivity").e(e, "Printer close failed")
|
Timber.tag("MainActivity").e(e, "Printer destroy failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. Clear Access Token
|
// 6. Clear Access Token
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,13 @@ import com.dp.dp_serialportlist.Serialport_Factory
|
||||||
import com.stand.standapp.AppConfig
|
import com.stand.standapp.AppConfig
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.ScheduledExecutorService
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
object PrinterManager {
|
object PrinterManager {
|
||||||
private var sf: Serialport_Factory? = null
|
private var sf: Serialport_Factory? = null
|
||||||
private var isConnecting = false
|
private var isConnecting = false
|
||||||
private val executor = Executors.newSingleThreadScheduledExecutor()
|
private var executor: ScheduledExecutorService? = null
|
||||||
private var mContext: Context? = null
|
private var mContext: Context? = null
|
||||||
private var hasPaper: Boolean = true // 默认为有纸
|
private var hasPaper: Boolean = true // 默认为有纸
|
||||||
|
|
||||||
|
|
@ -43,6 +44,9 @@ object PrinterManager {
|
||||||
fun init(context: Context) {
|
fun init(context: Context) {
|
||||||
if (sf != null) return
|
if (sf != null) return
|
||||||
mContext = context.applicationContext
|
mContext = context.applicationContext
|
||||||
|
if (executor == null || executor!!.isShutdown) {
|
||||||
|
executor = Executors.newSingleThreadScheduledExecutor()
|
||||||
|
}
|
||||||
|
|
||||||
// Serialport_Factory 构造函数内部会创建 Handler,必须在有 Looper 的线程(如主线程)调用
|
// Serialport_Factory 构造函数内部会创建 Handler,必须在有 Looper 的线程(如主线程)调用
|
||||||
mHandler.post {
|
mHandler.post {
|
||||||
|
|
@ -51,9 +55,9 @@ object PrinterManager {
|
||||||
sf = Serialport_Factory.getSerialport_Factory(mContext, mHandler)
|
sf = Serialport_Factory.getSerialport_Factory(mContext, mHandler)
|
||||||
|
|
||||||
// 成功创建 factory 后,将后续耗时的连接任务丢给子线程
|
// 成功创建 factory 后,将后续耗时的连接任务丢给子线程
|
||||||
executor.execute {
|
executor?.execute {
|
||||||
// 启动自动重连定时任务
|
// 启动自动重连定时任务
|
||||||
executor.scheduleWithFixedDelay({
|
executor?.scheduleWithFixedDelay({
|
||||||
checkAndConnect()
|
checkAndConnect()
|
||||||
}, 0, 10, TimeUnit.SECONDS)
|
}, 0, 10, TimeUnit.SECONDS)
|
||||||
}
|
}
|
||||||
|
|
@ -65,6 +69,26 @@ object PrinterManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
Timber.tag("Printer").i("正在销毁并重置打印机组件...")
|
||||||
|
try {
|
||||||
|
executor?.shutdownNow()
|
||||||
|
executor = null
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.tag("Printer").e(e, "关闭打印机线程池异常")
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
sf?.ClosePort()
|
||||||
|
sf = null
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.tag("Printer").e(e, "关闭打印机串口异常")
|
||||||
|
}
|
||||||
|
mContext = null
|
||||||
|
isConnecting = false
|
||||||
|
hasPaper = true
|
||||||
|
Timber.tag("Printer").i("打印机组件销毁完成")
|
||||||
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun checkAndConnect() {
|
private fun checkAndConnect() {
|
||||||
val factory = sf ?: return
|
val factory = sf ?: return
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,10 @@ object GpioManager {
|
||||||
mIsRunning = false
|
mIsRunning = false
|
||||||
mThread?.interrupt()
|
mThread?.interrupt()
|
||||||
mThread = null
|
mThread = null
|
||||||
|
mZysjSystemManager = null
|
||||||
|
isSpeaking = false
|
||||||
|
lastGpioValue = 1
|
||||||
|
Timber.tag("GPIO").i("GPIO listening stopped and state reset")
|
||||||
}
|
}
|
||||||
|
|
||||||
private var isSpeaking = false
|
private var isSpeaking = false
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue