1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.connection;
3 import android.app.Activity;
4 import android.graphics.Color;
5 import android.util.Log;
7 import androidx.annotation.NonNull;
9 import net.osdn.gokigen.a01d.R;
10 import net.osdn.gokigen.a01d.camera.ICameraConnection;
11 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
12 import net.osdn.gokigen.a01d.camera.nikon.wrapper.command.messages.specific.NikonRegistrationMessage;
13 import net.osdn.gokigen.a01d.camera.nikon.wrapper.status.NikonStatusChecker;
14 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
15 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
16 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
17 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages;
18 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
20 public class NikonCameraConnectSequence implements Runnable, IPtpIpCommandCallback, IPtpIpMessages
22 private final String TAG = this.toString();
24 private final Activity context;
25 private final ICameraConnection cameraConnection;
26 private final ICameraStatusReceiver cameraStatusReceiver;
27 private final IPtpIpInterfaceProvider interfaceProvider;
28 private final IPtpIpCommandPublisher commandIssuer;
29 private final NikonStatusChecker statusChecker;
30 private boolean isDumpLog = true;
32 NikonCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull NikonStatusChecker statusChecker)
34 Log.v(TAG, " NikonCameraConnectSequence");
35 this.context = context;
36 this.cameraConnection = cameraConnection;
37 this.cameraStatusReceiver = statusReceiver;
38 this.interfaceProvider = interfaceProvider;
39 this.commandIssuer = interfaceProvider.getCommandPublisher();
40 this.statusChecker = statusChecker;
49 IPtpIpCommandPublisher issuer = interfaceProvider.getCommandPublisher();
50 if (!issuer.isConnected())
52 if (!interfaceProvider.getCommandCommunication().connect())
55 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_nikon), false, true, Color.RED);
56 onConnectError(context.getString(R.string.dialog_title_connect_failed_nikon));
62 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
68 sendRegistrationMessage();
74 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_nikon), false, true, Color.RED);
75 onConnectError(e.getLocalizedMessage());
79 private void onConnectError(String reason)
81 cameraConnection.alertConnectingFailed(reason);
85 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
87 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
91 public boolean isReceiveMulti()
97 public void receivedMessage(int id, byte[] rx_body)
101 case SEQ_REGISTRATION:
102 if (checkRegistrationMessage(rx_body))
104 sendInitEventRequest(rx_body);
108 onConnectError(context.getString(R.string.connect_error_message));
112 case SEQ_EVENT_INITIALIZE:
113 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0);
114 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_INIT_SESSION, 50, isDumpLog, 0, 0x1001, 0, 0, 0, 0, 0)); // GetDeviceInfo
117 case SEQ_INIT_SESSION:
118 if (checkEventInitialize(rx_body))
120 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0);
121 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, 50, isDumpLog, 0, 0x1002, 4, 0x41, 0, 0, 0)); // OpenSession
125 onConnectError(context.getString(R.string.connect_error_message));
128 case SEQ_OPEN_SESSION:
129 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0);
130 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHANGE_REMOTE, 50, isDumpLog, 0, 0x902c, 4, 0x01, 0, 0, 0)); //
133 case SEQ_CHANGE_REMOTE:
134 case SEQ_SET_EVENT_MODE:
135 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0);
136 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0);
137 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
139 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
143 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
144 onConnectError(context.getString(R.string.connect_receive_unknown_message));
149 private void sendRegistrationMessage()
151 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
152 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
153 commandIssuer.enqueueCommand(new NikonRegistrationMessage(this));
156 private void sendInitEventRequest(byte[] receiveData)
158 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start_2), false, false, 0);
159 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2));
162 int eventConnectionNumber = (receiveData[8] & 0xff);
163 eventConnectionNumber = eventConnectionNumber + ((receiveData[9] & 0xff) << 8);
164 eventConnectionNumber = eventConnectionNumber + ((receiveData[10] & 0xff) << 16);
165 eventConnectionNumber = eventConnectionNumber + ((receiveData[11] & 0xff) << 24);
166 statusChecker.setEventConnectionNumber(eventConnectionNumber);
167 interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());
169 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, 50, isDumpLog, 0, 0x1002, 4, 0x41, 0, 0, 0));
177 private boolean checkRegistrationMessage(byte[] receiveData)
179 // データ(Connection Number)がないときにはエラーと判断する
180 return (!((receiveData == null)||(receiveData.length < 12)));
183 private boolean checkEventInitialize(byte[] receiveData)
185 Log.v(TAG, "checkEventInitialize() ");
186 return (!(receiveData == null));
189 private void connectFinished()
194 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
199 //interfaceProvider.getAsyncEventCommunication().connect();
200 //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener()); ステータスの定期確認は実施しない
203 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
213 private void onConnectNotify()
217 final Thread thread = new Thread(new Runnable()
223 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
224 cameraStatusReceiver.onCameraConnected();
225 Log.v(TAG, " onConnectNotify()");