OSDN Git Service

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