OSDN Git Service

6ac6bb7859bc95d385dc5326597c4bd66bd298bd
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / canon / wrapper / CanonInterfaceProvider.java
1 package net.osdn.gokigen.a01d.camera.canon.wrapper;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.util.Log;
6
7 import androidx.annotation.NonNull;
8 import androidx.preference.PreferenceManager;
9
10 import net.osdn.gokigen.a01d.IInformationReceiver;
11 import net.osdn.gokigen.a01d.camera.ICameraConnection;
12 import net.osdn.gokigen.a01d.camera.ICameraInformation;
13 import net.osdn.gokigen.a01d.camera.ICameraStatus;
14 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
15 import net.osdn.gokigen.a01d.camera.ICameraStatusWatcher;
16 import net.osdn.gokigen.a01d.camera.ICaptureControl;
17 import net.osdn.gokigen.a01d.camera.IDisplayInjector;
18 import net.osdn.gokigen.a01d.camera.IFocusingControl;
19 import net.osdn.gokigen.a01d.camera.IFocusingModeNotify;
20 import net.osdn.gokigen.a01d.camera.ILiveViewControl;
21 import net.osdn.gokigen.a01d.camera.IZoomLensControl;
22 import net.osdn.gokigen.a01d.camera.canon.operation.CanonCaptureControl;
23 import net.osdn.gokigen.a01d.camera.canon.operation.CanonFocusingControl;
24 import net.osdn.gokigen.a01d.camera.canon.wrapper.connection.CanonConnection;
25 import net.osdn.gokigen.a01d.camera.canon.operation.CanonZoomLensControl;
26 import net.osdn.gokigen.a01d.camera.canon.wrapper.hardware.CanonCameraInformation;
27 import net.osdn.gokigen.a01d.camera.canon.wrapper.liveview.CanonLiveViewControl;
28 import net.osdn.gokigen.a01d.camera.canon.wrapper.status.CanonStatusChecker;
29 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
30 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
31 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
32 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommunication;
33 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpAsyncResponseReceiver;
34 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpCommandPublisher;
35 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.status.IPtpIpRunModeHolder;
36 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.status.PtpIpRunMode;
37 import net.osdn.gokigen.a01d.liveview.IAutoFocusFrameDisplay;
38 import net.osdn.gokigen.a01d.liveview.ICameraStatusUpdateNotify;
39 import net.osdn.gokigen.a01d.liveview.IIndicatorControl;
40 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
41 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
42
43 public class CanonInterfaceProvider implements IPtpIpInterfaceProvider, IDisplayInjector
44 {
45     private final String TAG = toString();
46
47     private static final int ASYNC_RESPONSE_PORT = 15741;  // ??
48     private static final int CONTROL_PORT = 15740;
49     private static final int EVENT_PORT = 15740;
50     //private static final String CAMERA_IP = "192.168.0.1";
51
52     private final Activity activity;
53     private final PtpIpRunMode runMode;
54     private final CanonCameraInformation cameraInformation;
55     private CanonCaptureControl captureControl;
56     private CanonFocusingControl focusingControl;
57     private final CanonConnection canonConnection;
58     private final PtpIpCommandPublisher commandPublisher;
59     private final CanonLiveViewControl liveViewControl;
60     private final PtpIpAsyncResponseReceiver asyncReceiver;
61     private final CanonZoomLensControl zoomControl;
62     private final CanonStatusChecker statusChecker;
63     private final ICameraStatusUpdateNotify statusListener;
64     private final IInformationReceiver informationReceiver;
65
66     public CanonInterfaceProvider(@NonNull Activity context, @NonNull ICameraStatusReceiver provider, @NonNull ICameraStatusUpdateNotify statusListener, @NonNull IInformationReceiver informationReceiver)
67     {
68         this.activity = context;
69
70         String ipAddress;
71         int delayMs = 30;
72         int sequenceType = 0;
73         boolean isSearchJpegHeader = false;
74         try
75         {
76             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
77             ipAddress = preferences.getString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
78             if (ipAddress == null)
79             {
80                 ipAddress = "192.168.0.1";
81             }
82             try
83             {
84                 String delayMsStr = preferences.getString(IPreferencePropertyAccessor.CANON_LIVEVIEW_WAIT, IPreferencePropertyAccessor.CANON_LIVEVIEW_WAIT_DEFAULT_VALUE);
85                 if (delayMsStr != null)
86                 {
87                     delayMs = Integer.parseInt(delayMsStr);
88                 }
89                 if (delayMs < 10)
90                 {
91                     delayMs = 10;
92                 }
93             }
94             catch (Exception e)
95             {
96                 e.printStackTrace();
97             }
98             try
99             {
100                 String sequenceTypeStr = preferences.getString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
101                 if (sequenceTypeStr != null)
102                 {
103                     sequenceType = Integer.parseInt(sequenceTypeStr);
104                 }
105             }
106             catch (Exception e)
107             {
108                 e.printStackTrace();
109             }
110             if (sequenceType == 1)
111             {
112                 Log.v(TAG, " --- search JPEG header : true ");
113                 isSearchJpegHeader = true;
114             }
115         }
116         catch (Exception e)
117         {
118             e.printStackTrace();
119             ipAddress = "192.168.0.1";
120         }
121         Log.v(TAG, " Canon IP : " + ipAddress);
122         commandPublisher = new PtpIpCommandPublisher(ipAddress, CONTROL_PORT, false, false);
123         liveViewControl = new CanonLiveViewControl(context, this, delayMs, isSearchJpegHeader);  //
124         asyncReceiver = new PtpIpAsyncResponseReceiver(ipAddress, ASYNC_RESPONSE_PORT);
125         statusChecker = new CanonStatusChecker(commandPublisher, ipAddress, EVENT_PORT);
126         canonConnection = new CanonConnection(context, provider, this, statusChecker);
127         cameraInformation = new CanonCameraInformation();
128         zoomControl = new CanonZoomLensControl(context, commandPublisher);
129         this.statusListener = statusListener;
130         this.runMode = new PtpIpRunMode();
131         this.informationReceiver = informationReceiver;
132     }
133
134     @Override
135     public void injectDisplay(IAutoFocusFrameDisplay frameDisplayer, IIndicatorControl indicator, IFocusingModeNotify focusingModeNotify)
136     {
137         Log.v(TAG, " injectDisplay()");
138         captureControl = new CanonCaptureControl(commandPublisher, frameDisplayer);
139         focusingControl = new CanonFocusingControl(activity, commandPublisher, frameDisplayer, indicator);
140     }
141
142     @Override
143     public ICameraConnection getCameraConnection()
144     {
145         return (canonConnection);
146     }
147
148     @Override
149     public ILiveViewControl getLiveViewControl()
150     {
151         return (liveViewControl);
152     }
153
154     @Override
155     public ILiveViewListener getLiveViewListener()
156     {
157         return (liveViewControl.getLiveViewListener());
158     }
159
160     @Override
161     public IFocusingControl getFocusingControl()
162     {
163         return (focusingControl);
164     }
165
166     @Override
167     public ICameraInformation getCameraInformation()
168     {
169         return (cameraInformation);
170     }
171
172     @Override
173     public IZoomLensControl getZoomLensControl()
174     {
175         return (zoomControl);
176     }
177
178     @Override
179     public ICaptureControl getCaptureControl()
180     {
181         return (captureControl);
182     }
183
184     @Override
185     public IDisplayInjector getDisplayInjector()
186     {
187         return (this);
188     }
189
190     @Override
191     public IPtpIpRunModeHolder getRunModeHolder()
192     {
193         return (runMode);
194     }
195
196     @Override
197     public IPtpIpCommandCallback getStatusHolder() {
198         return (statusChecker);
199     }
200
201     @Override
202     public IPtpIpCommandPublisher getCommandPublisher()
203     {
204         return (commandPublisher);
205     }
206
207     @Override
208     public IPtpIpCommunication getLiveviewCommunication()
209     {
210         return (liveViewControl);
211     }
212
213     @Override
214     public IPtpIpCommunication getAsyncEventCommunication()
215     {
216         return (asyncReceiver);
217     }
218
219     @Override
220     public IPtpIpCommunication getCommandCommunication()
221     {
222         return (commandPublisher);
223     }
224
225     @Override
226     public ICameraStatusWatcher getCameraStatusWatcher()
227     {
228         return (statusChecker);
229     }
230
231     @Override
232     public ICameraStatusUpdateNotify getStatusListener()
233     {
234         return (statusListener);
235     }
236
237     @Override
238     public ICameraStatus getCameraStatusListHolder()
239     {
240         return (statusChecker);
241     }
242
243     @Override
244     public IInformationReceiver getInformationReceiver()
245     {
246         // ちょっとこの引き回しは気持ちがよくない...
247         return (informationReceiver);
248     }
249
250     @Override
251     public ICameraStatusWatcher getStatusWatcher()
252     {
253         return (statusChecker);
254     }
255
256     @Override
257     public void setAsyncEventReceiver(@NonNull IPtpIpCommandCallback receiver)
258     {
259         asyncReceiver.setEventSubscriber(receiver);
260     }
261
262 }