OSDN Git Service

RICOHの接続処理を微修正。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / ricohgr2 / wrapper / connection / RicohGr2Connection.java
index ca1ab4d..aabf067 100644 (file)
@@ -1,6 +1,5 @@
 package net.osdn.gokigen.gr2control.camera.ricohgr2.wrapper.connection;
 
-import android.app.Activity;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -21,6 +20,7 @@ import java.util.concurrent.Executors;
 
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.AlertDialog;
+import androidx.fragment.app.FragmentActivity;
 
 
 /**
@@ -30,9 +30,10 @@ import androidx.appcompat.app.AlertDialog;
 public class RicohGr2Connection implements ICameraConnection
 {
     private final String TAG = toString();
-    private final Activity context;
+    private final FragmentActivity context;
     private final ICameraStatusReceiver statusReceiver;
     private final BroadcastReceiver connectionReceiver;
+    private final IUseGR2CommandNotify gr2CommandNotify;
     //private final ConnectivityManager connectivityManager;
     private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
     //private final Handler networkConnectionTimeoutHandler;
@@ -44,11 +45,12 @@ public class RicohGr2Connection implements ICameraConnection
      *
      *
      */
-    public RicohGr2Connection(@NonNull final Activity context, @NonNull final ICameraStatusReceiver statusReceiver)
+    public RicohGr2Connection(@NonNull final FragmentActivity context, @NonNull final ICameraStatusReceiver statusReceiver, @NonNull IUseGR2CommandNotify gr2CommandNotify)
     {
-        Log.v(TAG, "RicohGr2Connection()");
+        Log.v(TAG, " RicohGr2Connection()");
         this.context = context;
         this.statusReceiver = statusReceiver;
+        this.gr2CommandNotify = gr2CommandNotify;
         connectionReceiver = new BroadcastReceiver()
         {
             @Override
@@ -87,17 +89,25 @@ public class RicohGr2Connection implements ICameraConnection
                     WifiInfo info = wifiManager.getConnectionInfo();
                     if (wifiManager.isWifiEnabled() && info != null)
                     {
-                        if (info.getNetworkId() != -1)
+                        if (info.getNetworkId() == -1)
                         {
                             Log.v(TAG, "Network ID is -1, there is no currently connected network.");
                         }
+                        else
+                        {
+                            Log.v(TAG, "Network ID is " + info.getNetworkId());
+                        }
                         // 自動接続が指示されていた場合は、カメラとの接続処理を行う
                         connectToCamera();
-                    } else {
+                    }
+                    else
+                    {
                         if (info == null)
                         {
                             Log.v(TAG, "NETWORK INFO IS NULL.");
-                        } else {
+                        }
+                        else
+                        {
                             Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
                         }
                     }
@@ -173,47 +183,41 @@ public class RicohGr2Connection implements ICameraConnection
     public void alertConnectingFailed(String message)
     {
         Log.v(TAG, "alertConnectingFailed() : " + message);
-        final AlertDialog.Builder builder = new AlertDialog.Builder(context)
-                .setTitle(context.getString(R.string.dialog_title_connect_failed))
-                .setMessage(message)
-                .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
-                    @Override
-                    public void onClick(DialogInterface dialog, int which)
-                    {
-                        connect();
-                    }
-                })
-                .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
-                    @Override
-                    public void onClick(DialogInterface dialog, int which)
-                    {
-                        try
-                        {
-                            // Wifi 設定画面を表示する
-                            context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
-                        }
-                        catch (android.content.ActivityNotFoundException ex)
-                        {
-                            // Activity が存在しなかった...設定画面が起動できなかった
-                            Log.v(TAG, "android.content.ActivityNotFoundException...");
-
-                            // この場合は、再試行と等価な動きとする
+        if (context != null)
+        {
+            final AlertDialog.Builder builder = new AlertDialog.Builder(context)
+                    .setTitle(context.getString(R.string.dialog_title_connect_failed))
+                    .setMessage(message)
+                    .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
                             connect();
                         }
-                        catch (Exception e)
-                        {
-                            e.printStackTrace();
+                    })
+                    .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            try {
+                                // Wifi 設定画面を表示する
+                                context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
+                            } catch (android.content.ActivityNotFoundException ex) {
+                                // Activity が存在しなかった...設定画面が起動できなかった
+                                Log.v(TAG, "android.content.ActivityNotFoundException...");
+
+                                // この場合は、再試行と等価な動きとする
+                                connect();
+                            } catch (Exception e) {
+                                e.printStackTrace();
+                            }
                         }
-                    }
-                });
-        context.runOnUiThread(new Runnable()
-        {
-            @Override
-            public void run()
-            {
-                builder.show();
-            }
-        });
+                    });
+            context.runOnUiThread(new Runnable() {
+                @Override
+                public void run() {
+                    builder.show();
+                }
+            });
+        }
     }
 
     @Override
@@ -259,7 +263,7 @@ public class RicohGr2Connection implements ICameraConnection
         connectionStatus = CameraConnectionStatus.CONNECTING;
         try
         {
-            cameraExecutor.execute(new RicohGr2CameraConnectSequence(context, statusReceiver, this));
+            cameraExecutor.execute(new RicohGr2CameraConnectSequence(context, statusReceiver, this, gr2CommandNotify));
         }
         catch (Exception e)
         {