OSDN Git Service

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