OSDN Git Service

GR2 / PENTAXモードの自動認識・切り替えをできるようにした。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / ricohgr2 / wrapper / connection / RicohGr2CameraConnectSequence.java
1 package net.osdn.gokigen.a01d.camera.ricohgr2.wrapper.connection;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.preference.PreferenceManager;
6 import android.util.Log;
7
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;
14
15 import androidx.annotation.NonNull;
16
17 class RicohGr2CameraConnectSequence implements Runnable
18 {
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;
24
25     RicohGr2CameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IUsePentaxCommand usePentaxCommand)
26     {
27         Log.v(TAG, "RicohGr2CameraConnectSequence");
28         this.context = context;
29         this.cameraConnection = cameraConnection;
30         this.cameraStatusReceiver = statusReceiver;
31         this.usePentaxCommand = usePentaxCommand;
32     }
33
34     @Override
35     public void run()
36     {
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;
40         try
41         {
42             String response = SimpleHttpClient.httpGet(areYouThereUrl, TIMEOUT_MS);
43             Log.v(TAG, areYouThereUrl + " " + response);
44             if (response.length() > 0)
45             {
46                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
47
48                 // 接続時、レンズロックOFF + GR2 コマンド有効/無効の確認
49                 {
50                     final String postData = "cmd=acclock off";
51                     String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
52                     Log.v(TAG, grCommandUrl + " " + response0);
53
54                     // GR2 専用コマンドを受け付けられるかどうかで、Preference を書き換える
55                     boolean pentaxCommand = !(response0.length() > 0);
56                     usePentaxCommand.setUsePentaxCommand(pentaxCommand);
57
58                     // 接続時、カメラの画面を消す
59                     if ((pentaxCommand)&&(preferences.getBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false)))
60                     {
61                         final String postData0 = "cmd=lcd sleep on";
62                         String response1 = SimpleHttpClient.httpPost(grCommandUrl, postData0, TIMEOUT_MS);
63                         Log.v(TAG, grCommandUrl + " " + response1);
64                     }
65                 }
66                 onConnectNotify();
67             }
68             else
69             {
70                 onConnectError(context.getString(R.string.camera_not_found));
71             }
72         }
73         catch (Exception e)
74         {
75             e.printStackTrace();
76             onConnectError(e.getLocalizedMessage());
77         }
78     }
79
80     private void onConnectNotify()
81     {
82         try
83         {
84             final Thread thread = new Thread(new Runnable()
85             {
86                 @Override
87                 public void run()
88                 {
89                     // カメラとの接続確立を通知する
90                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
91                     cameraStatusReceiver.onCameraConnected();
92                     Log.v(TAG, "onConnectNotify()");
93                 }
94             });
95             thread.start();
96         }
97         catch (Exception e)
98         {
99             e.printStackTrace();
100         }
101     }
102
103     private void waitForAMoment(long mills)
104     {
105         if (mills > 0)
106         {
107             try {
108                 Log.v(TAG, " WAIT " + mills + "ms");
109                 Thread.sleep(mills);
110             } catch (Exception e) {
111                 e.printStackTrace();
112             }
113         }
114     }
115
116     private void onConnectError(String reason)
117     {
118         cameraConnection.alertConnectingFailed(reason);
119     }
120 }