1 package net.osdn.gokigen.pkremote.camera.vendor.sony.wrapper.connection;
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;
15 import androidx.annotation.NonNull;
16 import androidx.appcompat.app.AlertDialog;
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;
24 import java.util.concurrent.Executor;
25 import java.util.concurrent.Executors;
31 public class SonyCameraConnection implements ICameraConnection
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;
45 public SonyCameraConnection(final Activity context, final ICameraStatusReceiver statusReceiver, @NonNull ISonyCameraHolder cameraHolder, final @NonNull ICameraChangeListener listener)
47 Log.v(TAG, "SonyCameraConnection()");
48 this.context = context;
49 this.statusReceiver = statusReceiver;
50 this.cameraHolder = cameraHolder;
51 this.listener = listener;
53 ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
54 networkConnectionTimeoutHandler = new Handler()
57 public void handleMessage(Message msg)
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;
70 connectionReceiver = new BroadcastReceiver()
73 public void onReceive(Context context, Intent intent)
75 onReceiveBroadcastOfConnection(context, intent);
80 private void onReceiveBroadcastOfConnection(Context context, Intent intent)
82 statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
83 Log.v(TAG,context.getString(R.string.connect_check_wifi));
85 String action = intent.getAction();
89 Log.v(TAG, "intent.getAction() : null");
95 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
97 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
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)
104 if (info.getNetworkId() != -1)
106 Log.v(TAG, "Network ID is -1, there is no currently connected network.");
108 // 自動接続が指示されていた場合は、カメラとの接続処理を行う
113 Log.v(TAG, "NETWORK INFO IS NULL.");
115 Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
123 Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
131 * (接続の実処理は onReceiveBroadcastOfConnection() で実施)
134 public void startWatchWifiStatus(Context context)
136 Log.v(TAG, "startWatchWifiStatus()");
137 statusReceiver.onStatusNotify("prepare");
139 IntentFilter filter = new IntentFilter();
140 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
141 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
142 context.registerReceiver(connectionReceiver, filter);
149 public void stopWatchWifiStatus(Context context)
151 Log.v(TAG, "stopWatchWifiStatus()");
152 context.unregisterReceiver(connectionReceiver);
159 * @param powerOff 真ならカメラの電源オフを伴う
162 public void disconnect(boolean powerOff)
164 Log.v(TAG, "disconnect()");
165 disconnectFromCamera(powerOff);
166 connectionStatus = CameraConnectionStatus.DISCONNECTED;
167 statusReceiver.onCameraDisconnected();
174 public void connect()
176 Log.v(TAG, "connect()");
183 * @param message 表示用のメッセージ
186 public void alertConnectingFailed(String message)
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))
192 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
194 public void onClick(DialogInterface dialog, int which)
199 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
201 public void onClick(DialogInterface dialog, int which)
206 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
208 catch (android.content.ActivityNotFoundException ex)
210 // Activity が存在しなかった...設定画面が起動できなかった
211 Log.v(TAG, "android.content.ActivityNotFoundException...");
213 // この場合は、再試行と等価な動きとする
222 context.runOnUiThread(new Runnable()
233 public CameraConnectionStatus getConnectionStatus()
235 Log.v(TAG, "getConnectionStatus()");
236 return (connectionStatus);
240 public void forceUpdateConnectionStatus(CameraConnectionStatus status)
242 Log.v(TAG, "forceUpdateConnectionStatus()");
243 connectionStatus = status;
249 private void disconnectFromCamera(final boolean powerOff)
251 Log.v(TAG, "disconnectFromCamera()");
254 cameraExecutor.execute(new SonyCameraDisconnectSequence(powerOff));
265 private void connectToCamera()
267 Log.v(TAG, "connectToCamera()");
268 connectionStatus = CameraConnectionStatus.CONNECTING;
271 cameraExecutor.execute(new SonyCameraConnectSequence(context,statusReceiver, this, cameraHolder, listener));
275 Log.v(TAG, "connectToCamera() EXCEPTION : " + e.getMessage());