1 package net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.connection;
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.graphics.Color;
6 import android.util.Log;
8 import androidx.annotation.NonNull;
9 import androidx.preference.PreferenceManager;
11 import net.osdn.gokigen.pkremote.R;
12 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraConnection;
13 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatusReceiver;
14 import net.osdn.gokigen.pkremote.camera.vendor.fujix.IFujiXInterfaceProvider;
15 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXCommandCallback;
16 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXCommandPublisher;
17 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXMessages;
18 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.QueryCameraCapabilities;
19 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.StatusRequestMessage;
20 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback1st;
21 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback2nd;
22 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback3rd;
23 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback4th;
24 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.CameraRemoteMessage;
25 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.RegistrationMessage;
26 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage;
27 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage2ndRead;
28 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage3rd;
29 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage4th;
30 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage5th;
31 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.status.IFujiXRunModeHolder;
32 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
35 public class FujiXCameraConnectSequenceForPlayback implements Runnable, IFujiXCommandCallback, IFujiXMessages
37 private final String TAG = this.toString();
39 private final Activity context;
40 private final ICameraConnection cameraConnection;
41 private final ICameraStatusReceiver cameraStatusReceiver;
42 private final IFujiXInterfaceProvider interfaceProvider;
43 private final IFujiXCommandPublisher commandIssuer;
44 private boolean isBothLiveView = false;
46 FujiXCameraConnectSequenceForPlayback(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IFujiXInterfaceProvider interfaceProvider)
48 Log.v(TAG, " FujiXCameraConnectSequenceForPlayback");
49 this.context = context;
50 this.cameraConnection = cameraConnection;
51 this.cameraStatusReceiver = statusReceiver;
52 this.interfaceProvider = interfaceProvider;
53 this.commandIssuer = interfaceProvider.getCommandPublisher();
63 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
64 isBothLiveView = preferences.getBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
68 //isBothLiveView = false;
73 IFujiXCommandPublisher issuer = interfaceProvider.getCommandPublisher();
74 if (!issuer.isConnected())
76 if (!interfaceProvider.getCommandCommunication().connect())
79 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_fuji), false, true, Color.RED);
80 onConnectError(context.getString(R.string.dialog_title_connect_failed_fuji));
86 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
92 sendRegistrationMessage();
98 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_fuji), false, true, Color.RED);
99 onConnectError(e.getLocalizedMessage());
103 private void onConnectError(String reason)
105 cameraConnection.alertConnectingFailed(reason);
109 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
111 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
115 public boolean isReceiveMulti()
121 public void receivedMessage(int id, byte[] rx_body)
123 //Log.v(TAG, "receivedMessage : " + id + "[" + rx_body.length + " bytes]");
124 //int bodyLength = 0;
125 IFujiXRunModeHolder runModeHolder;
128 case SEQ_REGISTRATION:
129 if (checkRegistrationMessage(rx_body))
131 commandIssuer.enqueueCommand(new StartMessage(this));
135 onConnectError(context.getString(R.string.connect_error_message));
140 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting1), false, false, 0);
141 commandIssuer.enqueueCommand(new StartMessage2ndRead(this));
144 case SEQ_START_2ND_READ:
145 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting2), false, false, 0);
146 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connecting));
147 Log.v(TAG, " RECV: (SEQ_START_2ND_READ) : " + rx_body.length);
148 if (rx_body.length == (int)rx_body[0])
150 // なぜかもうちょっとデータが飛んでくるので待つ
151 //commandIssuer.enqueueCommand(new ReceiveOnly(this));
153 commandIssuer.enqueueCommand(new StartMessage3rd(this));
157 IFujiXCommandCallback callback = interfaceProvider.getStatusHolder();
158 if (callback != null)
160 callback.receivedMessage(id, rx_body);
162 commandIssuer.enqueueCommand(new StartMessage3rd(this));
166 case SEQ_START_2ND_RECEIVE:
167 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting3), false, false, 0);
168 commandIssuer.enqueueCommand(new StartMessage3rd(this));
172 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting4), false, false, 0);
173 commandIssuer.enqueueCommand(new StartMessage4th(this));
177 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting5), false, false, 0);
180 // カメラのLCDと遠隔のライブビューを同時に表示する場合...
181 commandIssuer.enqueueCommand(new CameraRemoteMessage(this));
185 commandIssuer.enqueueCommand(new StartMessage5th(this));
190 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting6), false, false, 0);
191 commandIssuer.enqueueCommand(new QueryCameraCapabilities(this));
192 //commandIssuer.enqueueCommand(new StatusRequestMessage(this));
195 case SEQ_QUERY_CAMERA_CAPABILITIES:
196 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting7), false, false, 0);
197 commandIssuer.enqueueCommand(new CameraRemoteMessage(this));
200 case SEQ_CAMERA_REMOTE:
201 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting8), false, false, 0);
202 commandIssuer.enqueueCommand(new ChangeToPlayback1st(this));
203 runModeHolder = interfaceProvider.getRunModeHolder();
204 if (runModeHolder != null)
206 runModeHolder.transitToPlaybackMode(false);
211 case SEQ_CHANGE_TO_PLAYBACK_1ST:
212 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting9), false, false, 0);
213 //commandIssuer.enqueueCommand(new ChangeToPlayback2nd(this));
214 commandIssuer.enqueueCommand(new StatusRequestMessage(this));
217 case SEQ_CHANGE_TO_PLAYBACK_2ND:
218 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting10), false, false, 0);
219 commandIssuer.enqueueCommand(new ChangeToPlayback3rd(this));
222 case SEQ_CHANGE_TO_PLAYBACK_3RD:
223 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting11), false, false, 0);
224 commandIssuer.enqueueCommand(new ChangeToPlayback4th(this));
227 case SEQ_CHANGE_TO_PLAYBACK_4TH:
228 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting12), false, false, 0);
229 commandIssuer.enqueueCommand(new StatusRequestMessage(this));
232 case SEQ_STATUS_REQUEST:
233 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
234 IFujiXCommandCallback callback = interfaceProvider.getStatusHolder();
235 if (callback != null)
237 callback.receivedMessage(id, rx_body);
239 runModeHolder = interfaceProvider.getRunModeHolder();
240 if (runModeHolder != null)
242 runModeHolder.transitToPlaybackMode(true);
245 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
249 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
250 onConnectError(context.getString(R.string.connect_receive_unknown_message));
255 private void sendRegistrationMessage()
257 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
258 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
259 commandIssuer.enqueueCommand(new RegistrationMessage(this));
262 private boolean checkRegistrationMessage(byte[] receiveData)
265 if ((receiveData == null)||(receiveData.length < 8))
271 if (receiveData.length == 8)
273 if ((receiveData[0] == 0x05) && (receiveData[1] == 0x00) && (receiveData[2] == 0x00) && (receiveData[3] == 0x00) &&
274 (receiveData[4] == 0x19) && (receiveData[5] == 0x20) && (receiveData[6] == 0x00) && (receiveData[7] == 0x00)) {
276 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.error_reply_from_camera), false, true, Color.RED);
279 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.other_error_reply_from_camera), false, true, Color.RED);
282 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.registration_reply_from_camera), false, false, 0);
287 private void connectFinished()
292 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
297 interfaceProvider.getAsyncEventCommunication().connect();
298 //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener()); ステータスの定期確認は実施しない
301 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
311 private void onConnectNotify()
315 final Thread thread = new Thread(new Runnable()
321 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
322 cameraStatusReceiver.onCameraConnected();
323 Log.v(TAG, "onConnectNotify()");