1 package net.osdn.gokigen.a01d.camera.canon.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.a01d.R;
19 import net.osdn.gokigen.a01d.camera.ICameraConnection;
20 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
21 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
22 import net.osdn.gokigen.a01d.camera.canon.wrapper.status.CanonStatusChecker;
24 import java.util.concurrent.Executor;
25 import java.util.concurrent.Executors;
27 public class CanonConnection implements ICameraConnection
29 private final String TAG = toString();
30 private final Activity context;
31 private final ICameraStatusReceiver statusReceiver;
32 private final IPtpIpInterfaceProvider interfaceProvider;
33 private final BroadcastReceiver connectionReceiver;
34 private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
35 private final CanonStatusChecker statusChecker;
36 private CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
38 public CanonConnection(@NonNull final Activity context, @NonNull final ICameraStatusReceiver statusReceiver, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull CanonStatusChecker statusChecker)
40 Log.v(TAG, "CanonConnection()");
41 this.context = context;
42 this.statusReceiver = statusReceiver;
43 this.interfaceProvider = interfaceProvider;
44 this.statusChecker = statusChecker;
45 connectionReceiver = new BroadcastReceiver()
48 public void onReceive(Context context, Intent intent)
50 onReceiveBroadcastOfConnection(context, intent);
59 private void onReceiveBroadcastOfConnection(Context context, Intent intent)
61 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_check_wifi), false, false, 0);
62 statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
64 Log.v(TAG, context.getString(R.string.connect_check_wifi));
66 String action = intent.getAction();
69 Log.v(TAG, "intent.getAction() : null");
75 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
77 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
79 WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
80 if (wifiManager != null) {
81 WifiInfo info = wifiManager.getConnectionInfo();
82 if (wifiManager.isWifiEnabled() && info != null) {
83 if (info.getNetworkId() != -1) {
84 Log.v(TAG, "Network ID is -1, there is no currently connected network.");
86 // 自動接続が指示されていた場合は、カメラとの接続処理を行う
90 Log.v(TAG, "NETWORK INFO IS NULL.");
92 Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
97 } catch (Exception e) {
98 Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
104 public void startWatchWifiStatus(Context context)
106 Log.v(TAG, "startWatchWifiStatus()");
107 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_prepare), false, false, 0);
108 statusReceiver.onStatusNotify("prepare");
110 IntentFilter filter = new IntentFilter();
111 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
112 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
113 context.registerReceiver(connectionReceiver, filter);
117 public void stopWatchWifiStatus(Context context)
119 Log.v(TAG, "stopWatchWifiStatus()");
120 context.unregisterReceiver(connectionReceiver);
125 public void disconnect(boolean powerOff)
127 Log.v(TAG, "disconnect()");
128 disconnectFromCamera(powerOff);
129 connectionStatus = CameraConnectionStatus.DISCONNECTED;
130 statusReceiver.onCameraDisconnected();
134 public void connect()
136 Log.v(TAG, "connect()");
141 public void alertConnectingFailed(String message)
143 Log.v(TAG, "alertConnectingFailed() : " + message);
144 final AlertDialog.Builder builder = new AlertDialog.Builder(context)
145 .setTitle(context.getString(R.string.dialog_title_connect_failed_canon))
147 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
149 public void onClick(DialogInterface dialog, int which)
155 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
157 public void onClick(DialogInterface dialog, int which)
162 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
164 catch (android.content.ActivityNotFoundException ex)
166 // Activity が存在しなかった...設定画面が起動できなかった
167 Log.v(TAG, "android.content.ActivityNotFoundException...");
169 // この場合は、再試行と等価な動きとする
178 context.runOnUiThread(new Runnable()
189 public CameraConnectionStatus getConnectionStatus()
191 Log.v(TAG, " getConnectionStatus()");
192 return (connectionStatus);
196 public void forceUpdateConnectionStatus(CameraConnectionStatus status)
198 Log.v(TAG, " forceUpdateConnectionStatus()");
199 connectionStatus = status;
205 private void disconnectFromCamera(final boolean powerOff)
207 Log.v(TAG, " disconnectFromCamera()");
210 cameraExecutor.execute(new CanonCameraDisconnectSequence(context, interfaceProvider));
221 private void connectToCamera()
223 Log.v(TAG, " connectToCamera()");
224 connectionStatus = CameraConnectionStatus.CONNECTING;
227 cameraExecutor.execute(new CanonCameraConnectSequence(context, statusReceiver, this, interfaceProvider, statusChecker));
231 Log.v(TAG, " connectToCamera() EXCEPTION : " + e.getMessage());