OSDN Git Service

ce0f977360d592db2d2d55d716b4b220ffe6b737
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / nikon / wrapper / connection / NikonConnection.java
1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.connection;
2
3
4 import android.app.Activity;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.DialogInterface;
8 import android.content.Intent;
9 import android.content.IntentFilter;
10 import android.net.ConnectivityManager;
11 import android.net.wifi.WifiInfo;
12 import android.net.wifi.WifiManager;
13 import android.provider.Settings;
14 import android.util.Log;
15
16 import androidx.annotation.NonNull;
17 import androidx.appcompat.app.AlertDialog;
18
19 import net.osdn.gokigen.a01d.R;
20 import net.osdn.gokigen.a01d.camera.ICameraConnection;
21 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
22 import net.osdn.gokigen.a01d.camera.nikon.wrapper.status.NikonStatusChecker;
23 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
24
25 import java.util.concurrent.Executor;
26 import java.util.concurrent.Executors;
27
28 public class NikonConnection implements ICameraConnection
29 {
30     private final String TAG = toString();
31     private final Activity context;
32     private final ICameraStatusReceiver statusReceiver;
33     private final IPtpIpInterfaceProvider interfaceProvider;
34     private final BroadcastReceiver connectionReceiver;
35     private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
36
37     private ICameraConnection.CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
38
39     private final NikonCameraConnectSequence connectSequence;
40     private final NikonCameraDisconnectSequence disconnectSequence;
41
42     public NikonConnection(@NonNull final Activity context, @NonNull final ICameraStatusReceiver statusReceiver, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull NikonStatusChecker statusChecker)
43     {
44         Log.v(TAG, "NikonConnection()");
45         this.context = context;
46         this.statusReceiver = statusReceiver;
47         this.interfaceProvider = interfaceProvider;
48         connectionReceiver = new BroadcastReceiver()
49         {
50             @Override
51             public void onReceive(Context context, Intent intent)
52             {
53                 onReceiveBroadcastOfConnection(context, intent);
54             }
55         };
56         connectSequence = new NikonCameraConnectSequence(context, statusReceiver, this, interfaceProvider, statusChecker);
57         disconnectSequence = new NikonCameraDisconnectSequence(context, interfaceProvider, statusChecker);
58     }
59
60     /**
61      *
62      *
63      */
64     private void onReceiveBroadcastOfConnection(Context context, Intent intent)
65     {
66         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_check_wifi), false, false, 0);
67         statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
68
69         Log.v(TAG, context.getString(R.string.connect_check_wifi));
70
71         String action = intent.getAction();
72         if (action == null)
73         {
74             Log.v(TAG, "intent.getAction() : null");
75             return;
76         }
77
78         try
79         {
80             if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
81             {
82                 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
83
84                 WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
85                 if (wifiManager != null) {
86                     WifiInfo info = wifiManager.getConnectionInfo();
87                     if (wifiManager.isWifiEnabled() && info != null) {
88                         if (info.getNetworkId() != -1) {
89                             Log.v(TAG, "Network ID is -1, there is no currently connected network.");
90                         }
91                         // 自動接続が指示されていた場合は、カメラとの接続処理を行う
92                         connectToCamera();
93                     } else {
94                         if (info == null) {
95                             Log.v(TAG, "NETWORK INFO IS NULL.");
96                         } else {
97                             Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
98                         }
99                     }
100                 }
101             }
102         } catch (Exception e) {
103             Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
104             e.printStackTrace();
105         }
106     }
107
108     @Override
109     public void startWatchWifiStatus(Context context)
110     {
111         Log.v(TAG, "startWatchWifiStatus()");
112         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_prepare), false, false, 0);
113         statusReceiver.onStatusNotify("prepare");
114
115         IntentFilter filter = new IntentFilter();
116         filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
117         filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
118         context.registerReceiver(connectionReceiver, filter);
119     }
120
121     @Override
122     public void stopWatchWifiStatus(Context context)
123     {
124         Log.v(TAG, "stopWatchWifiStatus()");
125         context.unregisterReceiver(connectionReceiver);
126         disconnect(false);
127     }
128
129     @Override
130     public void disconnect(boolean powerOff)
131     {
132         Log.v(TAG, "disconnect()");
133         disconnectFromCamera(powerOff);
134         connectionStatus = CameraConnectionStatus.DISCONNECTED;
135         statusReceiver.onCameraDisconnected();
136     }
137
138     @Override
139     public void connect()
140     {
141         Log.v(TAG, "connect()");
142         connectToCamera();
143     }
144
145     @Override
146     public void alertConnectingFailed(String message)
147     {
148         Log.v(TAG, "alertConnectingFailed() : " + message);
149         final AlertDialog.Builder builder = new AlertDialog.Builder(context)
150                 .setTitle(context.getString(R.string.dialog_title_connect_failed_nikon))
151                 .setMessage(message)
152                 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
153                     @Override
154                     public void onClick(DialogInterface dialog, int which)
155                     {
156                         disconnect(false);
157                         connect();
158                     }
159                 })
160                 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
161                     @Override
162                     public void onClick(DialogInterface dialog, int which)
163                     {
164                         try
165                         {
166                             // Wifi 設定画面を表示する
167                             context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
168                         }
169                         catch (android.content.ActivityNotFoundException ex)
170                         {
171                             // Activity が存在しなかった...設定画面が起動できなかった
172                             Log.v(TAG, "android.content.ActivityNotFoundException...");
173
174                             // この場合は、再試行と等価な動きとする
175                             connect();
176                         }
177                         catch (Exception e)
178                         {
179                             e.printStackTrace();
180                         }
181                     }
182                 });
183         context.runOnUiThread(new Runnable()
184         {
185             @Override
186             public void run()
187             {
188                 builder.show();
189             }
190         });
191     }
192
193     @Override
194     public CameraConnectionStatus getConnectionStatus()
195     {
196         Log.v(TAG, " getConnectionStatus()");
197         return (connectionStatus);
198     }
199
200     @Override
201     public void forceUpdateConnectionStatus(CameraConnectionStatus status)
202     {
203         Log.v(TAG, " forceUpdateConnectionStatus()");
204         connectionStatus = status;
205     }
206
207     /**
208      * カメラとの切断処理
209      */
210     private void disconnectFromCamera(final boolean powerOff)
211     {
212         Log.v(TAG, " disconnectFromCamera()");
213         try
214         {
215             cameraExecutor.execute(disconnectSequence);
216         }
217         catch (Exception e)
218         {
219             e.printStackTrace();
220         }
221     }
222
223     /**
224      * カメラとの接続処理
225      */
226     private void connectToCamera()
227     {
228         Log.v(TAG, " connectToCamera()");
229         connectionStatus = CameraConnectionStatus.CONNECTING;
230         try
231         {
232             cameraExecutor.execute(connectSequence);
233         }
234         catch (Exception e)
235         {
236             Log.v(TAG, " connectToCamera() EXCEPTION : " + e.getMessage());
237             e.printStackTrace();
238         }
239     }
240 }