1 package net.osdn.gokigen.gr2control.preference.olympus;
3 import android.support.annotation.NonNull;
4 import android.support.v4.app.FragmentActivity;
5 import android.support.v7.app.AppCompatActivity;
6 import android.app.ProgressDialog;
7 import android.content.Context;
8 import android.content.SharedPreferences;
9 import android.os.Bundle;
10 import android.support.v7.preference.CheckBoxPreference;
11 import android.support.v7.preference.ListPreference;
12 import android.support.v7.preference.Preference;
13 import android.support.v7.preference.PreferenceFragmentCompat;
14 import android.support.v7.preference.PreferenceManager;
15 import android.util.Log;
17 import net.osdn.gokigen.gr2control.R;
18 import net.osdn.gokigen.gr2control.camera.ICameraHardwareStatus;
19 import net.osdn.gokigen.gr2control.camera.ICameraRunMode;
20 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
21 import net.osdn.gokigen.gr2control.camera.olympus.operation.CameraPowerOff;
22 import net.osdn.gokigen.gr2control.camera.olympus.wrapper.property.IOlyCameraProperty;
23 import net.osdn.gokigen.gr2control.camera.olympus.wrapper.property.IOlyCameraPropertyProvider;
24 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
25 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
26 import net.osdn.gokigen.gr2control.scene.IChangeScene;
28 import java.util.HashMap;
29 import java.util.Locale;
32 import jp.co.olympus.camerakit.OLYCamera;
39 public class PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, PreferenceSynchronizer.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 PreferenceSynchronizer preferenceSynchronizer = null;
52 public static PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
54 PreferenceFragment instance = new PreferenceFragment();
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 (preferenceSynchronizer == null)
96 preferenceSynchronizer = new PreferenceSynchronizer(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, false);
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);
163 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
165 Log.v(TAG, "onCreatePreferences()");
167 //super.onCreate(savedInstanceState);
168 addPreferencesFromResource(R.xml.preferences_opc);
171 final HashMap<String, String> sizeTable = new HashMap<>();
172 sizeTable.put("QVGA", "(320x240)");
173 sizeTable.put("VGA", "(640x480)");
174 sizeTable.put("SVGA", "(800x600)");
175 sizeTable.put("XGA", "(1024x768)");
176 sizeTable.put("QUAD_VGA", "(1280x960)");
178 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
179 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
181 public boolean onPreferenceChange(Preference preference, Object newValue) {
182 String key = (String) newValue;
183 preference.setSummary(newValue + " " + sizeTable.get(key));
187 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
189 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
190 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
192 public boolean onPreferenceChange(Preference preference, Object newValue) {
193 preference.setSummary(newValue + " ");
197 connectionMethod.setSummary(connectionMethod.getValue() + " ");
199 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
200 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
204 * ハードウェアのサマリ情報を取得し設定する
206 private void setHardwareSummary()
209 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
212 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
216 float minLength = hardwareStatusInterface.getMinimumFocalLength();
217 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
218 float actualLength = hardwareStatusInterface.getActualFocalLength();
219 if (minLength == maxLength)
221 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
225 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
227 findPreference("focal_length").setSummary(focalLength);
232 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
233 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
236 Log.v(TAG, "- - - - - - - - - -");
237 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
239 String value = (String) entry.getValue();
240 Log.v(TAG, entry.getKey() + " : " + value);
242 Log.v(TAG, "- - - - - - - - - -");
246 findPreference("camera_version").setSummary("Unknown");
255 private void setCameraProperty(String name, String value)
259 String propertyValue = "<" + name + "/" + value + ">";
260 Log.v(TAG, "setCameraProperty() : " + propertyValue);
261 propertyInterface.setCameraPropertyValue(name, propertyValue);
274 public void onResume()
277 Log.v(TAG, "onResume() Start");
279 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
280 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
282 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
283 changeRunModeExecutor.changeRunMode(true);
285 synchronizeCameraProperties(true);
286 Log.v(TAG, "onResume() End");
295 public void onPause()
298 Log.v(TAG, "onPause() Start");
300 // Preference変更のリスナを解除
301 preferences.unregisterOnSharedPreferenceChangeListener(this);
303 Log.v(TAG, "onPause() End");
307 * カメラプロパティとPreferenceとの同期処理を実行
309 private void synchronizeCameraProperties(boolean isPropertyLoad)
312 busyDialog = new ProgressDialog(getActivity());
313 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
314 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
315 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
316 busyDialog.setCancelable(false);
319 // データ読み込み処理(別スレッドで実行)
322 new Thread(preferenceSynchronizer).start();
327 * Preferenceが更新された時に呼び出される処理
329 * @param sharedPreferences sharedPreferences
333 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
335 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
336 String propertyValue;
342 case IPreferencePropertyAccessor.RAW:
343 value = preferences.getBoolean(key, true);
344 setBooleanPreference(key, key, value);
345 propertyValue = (value) ? "ON" : "OFF";
346 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
349 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
350 value = preferences.getBoolean(key, true);
351 Log.v(TAG, " " + key + " , " + value);
354 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
355 value = preferences.getBoolean(key, true);
356 Log.v(TAG, " " + key + " , " + value);
359 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
360 value = preferences.getBoolean(key, false);
361 Log.v(TAG, " " + key + " , " + value);
364 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
365 value = preferences.getBoolean(key, false);
366 Log.v(TAG, " " + key + " , " + value);
370 String strValue = preferences.getString(key, "");
371 setListPreference(key, key, strValue);
372 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
373 if (propertyKey != null)
375 setCameraProperty(propertyKey, strValue);
383 * ListPreference の表示データを設定
385 * @param pref_key Preference(表示)のキー
386 * @param key Preference(データ)のキー
387 * @param defaultValue Preferenceのデフォルト値
389 private void setListPreference(String pref_key, String key, String defaultValue)
392 pref = (ListPreference) findPreference(pref_key);
393 String value = preferences.getString(key, defaultValue);
396 pref.setValue(value);
397 pref.setSummary(value);
402 * BooleanPreference の表示データを設定
404 * @param pref_key Preference(表示)のキー
405 * @param key Preference(データ)のキー
406 * @param defaultValue Preferenceのデフォルト値
408 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
410 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
413 boolean value = preferences.getBoolean(key, defaultValue);
414 pref.setChecked(value);
422 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
424 String target = null;
431 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
432 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
446 public void synchronizedProperty()
448 FragmentActivity activity = getActivity();
449 if (activity == null)
453 busyDialog.dismiss();
462 activity.runOnUiThread(new Runnable() {
466 // Preferenceの画面に反映させる
467 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
468 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
469 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
470 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
473 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
474 if (hardwareStatusInterface != null)
477 setHardwareSummary();
481 busyDialog.dismiss();