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.support.annotation.NonNull;
14 import android.support.v7.app.AlertDialog;
15 import android.util.Log;
17 import net.osdn.gokigen.a01d.R;
18 import net.osdn.gokigen.a01d.camera.ICameraConnection;
19 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.Executors;
29 public class RicohGr2Connection implements ICameraConnection
31 private final String TAG = toString();
32 private final Activity context;
33 private final ICameraStatusReceiver statusReceiver;
34 private final BroadcastReceiver connectionReceiver;
35 //private final ConnectivityManager connectivityManager;
36 private final Executor cameraExecutor = Executors.newFixedThreadPool(1);
37 //private final Handler networkConnectionTimeoutHandler;
38 //private static final int MESSAGE_CONNECTIVITY_TIMEOUT = 1;
39 private CameraConnectionStatus connectionStatus = CameraConnectionStatus.UNKNOWN;
46 public RicohGr2Connection(@NonNull final Activity context, @NonNull final ICameraStatusReceiver statusReceiver)
48 Log.v(TAG, "SonyCameraConnection()");
49 this.context = context;
50 this.statusReceiver = statusReceiver;
51 connectionReceiver = new BroadcastReceiver()
54 public void onReceive(Context context, Intent intent)
56 onReceiveBroadcastOfConnection(context, intent);
65 private void onReceiveBroadcastOfConnection(Context context, Intent intent)
67 statusReceiver.onStatusNotify(context.getString(R.string.connect_check_wifi));
68 Log.v(TAG,context.getString(R.string.connect_check_wifi));
70 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)
89 if (info.getNetworkId() != -1)
91 Log.v(TAG, "Network ID is -1, there is no currently connected network.");
93 // 自動接続が指示されていた場合は、カメラとの接続処理を行う
98 Log.v(TAG, "NETWORK INFO IS NULL.");
100 Log.v(TAG, "isWifiEnabled : " + wifiManager.isWifiEnabled() + " NetworkId : " + info.getNetworkId());
108 Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.getMessage());
118 public void startWatchWifiStatus(Context context)
120 Log.v(TAG, "startWatchWifiStatus()");
121 statusReceiver.onStatusNotify("prepare");
123 IntentFilter filter = new IntentFilter();
124 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
125 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
126 context.registerReceiver(connectionReceiver, filter);
134 public void stopWatchWifiStatus(Context context)
136 Log.v(TAG, "stopWatchWifiStatus()");
137 context.unregisterReceiver(connectionReceiver);
146 public void disconnect(boolean powerOff)
148 Log.v(TAG, "disconnect()");
149 disconnectFromCamera(powerOff);
150 connectionStatus = CameraConnectionStatus.DISCONNECTED;
151 statusReceiver.onCameraDisconnected();
160 public void connect()
162 Log.v(TAG, "connect()");
172 public void alertConnectingFailed(String message)
174 Log.v(TAG, "alertConnectingFailed() : " + message);
175 final AlertDialog.Builder builder = new AlertDialog.Builder(context)
176 .setTitle(context.getString(R.string.dialog_title_connect_failed))
178 .setPositiveButton(context.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
180 public void onClick(DialogInterface dialog, int which)
185 .setNeutralButton(R.string.dialog_title_button_network_settings, new DialogInterface.OnClickListener() {
187 public void onClick(DialogInterface dialog, int which)
192 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
194 catch (android.content.ActivityNotFoundException ex)
196 // Activity が存在しなかった...設定画面が起動できなかった
197 Log.v(TAG, "android.content.ActivityNotFoundException...");
199 // この場合は、再試行と等価な動きとする
208 context.runOnUiThread(new Runnable()
219 public CameraConnectionStatus getConnectionStatus()
221 Log.v(TAG, "getConnectionStatus()");
222 return (connectionStatus);
230 public void forceUpdateConnectionStatus(CameraConnectionStatus status)
232 Log.v(TAG, "forceUpdateConnectionStatus()");
233 connectionStatus = status;
239 private void disconnectFromCamera(final boolean powerOff)
241 Log.v(TAG, "disconnectFromCamera()");
244 cameraExecutor.execute(new RicohGr2CameraDisconnectSequence(context, powerOff));
255 private void connectToCamera()
257 Log.v(TAG, "connectToCamera()");
258 connectionStatus = CameraConnectionStatus.CONNECTING;
261 cameraExecutor.execute(new RicohGr2CameraConnectSequence(context, statusReceiver, this));
265 Log.v(TAG, "connectToCamera() EXCEPTION : " + e.getMessage());