1 package net.osdn.gokigen.gr2control.liveview;
3 import android.app.Activity;
4 import android.content.Context;
5 import android.content.SharedPreferences;
6 import android.os.Bundle;
7 import android.os.Vibrator;
8 import android.support.annotation.NonNull;
9 import android.support.v4.app.Fragment;
10 import android.support.v4.content.res.ResourcesCompat;
11 import android.support.v7.app.ActionBar;
12 import android.support.v7.app.AppCompatActivity;
13 import android.support.v7.preference.PreferenceManager;
14 import android.util.Log;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.Button;
19 import android.widget.ImageButton;
20 import android.widget.ImageView;
21 import android.widget.TextView;
23 import net.osdn.gokigen.gr2control.R;
24 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
25 import net.osdn.gokigen.gr2control.camera.ICameraInformation;
26 import net.osdn.gokigen.gr2control.camera.ICameraRunMode;
27 import net.osdn.gokigen.gr2control.camera.ICameraStatusWatcher;
28 import net.osdn.gokigen.gr2control.camera.IDisplayInjector;
29 import net.osdn.gokigen.gr2control.camera.IFocusingModeNotify;
30 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
31 import net.osdn.gokigen.gr2control.camera.ILiveViewControl;
32 import net.osdn.gokigen.gr2control.camera.IZoomLensControl;
33 import net.osdn.gokigen.gr2control.liveview.liveviewlistener.ILiveViewListener;
34 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
35 import net.osdn.gokigen.gr2control.scene.IChangeScene;
37 import static android.content.Context.VIBRATOR_SERVICE;
43 public class LiveViewFragment extends Fragment implements IStatusViewDrawer, IFocusingModeNotify, IFavoriteSettingDialogKicker, ICameraStatusUpdateNotify
45 private final String TAG = this.toString();
47 private ILiveViewControl liveViewControl = null;
48 //private IZoomLensControl zoomLensControl = null;
49 private IInterfaceProvider interfaceProvider = null;
50 private IDisplayInjector interfaceInjector = null;
51 //private OlympusCameraLiveViewListenerImpl liveViewListener = null;
52 private IChangeScene changeScene = null;
53 private ICameraInformation cameraInformation = null;
54 private ICameraStatusWatcher statusWatcher = null;
55 private LiveViewClickTouchListener onClickTouchListener = null;
56 private LiveViewControlPanelClickListener onPanelClickListener = null;
57 private LiveViewKeyPanelClickListener onKeyPanelClickListener = null;
59 private TextView statusArea = null;
60 private TextView focalLengthArea = null;
61 private CameraLiveImageView imageView = null;
63 private ImageView manualFocus = null;
64 private ImageButton showGrid = null;
65 private ImageView connectStatus = null;
66 private Button changeLiveViewScale = null;
68 private boolean imageViewCreated = false;
69 private View myView = null;
70 private String messageValue = "";
72 private ICameraConnection.CameraConnectionStatus currentConnectionStatus = ICameraConnection.CameraConnectionStatus.UNKNOWN;
74 public static LiveViewFragment newInstance(IChangeScene sceneSelector, @NonNull IInterfaceProvider provider)
76 LiveViewFragment instance = new LiveViewFragment();
77 instance.prepare(sceneSelector, provider);
79 // パラメータはBundleにまとめておく
80 Bundle arguments = new Bundle();
81 //arguments.putString("title", title);
82 //arguments.putString("message", message);
83 instance.setArguments(arguments);
93 public void onCreate(Bundle savedInstanceState)
95 super.onCreate(savedInstanceState);
96 Log.v(TAG, "onCreate()");
98 if (liveViewListener == null)
100 liveViewListener = new OlympusCameraLiveViewListenerImpl();
110 public void onAttach(Context context)
112 super.onAttach(context);
113 Log.v(TAG, "onAttach()");
121 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
123 super.onCreateView(inflater, container, savedInstanceState);
125 Log.v(TAG, "onCreateView()");
126 if ((imageViewCreated)&&(myView != null))
129 Log.v(TAG, "onCreateView() : called again, so do nothing... : " + myView);
133 View view = inflater.inflate(R.layout.fragment_live_view, container, false);
135 imageViewCreated = true;
139 imageView = view.findViewById(R.id.cameraLiveImageView);
140 if (interfaceInjector != null)
142 interfaceInjector.injectDisplay(imageView, imageView, this);
146 Log.v(TAG, "interfaceInjector is NULL...");
148 Activity activity = this.getActivity();
149 Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
150 if ((onClickTouchListener == null)&&(activity != null))
152 onClickTouchListener = new LiveViewClickTouchListener(activity, imageView, this, changeScene, interfaceProvider, this);
154 imageView.setOnClickListener(onClickTouchListener);
155 imageView.setOnTouchListener(onClickTouchListener);
157 setOnClickListener(view, R.id.hideControlPanelTextView);
158 setOnClickListener(view, R.id.showControlPanelTextView);
159 setOnClickListener(view, R.id.showKeyPanelImageView);
160 setOnClickListener(view, R.id.hideKeyPanelTextView);
161 setOnClickListener(view, R.id.shutter_button);
162 setOnClickListener(view, R.id.focusUnlockImageView);
163 setOnClickListener(view, R.id.show_images_button);
164 setOnClickListener(view, R.id.camera_power_off_button);
165 setOnClickListener(view, R.id.show_preference_button);
167 if (onPanelClickListener == null)
169 onPanelClickListener = new LiveViewControlPanelClickListener(activity, interfaceProvider);
171 setPanelClickListener(view, R.id.takemodeTextView);
172 setPanelClickListener(view, R.id.shutterSpeedTextView);
173 setPanelClickListener(view, R.id.apertureValueTextView);
174 setPanelClickListener(view, R.id.exposureCompensationTextView);
175 setPanelClickListener(view, R.id.aeModeTextView);
176 setPanelClickListener(view, R.id.whiteBalanceImageView);
177 setPanelClickListener(view, R.id.setEffectImageView);
179 if (onKeyPanelClickListener == null)
181 onKeyPanelClickListener = new LiveViewKeyPanelClickListener(interfaceProvider, vibrator);
183 setKeyPanelClickListener(view, R.id.button_front_left);
184 setKeyPanelClickListener(view, R.id.button_front_right);
185 setKeyPanelClickListener(view, R.id.button_adjust_left);
186 setKeyPanelClickListener(view, R.id.button_adjust_enter);
187 setKeyPanelClickListener(view, R.id.button_adjust_right);
188 setKeyPanelClickListener(view, R.id.button_toggle_aeaf);
189 setKeyPanelClickListener(view, R.id.lever_ael_caf);
190 setKeyPanelClickListener(view, R.id.button_up);
191 setKeyPanelClickListener(view, R.id.button_left);
192 setKeyPanelClickListener(view, R.id.button_center_enter);
193 setKeyPanelClickListener(view, R.id.button_right);
194 setKeyPanelClickListener(view, R.id.button_down);
195 setKeyPanelClickListener(view, R.id.button_function_1);
196 setKeyPanelClickListener(view, R.id.button_function_2);
197 setKeyPanelClickListener(view, R.id.button_function_3);
198 setKeyPanelClickListener(view, R.id.button_plus);
199 setKeyPanelClickListener(view, R.id.button_minus);
200 setKeyPanelClickListener(view, R.id.button_playback);
203 view.findViewById(R.id.show_preference_button).setOnClickListener(onClickTouchListener);
204 view.findViewById(R.id.camera_property_settings_button).setOnClickListener(onClickTouchListener);
205 view.findViewById(R.id.shutter_button).setOnClickListener(onClickTouchListener);
206 view.findViewById(R.id.btn_zoomin).setOnClickListener(onClickTouchListener);
207 view.findViewById(R.id.btn_zoomout).setOnClickListener(onClickTouchListener);
209 manualFocus = view.findViewById(R.id.focusing_button);
210 changeLiveViewScale = view.findViewById(R.id.live_view_scale_button);
212 ICameraConnection.CameraConnectionMethod connectionMethod = interfaceProvider.getCammeraConnectionMethod();
214 if (connectionMethod == ICameraConnection.CameraConnectionMethod.OPC)
216 view.findViewById(R.id.show_favorite_settings_button).setOnClickListener(onClickTouchListener);
220 // お気に入りボタン(とMFボタン)は、SONYモード, RICOH GR2モードのときには表示しない
221 final View favoriteButton = view.findViewById(R.id.show_favorite_settings_button);
222 final View propertyButton = view.findViewById(R.id.camera_property_settings_button);
223 if ((favoriteButton != null)&&(manualFocus != null))
225 runOnUiThread(new Runnable()
230 favoriteButton.setVisibility(View.INVISIBLE);
231 if (manualFocus != null)
233 manualFocus.setVisibility(View.INVISIBLE);
235 propertyButton.setVisibility(View.INVISIBLE);
239 if (connectionMethod == ICameraConnection.CameraConnectionMethod.SONY)
241 if (changeLiveViewScale != null)
243 changeLiveViewScale.setVisibility(View.INVISIBLE);
246 else // if (connectionMethod == ICameraConnection.CameraConnectionMethod.RICOH_GR2)
248 if (changeLiveViewScale != null)
250 changeLiveViewScale.setVisibility(View.VISIBLE);
255 if (manualFocus != null)
257 manualFocus.setOnClickListener(onClickTouchListener);
259 changedFocusingMode();
261 if (changeLiveViewScale != null)
263 changeLiveViewScale.setOnClickListener(onClickTouchListener);
266 showGrid = view.findViewById(R.id.show_hide_grid_button);
267 showGrid.setOnClickListener(onClickTouchListener);
270 updateConnectionStatus(ICameraConnection.CameraConnectionStatus.UNKNOWN);
272 statusArea = view.findViewById(R.id.informationMessageTextView);
273 focalLengthArea = view.findViewById(R.id.focal_length_with_digital_zoom_view);
275 connectStatus = view.findViewById(R.id.connect_disconnect_button);
276 if (connectStatus != null)
278 connectStatus.setOnClickListener(onClickTouchListener);
289 private void setOnClickListener(View view, int id)
293 View button = view.findViewById(id);
296 button.setOnClickListener(onClickTouchListener);
305 private void setPanelClickListener(View view, int id)
309 View button = view.findViewById(id);
312 button.setOnClickListener(onPanelClickListener);
321 private void setKeyPanelClickListener(View view, int id)
325 View button = view.findViewById(id);
328 button.setOnClickListener(onKeyPanelClickListener);
340 private void prepare(IChangeScene sceneSelector, IInterfaceProvider interfaceProvider)
342 Log.v(TAG, "prepare()");
344 this.changeScene = sceneSelector;
345 this.interfaceProvider = interfaceProvider;
346 this.interfaceInjector = interfaceProvider.getDisplayInjector();
347 this.liveViewControl = interfaceProvider.getLiveViewControl();
348 //this.zoomLensControl = interfaceProvider.getZoomLensControl();
349 this.cameraInformation = interfaceProvider.getCameraInformation();
350 this.statusWatcher = interfaceProvider.getCameraStatusWatcher();
358 public void updateConnectionStatus(final ICameraConnection.CameraConnectionStatus connectionStatus)
362 currentConnectionStatus = connectionStatus;
363 runOnUiThread(new Runnable()
368 int id = R.drawable.ic_cloud_off_black_24dp;
369 if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTING)
371 id = R.drawable.ic_cloud_queue_black_24dp;
373 else if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTED)
375 id = R.drawable.ic_cloud_done_black_24dp;
377 if (connectStatus != null)
379 connectStatus.setImageDrawable(ResourcesCompat.getDrawable(getResources(), id, null));
380 connectStatus.invalidate();
382 if (imageView != null)
384 imageView.invalidate();
401 public void updateGridIcon()
405 int id = (imageView.isShowGrid()) ? R.drawable.ic_grid_off_black_24dp : R.drawable.ic_grid_on_black_24dp;
406 showGrid.setImageDrawable(ResourcesCompat.getDrawable(getResources(), id, null));
407 showGrid.invalidate();
408 imageView.invalidate();
421 public void changedFocusingMode()
425 if ((cameraInformation == null)||(manualFocus == null))
429 runOnUiThread(new Runnable()
434 if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTED)
436 manualFocus.setSelected(cameraInformation.isManualFocus());
437 manualFocus.invalidate();
449 public void updateLiveViewScale(boolean isChangeScale)
453 Log.v(TAG, "updateLiveViewScale() : " + isChangeScale);
456 liveViewControl.updateMagnifyingLiveViewScale(isChangeScale);
459 float scale = liveViewControl.getMagnifyingLiveViewScale();
460 final String datavalue = "LV: " + scale;
463 float digitalZoom = liveViewControl.getDigitalZoomScale();
464 final String digitalValue = (digitalZoom > 1.0f) ? "D x" + digitalZoom : "";
467 runOnUiThread(new Runnable()
472 changeLiveViewScale.setText(datavalue);
473 changeLiveViewScale.postInvalidate();
475 focalLengthArea.setText(digitalValue);
476 focalLengthArea.postInvalidate();
492 public void onStart()
495 Log.v(TAG, "onStart()");
503 public void onResume()
506 Log.v(TAG, "onResume() Start");
508 AppCompatActivity activity = (AppCompatActivity)getActivity();
509 if (activity != null)
511 ActionBar bar = activity.getSupportActionBar();
514 bar.hide(); // ActionBarの表示を消す
518 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
519 ICameraRunMode changeRunModeExecutor = interfaceProvider.getCameraRunMode();
520 if ((changeRunModeExecutor != null)&&(!changeRunModeExecutor.isRecordingMode()))
522 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。)
523 changeRunModeExecutor.changeRunMode(true);
528 camera.setCameraStatusListener(statusListener);
530 // 画面下部の表示エリアの用途を切り替える
531 setupLowerDisplayArea();
536 Context context = getContext();
539 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
541 // グリッド・フォーカスアシストの情報を戻す
542 boolean showGrid = preferences.getBoolean(IPreferencePropertyAccessor.SHOW_GRID_STATUS, false);
543 if ((imageView != null) && (imageView.isShowGrid() != showGrid)) {
544 imageView.toggleShowGridFrame();
545 imageView.postInvalidate();
548 if (currentConnectionStatus == ICameraConnection.CameraConnectionStatus.CONNECTED)
557 Log.v(TAG, "onResume() End");
565 public void onPause()
568 Log.v(TAG, "onPause() Start");
573 liveViewControl.stopLiveView();
581 Log.v(TAG, "onPause() End");
589 public void updateStatusView(String message)
591 messageValue = message;
592 runOnUiThread(new Runnable()
595 * カメラの状態(ステータステキスト)を更新する
596 * (ステータステキストは、プライベート変数で保持して、書き換える)
601 if (statusArea != null)
603 statusArea.setText(messageValue);
604 statusArea.invalidate();
615 public void startLiveView()
617 ICameraConnection.CameraConnectionMethod connectionMethod = interfaceProvider.getCammeraConnectionMethod();
618 if (liveViewControl == null)
620 if (connectionMethod == ICameraConnection.CameraConnectionMethod.OPC)
622 Log.v(TAG, "startLiveView() : liveViewControl is null.");
628 prepare(changeScene, interfaceProvider);
634 Context context = getContext();
635 boolean isCameraScreen = true;
638 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
639 liveViewControl.changeLiveViewSize(preferences.getString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE));
640 isCameraScreen = preferences.getBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
642 ILiveViewListener lvListener = interfaceProvider.getLiveViewListener();
643 if (lvListener != null)
645 lvListener.setCameraLiveImageView(imageView);
647 liveViewControl.startLiveView(isCameraScreen); // false : ライブビューのみ、 true : カメラ画面をミラー
659 public void showFavoriteSettingDialog()
663 Log.v(TAG, "showFavoriteSettingDialog()");
665 LoadSaveMyCameraPropertyDialog dialog = new LoadSaveMyCameraPropertyDialog();
666 dialog.setTargetFragment(this, COMMAND_MY_PROPERTY);
667 dialog.setPropertyOperationsHolder(new LoadSaveCameraProperties(getActivity(), interfaceProvider.getOlympusInterface()));
668 FragmentManager manager = getFragmentManager();
671 dialog.show(manager, "my_dialog");
685 private void startWatchStatus()
687 if (statusWatcher != null)
689 statusWatcher.startStatusWatch(this);
697 private void stopWatchStatus()
699 if (statusWatcher != null)
701 statusWatcher.stoptStatusWatch();
709 private void runOnUiThread(Runnable action)
711 Activity activity = getActivity();
712 if (activity == null)
716 activity.runOnUiThread(action);
720 public void updatedTakeMode(final String mode)
724 final Activity activity = getActivity();
725 if (activity == null)
729 activity.runOnUiThread(new Runnable()
734 TextView view = activity.findViewById(R.id.takemodeTextView);
750 public void updatedShutterSpeed(final String tv)
754 final String shutterSpeed = tv.replace(".", "/");
755 final Activity activity = getActivity();
756 if (activity == null)
760 activity.runOnUiThread(new Runnable()
765 TextView view = activity.findViewById(R.id.shutterSpeedTextView);
767 view.setText(shutterSpeed);
781 public void updatedAperture(final String av)
785 final String apertureValue = (av.length() > 1) ? ("F" + av) : "";
786 final Activity activity = getActivity();
787 if (activity == null)
791 activity.runOnUiThread(new Runnable()
796 TextView view = activity.findViewById(R.id.apertureValueTextView);
799 view.setText(apertureValue);
812 public void updatedExposureCompensation(final String xv)
816 final Activity activity = getActivity();
817 if (activity == null)
821 activity.runOnUiThread(new Runnable()
826 TextView view = activity.findViewById(R.id.exposureCompensationTextView);
842 public void updatedMeteringMode(final String meteringMode)
846 final Activity activity = getActivity();
847 if (activity == null)
851 activity.runOnUiThread(new Runnable()
856 TextView view = activity.findViewById(R.id.aeModeTextView);
859 view.setText(meteringMode);
872 public void updatedWBMode(final String wbMode)
874 // とりあえず何もしない... 選択肢は以下
875 // auto, multiAuto, daylight, shade, cloud, tungsten, warmWhiteFluorescent, daylightFluorescent, dayWhiteFluorescent, coolWhiteFluorescent, incandescent,manual1, cte, custom
883 public void updateRemainBattery(int percentage)
887 final Activity activity = getActivity();
888 if (activity == null)
895 iconId = R.drawable.ic_battery_alert_black_24dp;
897 else if (percentage < 60)
899 iconId = R.drawable.ic_battery_20_black_24dp;
901 else if (percentage < 80)
903 iconId = R.drawable.ic_battery_60_black_24dp;
907 iconId = R.drawable.ic_battery_full_black_24dp;
909 final int id = iconId;
910 activity.runOnUiThread(new Runnable()
915 ImageView view = activity.findViewById(R.id.currentBatteryImageView);
918 view.setImageDrawable(ResourcesCompat.getDrawable(getResources(), id, null));