1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.connection;
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;
16 import androidx.annotation.NonNull;
17 import androidx.appcompat.app.AlertDialog;
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;
25 import java.util.concurrent.Executor;
26 import java.util.concurrent.Executors;
28 public class NikonConnection implements ICameraConnection
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);
37 private ICameraConnection.CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
39 private final NikonCameraConnectSequence connectSequence;
40 private final NikonCameraDisconnectSequence disconnectSequence;
42 public NikonConnection(@NonNull final Activity context, @NonNull final ICameraStatusReceiver statusReceiver, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull NikonStatusChecker statusChecker)
44 Log.v(TAG, "NikonConnection()");
45 this.context = context;
46 this.statusReceiver = statusReceiver;
47 this.interfaceProvider = interfaceProvider;
48 connectionReceiver = new BroadcastReceiver()
51 public void onReceive(Context context, Intent intent)
53 onReceiveBroadcastOfConnection(context, intent);
56 connectSequence = new NikonCameraConnectSequence(context, statusReceiver, this, interfaceProvider, statusChecker);
57 disconnectSequence = new NikonCameraDisconnectSequence(context, interfaceProvider, statusChecker);
64 private void onReceiveBroadcastOfConnection(Context context, Intent intent)
66 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_check_wifi), false, false, 0);
67 statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
69 Log.v(TAG, context.getString(R.string.connect_check_wifi));
71 String action = intent.getAction();
74 Log.v(TAG, "intent.getAction() : null");
80 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
82 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
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.");
91 // 自動接続が指示されていた場合は、カメラとの接続処理を行う
95 Log.v(TAG, "NETWORK INFO IS NULL.");
97 Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
102 } catch (Exception e) {
103 Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
109 public void startWatchWifiStatus(Context context)
111 Log.v(TAG, "startWatchWifiStatus()");
112 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_prepare), false, false, 0);
113 statusReceiver.onStatusNotify("prepare");
115 IntentFilter filter = new IntentFilter();
116 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
117 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
118 context.registerReceiver(connectionReceiver, filter);
122 public void stopWatchWifiStatus(Context context)
124 Log.v(TAG, "stopWatchWifiStatus()");
125 context.unregisterReceiver(connectionReceiver);
130 public void disconnect(boolean powerOff)
132 Log.v(TAG, "disconnect()");
133 disconnectFromCamera(powerOff);
134 connectionStatus = CameraConnectionStatus.DISCONNECTED;
135 statusReceiver.onCameraDisconnected();
139 public void connect()
141 Log.v(TAG, "connect()");
146 public void alertConnectingFailed(String message)
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))
152 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
154 public void onClick(DialogInterface dialog, int which)
160 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
162 public void onClick(DialogInterface dialog, int which)
167 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
169 catch (android.content.ActivityNotFoundException ex)
171 // Activity が存在しなかった...設定画面が起動できなかった
172 Log.v(TAG, "android.content.ActivityNotFoundException...");
174 // この場合は、再試行と等価な動きとする
183 context.runOnUiThread(new Runnable()
194 public CameraConnectionStatus getConnectionStatus()
196 Log.v(TAG, " getConnectionStatus()");
197 return (connectionStatus);
201 public void forceUpdateConnectionStatus(CameraConnectionStatus status)
203 Log.v(TAG, " forceUpdateConnectionStatus()");
204 connectionStatus = status;
210 private void disconnectFromCamera(final boolean powerOff)
212 Log.v(TAG, " disconnectFromCamera()");
215 cameraExecutor.execute(disconnectSequence);
226 private void connectToCamera()
228 Log.v(TAG, " connectToCamera()");
229 connectionStatus = CameraConnectionStatus.CONNECTING;
232 cameraExecutor.execute(connectSequence);
236 Log.v(TAG, " connectToCamera() EXCEPTION : " + e.getMessage());