1 package net.osdn.gokigen.a01d.camera.ricohgr2.wrapper.connection;
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.preference.PreferenceManager;
6 import android.util.Log;
8 import net.osdn.gokigen.a01d.R;
9 import net.osdn.gokigen.a01d.camera.ICameraConnection;
10 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
11 import net.osdn.gokigen.a01d.camera.ricohgr2.wrapper.IUsePentaxCommand;
12 import net.osdn.gokigen.a01d.camera.utils.SimpleHttpClient;
13 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
15 import androidx.annotation.NonNull;
17 class RicohGr2CameraConnectSequence implements Runnable
19 private final String TAG = this.toString();
20 private final Activity context;
21 private final ICameraConnection cameraConnection;
22 private final ICameraStatusReceiver cameraStatusReceiver;
23 private final IUsePentaxCommand usePentaxCommand;
25 RicohGr2CameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IUsePentaxCommand usePentaxCommand)
27 Log.v(TAG, "RicohGr2CameraConnectSequence");
28 this.context = context;
29 this.cameraConnection = cameraConnection;
30 this.cameraStatusReceiver = statusReceiver;
31 this.usePentaxCommand = usePentaxCommand;
37 final String areYouThereUrl = "http://192.168.0.1/v1/ping";
38 final String grCommandUrl = "http://192.168.0.1/_gr";
39 final int TIMEOUT_MS = 5000;
42 String response = SimpleHttpClient.httpGet(areYouThereUrl, TIMEOUT_MS);
43 Log.v(TAG, areYouThereUrl + " " + response);
44 if (response.length() > 0)
46 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
48 // 接続時、レンズロックOFF + GR2 コマンド有効/無効の確認
50 final String postData = "cmd=acclock off";
51 String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
52 Log.v(TAG, grCommandUrl + " " + response0);
54 // GR2 専用コマンドを受け付けられるかどうかで、Preference を書き換える
55 boolean pentaxCommand = !(response0.length() > 0);
56 usePentaxCommand.setUsePentaxCommand(pentaxCommand);
59 if ((pentaxCommand)&&(preferences.getBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false)))
61 final String postData0 = "cmd=lcd sleep on";
62 String response1 = SimpleHttpClient.httpPost(grCommandUrl, postData0, TIMEOUT_MS);
63 Log.v(TAG, grCommandUrl + " " + response1);
70 onConnectError(context.getString(R.string.camera_not_found));
76 onConnectError(e.getLocalizedMessage());
80 private void onConnectNotify()
84 final Thread thread = new Thread(new Runnable()
90 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
91 cameraStatusReceiver.onCameraConnected();
92 Log.v(TAG, "onConnectNotify()");
103 private void waitForAMoment(long mills)
108 Log.v(TAG, " WAIT " + mills + "ms");
110 } catch (Exception e) {
116 private void onConnectError(String reason)
118 cameraConnection.alertConnectingFailed(reason);