OSDN Git Service

07ac50c7f367910d72497d33fdeb757a0a6fd976
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / liveview / LiveViewFragment.java
1 package net.osdn.gokigen.gr2control.liveview;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.graphics.Color;
6 import android.graphics.drawable.Drawable;
7 import android.os.Bundle;
8 import android.os.Vibrator;
9
10 import androidx.annotation.NonNull;
11 import androidx.appcompat.app.ActionBar;
12 import androidx.appcompat.app.AppCompatActivity;
13 import androidx.core.content.ContextCompat;
14 import androidx.core.content.res.ResourcesCompat;
15 import androidx.core.graphics.drawable.DrawableCompat;
16 import androidx.fragment.app.Fragment;
17 import androidx.fragment.app.FragmentActivity;
18 import androidx.preference.PreferenceManager;
19
20 import android.util.Log;
21 import android.view.KeyEvent;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.Button;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28
29 import net.osdn.gokigen.gr2control.R;
30 import net.osdn.gokigen.gr2control.camera.ICameraButtonControl;
31 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
32 import net.osdn.gokigen.gr2control.camera.ICameraInformation;
33 import net.osdn.gokigen.gr2control.camera.ICameraRunMode;
34 import net.osdn.gokigen.gr2control.camera.ICameraRunModeCallback;
35 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
36 import net.osdn.gokigen.gr2control.camera.ICameraStatusWatcher;
37 import net.osdn.gokigen.gr2control.camera.IDisplayInjector;
38 import net.osdn.gokigen.gr2control.camera.IFocusingModeNotify;
39 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
40 import net.osdn.gokigen.gr2control.camera.ILiveViewControl;
41 import net.osdn.gokigen.gr2control.camera.IZoomLensControl;
42 import net.osdn.gokigen.gr2control.camera.olympus.myolycameraprops.LoadSaveCameraProperties;
43 import net.osdn.gokigen.gr2control.camera.olympus.myolycameraprops.LoadSaveMyCameraPropertyDialog;
44 import net.osdn.gokigen.gr2control.liveview.liveviewlistener.ILiveViewListener;
45 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
46 import net.osdn.gokigen.gr2control.scene.IChangeScene;
47
48 import static android.content.Context.VIBRATOR_SERVICE;
49
50 /**
51  *  撮影用ライブビュー画面
52  *
53  */
54 public class LiveViewFragment extends Fragment implements IStatusViewDrawer, IFocusingModeNotify, IFavoriteSettingDialogKicker, ICameraStatusUpdateNotify, LiveViewKeyPanelClickListener.KeyPanelFeedback, ICameraRunModeCallback
55 {
56     private final String TAG = this.toString();
57
58     private ILiveViewControl liveViewControl = null;
59     private IZoomLensControl zoomLensControl = null;
60     private IInterfaceProvider interfaceProvider = null;
61     private IDisplayInjector interfaceInjector = null;
62     //private OlympusCameraLiveViewListenerImpl liveViewListener = null;
63     private IChangeScene changeScene = null;
64     private ICameraInformation cameraInformation = null;
65     private ICameraStatusWatcher statusWatcher = null;
66     private LiveViewClickTouchListener onClickTouchListener = null;
67     private LiveViewControlPanelClickListener onPanelClickListener = null;
68     private LiveViewKeyPanelClickListener onKeyPanelClickListener = null;
69     private LiveViewFujiXKeyPanelClickListener fujiXOnKeyPanelClickListener = null;
70
71     private TextView statusArea = null;
72     private TextView focalLengthArea = null;
73     private CameraLiveImageView imageView = null;
74
75     private ImageView manualFocus = null;
76     private ImageView showGrid = null;
77     private ImageView connectStatus = null;
78     private Button changeLiveViewScale = null;
79
80     private boolean imageViewCreated = false;
81     private View myView = null;
82     private String messageValue = "";
83
84     private ICameraConnection.CameraConnectionStatus currentConnectionStatus =  ICameraConnection.CameraConnectionStatus.UNKNOWN;
85
86     public static LiveViewFragment newInstance(IChangeScene sceneSelector, @NonNull IInterfaceProvider provider)
87     {
88         LiveViewFragment instance = new LiveViewFragment();
89         instance.prepare(sceneSelector, provider);
90
91         // パラメータはBundleにまとめておく
92         Bundle arguments = new Bundle();
93         //arguments.putString("title", title);
94         //arguments.putString("message", message);
95         instance.setArguments(arguments);
96
97         return (instance);
98     }
99
100     /**
101      *
102      *
103      */
104     @Override
105     public void onCreate(Bundle savedInstanceState)
106     {
107         super.onCreate(savedInstanceState);
108         Log.v(TAG, "onCreate()");
109 /*
110         if (liveViewListener == null)
111         {
112             liveViewListener = new OlympusCameraLiveViewListenerImpl();
113         }
114 */
115     }
116
117     /**
118      *
119      *
120      */
121     @Override
122     public void onAttach(@NonNull Context context)
123     {
124         super.onAttach(context);
125         Log.v(TAG, "onAttach()");
126     }
127
128     /**
129      *
130      *
131      */
132     @Override
133     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
134     {
135         super.onCreateView(inflater, container, savedInstanceState);
136
137         Log.v(TAG, "onCreateView()");
138         if ((imageViewCreated)&&(myView != null))
139         {
140             // Viewを再利用。。。
141             Log.v(TAG, "onCreateView() : called again, so do nothing... : " + myView);
142             return (myView);
143         }
144
145         View view = inflater.inflate(R.layout.fragment_live_view, container, false);
146         myView = view;
147         imageViewCreated = true;
148         try
149         {
150
151             imageView = view.findViewById(R.id.cameraLiveImageView);
152             if (interfaceInjector != null)
153             {
154                 interfaceInjector.injectDisplay(imageView, imageView, this);
155             }
156             else
157             {
158                 Log.v(TAG, "interfaceInjector is NULL...");
159             }
160             FragmentActivity activity = this.getActivity();
161             Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
162             if ((onClickTouchListener == null)&&(activity != null))
163             {
164                 onClickTouchListener = new LiveViewClickTouchListener(activity, imageView, this, changeScene, interfaceProvider, this);
165             }
166             imageView.setOnClickListener(onClickTouchListener);
167             imageView.setOnTouchListener(onClickTouchListener);
168
169             // キーイベントを拾うことにする
170             view.setOnKeyListener(onClickTouchListener);
171             view.setFocusableInTouchMode(true);
172
173             setOnClickListener(view, R.id.hideControlPanelTextView);
174             setOnClickListener(view, R.id.showControlPanelTextView);
175             setOnClickListener(view, R.id.showKeyPanelImageView);
176             setOnClickListener(view, R.id.hideKeyPanelTextView);
177             setOnClickListener(view, R.id.shutter_button);
178             setOnClickListener(view, R.id.focusUnlockImageView);
179             setOnClickListener(view, R.id.show_images_button);
180             setOnClickListener(view, R.id.camera_power_off_button);
181             setOnClickListener(view, R.id.show_preference_button);
182             setOnClickListener(view, R.id.show_hide_grid_button);
183             setOnClickListener(view, R.id.zoom_in_button);
184             setOnClickListener(view, R.id.zoom_out_button);
185             setOnClickListener(view, R.id.specialButtonImageView);
186             setOnClickListener(view, R.id.fuji_x_hideKeyPanelTextView);
187
188             if (onPanelClickListener == null)
189             {
190                 onPanelClickListener = new LiveViewControlPanelClickListener(activity, interfaceProvider);
191             }
192             setPanelClickListener(view, R.id.takemodeTextView);
193             setPanelClickListener(view, R.id.shutterSpeedTextView);
194             setPanelClickListener(view, R.id.apertureValueTextView);
195             setPanelClickListener(view, R.id.exposureCompensationTextView);
196             setPanelClickListener(view, R.id.aeModeImageView);
197             setPanelClickListener(view, R.id.whiteBalanceTextView);
198             setPanelClickListener(view, R.id.isoSensitivityTextView);
199             setPanelClickListener(view, R.id.setEffectImageView);
200
201             if (onKeyPanelClickListener == null)
202             {
203                 onKeyPanelClickListener = new LiveViewKeyPanelClickListener(interfaceProvider, this, vibrator);
204             }
205             setKeyPanelClickListener(view, R.id.button_front_left);
206             setKeyPanelClickListener(view, R.id.button_front_right);
207             setKeyPanelClickListener(view, R.id.button_adjust_left);
208             setKeyPanelClickListener(view, R.id.button_adjust_enter);
209             setKeyPanelClickListener(view, R.id.button_adjust_right);
210             setKeyPanelClickListener(view, R.id.button_toggle_aeaf);
211             setKeyPanelClickListener(view, R.id.lever_ael_caf);
212             setKeyPanelClickListener(view, R.id.button_up);
213             setKeyPanelClickListener(view, R.id.button_left);
214             setKeyPanelClickListener(view, R.id.button_center_enter);
215             setKeyPanelClickListener(view, R.id.button_right);
216             setKeyPanelClickListener(view, R.id.button_down);
217             setKeyPanelClickListener(view, R.id.button_function_1);
218             setKeyPanelClickListener(view, R.id.button_function_2);
219             setKeyPanelClickListener(view, R.id.button_function_3);
220             setKeyPanelClickListener(view, R.id.button_plus);
221             setKeyPanelClickListener(view, R.id.button_minus);
222             setKeyPanelClickListener(view, R.id.button_playback);
223             setKeyPanelClickListener(view, R.id.button_acclock);
224             setKeyPanelClickListener(view, R.id.button_lcd_onoff);
225             setKeyPanelClickListener(view, R.id.button_highlight);
226
227             if (fujiXOnKeyPanelClickListener == null)
228             {
229                 fujiXOnKeyPanelClickListener = new LiveViewFujiXKeyPanelClickListener(interfaceProvider, vibrator);
230             }
231             setFujiXKeyPanelClickListener(view, R.id.button_fuji_x_sv_minus);
232             setFujiXKeyPanelClickListener(view, R.id.button_fuji_x_sv_plus);
233             setFujiXKeyPanelClickListener(view, R.id.button_fuji_x_tv_minus);
234             setFujiXKeyPanelClickListener(view, R.id.button_fuji_x_tv_plus);
235             setFujiXKeyPanelClickListener(view, R.id.button_fuji_x_xv_minus);
236             setFujiXKeyPanelClickListener(view, R.id.button_fuji_x_xv_plus);
237
238             connectStatus = view.findViewById(R.id.connect_disconnect_button);
239             if (connectStatus != null)
240             {
241                 connectStatus.setOnClickListener(onClickTouchListener);
242             }
243
244             View keyPanel = view.findViewById(R.id.showKeyPanelImageView);
245             ICameraButtonControl buttonControl = interfaceProvider.getButtonControl();
246             if (keyPanel != null)
247             {
248                 keyPanel.setVisibility((buttonControl == null) ? View.INVISIBLE : View.VISIBLE);
249                 keyPanel.invalidate();
250             }
251         }
252         catch (Exception e)
253         {
254             e.printStackTrace();
255         }
256
257         return (view);
258     }
259
260     private void setOnClickListener(View view, int id)
261     {
262         try
263         {
264             View button = view.findViewById(id);
265             if (button != null)
266             {
267                 button.setOnClickListener(onClickTouchListener);
268             }
269         }
270         catch (Exception e)
271         {
272             e.printStackTrace();
273         }
274     }
275
276     private void setPanelClickListener(View view, int id)
277     {
278         try
279         {
280             View button = view.findViewById(id);
281             if (button != null)
282             {
283                 button.setOnClickListener(onPanelClickListener);
284             }
285         }
286         catch (Exception e)
287         {
288             e.printStackTrace();
289         }
290     }
291
292     private void setKeyPanelClickListener(View view, int id)
293     {
294         try
295         {
296             View button = view.findViewById(id);
297             if (button != null)
298             {
299                 button.setOnClickListener(onKeyPanelClickListener);
300                 button.setOnLongClickListener(onKeyPanelClickListener);
301             }
302         }
303         catch (Exception e)
304         {
305             e.printStackTrace();
306         }
307     }
308
309     private void setFujiXKeyPanelClickListener(View view, int id)
310     {
311         try
312         {
313             View button = view.findViewById(id);
314             if (button != null)
315             {
316                 button.setOnClickListener(fujiXOnKeyPanelClickListener);
317                 button.setOnLongClickListener(fujiXOnKeyPanelClickListener);
318             }
319         }
320         catch (Exception e)
321         {
322             e.printStackTrace();
323         }
324     }
325
326     /**
327      *
328      */
329     private void prepare(IChangeScene sceneSelector, IInterfaceProvider interfaceProvider)
330     {
331         Log.v(TAG, "prepare()");
332
333         this.changeScene = sceneSelector;
334         this.interfaceProvider = interfaceProvider;
335         this.interfaceInjector = interfaceProvider.getDisplayInjector();
336         this.liveViewControl = interfaceProvider.getLiveViewControl();
337         this.zoomLensControl = interfaceProvider.getZoomLensControl();
338         this.cameraInformation = interfaceProvider.getCameraInformation();
339         this.statusWatcher = interfaceProvider.getCameraStatusWatcher();
340     }
341
342     /**
343      *  カメラとの接続状態の更新
344      *
345      */
346     @Override
347     public void updateConnectionStatus(final ICameraConnection.CameraConnectionStatus connectionStatus)
348     {
349         try
350         {
351             currentConnectionStatus = connectionStatus;
352             runOnUiThread(new Runnable()
353             {
354                 @Override
355                 public void run()
356                 {
357                     int id = R.drawable.ic_cloud_off_black_24dp;
358                     if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTING)
359                     {
360                         id = R.drawable.ic_cloud_queue_black_24dp;
361                     }
362                     else if  (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTED)
363                     {
364                         id = R.drawable.ic_cloud_done_black_24dp;
365                     }
366                     if (connectStatus != null)
367                     {
368                         connectStatus.setImageDrawable(ResourcesCompat.getDrawable(getResources(), id, null));
369                         connectStatus.invalidate();
370                     }
371                     if (imageView != null)
372                     {
373                         imageView.invalidate();
374                     }
375                 }
376             });
377
378         }
379         catch (Exception e)
380         {
381             e.printStackTrace();
382         }
383     }
384
385     /**
386      *  グリッドの表示・非表示の更新
387      *
388      */
389     @Override
390     public void updateGridIcon()
391     {
392         try
393         {
394             FragmentActivity activity = getActivity();
395             if (activity != null)
396             {
397                 if (showGrid == null) {
398                     showGrid = activity.findViewById(R.id.show_hide_grid_button);
399                 }
400                 activity.runOnUiThread(new Runnable() {
401                     @Override
402                     public void run()
403                     {
404                         int id = (imageView.isShowGrid()) ? R.drawable.ic_grid_off_black_24dp : R.drawable.ic_grid_on_black_24dp;
405                         if (showGrid != null)
406                         {
407                             showGrid.setImageDrawable(ResourcesCompat.getDrawable(getResources(), id, null));
408                             showGrid.invalidate();
409                         }
410                         imageView.invalidate();
411                     }
412                 });
413             }
414         }
415         catch (Exception e)
416         {
417             e.printStackTrace();
418         }
419     }
420
421     /**
422      *
423      *
424      */
425     @Override
426     public void changedFocusingMode()
427     {
428         try
429         {
430             if ((cameraInformation == null)||(manualFocus == null))
431             {
432                 return;
433             }
434             runOnUiThread(new Runnable()
435             {
436                 @Override
437                 public void run()
438                 {
439                     if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTED)
440                     {
441                         manualFocus.setSelected(cameraInformation.isManualFocus());
442                         manualFocus.invalidate();
443                     }
444                 }
445             });
446         }
447         catch (Exception e)
448         {
449             e.printStackTrace();
450         }
451     }
452
453     @Override
454     public void updateLiveViewScale(boolean isChangeScale)
455     {
456         try
457         {
458             Log.v(TAG, "updateLiveViewScale() : " + isChangeScale);
459
460             // ライブビューの倍率設定
461             liveViewControl.updateMagnifyingLiveViewScale(isChangeScale);
462
463             // ボタンの文字を更新する
464             float scale = liveViewControl.getMagnifyingLiveViewScale();
465             final String datavalue = "LV: " + scale;
466
467             // デジタルズームの倍率を表示する
468             float digitalZoom = liveViewControl.getDigitalZoomScale();
469             final String digitalValue = (digitalZoom > 1.0f) ? "D x" + digitalZoom : "";
470
471             // 更新自体は、UIスレッドで行う
472             runOnUiThread(new Runnable()
473             {
474                 @Override
475                 public void run()
476                 {
477                     changeLiveViewScale.setText(datavalue);
478                     changeLiveViewScale.postInvalidate();
479
480                     focalLengthArea.setText(digitalValue);
481                     focalLengthArea.postInvalidate();
482                 }
483             });
484         }
485         catch (Exception e)
486         {
487             e.printStackTrace();
488         }
489     }
490
491     /**
492      *
493      *
494      *
495      */
496     @Override
497     public void onStart()
498     {
499         super.onStart();
500         Log.v(TAG, "onStart()");
501     }
502
503     /**
504      *
505      *
506      */
507     @Override
508     public void onResume()
509     {
510         super.onResume();
511         Log.v(TAG, "onResume() Start");
512
513         AppCompatActivity activity = (AppCompatActivity)getActivity();
514         if (activity != null)
515         {
516             ActionBar bar = activity.getSupportActionBar();
517             if (bar != null)
518             {
519                 bar.hide();   // ActionBarの表示を消す
520             }
521         }
522
523         // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
524         ICameraRunMode changeRunModeExecutor = interfaceProvider.getCameraRunMode();
525         if ((changeRunModeExecutor != null)&&(!changeRunModeExecutor.isRecordingMode()))
526         {
527             // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。)
528             changeRunModeExecutor.changeRunMode(true, this);
529             Log.v(TAG, "onResume() End");
530             return;
531         }
532         prepareToStart();
533         Log.v(TAG, "onResume() End");
534     }
535
536     private void prepareToStart()
537     {
538         try
539         {
540             Context context = getContext();
541             if (context != null)
542             {
543                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
544
545                 // グリッド・フォーカスアシストの情報を戻す
546                 boolean showGrid = preferences.getBoolean(IPreferencePropertyAccessor.SHOW_GRID_STATUS, false);
547                 if ((imageView != null) && (imageView.isShowGrid() != showGrid)) {
548                     imageView.toggleShowGridFrame();
549                     imageView.postInvalidate();
550                 }
551             }
552             if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTED)
553             {
554                 startLiveView();
555             }
556         }
557         catch (Exception e)
558         {
559             e.printStackTrace();
560         }
561     }
562
563     @Override
564     public void onCompleted(boolean isRecording)
565     {
566         prepareToStart();
567     }
568
569     @Override
570     public void onErrorOccurred(boolean isRecording)
571     {
572         prepareToStart();
573     }
574
575     /**
576      *
577      *
578      */
579     @Override
580     public void onPause()
581     {
582         super.onPause();
583         Log.v(TAG, "onPause() Start");
584
585         // ライブビューとステータス監視の停止
586         try
587         {
588             liveViewControl.stopLiveView();
589             stopWatchStatus();
590         }
591         catch (Exception e)
592         {
593             e.printStackTrace();
594         }
595
596         Log.v(TAG, "onPause() End");
597     }
598
599     /**
600      *   表示エリアに文字を表示する
601      *
602      */
603     @Override
604     public void updateStatusView(String message)
605     {
606         messageValue = message;
607         runOnUiThread(new Runnable()
608         {
609             /**
610              * カメラの状態(ステータステキスト)を更新する
611              * (ステータステキストは、プライベート変数で保持して、書き換える)
612              */
613             @Override
614             public void run()
615             {
616                 if (statusArea != null)
617                 {
618                     statusArea.setText(messageValue);
619                     statusArea.invalidate();
620                 }
621             }
622         });
623     }
624
625     /**
626      *   ライブビューの開始
627      *
628      */
629     @Override
630     public void startLiveView()
631     {
632         Log.v(TAG, " LiveViewFragment::startLiveView() ");
633         ICameraConnection.CameraConnectionMethod connectionMethod = interfaceProvider.getCammeraConnectionMethod();
634         if (liveViewControl == null)
635         {
636             if (connectionMethod == ICameraConnection.CameraConnectionMethod.OPC)
637             {
638                 Log.v(TAG, "startLiveView() : liveViewControl is null.");
639                 return;
640             }
641             else
642             {
643                 // ダミー
644                 prepare(changeScene, interfaceProvider);
645             }
646         }
647         try
648         {
649             // ライブビューの開始
650             Context context = getContext();
651             boolean isCameraScreen = true;
652             if (context != null)
653             {
654                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
655                 liveViewControl.changeLiveViewSize(preferences.getString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE));
656                 isCameraScreen = preferences.getBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
657             }
658             ILiveViewListener lvListener = interfaceProvider.getLiveViewListener();
659             if (lvListener != null)
660             {
661                 lvListener.setCameraLiveImageView(imageView);
662             }
663             liveViewControl.startLiveView(isCameraScreen);   // false : ライブビューのみ、 true : カメラ画面をミラー
664
665             // ここでグリッドアイコンを更新する
666             updateGridIcon();
667
668             // ここでズームレンズ制御ができるか確認する
669             if ((zoomLensControl != null)&&(zoomLensControl.canZoom()))
670             {
671                 //Log.v(TAG, "CAN ZOOM LENS");
672                 updateZoomlensControl(true);
673             }
674             else
675             {
676                 //Log.v(TAG, "NO ZOOM LENS");
677                 updateZoomlensControl(false);
678             }
679
680
681             // ステータス監視も実施する
682             startWatchStatus();
683         }
684         catch (Exception e)
685         {
686             e.printStackTrace();
687         }
688     }
689
690     @Override
691     public void showFavoriteSettingDialog()
692     {
693         try
694         {
695             Log.v(TAG, "showFavoriteSettingDialog()");
696             LoadSaveMyCameraPropertyDialog dialog = LoadSaveMyCameraPropertyDialog.newInstance(new LoadSaveCameraProperties(getActivity(), interfaceProvider.getOlympusInterfaceProvider()));
697             dialog.show(getChildFragmentManager(), "favorite_dialog");
698         }
699         catch (Exception e)
700         {
701             e.printStackTrace();
702         }
703     }
704
705     /**
706      *
707      *
708      *
709      */
710     private void updateZoomlensControl(final boolean isVisible)
711     {
712         runOnUiThread(new Runnable() {
713             @Override
714             public void run() {
715                 // isVisibleがtrueなら、ズームレンズボタンを有効にする
716                 FragmentActivity activity = getActivity();
717                 if (activity != null)
718                 {
719                     try
720                     {
721                         View view1 = activity.findViewById(R.id.zoom_out_button);
722                         if (view1 != null)
723                         {
724                             view1.setVisibility((isVisible) ? View.VISIBLE : View.INVISIBLE);
725                             view1.invalidate();
726                         }
727
728                         View view2 = activity.findViewById(R.id.zoom_in_button);
729                         if (view2 != null)
730                         {
731                             view2.setVisibility((isVisible) ? View.VISIBLE : View.INVISIBLE);
732                             view2.invalidate();
733                         }
734                     }
735                     catch (Exception e)
736                     {
737                         e.printStackTrace();
738                     }
739                 }
740             }
741         });
742     }
743
744     /**
745      *
746      *
747      */
748     private void startWatchStatus()
749     {
750         if (statusWatcher != null)
751         {
752             statusWatcher.startStatusWatch(this);
753         }
754     }
755
756     /**
757      *
758      *
759      */
760     private void stopWatchStatus()
761     {
762         if (statusWatcher != null)
763         {
764             statusWatcher.stopStatusWatch();
765         }
766     }
767
768     /**
769      *
770      *
771      */
772     private void runOnUiThread(Runnable action)
773     {
774         FragmentActivity activity = getActivity();
775         if (activity == null)
776         {
777             return;
778         }
779         activity.runOnUiThread(action);
780     }
781
782     @Override
783     public void updatedTakeMode(final String mode)
784     {
785         try
786         {
787             final FragmentActivity activity = getActivity();
788             if (activity == null)
789             {
790                 return;
791             }
792             activity.runOnUiThread(new Runnable()
793             {
794                 @Override
795                 public void run()
796                 {
797                     TextView view = activity.findViewById(R.id.takemodeTextView);
798                     if (view != null)
799                     {
800                         view.setText(mode);
801                         view.invalidate();
802                     }
803                 }
804             });
805         }
806         catch (Exception e)
807         {
808             e.printStackTrace();
809         }
810     }
811
812     @Override
813     public void updatedShutterSpeed(final String tv)
814     {
815         try
816         {
817             final String shutterSpeed = tv.replace(".", "/");
818             final FragmentActivity activity = getActivity();
819             if (activity == null)
820             {
821                 return;
822             }
823             activity.runOnUiThread(new Runnable()
824             {
825                 @Override
826                 public void run()
827                 {
828                     TextView view = activity.findViewById(R.id.shutterSpeedTextView);
829                     if (view != null) {
830                         view.setText(shutterSpeed);
831                         view.invalidate();
832                     }
833                 }
834             });
835         }
836         catch (Exception e)
837         {
838             e.printStackTrace();
839         }
840
841     }
842
843     @Override
844     public void updatedAperture(final String av)
845     {
846         try
847         {
848             final String apertureValue = (av.length() > 1) ? ("F" + av) : "";
849             final FragmentActivity activity = getActivity();
850             if (activity == null)
851             {
852                 return;
853             }
854             activity.runOnUiThread(new Runnable()
855             {
856                 @Override
857                 public void run()
858                 {
859                     TextView view = activity.findViewById(R.id.apertureValueTextView);
860                     if (view != null)
861                     {
862                         view.setText(apertureValue);
863                         view.invalidate();
864                     }
865                 }
866             });
867         }
868         catch (Exception e)
869         {
870             e.printStackTrace();
871         }
872     }
873
874     @Override
875     public void updatedExposureCompensation(final String xv)
876     {
877         try
878         {
879             final FragmentActivity activity = getActivity();
880             if (activity == null)
881             {
882                 return;
883             }
884             activity.runOnUiThread(new Runnable()
885             {
886                 @Override
887                 public void run()
888                 {
889                     TextView view = activity.findViewById(R.id.exposureCompensationTextView);
890                     if (view != null)
891                     {
892                         view.setText(xv);
893                         view.invalidate();
894                     }
895                 }
896             });
897         }
898         catch (Exception e)
899         {
900             e.printStackTrace();
901         }
902     }
903
904     @Override
905     public void updatedMeteringMode(final String meteringMode)
906     {
907         try
908         {
909             Log.v(TAG, "updatedMeteringMode() : " + meteringMode);
910             final FragmentActivity activity = getActivity();
911             if ((activity == null)||(meteringMode == null))
912             {
913                 return;
914             }
915
916             int iconId = R.drawable.ic_crop_free_black_24dp;
917             switch (meteringMode)
918             {
919                 case ICameraStatus.AE_STATUS_MULTI:
920                 case ICameraStatus.AE_STATUS_ESP:
921                     iconId = R.drawable.ic_crop_free_black_24dp;
922                     break;
923                 case ICameraStatus.AE_STATUS_CENTER:
924                 case ICameraStatus.AE_STATUS_CENTER2:
925                     iconId = R.drawable.ic_center_focus_weak_black_24dp;
926                     break;
927                 case ICameraStatus.AE_STATUS_SPOT:
928                 case ICameraStatus.AE_STATUS_PINPOINT:
929                     iconId = R.drawable.ic_filter_center_focus_black_24dp;
930                     break;
931             }
932             final int id = iconId;
933             activity.runOnUiThread(new Runnable()
934             {
935                 @Override
936                 public void run()
937                 {
938                     ImageView view = activity.findViewById(R.id.aeModeImageView);
939                     if (view != null)
940                     {
941                         view.setImageDrawable(ResourcesCompat.getDrawable(getResources(), id, null));
942                         view.invalidate();
943                     }
944                 }
945             });
946         }
947         catch (Exception e)
948         {
949             e.printStackTrace();
950         }
951     }
952
953     @Override
954     public void updatedWBMode(final String wbMode)
955     {
956         // とりあえず何もしない... 選択肢は以下 (Ricohの場合...)
957         // auto, multiAuto, daylight, shade, cloud, tungsten, warmWhiteFluorescent, daylightFluorescent, dayWhiteFluorescent, coolWhiteFluorescent, incandescent,manual1, cte, custom
958     }
959
960     /**
961      *   残りバッテリー状態をアイコンで示す
962      *
963      */
964     @Override
965     public void updateRemainBattery(final int percentage)
966     {
967         try
968         {
969             final FragmentActivity activity = getActivity();
970             if (activity == null)
971             {
972                 return;
973             }
974             int iconId;
975             if (percentage < 20)
976             {
977                 iconId = R.drawable.ic_battery_alert_black_24dp;
978             }
979             else if (percentage < 60)
980             {
981                 iconId = R.drawable.ic_battery_20_black_24dp;
982             }
983             else if (percentage < 80)
984             {
985                 iconId = R.drawable.ic_battery_60_black_24dp;
986             }
987             else
988             {
989                 iconId = R.drawable.ic_battery_full_black_24dp;
990             }
991             final int id = iconId;
992             activity.runOnUiThread(new Runnable()
993             {
994                 @Override
995                 public void run()
996                 {
997                     ImageView view = activity.findViewById(R.id.currentBatteryImageView);
998                     if (view != null)
999                     {
1000                         Drawable target = ResourcesCompat.getDrawable(getResources(), id, null);
1001                         if (target != null)
1002                         {
1003                             if (percentage <= 20)
1004                             {
1005                                 DrawableCompat.setTint(target, Color.RED);
1006                             } else if (percentage <= 40)
1007                             {
1008                                 DrawableCompat.setTint(target, Color.YELLOW);
1009                             }
1010                             view.setImageDrawable(target);
1011                             view.invalidate();
1012                         }
1013                     }
1014                 }
1015             });
1016         }
1017         catch (Exception e)
1018         {
1019             e.printStackTrace();
1020         }
1021     }
1022
1023     @Override
1024     public void updateFocusedStatus(final boolean focused, final boolean focusLocked)
1025     {
1026         final FragmentActivity activity = getActivity();
1027         try
1028         {
1029             if (activity != null)
1030             {
1031                 activity.runOnUiThread(new Runnable() {
1032                     @Override
1033                     public void run() {
1034                         try
1035                         {
1036                             ImageView view = activity.findViewById(R.id.focusUnlockImageView);
1037                             if (focused) {
1038                                 Drawable icon = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_center_focus_strong_black_24dp, null);
1039                                 if (icon != null) {
1040                                     DrawableCompat.setTint(icon, Color.GREEN);
1041                                     view.setImageDrawable(icon);
1042                                 }
1043                             } else {
1044                                 Drawable icon = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_focus_free_black_24dp, null);
1045                                 if (icon != null) {
1046                                     int color = Color.BLACK;
1047                                     if (focusLocked) {
1048                                         color = Color.RED;
1049                                     }
1050                                     DrawableCompat.setTint(icon, color);
1051                                     view.setImageDrawable(icon);
1052                                 }
1053                             }
1054                             view.invalidate();
1055                         }
1056                         catch (Exception e)
1057                         {
1058                             e.printStackTrace();
1059                         }
1060                     }
1061                 });
1062             }
1063         }
1064         catch (Exception e)
1065         {
1066             e.printStackTrace();
1067         }
1068     }
1069
1070     @Override
1071     public void updateIsoSensitivity(String sv)
1072     {
1073         Log.v(TAG, "updateIsoSensitivity : " + sv);
1074     }
1075
1076     @Override
1077     public void updateWarning(String warning)
1078     {
1079         Log.v(TAG, "updateWarning : " + warning);
1080     }
1081
1082     @Override
1083     public void updateStorageStatus(String status)
1084     {
1085         Log.v(TAG, "updateStorageStatus : " + status);
1086     }
1087
1088     @Override
1089     public void updateToggleButton(boolean isOn)
1090     {
1091         try
1092         {
1093             FragmentActivity activity = getActivity();
1094             if (activity != null)
1095             {
1096                 ImageView imageView = activity.findViewById(R.id.button_toggle_aeaf);
1097                 if (isOn)
1098                 {
1099                     imageView.setImageDrawable(ContextCompat.getDrawable(activity,R.drawable.ic_radio_button_checked_black_24dp));
1100                 }
1101                 else
1102                 {
1103                     imageView.setImageDrawable(ContextCompat.getDrawable(activity,R.drawable.ic_radio_button_unchecked_black_24dp));
1104                 }
1105                 imageView.invalidate();
1106             }
1107         }
1108         catch (Exception e)
1109         {
1110             e.printStackTrace();
1111         }
1112     }
1113
1114     @Override
1115     public void updateLcdOnOff(boolean isOn)
1116     {
1117         Log.v(TAG, "updateLcdOnOff() " + isOn);
1118     }
1119
1120     @Override
1121     public void updateAFLlever(boolean isCaf)
1122     {
1123         try
1124         {
1125             FragmentActivity activity = getActivity();
1126             if (activity != null)
1127             {
1128                 TextView textView = activity.findViewById(R.id.lever_ael_caf);
1129                 if (isCaf)
1130                 {
1131                     textView.setText(getString(R.string.label_c_af));
1132                 }
1133                 else
1134                 {
1135                     textView.setText(getString(R.string.label_aelock));
1136                 }
1137                 textView.invalidate();
1138             }
1139         }
1140         catch (Exception e)
1141         {
1142             e.printStackTrace();
1143         }
1144     }
1145
1146     public boolean handleKeyDown(int keyCode, KeyEvent event)
1147     {
1148         return (onClickTouchListener.onKey(null, keyCode, event));
1149     }
1150 }