1 package net.osdn.gokigen.a01d.camera.ricohgr2.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 net.osdn.gokigen.a01d.R;
16 import net.osdn.gokigen.a01d.camera.ICameraConnection;
17 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.Executors;
22 import androidx.annotation.NonNull;
23 import androidx.appcompat.app.AlertDialog;
30 public class RicohGr2Connection implements ICameraConnection
32 private final String TAG = toString();
33 private final Activity context;
34 private final ICameraStatusReceiver statusReceiver;
35 private final BroadcastReceiver connectionReceiver;
36 //private final ConnectivityManager connectivityManager;
37 private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
38 //private final Handler networkConnectionTimeoutHandler;
39 //private static final int MESSAGE_CONNECTIVITY_TIMEOUT = 1;
40 private CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
47 public RicohGr2Connection(@NonNull final Activity context, @NonNull final ICameraStatusReceiver statusReceiver)
49 Log.v(TAG, "RicohGr2Connection()");
50 this.context = context;
51 this.statusReceiver = statusReceiver;
52 connectionReceiver = new BroadcastReceiver()
55 public void onReceive(Context context, Intent intent)
57 onReceiveBroadcastOfConnection(context, intent);
66 private void onReceiveBroadcastOfConnection(Context context, Intent intent)
68 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();
75 Log.v(TAG, "intent.getAction() : null");
81 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
83 Log.v(TAG, "onReceiveBroadcastOfConnection() : CONNECTIVITY_ACTION");
85 WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
86 if (wifiManager != null) {
87 WifiInfo info = wifiManager.getConnectionInfo();
88 if (wifiManager.isWifiEnabled() && info != null)
90 if (info.getNetworkId() != -1)
92 Log.v(TAG, "Network ID is -1, there is no currently connected network.");
94 // 自動接続が指示されていた場合は、カメラとの接続処理を行う
99 Log.v(TAG, "NETWORK INFO IS NULL.");
101 Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
109 Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
119 public void startWatchWifiStatus(Context context)
121 Log.v(TAG, "startWatchWifiStatus()");
122 statusReceiver.onStatusNotify("prepare");
124 IntentFilter filter = new IntentFilter();
125 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
126 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
127 context.registerReceiver(connectionReceiver, filter);
135 public void stopWatchWifiStatus(Context context)
137 Log.v(TAG, "stopWatchWifiStatus()");
138 context.unregisterReceiver(connectionReceiver);
147 public void disconnect(boolean powerOff)
149 Log.v(TAG, "disconnect()");
150 disconnectFromCamera(powerOff);
151 connectionStatus = CameraConnectionStatus.DISCONNECTED;
152 statusReceiver.onCameraDisconnected();
161 public void connect()
163 Log.v(TAG, "connect()");
173 public void alertConnectingFailed(String message)
175 Log.v(TAG, "alertConnectingFailed() : " + message);
176 final AlertDialog.Builder builder = new AlertDialog.Builder(context)
177 .setTitle(context.getString(R.string.dialog_title_connect_failed_ricoh))
179 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
181 public void onClick(DialogInterface dialog, int which)
186 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
188 public void onClick(DialogInterface dialog, int which)
193 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
195 catch (android.content.ActivityNotFoundException ex)
197 // Activity が存在しなかった...設定画面が起動できなかった
198 Log.v(TAG, "android.content.ActivityNotFoundException...");
200 // この場合は、再試行と等価な動きとする
209 context.runOnUiThread(new Runnable()
220 public CameraConnectionStatus getConnectionStatus()
222 Log.v(TAG, "getConnectionStatus()");
223 return (connectionStatus);
231 public void forceUpdateConnectionStatus(CameraConnectionStatus status)
233 Log.v(TAG, "forceUpdateConnectionStatus()");
234 connectionStatus = status;
240 private void disconnectFromCamera(final boolean powerOff)
242 Log.v(TAG, "disconnectFromCamera()");
245 cameraExecutor.execute(new RicohGr2CameraDisconnectSequence(context, powerOff));
256 private void connectToCamera()
258 Log.v(TAG, "connectToCamera()");
259 connectionStatus = CameraConnectionStatus.CONNECTING;
262 cameraExecutor.execute(new RicohGr2CameraConnectSequence(context, statusReceiver, this));
266 Log.v(TAG, "connectToCamera() EXCEPTION : " + e.getMessage());