OSDN Git Service

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