1 package net.osdn.gokigen.gr2control.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.gr2control.R;
9 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
10 import net.osdn.gokigen.gr2control.camera.ICameraStatusReceiver;
11 import net.osdn.gokigen.gr2control.camera.utils.SimpleHttpClient;
12 import net.osdn.gokigen.gr2control.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);
63 onConnectError(context.getString(R.string.camera_not_found));
69 onConnectError(e.getLocalizedMessage());
73 private void onConnectNotify()
77 final Thread thread = new Thread(new Runnable()
83 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
84 cameraStatusReceiver.onCameraConnected();
85 Log.v(TAG, "onConnectNotify()");
97 private void waitForAMoment(long mills)
102 Log.v(TAG, " WAIT " + mills + "ms");
104 } catch (Exception e) {
111 private void onConnectError(String reason)
113 cameraConnection.alertConnectingFailed(reason);