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);
210 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
212 Log.v(TAG, "onCreatePreferences()");
214 //super.onCreate(savedInstanceState);
215 addPreferencesFromResource(R.xml.preferences_opc);
218 final HashMap<String, String> sizeTable = new HashMap<>();
219 sizeTable.put("QVGA", "(320x240)");
220 sizeTable.put("VGA", "(640x480)");
221 sizeTable.put("SVGA", "(800x600)");
222 sizeTable.put("XGA", "(1024x768)");
223 sizeTable.put("QUAD_VGA", "(1280x960)");
225 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
226 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
228 public boolean onPreferenceChange(Preference preference, Object newValue) {
229 String key = (String) newValue;
230 preference.setSummary(newValue + " " + sizeTable.get(key));
234 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
236 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
237 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
239 public boolean onPreferenceChange(Preference preference, Object newValue) {
240 preference.setSummary(newValue + " ");
244 connectionMethod.setSummary(connectionMethod.getValue() + " ");
246 ListPreference smallPictureSize = (ListPreference) findPreference(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE);
247 smallPictureSize.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
249 public boolean onPreferenceChange(Preference preference, Object newValue) {
250 preference.setSummary(newValue + " ");
254 smallPictureSize.setSummary(smallPictureSize.getValue() + " ");
256 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
257 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
258 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
262 * ハードウェアのサマリ情報を取得し設定する
264 private void setHardwareSummary()
267 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
270 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
274 float minLength = hardwareStatusInterface.getMinimumFocalLength();
275 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
276 float actualLength = hardwareStatusInterface.getActualFocalLength();
277 if (minLength == maxLength)
279 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
283 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
285 findPreference("focal_length").setSummary(focalLength);
290 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
291 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
294 Log.v(TAG, "- - - - - - - - - -");
295 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
297 String value = (String) entry.getValue();
298 Log.v(TAG, entry.getKey() + " : " + value);
300 Log.v(TAG, "- - - - - - - - - -");
304 findPreference("camera_version").setSummary("Unknown");
313 private void setCameraProperty(String name, String value)
317 String propertyValue = "<" + name + "/" + value + ">";
318 Log.v(TAG, "setCameraProperty() : " + propertyValue);
319 propertyInterface.setCameraPropertyValue(name, propertyValue);
332 public void onResume()
335 Log.v(TAG, "onResume() Start");
337 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
338 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
340 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
341 changeRunModeExecutor.changeRunMode(true);
343 synchronizeCameraProperties(true);
344 Log.v(TAG, "onResume() End");
353 public void onPause()
356 Log.v(TAG, "onPause() Start");
358 // Preference変更のリスナを解除
359 preferences.unregisterOnSharedPreferenceChangeListener(this);
361 Log.v(TAG, "onPause() End");
365 * カメラプロパティとPreferenceとの同期処理を実行
367 private void synchronizeCameraProperties(boolean isPropertyLoad)
370 busyDialog = new ProgressDialog(getActivity());
371 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
372 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
373 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
374 busyDialog.setCancelable(false);
377 // データ読み込み処理(別スレッドで実行)
380 new Thread(opcPreferenceSynchronizer).start();
385 * Preferenceが更新された時に呼び出される処理
387 * @param sharedPreferences sharedPreferences
391 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
393 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
394 String propertyValue;
400 case IPreferencePropertyAccessor.RAW:
401 value = preferences.getBoolean(key, true);
402 setBooleanPreference(key, key, value);
403 propertyValue = (value) ? "ON" : "OFF";
404 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
407 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
408 value = preferences.getBoolean(key, true);
409 Log.v(TAG, " " + key + " , " + value);
412 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
413 value = preferences.getBoolean(key, true);
414 Log.v(TAG, " " + key + " , " + value);
417 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
418 value = preferences.getBoolean(key, false);
419 Log.v(TAG, " " + key + " , " + value);
422 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
423 value = preferences.getBoolean(key, true);
424 Log.v(TAG, " " + key + " , " + value);
428 String strValue = preferences.getString(key, "");
429 setListPreference(key, key, strValue);
430 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
431 if (propertyKey != null)
433 setCameraProperty(propertyKey, strValue);
441 * ListPreference の表示データを設定
443 * @param pref_key Preference(表示)のキー
444 * @param key Preference(データ)のキー
445 * @param defaultValue Preferenceのデフォルト値
447 private void setListPreference(String pref_key, String key, String defaultValue)
450 pref = (ListPreference) findPreference(pref_key);
451 String value = preferences.getString(key, defaultValue);
454 pref.setValue(value);
455 pref.setSummary(value);
460 * BooleanPreference の表示データを設定
462 * @param pref_key Preference(表示)のキー
463 * @param key Preference(データ)のキー
464 * @param defaultValue Preferenceのデフォルト値
466 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
468 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
471 boolean value = preferences.getBoolean(key, defaultValue);
472 pref.setChecked(value);
480 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
482 String target = null;
489 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
490 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
504 public void synchronizedProperty()
506 FragmentActivity activity = getActivity();
507 if (activity == null)
511 busyDialog.dismiss();
520 activity.runOnUiThread(new Runnable() {
524 // Preferenceの画面に反映させる
525 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
526 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
527 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
528 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
531 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
532 if (hardwareStatusInterface != null)
535 setHardwareSummary();
539 busyDialog.dismiss();
551 public boolean onPreferenceClick(Preference preference)
555 String preferenceKey = preference.getKey();
556 if (preferenceKey.contains(WIFI_SETTINGS))
559 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
562 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));