OSDN Git Service

CanonのカメラのIPアドレスを自動特定できるようにした。
authorMRSa <mrsa@myad.jp>
Tue, 1 Aug 2023 13:19:47 +0000 (22:19 +0900)
committerMRSa <mrsa@myad.jp>
Tue, 1 Aug 2023 13:19:47 +0000 (22:19 +0900)
17 files changed:
app/build.gradle
app/src/main/java/net/osdn/gokigen/pkremote/camera/vendor/ptpip/wrapper/PtpIpInterfaceProvider.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/IPreferencePropertyAccessor.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/canon/CanonPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/fujix/FujiXPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/nikon/NikonPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/olympus/OpcPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/olympuspen/OlympusPenPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/panasonic/PanasonicPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/pixpro/PixproPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/ricohgr2/RicohGr2PreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/sony/SonyPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/theta/ThetaPreferenceFragment.java
app/src/main/java/net/osdn/gokigen/pkremote/preference/visionkids/VisionKidsPreferenceFragment.kt
app/src/main/res/values-ja/strings.xml
app/src/main/res/values/strings.xml
app/src/main/res/xml/preferences_canon.xml

index c11cdd1..12e669a 100644 (file)
@@ -2,13 +2,12 @@ apply plugin: 'com.android.application'
 apply plugin: 'kotlin-android'
 
 android {
-    compileSdkVersion 34
     defaultConfig {
         applicationId "net.osdn.gokigen.pkremote"
         minSdkVersion 14
         targetSdkVersion 34
-        versionCode 10806
-        versionName "1.8.6"
+        versionCode 10807
+        versionName "1.8.7"
         multiDexEnabled true
     }
     buildTypes {
index dd6a92a..3ff7e7a 100644 (file)
@@ -1,7 +1,13 @@
 package net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper;
 
 import android.app.Activity;
+import android.content.Context;
 import android.content.SharedPreferences;
+import android.net.ConnectivityManager;
+import android.net.LinkProperties;
+import android.net.Network;
+import android.net.RouteInfo;
+import android.os.Build;
 import android.util.Log;
 
 import androidx.annotation.NonNull;
@@ -34,7 +40,6 @@ import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpComma
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommunication;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.PtpIpAsyncResponseReceiver;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.PtpIpCommandPublisher;
-import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.PtpIpCommandPublisher0;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.connection.CanonConnection;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.liveview.PtpIpLiveViewControl;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.playback.CanonPlaybackControl;
@@ -42,6 +47,9 @@ import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.status.IPtpIpRunMod
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.status.PtpIpStatusChecker;
 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
 
+import java.net.InetAddress;
+import java.util.List;
+
 public class PtpIpInterfaceProvider implements IPtpIpInterfaceProvider, IDisplayInjector
 {
     private final String TAG = toString();
@@ -77,19 +85,12 @@ public class PtpIpInterfaceProvider implements IPtpIpInterfaceProvider, IDisplay
         int sequenceType = 0;
         try
         {
-            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
-            ipAddress = preferences.getString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
-            if (ipAddress == null)
-            {
-                ipAddress = "192.168.0.1";
-            }
+            ipAddress = getHostAddress(context);
             try
             {
+                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                 String sequenceTypeStr = preferences.getString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
-                if (sequenceTypeStr != null)
-                {
-                    sequenceType = Integer.parseInt(sequenceTypeStr);
-                }
+                sequenceType = Integer.parseInt(sequenceTypeStr);
             }
             catch (Exception e)
             {
@@ -117,6 +118,55 @@ public class PtpIpInterfaceProvider implements IPtpIpInterfaceProvider, IDisplay
         this.informationReceiver = informationReceiver;
     }
 
+    private String getHostAddress(@NonNull Activity context)
+    {
+        String ipAddress = "192.168.0.1";
+        try
+        {
+            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
+            boolean autoDetactHostIp = preferences.getBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            ipAddress = preferences.getString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
+            if ((autoDetactHostIp)&&(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M))
+            {
+                ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
+                Network activeNetwork = connectivityManager.getActiveNetwork();
+                if (activeNetwork == null)
+                {
+                    return (ipAddress);
+                }
+                LinkProperties linkProperties = connectivityManager.getLinkProperties(activeNetwork);
+                if (linkProperties == null)
+                {
+                    return (ipAddress);
+                }
+                List<RouteInfo> routes = linkProperties.getRoutes();
+                for (RouteInfo route: routes)
+                {
+                    try
+                    {
+                        InetAddress gateway = route.getGateway();
+                        if ((route.isDefaultRoute())&&(gateway != null))
+                        {
+                            ipAddress = gateway.toString().replace("/","");
+                            Log.v(TAG, " --------- default Gateway : ipAddress  --------- ");
+                            break;
+                        }
+
+                    }
+                    catch (Exception ee)
+                    {
+                        ee.printStackTrace();
+                    }
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        return (ipAddress);
+    }
+
     @Override
     public void injectDisplay(IAutoFocusFrameDisplay frameDisplayer, IIndicatorControl indicator, IFocusingModeNotify focusingModeNotify)
     {
index 5213867..9c9637a 100644 (file)
@@ -134,6 +134,8 @@ public interface IPreferencePropertyAccessor
     String CANON_HOST_IP = "canon_host_ip";
     String CANON_HOST_IP_DEFAULT_VALUE = "192.168.0.1";
 
+    String CANON_AUTO_DETECT_HOST_IP = "canon_auto_detect_host_ip";
+
     String CANON_CONNECTION_SEQUENCE = "canon_connection_mode";
     String CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE = "0";
 
index bc635e3..a82a1f8 100644 (file)
@@ -176,6 +176,9 @@ public class CanonPreferenceFragment  extends PreferenceFragmentCompat implement
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index c9ca243..76a9201 100644 (file)
@@ -176,6 +176,9 @@ public class FujiXPreferenceFragment  extends PreferenceFragmentCompat implement
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index 93f0c12..b6a108d 100644 (file)
@@ -173,6 +173,9 @@ public class NikonPreferenceFragment  extends PreferenceFragmentCompat implement
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index acb1e97..17a1a92 100644 (file)
@@ -202,6 +202,9 @@ public class OpcPreferenceFragment extends PreferenceFragmentCompat implements S
         if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
             editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
         }
+        if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+            editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+        }
         if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
             editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
         }
index c4249fd..6dde63f 100644 (file)
@@ -167,6 +167,9 @@ public class OlympusPenPreferenceFragment  extends PreferenceFragmentCompat impl
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index be8a276..3dcdbaf 100644 (file)
@@ -165,6 +165,9 @@ public class PanasonicPreferenceFragment  extends PreferenceFragmentCompat imple
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index e364bba..343892e 100644 (file)
@@ -164,6 +164,9 @@ public class PixproPreferenceFragment  extends PreferenceFragmentCompat implemen
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index 06cd0dd..9cd2347 100644 (file)
@@ -180,6 +180,9 @@ public class RicohGr2PreferenceFragment  extends PreferenceFragmentCompat implem
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index 4a2bb5e..9c652f5 100644 (file)
@@ -162,6 +162,9 @@ public class SonyPreferenceFragment  extends PreferenceFragmentCompat implements
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index dcf12c5..7bb9258 100644 (file)
@@ -164,6 +164,9 @@ public class ThetaPreferenceFragment  extends PreferenceFragmentCompat implement
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true);
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
             }
index caf247a..db1237a 100644 (file)
@@ -136,6 +136,9 @@ class VisionKidsPreferenceFragment: PreferenceFragmentCompat(), SharedPreference
                     IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE
                 )
             }
+            if (!items.containsKey(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP)) {
+                editor?.putBoolean(IPreferencePropertyAccessor.CANON_AUTO_DETECT_HOST_IP, true)
+            }
             if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
                 editor?.putString(
                     IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE,
index 9883603..46e81e2 100644 (file)
     <string name="canon_get_image_screennail">画像取得中&#8230;</string>
     <string name="canon_get_image_screennail_done">完了</string>
 
+    <string name="pref_canon_auto_detect_host_ip">カメラIPアドレスを自動設定</string>
+    <string name="pref_summary_canon_auto_detect_host_ip">通常、ONのままでご使用ください。</string>
+
     <string name="pref_nikon_host_ip">カメラIPアドレス</string>
     <string name="pref_summary_nikon_host_ip">通常、変更は不要です</string>
 
index f01e4a2..43a4a89 100644 (file)
     <string name="canon_get_image_screennail">Get Image&#8230;</string>
     <string name="canon_get_image_screennail_done">Done.</string>
 
+    <string name="pref_canon_auto_detect_host_ip">Set camera ip automatically.</string>
+    <string name="pref_summary_canon_auto_detect_host_ip">Normally to keep ON.</string>
+
     <string name="pref_nikon_host_ip">Camera IP Address</string>
     <string name="pref_summary_nikon_host_ip"> </string>
 
index b14e7bb..e213635 100644 (file)
             android:key="canon_connection_mode"
             android:defaultValue="0"/>
 
+        <CheckBoxPreference
+            android:key="canon_auto_detect_host_ip"
+            android:title="@string/pref_canon_auto_detect_host_ip"
+            android:summary="@string/pref_summary_canon_auto_detect_host_ip" />
+
         <!--
 
         <PreferenceScreen