1 package net.osdn.gokigen.pkremote.preference.olympus;
4 import android.app.ProgressDialog;
5 import android.content.Context;
6 import android.content.SharedPreferences;
7 import android.os.Bundle;
8 import android.util.Log;
10 import net.osdn.gokigen.pkremote.R;
11 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
12 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraRunMode;
13 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraHardwareStatus;
14 import net.osdn.gokigen.pkremote.camera.vendor.olympus.operation.CameraPowerOff;
15 import net.osdn.gokigen.pkremote.camera.vendor.olympus.wrapper.property.IOlyCameraProperty;
16 import net.osdn.gokigen.pkremote.camera.vendor.olympus.wrapper.property.IOlyCameraPropertyProvider;
17 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
18 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
19 import net.osdn.gokigen.pkremote.scene.IChangeScene;
21 import java.util.HashMap;
22 import java.util.Locale;
25 import androidx.annotation.NonNull;
26 import androidx.appcompat.app.AppCompatActivity;
27 import androidx.fragment.app.FragmentActivity;
28 import androidx.preference.CheckBoxPreference;
29 import androidx.preference.ListPreference;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceFragmentCompat;
32 import androidx.preference.PreferenceManager;
33 import jp.co.olympus.camerakit.OLYCamera;
39 public class OpcPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, OpcPreferenceSynchronizer.IPropertySynchronizeCallback
41 private final String TAG = toString();
42 private IOlyCameraPropertyProvider propertyInterface = null;
43 private ICameraHardwareStatus hardwareStatusInterface = null;
44 private ICameraRunMode changeRunModeExecutor = null;
45 private CameraPowerOff powerOffController = null;
46 private LogCatViewer logCatViewer = null;
47 private SharedPreferences preferences = null;
48 private ProgressDialog busyDialog = null;
49 private OpcPreferenceSynchronizer opcPreferenceSynchronizer = null;
52 public static OpcPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
54 OpcPreferenceFragment instance = new OpcPreferenceFragment();
55 instance.setInterface(context, factory, changeScene);
57 // パラメータはBundleにまとめておく
58 Bundle arguments = new Bundle();
59 //arguments.putString("title", title);
60 //arguments.putString("message", message);
61 instance.setArguments(arguments);
70 private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
72 Log.v(TAG, "setInterface()");
73 this.propertyInterface = factory.getOlympusInterfaceProvider().getCameraPropertyProvider();
74 this.changeRunModeExecutor = factory.getCameraRunMode();
75 hardwareStatusInterface = factory.getHardwareStatus();
76 powerOffController = new CameraPowerOff(context, changeScene);
77 powerOffController.prepare();
78 logCatViewer = new LogCatViewer(changeScene);
79 logCatViewer.prepare();
87 public void onAttach(Context activity)
89 super.onAttach(activity);
90 Log.v(TAG, "onAttach()");
93 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
94 if (opcPreferenceSynchronizer == null)
96 opcPreferenceSynchronizer = new OpcPreferenceSynchronizer(this.propertyInterface, preferences, this);
100 initializePreferences();
102 preferences.registerOnSharedPreferenceChangeListener(this);
108 private void initializePreferences()
110 Map<String, ?> items = preferences.getAll();
111 SharedPreferences.Editor editor = preferences.edit();
113 if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
115 editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
117 if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
119 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
121 if (!items.containsKey(IPreferencePropertyAccessor.RAW))
123 editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
125 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
127 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
129 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
131 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
133 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
135 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
137 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
138 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
140 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
141 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
143 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
144 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
146 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
147 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
149 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
150 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
152 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
153 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
155 if (!items.containsKey(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE)) {
156 editor.putString(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE, IPreferencePropertyAccessor.SMALL_PICTURE_SIZE_DEFAULT_VALUE);
158 if (!items.containsKey(IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE)) {
159 editor.putString(IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE, IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE_DEFAULT_VALUE);
169 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
171 Log.v(TAG, "onCreatePreferences()");
173 //super.onCreate(savedInstanceState);
174 addPreferencesFromResource(R.xml.preferences_opc);
177 final HashMap<String, String> sizeTable = new HashMap<>();
178 sizeTable.put("QVGA", "(320x240)");
179 sizeTable.put("VGA", "(640x480)");
180 sizeTable.put("SVGA", "(800x600)");
181 sizeTable.put("XGA", "(1024x768)");
182 sizeTable.put("QUAD_VGA", "(1280x960)");
184 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
185 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
187 public boolean onPreferenceChange(Preference preference, Object newValue) {
188 String key = (String) newValue;
189 preference.setSummary(newValue + " " + sizeTable.get(key));
193 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
195 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
196 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
198 public boolean onPreferenceChange(Preference preference, Object newValue) {
199 preference.setSummary(newValue + " ");
203 connectionMethod.setSummary(connectionMethod.getValue() + " ");
205 ListPreference smallPictureSize = (ListPreference) findPreference(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE);
206 smallPictureSize.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
208 public boolean onPreferenceChange(Preference preference, Object newValue) {
209 preference.setSummary(newValue + " ");
213 smallPictureSize.setSummary(smallPictureSize.getValue() + " ");
215 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
216 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
220 * ハードウェアのサマリ情報を取得し設定する
222 private void setHardwareSummary()
225 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
228 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
232 float minLength = hardwareStatusInterface.getMinimumFocalLength();
233 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
234 float actualLength = hardwareStatusInterface.getActualFocalLength();
235 if (minLength == maxLength)
237 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
241 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
243 findPreference("focal_length").setSummary(focalLength);
248 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
249 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
252 Log.v(TAG, "- - - - - - - - - -");
253 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
255 String value = (String) entry.getValue();
256 Log.v(TAG, entry.getKey() + " : " + value);
258 Log.v(TAG, "- - - - - - - - - -");
262 findPreference("camera_version").setSummary("Unknown");
271 private void setCameraProperty(String name, String value)
275 String propertyValue = "<" + name + "/" + value + ">";
276 Log.v(TAG, "setCameraProperty() : " + propertyValue);
277 propertyInterface.setCameraPropertyValue(name, propertyValue);
290 public void onResume()
293 Log.v(TAG, "onResume() Start");
295 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
296 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
298 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
299 changeRunModeExecutor.changeRunMode(true);
301 synchronizeCameraProperties(true);
302 Log.v(TAG, "onResume() End");
311 public void onPause()
314 Log.v(TAG, "onPause() Start");
316 // Preference変更のリスナを解除
317 preferences.unregisterOnSharedPreferenceChangeListener(this);
319 Log.v(TAG, "onPause() End");
323 * カメラプロパティとPreferenceとの同期処理を実行
325 private void synchronizeCameraProperties(boolean isPropertyLoad)
328 busyDialog = new ProgressDialog(getActivity());
329 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
330 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
331 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
332 busyDialog.setCancelable(false);
335 // データ読み込み処理(別スレッドで実行)
338 new Thread(opcPreferenceSynchronizer).start();
343 * Preferenceが更新された時に呼び出される処理
345 * @param sharedPreferences sharedPreferences
349 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
351 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
352 String propertyValue;
358 case IPreferencePropertyAccessor.RAW:
359 value = preferences.getBoolean(key, true);
360 setBooleanPreference(key, key, value);
361 propertyValue = (value) ? "ON" : "OFF";
362 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
365 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
366 value = preferences.getBoolean(key, true);
367 Log.v(TAG, " " + key + " , " + value);
370 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
371 value = preferences.getBoolean(key, true);
372 Log.v(TAG, " " + key + " , " + value);
375 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
376 value = preferences.getBoolean(key, false);
377 Log.v(TAG, " " + key + " , " + value);
380 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
381 value = preferences.getBoolean(key, true);
382 Log.v(TAG, " " + key + " , " + value);
386 String strValue = preferences.getString(key, "");
387 setListPreference(key, key, strValue);
388 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
389 if (propertyKey != null)
391 setCameraProperty(propertyKey, strValue);
399 * ListPreference の表示データを設定
401 * @param pref_key Preference(表示)のキー
402 * @param key Preference(データ)のキー
403 * @param defaultValue Preferenceのデフォルト値
405 private void setListPreference(String pref_key, String key, String defaultValue)
408 pref = (ListPreference) findPreference(pref_key);
409 String value = preferences.getString(key, defaultValue);
412 pref.setValue(value);
413 pref.setSummary(value);
418 * BooleanPreference の表示データを設定
420 * @param pref_key Preference(表示)のキー
421 * @param key Preference(データ)のキー
422 * @param defaultValue Preferenceのデフォルト値
424 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
426 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
429 boolean value = preferences.getBoolean(key, defaultValue);
430 pref.setChecked(value);
438 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
440 String target = null;
447 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
448 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
462 public void synchronizedProperty()
464 FragmentActivity activity = getActivity();
465 if (activity == null)
469 busyDialog.dismiss();
478 activity.runOnUiThread(new Runnable() {
482 // Preferenceの画面に反映させる
483 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
484 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
485 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
486 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
489 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
490 if (hardwareStatusInterface != null)
493 setHardwareSummary();
497 busyDialog.dismiss();