1 package net.osdn.gokigen.pkremote.camera.vendor.nikon.wrapper;
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;
12 import androidx.annotation.NonNull;
13 import androidx.appcompat.app.AppCompatActivity;
14 import androidx.preference.PreferenceManager;
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;
53 import java.net.InetAddress;
54 import java.util.List;
56 public class NikonInterfaceProvider implements INikonInterfaceProvider, IDisplayInjector
58 private final String TAG = toString();
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;
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;
81 public NikonInterfaceProvider(@NonNull AppCompatActivity context, @NonNull ICameraStatusReceiver provider, @NonNull ICameraStatusUpdateNotify statusListener, @NonNull IInformationReceiver informationReceiver)
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;
98 private String getHostAddress(@NonNull AppCompatActivity context)
100 String ipAddress = IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS;
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))
108 ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
109 Network activeNetwork = connectivityManager.getActiveNetwork();
110 if (activeNetwork == null)
114 LinkProperties linkProperties = connectivityManager.getLinkProperties(activeNetwork);
115 if (linkProperties == null)
119 List<RouteInfo> routes = linkProperties.getRoutes();
120 for (RouteInfo route: routes)
124 InetAddress gateway = route.getGateway();
125 if ((route.isDefaultRoute())&&(gateway != null))
127 ipAddress = gateway.toString().replace("/","");
128 Log.v(TAG, " --------- default Gateway : ipAddress --------- ");
134 ee.printStackTrace();
147 public void injectDisplay(IAutoFocusFrameDisplay frameDisplayer, IIndicatorControl indicator, IFocusingModeNotify focusingModeNotify)
149 Log.v(TAG, "injectDisplay()");
150 //captureControl = new FujiXCaptureControl(commandPublisher, frameDisplayer);
151 //focusingControl = new FujiXFocusingControl(activity, commandPublisher, frameDisplayer, indicator);
155 public ICameraConnection getPtpIpCameraConnection()
157 return (nikonConnection);
161 public ILiveViewControl getLiveViewControl()
163 return (liveViewControl);
167 public ILiveViewListener getLiveViewListener()
169 return (liveViewControl.getLiveViewListener());
173 public IFocusingControl getFocusingControl()
179 public ICameraInformation getCameraInformation()
185 public IZoomLensControl getZoomLensControl()
187 return (zoomControl);
191 public ICaptureControl getCaptureControl()
197 public IDisplayInjector getDisplayInjector()
203 public IPtpIpRunModeHolder getRunModeHolder()
209 public IPtpIpCommandCallback getStatusHolder() {
210 return (statusChecker);
214 public IPtpIpCommandPublisher getCommandPublisher()
216 return (commandPublisher);
220 public IPtpIpCommunication getLiveviewCommunication()
222 return (liveViewControl);
226 public IPtpIpCommunication getAsyncEventCommunication()
228 return (asyncReceiver);
232 public IPtpIpCommunication getCommandCommunication()
234 return (commandPublisher);
238 public ICameraStatusWatcher getCameraStatusWatcher()
240 return (statusChecker);
244 public ICameraStatusUpdateNotify getStatusListener()
246 return (statusListener);
250 public ICameraStatus getCameraStatusListHolder()
252 return (statusChecker);
256 public ICameraButtonControl getButtonControl()
258 return (ptpIpButtonControl);
262 public IPlaybackControl getPlaybackControl()
264 return (playbackControl);
268 public ICameraHardwareStatus getHardwareStatus()
270 return (hardwareStatus);
274 public ICameraRunMode getCameraRunMode()
280 public IInformationReceiver getInformationReceiver()
282 // ちょっとこの引き回しは気持ちがよくない...
283 return (informationReceiver);
287 public void setAsyncEventReceiver(@NonNull IPtpIpCommandCallback receiver)
289 asyncReceiver.setEventSubscriber(receiver);
293 public String getIpAddress()
295 return getHostAddress(activity);
299 public int getControlPortNumber()
301 return (CONTROL_PORT);
305 public int getEventPortNumber()