1 package net.osdn.gokigen.gr2control.preference.olympus;
3 import android.app.ProgressDialog;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.os.Bundle;
8 import android.provider.Settings;
9 import android.util.Log;
11 import net.osdn.gokigen.gr2control.R;
12 import net.osdn.gokigen.gr2control.camera.ICameraHardwareStatus;
13 import net.osdn.gokigen.gr2control.camera.ICameraRunMode;
14 import net.osdn.gokigen.gr2control.camera.ICameraRunModeCallback;
15 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
16 import net.osdn.gokigen.gr2control.camera.olympus.operation.CameraPowerOff;
17 import net.osdn.gokigen.gr2control.camera.olympus.wrapper.property.IOlyCameraProperty;
18 import net.osdn.gokigen.gr2control.camera.olympus.wrapper.property.IOlyCameraPropertyProvider;
19 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
20 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
21 import net.osdn.gokigen.gr2control.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;
42 public class PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, PreferenceSynchronizer.IPropertySynchronizeCallback, Preference.OnPreferenceClickListener, ICameraRunModeCallback
44 private final String TAG = toString();
45 private AppCompatActivity context = null;
46 private IOlyCameraPropertyProvider propertyInterface = null;
47 private ICameraHardwareStatus hardwareStatusInterface = null;
48 private ICameraRunMode changeRunModeExecutor = null;
49 private CameraPowerOff powerOffController = null;
50 private LogCatViewer logCatViewer = null;
51 private SharedPreferences preferences = null;
52 private ProgressDialog busyDialog = null;
53 private PreferenceSynchronizer preferenceSynchronizer = null;
56 public static PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
58 PreferenceFragment instance = new PreferenceFragment();
59 instance.setInterface(context, factory, changeScene);
61 // パラメータはBundleにまとめておく
62 Bundle arguments = new Bundle();
63 //arguments.putString("title", title);
64 //arguments.putString("message", message);
65 instance.setArguments(arguments);
74 private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
76 Log.v(TAG, "setInterface()");
79 this.propertyInterface = factory.getOlympusInterfaceProvider().getCameraPropertyProvider();
80 this.changeRunModeExecutor = factory.getCameraRunMode();
81 hardwareStatusInterface = factory.getHardwareStatus();
82 powerOffController = new CameraPowerOff(context, changeScene);
83 powerOffController.prepare();
84 logCatViewer = new LogCatViewer(changeScene);
85 logCatViewer.prepare();
87 this.context = context;
100 public void onAttach(@NonNull Context activity)
102 super.onAttach(activity);
103 Log.v(TAG, "onAttach()");
107 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
108 if (preferenceSynchronizer == null)
110 preferenceSynchronizer = new PreferenceSynchronizer(this.propertyInterface, preferences, this);
113 // Preference を初期設定する
114 initializePreferences();
116 preferences.registerOnSharedPreferenceChangeListener(this);
127 private void initializePreferences()
129 Map<String, ?> items = preferences.getAll();
130 SharedPreferences.Editor editor = preferences.edit();
131 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
133 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
135 if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
137 editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
139 if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
141 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
143 if (!items.containsKey(IPreferencePropertyAccessor.RAW))
145 editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
147 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
149 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
151 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
153 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
155 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
157 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
159 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
160 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
162 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
163 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
165 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
166 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
168 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
169 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
171 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
172 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
174 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
175 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
177 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW)) {
178 editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW, false);
180 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY)) {
181 editor.putString(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY, IPreferencePropertyAccessor.FUJI_X_FOCUS_XY_DEFAULT_VALUE);
183 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT)) {
184 editor.putString(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT, IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT_DEFAULT_VALUE);
186 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT)) {
187 editor.putString(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT, IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT_DEFAULT_VALUE);
189 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE)) {
190 editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE, false);
200 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
202 Log.v(TAG, "onCreatePreferences()");
204 //super.onCreate(savedInstanceState);
205 addPreferencesFromResource(R.xml.preferences_opc);
209 final HashMap<String, String> sizeTable = new HashMap<>();
210 sizeTable.put("QVGA", "(320x240)");
211 sizeTable.put("VGA", "(640x480)");
212 sizeTable.put("SVGA", "(800x600)");
213 sizeTable.put("XGA", "(1024x768)");
214 sizeTable.put("QUAD_VGA", "(1280x960)");
216 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
217 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
219 public boolean onPreferenceChange(Preference preference, Object newValue) {
220 String key = (String) newValue;
221 preference.setSummary(newValue + " " + sizeTable.get(key));
225 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
227 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
228 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
230 public boolean onPreferenceChange(Preference preference, Object newValue) {
231 preference.setSummary(newValue + " ");
235 connectionMethod.setSummary(connectionMethod.getValue() + " ");
237 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
238 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
239 findPreference("wifi_settings").setOnPreferenceClickListener(this);
248 * ハードウェアのサマリ情報を取得し設定する
250 private void setHardwareSummary()
255 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
258 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
262 float minLength = hardwareStatusInterface.getMinimumFocalLength();
263 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
264 float actualLength = hardwareStatusInterface.getActualFocalLength();
265 if (minLength == maxLength)
267 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
271 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
273 findPreference("focal_length").setSummary(focalLength);
283 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
284 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
287 Log.v(TAG, "- - - - - - - - - -");
288 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
290 String value = (String) entry.getValue();
291 Log.v(TAG, entry.getKey() + " : " + value);
293 Log.v(TAG, "- - - - - - - - - -");
297 findPreference("camera_version").setSummary("Unknown");
306 private void setCameraProperty(String name, String value)
310 String propertyValue = "<" + name + "/" + value + ">";
311 Log.v(TAG, "setCameraProperty() : " + propertyValue);
312 propertyInterface.setCameraPropertyValue(name, propertyValue);
325 public void onResume()
328 Log.v(TAG, "onResume() Start");
331 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
332 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
334 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
335 changeRunModeExecutor.changeRunMode(true, this);
338 synchronizeCameraProperties(true);
344 Log.v(TAG, "onResume() End");
348 public void onCompleted(boolean isRecording)
350 synchronizeCameraProperties(true);
354 public void onErrorOccurred(boolean isRecording)
356 Log.v(TAG, " onErrorOccurred()");
357 synchronizeCameraProperties(true);
365 public void onPause()
368 Log.v(TAG, "onPause() Start");
370 // Preference変更のリスナを解除
371 preferences.unregisterOnSharedPreferenceChangeListener(this);
373 Log.v(TAG, "onPause() End");
377 * カメラプロパティとPreferenceとの同期処理を実行
379 private void synchronizeCameraProperties(boolean isPropertyLoad)
382 busyDialog = new ProgressDialog(getActivity());
383 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
384 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
385 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
386 busyDialog.setCancelable(false);
389 // データ読み込み処理(別スレッドで実行)
392 new Thread(preferenceSynchronizer).start();
397 * Preferenceが更新された時に呼び出される処理
399 * @param sharedPreferences sharedPreferences
403 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
405 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
406 String propertyValue;
412 case IPreferencePropertyAccessor.RAW:
413 value = preferences.getBoolean(key, true);
414 setBooleanPreference(key, key, value);
415 propertyValue = (value) ? "ON" : "OFF";
416 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
419 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
420 value = preferences.getBoolean(key, true);
421 Log.v(TAG, " " + key + " , " + value);
424 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
425 value = preferences.getBoolean(key, true);
426 Log.v(TAG, " " + key + " , " + value);
429 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
430 value = preferences.getBoolean(key, false);
431 Log.v(TAG, " " + key + " , " + value);
434 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
435 value = preferences.getBoolean(key, false);
436 Log.v(TAG, " " + key + " , " + value);
440 String strValue = preferences.getString(key, "");
441 setListPreference(key, key, strValue);
442 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
443 if (propertyKey != null)
445 setCameraProperty(propertyKey, strValue);
453 * ListPreference の表示データを設定
455 * @param pref_key Preference(表示)のキー
456 * @param key Preference(データ)のキー
457 * @param defaultValue Preferenceのデフォルト値
459 private void setListPreference(String pref_key, String key, String defaultValue)
462 pref = (ListPreference) findPreference(pref_key);
463 String value = preferences.getString(key, defaultValue);
466 pref.setValue(value);
467 pref.setSummary(value);
472 * BooleanPreference の表示データを設定
474 * @param pref_key Preference(表示)のキー
475 * @param key Preference(データ)のキー
476 * @param defaultValue Preferenceのデフォルト値
478 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
480 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
483 boolean value = preferences.getBoolean(key, defaultValue);
484 pref.setChecked(value);
492 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
494 String target = null;
501 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
502 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
516 public void synchronizedProperty()
518 FragmentActivity activity = getActivity();
519 if (activity == null)
523 busyDialog.dismiss();
532 activity.runOnUiThread(new Runnable() {
536 // Preferenceの画面に反映させる
537 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
538 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
539 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
540 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
543 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
544 if (hardwareStatusInterface != null)
547 setHardwareSummary();
551 busyDialog.dismiss();
564 public boolean onPreferenceClick(Preference preference)
568 String preferenceKey = preference.getKey();
569 if (preferenceKey.contains("wifi_settings"))
572 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
575 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));