OSDN Git Service

androidx に置き換えた。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / olympus / wrapper / connection / OlyCameraConnection.java
1 package net.osdn.gokigen.gr2control.camera.olympus.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.net.ConnectivityManager;
10 import android.net.wifi.WifiInfo;
11 import android.net.wifi.WifiManager;
12 import android.provider.Settings;
13 import android.util.Log;
14
15 import net.osdn.gokigen.gr2control.R;
16 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
17 import net.osdn.gokigen.gr2control.camera.ICameraStatusReceiver;
18
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.Executors;
21
22 import androidx.appcompat.app.AlertDialog;
23 import jp.co.olympus.camerakit.OLYCamera;
24 import jp.co.olympus.camerakit.OLYCameraConnectionListener;
25 import jp.co.olympus.camerakit.OLYCameraKitException;
26
27
28 /**
29  *   カメラの接続・切断処理クラス
30  *
31  */
32 public class OlyCameraConnection implements ICameraConnection, OLYCameraConnectionListener
33 {
34     private final String TAG = toString();
35     private final Activity context;
36     private final OLYCamera camera;
37     private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
38     private final BroadcastReceiver connectionReceiver;
39     private final ICameraStatusReceiver statusReceiver;
40
41     //private boolean isWatchingWifiStatus = false;
42     private CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
43
44     //private ConnectivityManager connectivityManager;
45     //private ConnectivityManager.NetworkCallback networkCallback = null;
46
47     // Handler for dealing with network connection timeouts.
48    // private Handler networkConnectionTimeoutHandler;
49
50     // Message to notify the network request timout handler that too much time has passed.
51     //private static final int MESSAGE_CONNECTIVITY_TIMEOUT = 1;
52
53     /**
54      *    コンストラクタ
55      *
56      */
57     public OlyCameraConnection(final Activity context, OLYCamera camera, ICameraStatusReceiver statusReceiver)
58     {
59         Log.v(TAG, "OlyCameraConnection()");
60         this.context = context;
61         this.camera = camera;
62         this.camera.setConnectionListener(this);
63 /*
64         ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
65         Handler networkConnectionTimeoutHandler = new Handler()
66         {
67             @Override
68             public void handleMessage(Message msg)
69             {
70                 switch (msg.what)
71                 {
72                     case MESSAGE_CONNECTIVITY_TIMEOUT:
73                         Log.d(TAG, "Network connection timeout");
74                         alertConnectingFailed(context.getString(R.string.network_connection_timeout));
75                         connectionStatus = CameraConnectionStatus.DISCONNECTED;
76                         break;
77                 }
78             }
79         };
80 */
81         this.statusReceiver = statusReceiver;
82         connectionReceiver = new BroadcastReceiver()
83         {
84             @Override
85             public void onReceive(Context context, Intent intent)
86             {
87                 onReceiveBroadcastOfConnection(context, intent);
88             }
89         };
90
91     }
92
93     /**
94      *   Wifiが使える状態だったら、カメラと接続して動作するよ
95      *
96      */
97     private void onReceiveBroadcastOfConnection(Context context, Intent intent)
98     {
99         statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
100         Log.v(TAG,context.getString(R.string.connect_check_wifi));
101
102         String action = intent.getAction();
103         try
104         {
105             if ((action != null)&&(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)))
106             {
107                 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
108
109                 WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
110                 if (wifiManager != null) {
111                     WifiInfo info = wifiManager.getConnectionInfo();
112                     if (wifiManager.isWifiEnabled() && info != null) {
113                         if (info.getNetworkId() != -1) {
114                             Log.v(TAG, "Network ID is -1, there is no currently connected network.");
115                         }
116                         // 自動接続が指示されていた場合は、カメラとの接続処理を行う
117                         connectToCamera();
118                     } else {
119                         if (info == null) {
120                             Log.v(TAG, "NETWORK INFO IS NULL.");
121                         } else {
122                             Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
123                         }
124                     }
125                 }
126             }
127         }
128         catch (Exception e)
129         {
130             Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
131             e.printStackTrace();
132         }
133     }
134
135     /**
136      * Wifi接続状態の監視
137      * (接続の実処理は onReceiveBroadcastOfConnection() で実施)
138      */
139     @Override
140     public void startWatchWifiStatus(Context context)
141     {
142         Log.v(TAG, "startWatchWifiStatus()");
143         statusReceiver.onStatusNotify("prepare");
144
145         IntentFilter filter = new IntentFilter();
146         filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
147         filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
148         context.registerReceiver(connectionReceiver, filter);
149     }
150
151     /**
152      * Wifi接続状態の監視終了
153      */
154     @Override
155     public void stopWatchWifiStatus(Context context)
156     {
157         Log.v(TAG, "stopWatchWifiStatus()");
158         context.unregisterReceiver(connectionReceiver);
159         disconnect(false);
160     }
161
162     /**
163      *   カメラとの接続を解除する
164      *
165      * @param powerOff 真ならカメラの電源オフを伴う
166      */
167     @Override
168     public void disconnect(final boolean powerOff)
169     {
170         Log.v(TAG, "disconnect()");
171         disconnectFromCamera(powerOff);
172         connectionStatus = CameraConnectionStatus.DISCONNECTED;
173         statusReceiver.onCameraDisconnected();
174     }
175
176     /**
177      * カメラとの再接続を指示する
178      */
179     @Override
180     public void connect() {
181         connectToCamera();
182     }
183
184     @Override
185     public CameraConnectionStatus getConnectionStatus()
186     {
187         return (connectionStatus);
188     }
189
190     /**
191      *   カメラの接続状態を強制更新する
192      *
193      */
194     @Override
195     public void forceUpdateConnectionStatus(CameraConnectionStatus status)
196     {
197         connectionStatus = status;
198     }
199
200
201     /**
202      * カメラの通信状態変化を監視するためのインターフェース
203      *
204      * @param camera 例外が発生した OLYCamera
205      * @param e      カメラクラスの例外
206      */
207     @Override
208     public void onDisconnectedByError(OLYCamera camera, OLYCameraKitException e)
209     {
210         Log.v(TAG, "onDisconnectedByError()");
211         connectionStatus = CameraConnectionStatus.DISCONNECTED;
212
213         // カメラが切れた時に通知する
214         statusReceiver.onCameraDisconnected();
215     }
216
217     /**
218      * カメラとの切断処理
219      */
220     private void disconnectFromCamera(final boolean powerOff)
221     {
222         Log.v(TAG, "disconnectFromCamera()");
223         try
224         {
225             cameraExecutor.execute(new CameraDisconnectSequence(camera, powerOff));
226         }
227         catch (Exception e)
228         {
229             e.printStackTrace();
230         }
231     }
232
233     /**
234      * カメラとの接続処理
235      */
236     private void connectToCamera()
237     {
238         Log.v(TAG, "connectToCamera()");
239         connectionStatus = CameraConnectionStatus.CONNECTING;
240         try
241         {
242             cameraExecutor.execute(new CameraConnectSequence(context, camera, statusReceiver));
243         }
244         catch (Exception e)
245         {
246             Log.v(TAG, "connectToCamera() EXCEPTION : " + e.getMessage());
247             e.printStackTrace();
248         }
249     }
250
251     /**
252      *   接続リトライのダイアログを出す
253      *
254      * @param message 表示用のメッセージ
255      */
256     @Override
257     public void alertConnectingFailed(String message)
258     {
259         final AlertDialog.Builder builder = new AlertDialog.Builder(context)
260                 .setTitle(context.getString(R.string.dialog_title_connect_failed))
261                 .setMessage(message)
262                 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
263                     @Override
264                     public void onClick(DialogInterface dialog, int which)
265                     {
266                         connect();
267                     }
268                 })
269                 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
270                     @Override
271                     public void onClick(DialogInterface dialog, int which)
272                     {
273                         try
274                         {
275                             // Wifi 設定画面を表示する
276                             context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
277                         }
278                         catch (android.content.ActivityNotFoundException ex)
279                         {
280                             // Activity が存在しなかった...設定画面が起動できなかった
281                             Log.v(TAG, "android.content.ActivityNotFoundException...");
282
283                             // この場合は、再試行と等価な動きとする
284                             connect();
285                         }
286                         catch (Exception e)
287                         {
288                             e.printStackTrace();
289                         }
290                     }
291                 });
292         context.runOnUiThread(new Runnable()
293         {
294             @Override
295             public void run()
296             {
297                 builder.show();
298             }
299         });
300     }
301 }