OSDN Git Service

0c688a65f10ed545f3eeb3e86dece87a1b536737
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / nikon / wrapper / NikonInterfaceProvider.java
1 package net.osdn.gokigen.pkremote.camera.vendor.nikon.wrapper;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.net.ConnectivityManager;
6 import android.net.LinkProperties;
7 import android.net.Network;
8 import android.net.RouteInfo;
9 import android.os.Build;
10 import android.util.Log;
11
12 import androidx.annotation.NonNull;
13 import androidx.appcompat.app.AppCompatActivity;
14 import androidx.preference.PreferenceManager;
15
16 import net.osdn.gokigen.pkremote.IInformationReceiver;
17 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraButtonControl;
18 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraConnection;
19 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraRunMode;
20 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICaptureControl;
21 import net.osdn.gokigen.pkremote.camera.interfaces.control.IFocusingControl;
22 import net.osdn.gokigen.pkremote.camera.interfaces.control.IFocusingModeNotify;
23 import net.osdn.gokigen.pkremote.camera.interfaces.control.IZoomLensControl;
24 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.IAutoFocusFrameDisplay;
25 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.ICameraStatusUpdateNotify;
26 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.IDisplayInjector;
27 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.IIndicatorControl;
28 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.ILiveViewControl;
29 import net.osdn.gokigen.pkremote.camera.interfaces.liveview.ILiveViewListener;
30 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
31 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraHardwareStatus;
32 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraInformation;
33 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatus;
34 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatusReceiver;
35 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatusWatcher;
36 import net.osdn.gokigen.pkremote.camera.vendor.nikon.INikonInterfaceProvider;
37 import net.osdn.gokigen.pkremote.camera.vendor.nikon.wrapper.playback.NikonPlaybackControl;
38 import net.osdn.gokigen.pkremote.camera.vendor.nikon.wrapper.status.NikonStatusChecker;
39 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.operation.PtpIpZoomControl;
40 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.PtpIpButtonControl;
41 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.PtpIpHardwareStatus;
42 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.PtpIpRunMode;
43 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback;
44 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandPublisher;
45 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommunication;
46 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.PtpIpAsyncResponseReceiver;
47 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.PtpIpCommandPublisher0;
48 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.connection.NikonConnection;
49 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.liveview.PtpIpLiveViewControl;
50 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.status.IPtpIpRunModeHolder;
51 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
52
53 import java.net.InetAddress;
54 import java.util.List;
55
56 public class NikonInterfaceProvider implements INikonInterfaceProvider, IDisplayInjector
57 {
58     private final String TAG = toString();
59
60     private static final int STREAM_PORT = 15742;   // ??
61     private static final int ASYNC_RESPONSE_PORT = 15741;  // ??
62     private static final int CONTROL_PORT = 15740;
63     private static final int EVENT_PORT = 15740;
64
65     private final AppCompatActivity activity;
66     private final PtpIpRunMode runMode;
67     private final PtpIpHardwareStatus hardwareStatus;
68     private final PtpIpButtonControl ptpIpButtonControl;
69     private final NikonConnection nikonConnection;
70     private final PtpIpCommandPublisher0 commandPublisher;
71     private final PtpIpLiveViewControl liveViewControl;
72     private final PtpIpAsyncResponseReceiver asyncReceiver;
73     private final PtpIpZoomControl zoomControl;
74     //private PtpIpCaptureControl captureControl;
75     //private PtpIpFocusingControl focusingControl;
76     private final NikonStatusChecker statusChecker;
77     private final ICameraStatusUpdateNotify statusListener;
78     private final NikonPlaybackControl playbackControl;
79     private final IInformationReceiver informationReceiver;
80
81     public NikonInterfaceProvider(@NonNull AppCompatActivity context, @NonNull ICameraStatusReceiver provider, @NonNull ICameraStatusUpdateNotify statusListener, @NonNull IInformationReceiver informationReceiver)
82     {
83         this.activity = context;
84         commandPublisher = new PtpIpCommandPublisher0();
85         liveViewControl = new PtpIpLiveViewControl(context, false);
86         asyncReceiver = new PtpIpAsyncResponseReceiver();
87         statusChecker = new NikonStatusChecker(activity, this);
88         nikonConnection = new NikonConnection(context, provider, this, statusChecker);
89         zoomControl = new PtpIpZoomControl();
90         this.statusListener = statusListener;
91         this.runMode = new PtpIpRunMode();
92         this.hardwareStatus = new PtpIpHardwareStatus();
93         this.ptpIpButtonControl = new PtpIpButtonControl();
94         this.playbackControl = new NikonPlaybackControl(activity, this);
95         this.informationReceiver = informationReceiver;
96     }
97
98     private String getHostAddress(@NonNull AppCompatActivity context)
99     {
100         String ipAddress = IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS;
101         try
102         {
103             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
104             boolean autoDetactHostIp = preferences.getBoolean(IPreferencePropertyAccessor.NIKON_AUTO_DETECT_HOST_IP, true);
105             ipAddress = preferences.getString(IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS, IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS);
106             if ((autoDetactHostIp)&&(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M))
107             {
108                 ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
109                 Network activeNetwork = connectivityManager.getActiveNetwork();
110                 if (activeNetwork == null)
111                 {
112                     return (ipAddress);
113                 }
114                 LinkProperties linkProperties = connectivityManager.getLinkProperties(activeNetwork);
115                 if (linkProperties == null)
116                 {
117                     return (ipAddress);
118                 }
119                 List<RouteInfo> routes = linkProperties.getRoutes();
120                 for (RouteInfo route: routes)
121                 {
122                     try
123                     {
124                         InetAddress gateway = route.getGateway();
125                         if ((route.isDefaultRoute())&&(gateway != null))
126                         {
127                             ipAddress = gateway.toString().replace("/","");
128                             Log.v(TAG, " --------- default Gateway : ipAddress  --------- ");
129                             break;
130                         }
131                     }
132                     catch (Exception ee)
133                     {
134                         ee.printStackTrace();
135                     }
136                 }
137             }
138         }
139         catch (Exception e)
140         {
141             e.printStackTrace();
142         }
143         return (ipAddress);
144     }
145
146     @Override
147     public void injectDisplay(IAutoFocusFrameDisplay frameDisplayer, IIndicatorControl indicator, IFocusingModeNotify focusingModeNotify)
148     {
149         Log.v(TAG, "injectDisplay()");
150         //captureControl = new FujiXCaptureControl(commandPublisher, frameDisplayer);
151         //focusingControl = new FujiXFocusingControl(activity, commandPublisher, frameDisplayer, indicator);
152     }
153
154     @Override
155     public ICameraConnection getPtpIpCameraConnection()
156     {
157         return (nikonConnection);
158     }
159
160     @Override
161     public ILiveViewControl getLiveViewControl()
162     {
163         return (liveViewControl);
164     }
165
166     @Override
167     public ILiveViewListener getLiveViewListener()
168     {
169         return (liveViewControl.getLiveViewListener());
170     }
171
172     @Override
173     public IFocusingControl getFocusingControl()
174     {
175         return (null);
176     }
177
178     @Override
179     public ICameraInformation getCameraInformation()
180     {
181         return null;
182     }
183
184     @Override
185     public IZoomLensControl getZoomLensControl()
186     {
187         return (zoomControl);
188     }
189
190     @Override
191     public ICaptureControl getCaptureControl()
192     {
193         return (null);
194     }
195
196     @Override
197     public IDisplayInjector getDisplayInjector()
198     {
199         return (this);
200     }
201
202     @Override
203     public IPtpIpRunModeHolder getRunModeHolder()
204     {
205         return (runMode);
206     }
207
208     @Override
209     public IPtpIpCommandCallback getStatusHolder() {
210         return (statusChecker);
211     }
212
213     @Override
214     public IPtpIpCommandPublisher getCommandPublisher()
215     {
216         return (commandPublisher);
217     }
218
219     @Override
220     public IPtpIpCommunication getLiveviewCommunication()
221     {
222         return (liveViewControl);
223     }
224
225     @Override
226     public IPtpIpCommunication getAsyncEventCommunication()
227     {
228         return (asyncReceiver);
229     }
230
231     @Override
232     public IPtpIpCommunication getCommandCommunication()
233     {
234         return (commandPublisher);
235     }
236
237     @Override
238     public ICameraStatusWatcher getCameraStatusWatcher()
239     {
240         return (statusChecker);
241     }
242
243     @Override
244     public ICameraStatusUpdateNotify getStatusListener()
245     {
246         return (statusListener);
247     }
248
249     @Override
250     public ICameraStatus getCameraStatusListHolder()
251     {
252         return (statusChecker);
253     }
254
255     @Override
256     public ICameraButtonControl getButtonControl()
257     {
258         return (ptpIpButtonControl);
259     }
260
261     @Override
262     public IPlaybackControl getPlaybackControl()
263     {
264         return (playbackControl);
265     }
266
267     @Override
268     public ICameraHardwareStatus getHardwareStatus()
269     {
270         return (hardwareStatus);
271     }
272
273     @Override
274     public ICameraRunMode getCameraRunMode()
275     {
276         return (runMode);
277     }
278
279     @Override
280     public IInformationReceiver getInformationReceiver()
281     {
282         // ちょっとこの引き回しは気持ちがよくない...
283         return (informationReceiver);
284     }
285
286     @Override
287     public void setAsyncEventReceiver(@NonNull IPtpIpCommandCallback receiver)
288     {
289         asyncReceiver.setEventSubscriber(receiver);
290     }
291
292     @Override
293     public String getIpAddress()
294     {
295         return getHostAddress(activity);
296     }
297
298     @Override
299     public int getControlPortNumber()
300     {
301         return (CONTROL_PORT);
302     }
303
304     @Override
305     public int getEventPortNumber()
306     {
307         return (EVENT_PORT);
308     }
309
310 }