OSDN Git Service

物理ボタン(音量+)キーでシャッターが切れるようにする。(反応をたべるようにした)
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / liveview / LiveViewClickTouchListener.java
1 package net.osdn.gokigen.gr2control.liveview;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.util.Log;
6 import android.view.KeyEvent;
7 import android.view.MotionEvent;
8 import android.view.View;
9
10 import net.osdn.gokigen.gr2control.R;
11 import net.osdn.gokigen.gr2control.camera.ICameraButtonControl;
12 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
13 import net.osdn.gokigen.gr2control.camera.ICaptureControl;
14 import net.osdn.gokigen.gr2control.camera.IFocusingControl;
15 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
16 import net.osdn.gokigen.gr2control.camera.IZoomLensControl;
17 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
18 import net.osdn.gokigen.gr2control.scene.ConfirmationDialog;
19 import net.osdn.gokigen.gr2control.scene.IChangeScene;
20
21 import androidx.annotation.NonNull;
22 import androidx.preference.PreferenceManager;
23
24 /**
25  *
26  *
27  */
28 class LiveViewClickTouchListener implements View.OnClickListener, View.OnTouchListener, View.OnKeyListener
29 {
30     private final String TAG = toString();
31     private final Activity context;
32     private final ILiveImageStatusNotify statusNotify;
33     private final IStatusViewDrawer statusViewDrawer;
34     private final IChangeScene changeScene;
35     private final IInterfaceProvider interfaceProvider;
36     private final IFocusingControl focusingControl;
37     private final ICaptureControl captureControl;
38     //private final IOlyCameraPropertyProvider propertyProvider;
39     //private final ICameraInformation cameraInformation;
40     private final ICameraConnection cameraConnection;
41     private final IFavoriteSettingDialogKicker dialogKicker;
42     private final IZoomLensControl zoomLensControl;
43
44     LiveViewClickTouchListener(@NonNull Activity context, @NonNull ILiveImageStatusNotify imageStatusNotify, @NonNull IStatusViewDrawer statusView, @NonNull IChangeScene changeScene, @NonNull IInterfaceProvider interfaceProvider, @NonNull IFavoriteSettingDialogKicker dialogKicker)
45     {
46         this.context = context;
47         this.statusNotify = imageStatusNotify;
48         this.statusViewDrawer = statusView;
49         this.changeScene = changeScene;
50         this.interfaceProvider = interfaceProvider;
51
52         this.focusingControl = interfaceProvider.getFocusingControl();
53         this.captureControl = interfaceProvider.getCaptureControl();
54         this.cameraConnection = interfaceProvider.getCameraConnection();
55         this.zoomLensControl = interfaceProvider.getZoomLensControl();
56
57         this.dialogKicker = dialogKicker;
58     }
59
60     /**
61      *   オブジェクトをクリックする処理
62      *
63      */
64     @Override
65     public void onClick(View view)
66     {
67         int id = view.getId();
68         //Log.v(TAG, "onClick() : " + id);
69         try
70         {
71             switch (id)
72             {
73                 case R.id.hideControlPanelTextView:
74                     // 制御パネルを隠す
75                     showHideControlPanel(false);
76                     break;
77
78                 case R.id.showControlPanelTextView:
79                     // 制御パネルを表示する
80                     showHideControlPanel(true);
81                     break;
82
83                 case R.id.showKeyPanelImageView:
84                     // キーパネルを表示する
85                     showHideKeyPanel(true);
86                     break;
87
88                 case R.id.hideKeyPanelTextView:
89                     // キーパネルを隠す
90                     showHideKeyPanel(false);
91                     break;
92
93                 case R.id.connect_disconnect_button:
94                     // カメラと接続・切断のボタンが押された
95                     changeScene.changeCameraConnection();
96                     break;
97
98                 case R.id.shutter_button:
99                     // シャッターボタンが押された (撮影)
100                     pushedShutterButton();
101                     break;
102
103                 case R.id.focusUnlockImageView:
104                     // フォーカスアンロックボタンが押された
105                     pushedFocusUnlock();
106                     break;
107
108                 case R.id.show_images_button:
109                     // 画像一覧表示ボタンが押された...画像一覧画面を開く
110                     changeScene.changeScenceToImageList();
111                     break;
112
113                 case R.id.camera_power_off_button:
114                     // 電源ボタンが押された...終了してよいか確認して、終了する
115                     confirmExitApplication();
116                     break;
117
118                 case R.id.show_preference_button:
119                     // カメラの設定
120                     changeScene.changeSceneToConfiguration();
121                     break;
122
123                 case R.id.show_hide_grid_button:
124                     // グリッドの ON/OFF
125                     statusNotify.toggleShowGridFrame();
126                     statusViewDrawer.updateGridIcon();
127                     break;
128                 case R.id.zoom_in_button:
129                     // ズームインのボタンが押された
130                     actionZoomin();
131                     break;
132                 case R.id.zoom_out_button:
133                     // ズームアウトのボタンが押された
134                     actionZoomout();
135                     break;
136                 case R.id.specialButtonImageView:
137                     // スペシャルボタンが押された
138                     pushedSpecialButton();
139                     break;
140 /*
141                 case R.id.camera_property_settings_button:
142                     // カメラのプロパティ設定
143                     changeScene.changeSceneToCameraPropertyList();
144                     break;
145
146                 case R.id.focusing_button:
147                     // AF と MFの切り替えボタンが押された
148                     changeFocusingMode();
149                     break;
150
151                 case R.id.live_view_scale_button:
152                     //  ライブビューの倍率を更新する
153                     statusViewDrawer.updateLiveViewScale(true);
154                     break;
155
156                 case R.id.show_favorite_settings_button:
157                     // お気に入り設定のダイアログを表示する
158                     showFavoriteDialog();
159                     break;
160 */
161                 default:
162                     Log.v(TAG, "onClick() : " + id);
163                     break;
164             }
165         }
166         catch (Exception e)
167         {
168             e.printStackTrace();
169         }
170     }
171
172     /**
173      *   コントロールパネルの表示・非表示
174      *
175      */
176     private void showHideControlPanel(boolean isShow)
177     {
178         try
179         {
180             View target = context.findViewById(R.id.controlPanelLayout);
181             View target2 = context.findViewById(R.id.showControlPanelTextView);
182             if (target != null)
183             {
184                 target.setVisibility((isShow) ? View.VISIBLE : View.INVISIBLE);
185                 target.invalidate();
186                 if (target2 != null)
187                 {
188                     target2.setVisibility((isShow) ? View.INVISIBLE : View.VISIBLE);
189                     target2.invalidate();
190                 }
191             }
192         }
193         catch (Exception e)
194         {
195             e.printStackTrace();
196         }
197     }
198
199     /**
200      *   キー操作パネルの表示・非表示
201      *
202      */
203     private void showHideKeyPanel(boolean isShow)
204     {
205         try
206         {
207             View target = context.findViewById(R.id.keyPanelLayout);
208             View target2 = context.findViewById(R.id.showKeyPanelImageView);
209             if (target != null)
210             {
211                 target.setVisibility((isShow) ? View.VISIBLE : View.INVISIBLE);
212                 target.invalidate();
213                 if (target2 != null)
214                 {
215                     target2.setVisibility((isShow) ? View.INVISIBLE : View.VISIBLE);
216                     target2.invalidate();
217                 }
218             }
219         }
220         catch (Exception e)
221         {
222             e.printStackTrace();
223         }
224     }
225
226     private void confirmExitApplication()
227     {
228         try
229         {
230             // 確認ダイアログの生成と表示
231             ConfirmationDialog dialog = ConfirmationDialog.newInstance(context);
232             dialog.show(R.string.dialog_title_confirmation, R.string.dialog_message_power_off, new ConfirmationDialog.Callback() {
233                 @Override
234                 public void confirm()
235                 {
236                     changeScene.exitApplication();
237                 }
238             });
239         }
240         catch (Exception e)
241         {
242             e.printStackTrace();
243         }
244     }
245
246     private void actionZoomin()
247     {
248         Log.v(TAG, "actionZoomin()");
249         try
250         {
251             // ズーム可能な場合、ズームインする
252             if (zoomLensControl.canZoom())
253             {
254                 zoomLensControl.driveZoomLens(true);
255             }
256         }
257         catch (Exception e)
258         {
259             e.printStackTrace();
260         }
261     }
262
263     private void actionZoomout()
264     {
265         Log.v(TAG, "actionZoomout()");
266         try
267         {
268             // ズーム可能な場合、ズームアウトする
269             if (zoomLensControl.canZoom())
270             {
271                 zoomLensControl.driveZoomLens(false);
272             }
273         }
274         catch (Exception e)
275         {
276             e.printStackTrace();
277         }
278     }
279
280
281     /**
282      *   シャッターボタンが押された時の処理
283      *
284      *
285      */
286     private void pushedShutterButton()
287     {
288         Log.v(TAG, "pushedShutterButton()");
289         try
290         {
291             // カメラで撮影する
292             captureControl.doCapture(0);
293
294             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
295             if (preferences.getBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true))
296             {
297                 // ライブビュー画像も保管する
298                 statusNotify.takePicture();
299             }
300         }
301         catch (Exception e)
302         {
303             e.printStackTrace();
304         }
305     }
306
307     /**
308      *   フォーカスアンロックボタンが押された時の処理
309      *
310      */
311     private void pushedFocusUnlock()
312     {
313         Log.v(TAG, "pushedFocusUnlock()");
314         try
315         {
316             // フォーカスアンロックする
317             focusingControl.unlockAutoFocus();
318         }
319         catch (Exception e)
320         {
321             e.printStackTrace();
322         }
323     }
324
325     /**
326      *   スペシャルボタンが押された時の処理
327      *
328      */
329     private void pushedSpecialButton()
330     {
331         showFavoriteDialog();
332     }
333
334     /**
335      *   お気に入り設定ダイアログの表示
336      *
337      */
338     private void showFavoriteDialog()
339     {
340         Log.v(TAG, "showFavoriteDialog()");
341         try
342         {
343             if (cameraConnection.getConnectionStatus() != ICameraConnection.CameraConnectionStatus.CONNECTED)
344             {
345                 // カメラと接続されていない時には、何もしない
346                 return;
347             }
348
349             if (interfaceProvider.getCammeraConnectionMethod() == ICameraConnection.CameraConnectionMethod.OPC)
350             {
351                 //  OPCカメラの場合には、お気に入り設定のダイアログを表示する
352                 dialogKicker.showFavoriteSettingDialog();
353                 return;
354             }
355
356
357             ICameraButtonControl btnCtl = interfaceProvider.getButtonControl();
358             if (btnCtl != null)
359             {
360                 // 'GREEN' ボタンが押されたこととする
361                 btnCtl.pushedButton(ICameraButtonControl.SPECIAL_GREEN_BUTTON, false);
362             }
363         }
364         catch (Exception e)
365         {
366             e.printStackTrace();
367         }
368     }
369
370     /**
371      *   オブジェクトをタッチする処理
372      *
373      */
374     @Override
375     public boolean onTouch(View view, MotionEvent motionEvent)
376     {
377         int id = view.getId();
378         if (focusingControl == null)
379         {
380             Log.v(TAG, "focusingControl is NULL.");
381             view.performClick();  // ダミー処理...
382             return (false);
383         }
384         Log.v(TAG, "onTouch() : " + id + " (" + motionEvent.getX() + "," + motionEvent.getY() + ")");
385         return ((id == R.id.cameraLiveImageView)&&(focusingControl.driveAutoFocus(motionEvent)));
386     }
387
388     /**
389      *   ボタンを押したときの対応
390      *
391      */
392     @Override
393     public boolean onKey(View view, int keyCode, @NonNull KeyEvent keyEvent)
394     {
395         Log.v(TAG, "onKey() : " + keyCode);
396         try
397         {
398             if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN)&&
399                     ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)))
400             {
401                 pushedShutterButton();
402                 return (true);
403             }
404         }
405         catch (Exception e)
406         {
407             e.printStackTrace();
408         }
409         return (false);
410     }
411 }