1 package net.osdn.gokigen.pkremote.preference.olympus;
4 import android.app.ProgressDialog;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.SharedPreferences;
8 import android.os.Bundle;
9 import android.provider.Settings;
10 import android.util.Log;
12 import net.osdn.gokigen.pkremote.R;
13 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
14 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraRunMode;
15 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraHardwareStatus;
16 import net.osdn.gokigen.pkremote.camera.vendor.olympus.operation.CameraPowerOff;
17 import net.osdn.gokigen.pkremote.camera.vendor.olympus.wrapper.property.IOlyCameraProperty;
18 import net.osdn.gokigen.pkremote.camera.vendor.olympus.wrapper.property.IOlyCameraPropertyProvider;
19 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
20 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
21 import net.osdn.gokigen.pkremote.scene.IChangeScene;
23 import java.util.HashMap;
24 import java.util.Locale;
27 import androidx.annotation.NonNull;
28 import androidx.appcompat.app.AppCompatActivity;
29 import androidx.fragment.app.FragmentActivity;
30 import androidx.preference.CheckBoxPreference;
31 import androidx.preference.ListPreference;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceFragmentCompat;
34 import androidx.preference.PreferenceManager;
35 import jp.co.olympus.camerakit.OLYCamera;
37 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
38 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
39 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
45 public class OpcPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, OpcPreferenceSynchronizer.IPropertySynchronizeCallback, Preference.OnPreferenceClickListener
47 private final String TAG = toString();
48 private AppCompatActivity context = null;
49 private IOlyCameraPropertyProvider propertyInterface = null;
50 private ICameraHardwareStatus hardwareStatusInterface = null;
51 private ICameraRunMode changeRunModeExecutor = null;
52 private CameraPowerOff powerOffController = null;
53 private LogCatViewer logCatViewer = null;
54 private SharedPreferences preferences = null;
55 private ProgressDialog busyDialog = null;
56 private OpcPreferenceSynchronizer opcPreferenceSynchronizer = null;
59 public static OpcPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
61 OpcPreferenceFragment instance = new OpcPreferenceFragment();
62 instance.setInterface(context, factory, changeScene);
64 // パラメータはBundleにまとめておく
65 Bundle arguments = new Bundle();
66 //arguments.putString("title", title);
67 //arguments.putString("message", message);
68 instance.setArguments(arguments);
77 private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
79 Log.v(TAG, "setInterface()");
80 this.propertyInterface = factory.getOlympusInterfaceProvider().getCameraPropertyProvider();
81 this.changeRunModeExecutor = factory.getCameraRunMode();
82 hardwareStatusInterface = factory.getHardwareStatus();
83 powerOffController = new CameraPowerOff(context, changeScene);
84 powerOffController.prepare();
85 logCatViewer = new LogCatViewer(changeScene);
86 logCatViewer.prepare();
88 this.context = context;
96 public void onAttach(Context activity)
98 super.onAttach(activity);
99 Log.v(TAG, "onAttach()");
102 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
103 if (opcPreferenceSynchronizer == null)
105 opcPreferenceSynchronizer = new OpcPreferenceSynchronizer(this.propertyInterface, preferences, this);
108 // Preference を初期設定する
109 initializePreferences();
111 preferences.registerOnSharedPreferenceChangeListener(this);
117 private void initializePreferences()
119 Map<String, ?> items = preferences.getAll();
120 SharedPreferences.Editor editor = preferences.edit();
122 if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
124 editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
126 if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
128 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
130 if (!items.containsKey(IPreferencePropertyAccessor.RAW))
132 editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
134 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
136 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
138 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
140 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
142 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
144 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
146 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
147 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
149 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
150 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
152 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
153 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
155 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
156 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
158 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
159 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
161 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
162 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
164 if (!items.containsKey(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE)) {
165 editor.putString(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE, IPreferencePropertyAccessor.SMALL_PICTURE_SIZE_DEFAULT_VALUE);
167 if (!items.containsKey(IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE)) {
168 editor.putString(IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE, IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE_DEFAULT_VALUE);
170 if (!items.containsKey(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA))
172 editor.putBoolean(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
174 if (!items.containsKey(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE))
176 editor.putBoolean(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, false);
178 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
180 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
182 if (!items.containsKey(IPreferencePropertyAccessor.USE_OSC_THETA_V21))
184 editor.putBoolean(IPreferencePropertyAccessor.USE_OSC_THETA_V21, false);
186 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_HOST_IP))
188 editor.putString(IPreferencePropertyAccessor.PIXPRO_HOST_IP, IPreferencePropertyAccessor.PIXPRO_HOST_IP_DEFAULT_VALUE);
190 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT))
192 editor.putString(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT, IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT_DEFAULT_VALUE);
194 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT))
196 editor.putString(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
198 if (!items.containsKey(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE))
200 editor.putString(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE, IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE_DEFAULT_VALUE);
202 if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
203 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
205 if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
206 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
216 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
218 Log.v(TAG, "onCreatePreferences()");
220 //super.onCreate(savedInstanceState);
221 addPreferencesFromResource(R.xml.preferences_opc);
224 final HashMap<String, String> sizeTable = new HashMap<>();
225 sizeTable.put("QVGA", "(320x240)");
226 sizeTable.put("VGA", "(640x480)");
227 sizeTable.put("SVGA", "(800x600)");
228 sizeTable.put("XGA", "(1024x768)");
229 sizeTable.put("QUAD_VGA", "(1280x960)");
231 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
232 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
234 public boolean onPreferenceChange(Preference preference, Object newValue) {
235 String key = (String) newValue;
236 preference.setSummary(newValue + " " + sizeTable.get(key));
240 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
242 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
243 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
245 public boolean onPreferenceChange(Preference preference, Object newValue) {
246 preference.setSummary(newValue + " ");
250 connectionMethod.setSummary(connectionMethod.getValue() + " ");
252 ListPreference smallPictureSize = (ListPreference) findPreference(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE);
253 smallPictureSize.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
255 public boolean onPreferenceChange(Preference preference, Object newValue) {
256 preference.setSummary(newValue + " ");
260 smallPictureSize.setSummary(smallPictureSize.getValue() + " ");
262 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
263 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
264 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
268 * ハードウェアのサマリ情報を取得し設定する
270 private void setHardwareSummary()
273 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
276 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
280 float minLength = hardwareStatusInterface.getMinimumFocalLength();
281 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
282 float actualLength = hardwareStatusInterface.getActualFocalLength();
283 if (minLength == maxLength)
285 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
289 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
291 findPreference("focal_length").setSummary(focalLength);
296 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
297 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
300 Log.v(TAG, "- - - - - - - - - -");
301 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
303 String value = (String) entry.getValue();
304 Log.v(TAG, entry.getKey() + " : " + value);
306 Log.v(TAG, "- - - - - - - - - -");
310 findPreference("camera_version").setSummary("Unknown");
319 private void setCameraProperty(String name, String value)
323 String propertyValue = "<" + name + "/" + value + ">";
324 Log.v(TAG, "setCameraProperty() : " + propertyValue);
325 propertyInterface.setCameraPropertyValue(name, propertyValue);
338 public void onResume()
341 Log.v(TAG, "onResume() Start");
343 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
344 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
346 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
347 changeRunModeExecutor.changeRunMode(true);
349 synchronizeCameraProperties(true);
350 Log.v(TAG, "onResume() End");
359 public void onPause()
362 Log.v(TAG, "onPause() Start");
364 // Preference変更のリスナを解除
365 preferences.unregisterOnSharedPreferenceChangeListener(this);
367 Log.v(TAG, "onPause() End");
371 * カメラプロパティとPreferenceとの同期処理を実行
373 private void synchronizeCameraProperties(boolean isPropertyLoad)
376 busyDialog = new ProgressDialog(getActivity());
377 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
378 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
379 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
380 busyDialog.setCancelable(false);
383 // データ読み込み処理(別スレッドで実行)
386 new Thread(opcPreferenceSynchronizer).start();
391 * Preferenceが更新された時に呼び出される処理
393 * @param sharedPreferences sharedPreferences
397 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
399 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
400 String propertyValue;
406 case IPreferencePropertyAccessor.RAW:
407 value = preferences.getBoolean(key, true);
408 setBooleanPreference(key, key, value);
409 propertyValue = (value) ? "ON" : "OFF";
410 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
413 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
414 value = preferences.getBoolean(key, true);
415 Log.v(TAG, " " + key + " , " + value);
418 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
419 value = preferences.getBoolean(key, true);
420 Log.v(TAG, " " + key + " , " + value);
423 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
424 value = preferences.getBoolean(key, false);
425 Log.v(TAG, " " + key + " , " + value);
428 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
429 value = preferences.getBoolean(key, true);
430 Log.v(TAG, " " + key + " , " + value);
434 String strValue = preferences.getString(key, "");
435 setListPreference(key, key, strValue);
436 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
437 if (propertyKey != null)
439 setCameraProperty(propertyKey, strValue);
447 * ListPreference の表示データを設定
449 * @param pref_key Preference(表示)のキー
450 * @param key Preference(データ)のキー
451 * @param defaultValue Preferenceのデフォルト値
453 private void setListPreference(String pref_key, String key, String defaultValue)
456 pref = (ListPreference) findPreference(pref_key);
457 String value = preferences.getString(key, defaultValue);
460 pref.setValue(value);
461 pref.setSummary(value);
466 * BooleanPreference の表示データを設定
468 * @param pref_key Preference(表示)のキー
469 * @param key Preference(データ)のキー
470 * @param defaultValue Preferenceのデフォルト値
472 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
474 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
477 boolean value = preferences.getBoolean(key, defaultValue);
478 pref.setChecked(value);
486 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
488 String target = null;
495 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
496 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
510 public void synchronizedProperty()
512 FragmentActivity activity = getActivity();
513 if (activity == null)
517 busyDialog.dismiss();
526 activity.runOnUiThread(new Runnable() {
530 // Preferenceの画面に反映させる
531 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
532 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
533 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
534 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
537 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
538 if (hardwareStatusInterface != null)
541 setHardwareSummary();
545 busyDialog.dismiss();
557 public boolean onPreferenceClick(Preference preference)
561 String preferenceKey = preference.getKey();
562 if (preferenceKey.contains(WIFI_SETTINGS))
565 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
568 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));