1 package net.osdn.gokigen.a01d.camera.fujix.wrapper.connection;
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.util.Log;
7 import androidx.annotation.NonNull;
8 import androidx.preference.PreferenceManager;
10 import net.osdn.gokigen.a01d.R;
11 import net.osdn.gokigen.a01d.camera.ICameraConnection;
12 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
13 import net.osdn.gokigen.a01d.camera.fujix.IFujiXInterfaceProvider;
14 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCommandCallback;
15 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCommandIssuer;
16 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.CameraRemoteMessage;
17 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.RegistrationMessage;
18 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage;
19 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage2nd;
20 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage3rd;
21 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage4th;
22 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartReceiveOnly;
23 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
25 public class FujiXCameraConnectSequence implements Runnable, IFujiXCommandCallback
27 private final String TAG = this.toString();
29 public static final int SEQ_DUMMY = 0;
30 public static final int SEQ_REGISTRATION = 1;
31 public static final int SEQ_START = 2;
32 public static final int SEQ_START_2ND = 3;
33 public static final int SEQ_START_2ND_RECEIVE = 4;
34 public static final int SEQ_START_3RD = 5;
35 public static final int SEQ_START_4TH = 6;
36 public static final int SEQ_CAMERA_REMOTE = 7;
39 private final Activity context;
40 private final ICameraConnection cameraConnection;
41 private final ICameraStatusReceiver cameraStatusReceiver;
42 private final IFujiXInterfaceProvider interfaceProvider;
43 private final IFujiXCommandIssuer commandIssuer;
44 private boolean isBothLiveView = false;
46 FujiXCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IFujiXInterfaceProvider interfaceProvider)
48 Log.v(TAG, "FujiXCameraConnectSequence");
49 this.context = context;
50 this.cameraConnection = cameraConnection;
51 this.cameraStatusReceiver = statusReceiver;
52 this.interfaceProvider = interfaceProvider;
53 this.commandIssuer = interfaceProvider.getCommandIssuer();
63 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
64 isBothLiveView = preferences.getBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
68 //isBothLiveView = false;
73 IFujiXCommandIssuer issuer = interfaceProvider.getCommandIssuer();
74 if (!issuer.isConnected())
76 if (!interfaceProvider.getCommandCommunication().connect())
79 onConnectError(context.getString(R.string.dialog_title_connect_failed));
87 sendRegistrationMessage();
93 onConnectError(e.getLocalizedMessage());
97 private void onConnectError(String reason)
99 cameraConnection.alertConnectingFailed(reason);
103 public void receivedMessage(int id, byte[] rx_body)
105 Log.v(TAG, "receivedMessage : " + id + "[" + rx_body.length + " bytes]");
108 case SEQ_REGISTRATION:
109 if (checkRegistrationMessage(rx_body))
111 commandIssuer.enqueueCommand(new StartMessage(this));
116 commandIssuer.enqueueCommand(new StartMessage2nd(this));
120 if (rx_body.length == (int)rx_body[0])
122 // なぜかもうちょっとデータが飛んでくるので待つ
123 //commandIssuer.enqueueCommand(new StartReceiveOnly(this));
125 commandIssuer.enqueueCommand(new StartMessage3rd(this));
129 commandIssuer.enqueueCommand(new StartMessage3rd(this));
133 case SEQ_START_2ND_RECEIVE:
134 commandIssuer.enqueueCommand(new StartMessage3rd(this));
138 commandIssuer.enqueueCommand(new StartMessage4th(this));
142 commandIssuer.enqueueCommand(new CameraRemoteMessage(this));
145 case SEQ_CAMERA_REMOTE:
146 connectFinished(rx_body);
150 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
151 onConnectError(context.getString(R.string.connect_receive_unknown_message));
156 private void sendRegistrationMessage()
158 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
159 commandIssuer.enqueueCommand(new RegistrationMessage(this));
162 private boolean checkRegistrationMessage(byte[] receiveData)
165 if (receiveData.length == 8)
167 if ((receiveData[0] == 0x05) && (receiveData[1] == 0x00) && (receiveData[2] == 0x00) && (receiveData[3] == 0x00) &&
168 (receiveData[4] == 0x19) && (receiveData[5] == 0x20) && (receiveData[6] == 0x00) && (receiveData[7] == 0x00)) {
177 private void connectFinished(byte[] rx_body)
183 interfaceProvider.getAsyncEventCommunication().connect();
193 private void onConnectNotify()
197 final Thread thread = new Thread(new Runnable()
203 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
204 cameraStatusReceiver.onCameraConnected();
205 Log.v(TAG, "onConnectNotify()");