1 package net.osdn.gokigen.gr2control.camera.ricohgr2.wrapper.connection;
3 import android.content.BroadcastReceiver;
4 import android.content.Context;
5 import android.content.DialogInterface;
6 import android.content.Intent;
7 import android.content.IntentFilter;
8 import android.net.ConnectivityManager;
9 import android.net.wifi.WifiInfo;
10 import android.net.wifi.WifiManager;
11 import android.provider.Settings;
12 import android.util.Log;
14 import net.osdn.gokigen.gr2control.R;
15 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
16 import net.osdn.gokigen.gr2control.camera.ICameraStatusReceiver;
18 import java.util.concurrent.Executor;
19 import java.util.concurrent.Executors;
21 import androidx.annotation.NonNull;
22 import androidx.appcompat.app.AlertDialog;
23 import androidx.fragment.app.FragmentActivity;
30 public class RicohGr2Connection implements ICameraConnection
32 private final String TAG = toString();
33 private final FragmentActivity context;
34 private final ICameraStatusReceiver statusReceiver;
35 private final BroadcastReceiver connectionReceiver;
36 private final IUseGR2CommandNotify gr2CommandNotify;
37 //private final ConnectivityManager connectivityManager;
38 private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
39 //private final Handler networkConnectionTimeoutHandler;
40 //private static final int MESSAGE_CONNECTIVITY_TIMEOUT = 1;
41 private CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
48 public RicohGr2Connection(@NonNull final FragmentActivity context, @NonNull final ICameraStatusReceiver statusReceiver, @NonNull IUseGR2CommandNotify gr2CommandNotify)
50 Log.v(TAG, " RicohGr2Connection()");
51 this.context = context;
52 this.statusReceiver = statusReceiver;
53 this.gr2CommandNotify = gr2CommandNotify;
54 connectionReceiver = new BroadcastReceiver()
57 public void onReceive(Context context, Intent intent)
59 onReceiveBroadcastOfConnection(context, intent);
68 private void onReceiveBroadcastOfConnection(Context context, Intent intent)
70 statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
71 Log.v(TAG,context.getString(R.string.connect_check_wifi));
73 String action = intent.getAction();
77 Log.v(TAG, "intent.getAction() : null");
83 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
85 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
87 WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
88 if (wifiManager != null) {
89 WifiInfo info = wifiManager.getConnectionInfo();
90 if (wifiManager.isWifiEnabled() && info != null)
92 if (info.getNetworkId() == -1)
94 Log.v(TAG, "Network ID is -1, there is no currently connected network.");
98 Log.v(TAG, "Network ID is " + info.getNetworkId());
100 // 自動接続が指示されていた場合は、カメラとの接続処理を行う
107 Log.v(TAG, "NETWORK INFO IS NULL.");
111 Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
119 Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
129 public void startWatchWifiStatus(Context context)
131 Log.v(TAG, "startWatchWifiStatus()");
132 statusReceiver.onStatusNotify("prepare");
134 IntentFilter filter = new IntentFilter();
135 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
136 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
137 context.registerReceiver(connectionReceiver, filter);
145 public void stopWatchWifiStatus(Context context)
147 Log.v(TAG, "stopWatchWifiStatus()");
148 context.unregisterReceiver(connectionReceiver);
157 public void disconnect(boolean powerOff)
159 Log.v(TAG, "disconnect()");
160 disconnectFromCamera(powerOff);
161 connectionStatus = CameraConnectionStatus.DISCONNECTED;
162 statusReceiver.onCameraDisconnected();
171 public void connect()
173 Log.v(TAG, "connect()");
183 public void alertConnectingFailed(String message)
185 Log.v(TAG, "alertConnectingFailed() : " + message);
188 final AlertDialog.Builder builder = new AlertDialog.Builder(context)
189 .setTitle(context.getString(R.string.dialog_title_connect_failed))
191 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
193 public void onClick(DialogInterface dialog, int which) {
197 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
199 public void onClick(DialogInterface dialog, int which) {
202 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
203 } catch (android.content.ActivityNotFoundException ex) {
204 // Activity が存在しなかった...設定画面が起動できなかった
205 Log.v(TAG, "android.content.ActivityNotFoundException...");
207 // この場合は、再試行と等価な動きとする
209 } catch (Exception e) {
214 context.runOnUiThread(new Runnable() {
224 public CameraConnectionStatus getConnectionStatus()
226 Log.v(TAG, "getConnectionStatus()");
227 return (connectionStatus);
235 public void forceUpdateConnectionStatus(CameraConnectionStatus status)
237 Log.v(TAG, "forceUpdateConnectionStatus()");
238 connectionStatus = status;
244 private void disconnectFromCamera(final boolean powerOff)
246 Log.v(TAG, "disconnectFromCamera()");
249 cameraExecutor.execute(new RicohGr2CameraDisconnectSequence(context, powerOff));
260 private void connectToCamera()
262 Log.v(TAG, "connectToCamera()");
263 connectionStatus = CameraConnectionStatus.CONNECTING;
266 cameraExecutor.execute(new RicohGr2CameraConnectSequence(context, statusReceiver, this, gr2CommandNotify));
270 Log.v(TAG, "connectToCamera() EXCEPTION : " + e.getMessage());