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.utils.SimpleHttpClient;
12 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
14 import androidx.annotation.NonNull;
16 class RicohGr2CameraConnectSequence implements Runnable
18 private final String TAG = this.toString();
19 private final Activity context;
20 private final ICameraConnection cameraConnection;
21 private final ICameraStatusReceiver cameraStatusReceiver;
23 RicohGr2CameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection)
25 Log.v(TAG, "RicohGr2CameraConnectSequence");
26 this.context = context;
27 this.cameraConnection = cameraConnection;
28 this.cameraStatusReceiver = statusReceiver;
34 final String areYouThereUrl = "http://192.168.0.1/v1/ping";
35 final String grCommandUrl = "http://192.168.0.1/_gr";
36 final int TIMEOUT_MS = 5000;
39 String response = SimpleHttpClient.httpGet(areYouThereUrl, TIMEOUT_MS);
40 Log.v(TAG, areYouThereUrl + " " + response);
41 if (response.length() > 0)
43 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
47 final String postData = "cmd=acclock off";
48 String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
49 Log.v(TAG, grCommandUrl + " " + response0);
53 if (preferences.getBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false))
55 final String postData = "cmd=lcd sleep on";
56 String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
57 Log.v(TAG, grCommandUrl + " " + response0);
60 // 表示するディスプレイモードを切り替える
61 String dispMode = preferences.getString(IPreferencePropertyAccessor.GR2_DISPLAY_MODE, IPreferencePropertyAccessor.GR2_DISPLAY_MODE_DEFAULT_VALUE);
62 if (dispMode.contains("1"))
65 final String postData = "cmd=bdisp";
66 String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
67 Log.v(TAG, grCommandUrl + " " + response0);
70 else if (dispMode.contains("2"))
73 final String postData = "cmd=bdisp";
74 String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
75 String response1 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
76 Log.v(TAG, grCommandUrl + " " + response0 + " " + response1);
78 else if (dispMode.contains("3"))
81 final String postData = "cmd=bdisp";
82 String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
83 String response1 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
84 String response2 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
85 Log.v(TAG, grCommandUrl + " " + response0 + " " + response1 + response2);
91 onConnectError(context.getString(R.string.camera_not_found));
97 onConnectError(e.getLocalizedMessage());
101 private void onConnectNotify()
105 final Thread thread = new Thread(new Runnable()
111 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
112 cameraStatusReceiver.onCameraConnected();
113 Log.v(TAG, "onConnectNotify()");
124 private void waitForAMoment(long mills)
129 Log.v(TAG, " WAIT " + mills + "ms");
131 } catch (Exception e) {
137 private void onConnectError(String reason)
139 cameraConnection.alertConnectingFailed(reason);