From e4c171d46d260526d4777a50420fd097c6a72153 Mon Sep 17 00:00:00 2001 From: "3075067877@qq.com" <3075067877@qq.com> Date: Tue, 9 Sep 2025 18:04:49 +0800 Subject: [PATCH] add camera pause --- .../com/ouxuan/oxface/OXFaceOnlineActivity.java | 114 +++++++++++++++++++-- .../oxface/orderOX/OrderSelectionActivity.java | 36 +++++++ .../orderOX/OrderVerificationResultActivity.java | 40 ++++++++ .../oxface/orderOX/VerificationCodeActivity.java | 45 ++++++++ 4 files changed, 224 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java b/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java index 33c0e30..60e0326 100644 --- a/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java @@ -175,6 +175,12 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi // 网络请求管理器 private OrderVerificationManager orderVerificationManager; private OrderVerificationResultHandler orderVerificationResultHandler; + + // 摄像头暂停管理相关 + private Handler cameraTimeoutHandler; + private static final int CAMERA_RESUME_TIMEOUT = 30000; // 30秒超时 + private boolean isCameraPausedByDialog = false; // 标记是否由对话框暂停 + private int pauseRequestCount = 0; // 暂停请求计数 @Override protected void onCreate(Bundle savedInstanceState) { @@ -280,6 +286,10 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi @Override public void navigateToOrderSelectionPage(String orderData, int verificationType, String faceBase64) { + // 在跳转到订单选择页面时暂停摄像头 + pauseCameraWithTimeout(); + LogManager.logInfo(TAG, "跳转订单选择页面时暂停摄像头"); + // 跳转到订单选择页面 Intent intent = new Intent(OXFaceOnlineActivity.this, OrderSelectionActivity.class); intent.putExtra(OrderSelectionActivity.EXTRA_ORDER_DATA, orderData); @@ -894,9 +904,9 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi private void handleVerificationCodeClick() { LogManager.logInfo(TAG, "用户点击验证码开门"); - // 暂停摄像头预览 - isNeedCamera = false; - LogManager.logInfo(TAG, "暂停摄像头预览 - isNeedCamera设置为false"); + // 使用新的暂停摄像头方法 + pauseCameraWithTimeout(); + LogManager.logInfo(TAG, "暂停摄像头预览 - 使用带超时的暂停方法"); // 启动验证码Activity Intent intent = new Intent(this, VerificationCodeActivity.class); @@ -1442,8 +1452,8 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi if (requestCode == 1001) { // 验证码验证获取输入结果 // 恢复摄像头预览 - isNeedCamera = true; - LogManager.logInfo(TAG, "恢复摄像头预览 - isNeedCamera设置为true"); + resumeCamera(); + LogManager.logInfo(TAG, "恢复摄像头预览 - 使用resumeCamera方法"); if (resultCode == RESULT_OK && data != null) { String verificationCode = data.getStringExtra("verification_code"); @@ -1461,8 +1471,8 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi } } else if (requestCode == 1002) { // 订单选择页面返回结果 // 恢复摄像头预览 - isNeedCamera = true; - LogManager.logInfo(TAG, "恢复摄像头预览 - isNeedCamera设置为true"); + resumeCamera(); + LogManager.logInfo(TAG, "恢复摄像头预览 - 使用resumeCamera方法"); if (resultCode == RESULT_OK && data != null) { // 处理订单选择结果 @@ -1583,6 +1593,9 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi private void getCheckOrder() { LogManager.logInfo(TAG, "开始查验订单列表,modeType: " + modeType); + // 在网络请求开始时暂停摄像头 + pauseCameraWithTimeout(); + // 使用新的网络请求管理器执行验证 orderVerificationManager.performVerification(modeType, verifyCode, null); } @@ -1650,13 +1663,92 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi String action = intent.getAction(); if ("com.ouxuan.oxface.ACTION_PAUSE_CAMERA".equals(action)) { // 暂停摄像头预览 - isNeedCamera = false; - LogManager.logInfo(TAG, "接收到暂停摄像头预览广播 - isNeedCamera设置为false"); + pauseCameraWithTimeout(); + LogManager.logInfo(TAG, "接收到暂停摄像头预览广播"); } else if ("com.ouxuan.oxface.ACTION_RESUME_CAMERA".equals(action)) { // 恢复摄像头预览 - isNeedCamera = true; - LogManager.logInfo(TAG, "接收到恢复摄像头预览广播 - isNeedCamera设置为true"); + resumeCamera(); + LogManager.logInfo(TAG, "接收到恢复摄像头预览广播"); } } } + + /** + * 暂停摄像头并设置超时恢复 + */ + private void pauseCameraWithTimeout() { + pauseRequestCount++; + isCameraPausedByDialog = true; + isNeedCamera = false; + + // 取消之前的超时任务 + if (cameraTimeoutHandler != null) { + cameraTimeoutHandler.removeCallbacksAndMessages(null); + } + + // 设置新的超时任务 + cameraTimeoutHandler = new Handler(); + cameraTimeoutHandler.postDelayed(new Runnable() { + @Override + public void run() { + LogManager.logWarning(TAG, "摄像头暂停超时,自动恢复并关闭所有对话框"); + forceResumeAndCloseAllDialogs(); + } + }, CAMERA_RESUME_TIMEOUT); + + LogManager.logInfo(TAG, "摄像头已暂停,请求计数: " + pauseRequestCount + ", 超时时间: " + CAMERA_RESUME_TIMEOUT + "ms"); + } + + /** + * 恢复摄像头 + */ + private void resumeCamera() { + if (pauseRequestCount > 0) { + pauseRequestCount--; + } + + // 只有当所有暂停请求都结束时才恢复 + if (pauseRequestCount <= 0) { + isCameraPausedByDialog = false; + isNeedCamera = true; + pauseRequestCount = 0; // 确保不为负数 + + // 取消超时任务 + if (cameraTimeoutHandler != null) { + cameraTimeoutHandler.removeCallbacksAndMessages(null); + cameraTimeoutHandler = null; + } + + LogManager.logInfo(TAG, "摄像头已恢复"); + } else { + LogManager.logInfo(TAG, "仍有暂停请求,暂不恢复摄像头,剩余请求数: " + pauseRequestCount); + } + } + + /** + * 强制恢复摄像头并关闭所有对话框(超时后调用) + */ + private void forceResumeAndCloseAllDialogs() { + // 强制恢复摄像头 + isCameraPausedByDialog = false; + isNeedCamera = true; + pauseRequestCount = 0; + + // 取消超时任务 + if (cameraTimeoutHandler != null) { + cameraTimeoutHandler.removeCallbacksAndMessages(null); + cameraTimeoutHandler = null; + } + + // 关闭所有可能的对话框(发送广播通知所有Activity关闭) + try { + Intent intent = new Intent("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS"); + sendBroadcast(intent); + LogManager.logInfo(TAG, "已发送强制关闭对话框广播"); + } catch (Exception e) { + LogManager.logError(TAG, "发送强制关闭对话框广播失败", e); + } + + LogManager.logWarning(TAG, "摄像头已强制恢复,所有对话框已关闭"); + } } \ No newline at end of file diff --git a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java index 2cb2a1e..0a200d1 100644 --- a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java @@ -46,6 +46,9 @@ public class OrderSelectionActivity extends AppCompatActivity { private String faceBase64; private Handler countdownHandler; private int countdown = 60; + + // 强制关闭广播接收器 + private BroadcastReceiver forceCloseReceiver; @Override protected void onCreate(Bundle savedInstanceState) { @@ -69,6 +72,9 @@ public class OrderSelectionActivity extends AppCompatActivity { setupListeners(); startCountdown(); + // 注册强制关闭广播接收器 + registerForceCloseReceiver(); + LogManager.logInfo(TAG, "订单选择页面启动成功"); } @@ -305,9 +311,39 @@ public class OrderSelectionActivity extends AppCompatActivity { protected void onDestroy() { super.onDestroy(); stopCountdown(); + + // 取消注册广播接收器 + if (forceCloseReceiver != null) { + try { + unregisterReceiver(forceCloseReceiver); + } catch (Exception e) { + LogManager.logError(TAG, "取消注册强制关闭广播接收器失败", e); + } + } + LogManager.logInfo(TAG, "订单选择页面销毁"); } + /** + * 注册强制关闭广播接收器 + */ + private void registerForceCloseReceiver() { + forceCloseReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if ("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS".equals(intent.getAction())) { + LogManager.logInfo(TAG, "接收到强制关闭广播,关闭订单选择页面"); + finish(); + } + } + }; + + IntentFilter filter = new IntentFilter(); + filter.addAction("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS"); + registerReceiver(forceCloseReceiver, filter); + LogManager.logInfo(TAG, "强制关闭广播接收器已注册"); + } + @Override public void onBackPressed() { super.onBackPressed(); diff --git a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java index fba5b61..dcb773a 100644 --- a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java @@ -1,6 +1,9 @@ package com.ouxuan.oxface.orderOX; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.Window; @@ -28,6 +31,9 @@ public class OrderVerificationResultActivity extends AppCompatActivity { private TextView tvTitle, tvOrderNo, tvVerificationCode, tvOrderType, tvCardNo, tvStatus; private String orderNo, verificationCode, orderType, cardNo, status; private int verificationType; // 1-验证码验证, 4-扫码器验证 + + // 强制关闭广播接收器 + private BroadcastReceiver forceCloseReceiver; @Override protected void onCreate(Bundle savedInstanceState) { @@ -54,6 +60,9 @@ public class OrderVerificationResultActivity extends AppCompatActivity { // 暂停摄像头预览 pauseCameraPreview(); + // 注册强制关闭广播接收器 + registerForceCloseReceiver(); + LogManager.logInfo(TAG, "订单核销结果Activity启动成功"); } @@ -173,8 +182,39 @@ public class OrderVerificationResultActivity extends AppCompatActivity { @Override protected void onDestroy() { super.onDestroy(); + // 恢复摄像头预览 resumeCameraPreview(); + + // 取消注册广播接收器 + if (forceCloseReceiver != null) { + try { + unregisterReceiver(forceCloseReceiver); + } catch (Exception e) { + LogManager.logError(TAG, "取消注册强制关闭广播接收器失败", e); + } + } + LogManager.logInfo(TAG, "订单核销结果Activity已销毁"); } + + /** + * 注册强制关闭广播接收器 + */ + private void registerForceCloseReceiver() { + forceCloseReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if ("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS".equals(intent.getAction())) { + LogManager.logInfo(TAG, "接收到强制关闭广播,关闭订单核销结果页面"); + finish(); + } + } + }; + + IntentFilter filter = new IntentFilter(); + filter.addAction("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS"); + registerReceiver(forceCloseReceiver, filter); + LogManager.logInfo(TAG, "强制关闭广播接收器已注册"); + } } \ No newline at end of file diff --git a/app/src/main/java/com/ouxuan/oxface/orderOX/VerificationCodeActivity.java b/app/src/main/java/com/ouxuan/oxface/orderOX/VerificationCodeActivity.java index f81f2f3..74dfbee 100644 --- a/app/src/main/java/com/ouxuan/oxface/orderOX/VerificationCodeActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/orderOX/VerificationCodeActivity.java @@ -1,6 +1,9 @@ package com.ouxuan.oxface.orderOX; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -35,6 +38,9 @@ public class VerificationCodeActivity extends AppCompatActivity { private TextView tvTitle, tvSubtitle; private EditText[] verificationCodeInputs; + + // 强制关闭广播接收器 + private BroadcastReceiver forceCloseReceiver; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,6 +63,9 @@ public class VerificationCodeActivity extends AppCompatActivity { setupListeners(); setupAutoFocus(); + // 注册强制关闭广播接收器 + registerForceCloseReceiver(); + LogManager.logInfo(TAG, "验证码Activity启动成功"); } @@ -203,4 +212,40 @@ public class VerificationCodeActivity extends AppCompatActivity { super.onBackPressed(); setResult(RESULT_CANCELED); } + + @Override + protected void onDestroy() { + super.onDestroy(); + + // 取消注册广播接收器 + if (forceCloseReceiver != null) { + try { + unregisterReceiver(forceCloseReceiver); + } catch (Exception e) { + LogManager.logError(TAG, "取消注册强制关闭广播接收器失败", e); + } + } + + LogManager.logInfo(TAG, "验证码页面销毁"); + } + + /** + * 注册强制关闭广播接收器 + */ + private void registerForceCloseReceiver() { + forceCloseReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if ("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS".equals(intent.getAction())) { + LogManager.logInfo(TAG, "接收到强制关闭广播,关闭验证码页面"); + finish(); + } + } + }; + + IntentFilter filter = new IntentFilter(); + filter.addAction("com.ouxuan.oxface.ACTION_FORCE_CLOSE_DIALOGS"); + registerReceiver(forceCloseReceiver, filter); + LogManager.logInfo(TAG, "强制关闭广播接收器已注册"); + } } \ No newline at end of file