@@ -31,6 +31,9 @@ public class CameraService extends Service {
|
||
| 31 | 31 |
public static final int MSG_CAMERA_CONN_ERROR = 8001; |
| 32 | 32 |
public static final int MSG_CAMERA_NEW_PHOTO_FOUND = 8002; |
| 33 | 33 |
|
| 34 |
+ |
|
| 35 |
+ public static final String EXTRA_CMD = "cmd"; |
|
| 36 |
+ public static final String EXTRA_SESSION_DIR = "session_dir"; |
|
| 34 | 37 |
public static final int CMD_INIT_CAMERA_CONNECTION = 9000; |
| 35 | 38 |
public static final int CMD_EXIT_CAMERA_CONNECTION = 9001; |
| 36 | 39 |
public static final int CMD_START_CAPTURE_PHOTO = 9002; |
@@ -51,8 +54,8 @@ public class CameraService extends Service {
|
||
| 51 | 54 |
|
| 52 | 55 |
@Override |
| 53 | 56 |
public int onStartCommand(Intent intent, int flags, int startId) {
|
| 54 |
- if(intent!=null&&intent.getIntExtra("cmd",0)>0){
|
|
| 55 |
- int cmd = intent.getIntExtra("cmd",0);
|
|
| 57 |
+ if(intent!=null&&intent.getIntExtra(EXTRA_CMD,0)>0){
|
|
| 58 |
+ int cmd = intent.getIntExtra(EXTRA_CMD,0); |
|
| 56 | 59 |
if(cmd == CMD_EXIT_CAMERA_CONNECTION){
|
| 57 | 60 |
stopCameraService(); |
| 58 | 61 |
return START_NOT_STICKY; |
@@ -63,7 +66,7 @@ public class CameraService extends Service {
|
||
| 63 | 66 |
} |
| 64 | 67 |
}else if(cmd == CMD_START_CAPTURE_PHOTO){
|
| 65 | 68 |
if(isInitExecuted){
|
| 66 |
- sessionWorkingDirPath = intent.getStringExtra("session_dir");
|
|
| 69 |
+ sessionWorkingDirPath = intent.getStringExtra(EXTRA_SESSION_DIR); |
|
| 67 | 70 |
if(!TextUtils.isEmpty(sessionWorkingDirPath)){
|
| 68 | 71 |
startCapture(); |
| 69 | 72 |
} |
@@ -1,37 +1,42 @@ |
||
| 1 | 1 |
package ai.pai.lensman.session; |
| 2 | 2 |
|
| 3 |
+import android.content.BroadcastReceiver; |
|
| 4 |
+import android.content.Context; |
|
| 5 |
+import android.content.Intent; |
|
| 6 |
+import android.content.IntentFilter; |
|
| 3 | 7 |
import android.os.Handler; |
| 4 | 8 |
import android.os.Handler.Callback; |
| 5 | 9 |
import android.os.Message; |
| 6 |
-import android.text.TextUtils; |
|
| 7 | 10 |
|
| 8 | 11 |
import com.android.common.utils.LogHelper; |
| 9 | 12 |
|
| 10 | 13 |
import java.io.File; |
| 11 |
-import java.util.Timer; |
|
| 12 |
-import java.util.TimerTask; |
|
| 13 | 14 |
|
| 15 |
+import ai.pai.lensman.App; |
|
| 14 | 16 |
import ai.pai.lensman.bean.PhotoBean; |
| 15 | 17 |
import ai.pai.lensman.bean.SessionBean; |
| 16 |
-import ai.pai.lensman.dslr.CameraJNIInterface; |
|
| 18 |
+import ai.pai.lensman.dslr.CameraService; |
|
| 17 | 19 |
import ai.pai.lensman.service.Constants; |
| 18 | 20 |
|
| 21 |
+import static ai.pai.lensman.dslr.CameraService.ACTION_CAMERA_SERVICE_STATUS_CHANGE; |
|
| 22 |
+import static ai.pai.lensman.dslr.CameraService.CMD_EXIT_CAMERA_CONNECTION; |
|
| 23 |
+import static ai.pai.lensman.dslr.CameraService.CMD_INIT_CAMERA_CONNECTION; |
|
| 24 |
+import static ai.pai.lensman.dslr.CameraService.CMD_START_CAPTURE_PHOTO; |
|
| 25 |
+import static ai.pai.lensman.dslr.CameraService.EXTRA_CMD; |
|
| 26 |
+import static ai.pai.lensman.dslr.CameraService.EXTRA_DATA_PART; |
|
| 27 |
+import static ai.pai.lensman.dslr.CameraService.EXTRA_STATUS_PART; |
|
| 28 |
+import static ai.pai.lensman.dslr.CameraService.MSG_CAMERA_CONN_ERROR; |
|
| 29 |
+import static ai.pai.lensman.dslr.CameraService.MSG_CAMERA_INIT_SUCCESS; |
|
| 30 |
+import static ai.pai.lensman.dslr.CameraService.MSG_CAMERA_NEW_PHOTO_FOUND; |
|
| 31 |
+ |
|
| 19 | 32 |
public class SessionInteractor implements Callback{
|
| 20 | 33 |
|
| 21 |
- private Timer photoCaptureTimer; |
|
| 34 |
+ private Handler mHandler; |
|
| 22 | 35 |
private SessionBean sessionBean; |
| 23 | 36 |
private SessionListener listener; |
| 24 | 37 |
private String sessionWorkingDirPath; |
| 25 | 38 |
|
| 26 |
- private boolean isWorking; |
|
| 27 |
- private boolean isLastQueryReturned = true; |
|
| 28 |
- |
|
| 29 |
- private Handler cameraInitHandler; |
|
| 30 |
- private static final int MSG_CAMERA_INIT_EVENT = 1001; |
|
| 31 |
- |
|
| 32 |
- private static final String MSG_TYPE_CAMERA_ERROR = "camero error";//相机错误,可能是线松动了。先exit,然后重新init |
|
| 33 |
- private static final String MSG_TYPE_NOT_INIT = "not init"; //需要初始化 |
|
| 34 |
- private static final String MSG_TYPE_TIME_OUT = "time out"; //继续调用waitforevent |
|
| 39 |
+ private static final int MSG_RETRY_INIT_CAMERA = 1001; |
|
| 35 | 40 |
|
| 36 | 41 |
private static final String TAG = "SessionInteractor"; |
| 37 | 42 |
|
@@ -42,16 +47,10 @@ public class SessionInteractor implements Callback{
|
||
| 42 | 47 |
|
| 43 | 48 |
@Override |
| 44 | 49 |
public boolean handleMessage(Message message) {
|
| 45 |
- if(message.what==MSG_CAMERA_INIT_EVENT){
|
|
| 46 |
- int result = CameraJNIInterface.getInstance().java_mygpcamerainit(); |
|
| 47 |
- listener.onCameraStatusChanged("mygpcamerainit return "+result);
|
|
| 48 |
- if(result>=0){
|
|
| 49 |
- listener.onSessionStartSuccess(sessionBean.sessionId); |
|
| 50 |
- isWorking = true; |
|
| 51 |
- startCapture(); |
|
| 52 |
- }else{
|
|
| 53 |
- cameraInitHandler.sendEmptyMessageDelayed(MSG_CAMERA_INIT_EVENT,1000); |
|
| 54 |
- } |
|
| 50 |
+ if(message.what == MSG_RETRY_INIT_CAMERA){
|
|
| 51 |
+ Intent intent = new Intent(App.getAppContext(), CameraService.class); |
|
| 52 |
+ intent.putExtra(EXTRA_CMD,CMD_INIT_CAMERA_CONNECTION); |
|
| 53 |
+ App.getAppContext().startService(intent); |
|
| 55 | 54 |
} |
| 56 | 55 |
return false; |
| 57 | 56 |
} |
@@ -66,105 +65,36 @@ public class SessionInteractor implements Callback{
|
||
| 66 | 65 |
|
| 67 | 66 |
public void startSession(){
|
| 68 | 67 |
LogHelper.d(TAG,"startSession"); |
| 68 |
+ mHandler = new Handler(this); |
|
| 69 | 69 |
sessionWorkingDirPath = Constants.APP_IMAGE_DIR + File.separator + sessionBean.sessionId |
| 70 | 70 |
+ File.separator + ai.pai.lensman.utils.Constants.ORIGIN_DIR_NAME; |
| 71 | 71 |
new File(sessionWorkingDirPath).mkdirs(); |
| 72 |
- |
|
| 73 |
- int result = CameraJNIInterface.getInstance().java_mygpcamerainit(); |
|
| 74 |
- listener.onCameraStatusChanged("mygpcamerainit return "+result);
|
|
| 75 |
- if(result>=0){
|
|
| 76 |
- LogHelper.d("czy","mygpcamerainit init success="+result);
|
|
| 77 |
- listener.onSessionStartSuccess(sessionBean.sessionId); |
|
| 78 |
- isWorking = true; |
|
| 79 |
- startCapture(); |
|
| 80 |
- }else{
|
|
| 81 |
- LogHelper.d("czy","mygpcamerainit init fail ="+result+" and schedule retry");
|
|
| 82 |
- if(cameraInitHandler!=null){
|
|
| 83 |
- cameraInitHandler.removeCallbacksAndMessages(null); |
|
| 84 |
- cameraInitHandler = null; |
|
| 85 |
- } |
|
| 86 |
- cameraInitHandler = new Handler(this); |
|
| 87 |
- cameraInitHandler.sendEmptyMessageDelayed(MSG_CAMERA_INIT_EVENT,1000); |
|
| 88 |
- listener.onSessionStartError(sessionBean.sessionId); |
|
| 89 |
- } |
|
| 90 |
- |
|
| 72 |
+ IntentFilter intentFilter = new IntentFilter(ACTION_CAMERA_SERVICE_STATUS_CHANGE); |
|
| 73 |
+ App.getAppContext().registerReceiver(cameraReceiver,intentFilter); |
|
| 74 |
+ Intent intent = new Intent(App.getAppContext(), CameraService.class); |
|
| 75 |
+ intent.putExtra(EXTRA_CMD,CMD_INIT_CAMERA_CONNECTION); |
|
| 76 |
+ App.getAppContext().startService(intent); |
|
| 91 | 77 |
} |
| 92 | 78 |
|
| 93 | 79 |
public void startCapture() {
|
| 94 |
- if(photoCaptureTimer !=null){
|
|
| 95 |
- photoCaptureTimer.cancel(); |
|
| 96 |
- photoCaptureTimer = null; |
|
| 97 |
- } |
|
| 98 |
- photoCaptureTimer = new Timer(); |
|
| 99 |
- photoCaptureTimer.schedule(new TimerTask() {
|
|
| 100 |
- @Override |
|
| 101 |
- public void run() {
|
|
| 102 |
- fetchPhotoTask(); |
|
| 103 |
- } |
|
| 104 |
- },1000,10); |
|
| 105 |
- LogHelper.d("czy","startCapture ,schedule fetchPhotoTask ");
|
|
| 106 |
- } |
|
| 107 |
- |
|
| 108 |
- private void fetchPhotoTask(){
|
|
| 109 |
- if(!isWorking){
|
|
| 110 |
- return; |
|
| 111 |
- } |
|
| 80 |
+ Intent intent = new Intent(App.getAppContext(), CameraService.class); |
|
| 81 |
+ intent.putExtra(EXTRA_CMD,CMD_START_CAPTURE_PHOTO); |
|
| 82 |
+ App.getAppContext().startService(intent); |
|
| 112 | 83 |
|
| 113 |
- if(!isLastQueryReturned){
|
|
| 114 |
- LogHelper.d("czy","fetchPhotoTask last query not finished,return ");
|
|
| 115 |
- return; |
|
| 116 |
- } |
|
| 117 |
- |
|
| 118 |
- isLastQueryReturned = false; |
|
| 119 |
- String eventMsg = CameraJNIInterface.getInstance().java_mygpcamerawaitforevent(sessionWorkingDirPath); |
|
| 120 |
- listener.onCameraStatusChanged("mygpcamerawaitforevent return "+eventMsg);
|
|
| 121 |
- LogHelper.d("czy","mygpcamerawaitforevent return result = "+eventMsg);
|
|
| 122 |
- if(eventMsg!=null && eventMsg.length()>0){
|
|
| 123 |
- if(MSG_TYPE_NOT_INIT.equalsIgnoreCase(eventMsg)||MSG_TYPE_CAMERA_ERROR.equalsIgnoreCase(eventMsg)){
|
|
| 124 |
- endSession(); |
|
| 125 |
- startSession(); |
|
| 126 |
- }else if(MSG_TYPE_TIME_OUT.equalsIgnoreCase(eventMsg)){
|
|
| 127 |
- |
|
| 128 |
- }else{
|
|
| 129 |
- String sub = eventMsg.substring(0,1); |
|
| 130 |
- if(TextUtils.isDigitsOnly(sub)){
|
|
| 131 |
- LogHelper.d(TAG,"fetchPhotoTask new photo found"); |
|
| 132 |
- PhotoBean bean = new PhotoBean(); |
|
| 133 |
- bean.photoName = eventMsg; |
|
| 134 |
- bean.photoId = Long.parseLong(eventMsg.substring(0,eventMsg.lastIndexOf(".")));
|
|
| 135 |
- bean.captureTime = bean.photoId; |
|
| 136 |
- bean.isRawPhoto = true; |
|
| 137 |
- bean.uploadStatus = PhotoBean.UploadStatus.STATUS_NO_BEGIN; |
|
| 138 |
- bean.sessionId = sessionBean.sessionId; |
|
| 139 |
- bean.lensmanId = sessionBean.lensmanId; |
|
| 140 |
- bean.sessionSeq = sessionBean.sessionSeq; |
|
| 141 |
- bean.sessionDate = sessionBean.sessionDate; |
|
| 142 |
- bean.sessionCreateTime = sessionBean.createTime; |
|
| 143 |
- bean.photoPath = sessionWorkingDirPath+File.separator+eventMsg; |
|
| 144 |
- listener.onSessionPhotoCaptured(bean); |
|
| 145 |
- } |
|
| 146 |
- } |
|
| 147 |
- } |
|
| 148 |
- |
|
| 149 |
- isLastQueryReturned = true; |
|
| 150 | 84 |
} |
| 151 | 85 |
|
| 152 | 86 |
|
| 153 |
- |
|
| 154 | 87 |
public void endSession(){
|
| 155 |
- isWorking = false; |
|
| 156 |
- if(photoCaptureTimer !=null){
|
|
| 157 |
- photoCaptureTimer.cancel(); |
|
| 158 |
- photoCaptureTimer = null; |
|
| 159 |
- } |
|
| 160 |
- int result = CameraJNIInterface.getInstance().java_mygpcameraexit(); |
|
| 161 |
- listener.onCameraStatusChanged("mygpcameraexit return "+result);
|
|
| 162 |
- listener.onSessionEnd(sessionBean.sessionId); |
|
| 163 |
- if(cameraInitHandler!=null){
|
|
| 164 |
- cameraInitHandler.removeCallbacksAndMessages(null); |
|
| 165 |
- cameraInitHandler = null; |
|
| 88 |
+ if(mHandler!=null){
|
|
| 89 |
+ mHandler.removeCallbacksAndMessages(null); |
|
| 90 |
+ mHandler = null; |
|
| 166 | 91 |
} |
| 92 |
+ Intent intent = new Intent(App.getAppContext(), CameraService.class); |
|
| 93 |
+ intent.putExtra(EXTRA_CMD,CMD_EXIT_CAMERA_CONNECTION); |
|
| 94 |
+ App.getAppContext().startService(intent); |
|
| 95 |
+ App.getAppContext().unregisterReceiver(cameraReceiver); |
|
| 167 | 96 |
LogHelper.d("czy","endSession ");
|
| 97 |
+ listener.onSessionEnd(sessionBean.sessionId); |
|
| 168 | 98 |
} |
| 169 | 99 |
|
| 170 | 100 |
public void deletePhoto(PhotoBean photoBean){
|
@@ -172,4 +102,41 @@ public class SessionInteractor implements Callback{
|
||
| 172 | 102 |
new File(photoBean.photoPath).delete(); |
| 173 | 103 |
} |
| 174 | 104 |
|
| 105 |
+ private BroadcastReceiver cameraReceiver = new BroadcastReceiver() {
|
|
| 106 |
+ @Override |
|
| 107 |
+ public void onReceive(Context context, Intent intent) {
|
|
| 108 |
+ if(!ACTION_CAMERA_SERVICE_STATUS_CHANGE.equals(intent.getAction())){
|
|
| 109 |
+ return; |
|
| 110 |
+ } |
|
| 111 |
+ int status = intent.getIntExtra(EXTRA_STATUS_PART,0); |
|
| 112 |
+ if(status==MSG_CAMERA_INIT_SUCCESS){
|
|
| 113 |
+ listener.onSessionStartSuccess(sessionBean.sessionId); |
|
| 114 |
+ listener.onCameraStatusChanged("camera init success");
|
|
| 115 |
+ startCapture(); |
|
| 116 |
+ }else if(status == MSG_CAMERA_CONN_ERROR){
|
|
| 117 |
+ listener.onCameraStatusChanged("camera connection error");
|
|
| 118 |
+ listener.onSessionStartError(sessionBean.sessionId); |
|
| 119 |
+ mHandler.sendEmptyMessageDelayed(MSG_RETRY_INIT_CAMERA,3000); |
|
| 120 |
+ }else if(status == MSG_CAMERA_NEW_PHOTO_FOUND){
|
|
| 121 |
+ String eventMsg = intent.getStringExtra(EXTRA_DATA_PART); |
|
| 122 |
+ LogHelper.d(TAG,"fetchPhotoTask new photo found"); |
|
| 123 |
+ listener.onCameraStatusChanged("camera new Photo found "+eventMsg);
|
|
| 124 |
+ PhotoBean bean = new PhotoBean(); |
|
| 125 |
+ bean.photoName = eventMsg; |
|
| 126 |
+ bean.photoId = Long.parseLong(eventMsg.substring(0,eventMsg.lastIndexOf(".")));
|
|
| 127 |
+ bean.captureTime = bean.photoId; |
|
| 128 |
+ bean.isRawPhoto = true; |
|
| 129 |
+ bean.uploadStatus = PhotoBean.UploadStatus.STATUS_NO_BEGIN; |
|
| 130 |
+ bean.sessionId = sessionBean.sessionId; |
|
| 131 |
+ bean.lensmanId = sessionBean.lensmanId; |
|
| 132 |
+ bean.sessionSeq = sessionBean.sessionSeq; |
|
| 133 |
+ bean.sessionDate = sessionBean.sessionDate; |
|
| 134 |
+ bean.sessionCreateTime = sessionBean.createTime; |
|
| 135 |
+ bean.photoPath = sessionWorkingDirPath+File.separator+eventMsg; |
|
| 136 |
+ listener.onSessionPhotoCaptured(bean); |
|
| 137 |
+ } |
|
| 138 |
+ |
|
| 139 |
+ } |
|
| 140 |
+ }; |
|
| 141 |
+ |
|
| 175 | 142 |
} |