1 package net.osdn.gokigen.a01d.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.a01d.IChangeScene;
18 import net.osdn.gokigen.a01d.R;
19 import net.osdn.gokigen.a01d.camera.IInterfaceProvider;
20 import net.osdn.gokigen.a01d.camera.olympus.wrapper.ICameraHardwareStatus;
21 import net.osdn.gokigen.a01d.camera.olympus.wrapper.ICameraRunMode;
22 import net.osdn.gokigen.a01d.camera.olympus.wrapper.connection.ble.OlyCameraPowerOnSelector;
23 import net.osdn.gokigen.a01d.camera.olympus.wrapper.property.IOlyCameraProperty;
24 import net.osdn.gokigen.a01d.camera.olympus.wrapper.property.IOlyCameraPropertyProvider;
25 import net.osdn.gokigen.a01d.camera.olympus.operation.CameraPowerOff;
26 import net.osdn.gokigen.a01d.logcat.LogCatViewer;
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 OlyCameraPowerOnSelector powerOnSelector = null;
47 private LogCatViewer logCatViewer = null;
48 private SharedPreferences preferences = null;
49 private ProgressDialog busyDialog = null;
50 private PreferenceSynchronizer preferenceSynchronizer = null;
53 public static PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
55 PreferenceFragment instance = new PreferenceFragment();
56 instance.setInterface(context, factory, changeScene);
58 // パラメータはBundleにまとめておく
59 Bundle arguments = new Bundle();
60 //arguments.putString("title", title);
61 //arguments.putString("message", message);
62 instance.setArguments(arguments);
72 private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
74 Log.v(TAG, "setInterface()");
75 this.propertyInterface = factory.getOlympusInterface().getCameraPropertyProvider();
76 this.changeRunModeExecutor = factory.getOlympusInterface().getCameraRunMode();
77 hardwareStatusInterface = factory.getOlympusInterface().getHardwareStatus();
78 powerOffController = new CameraPowerOff(context, changeScene);
79 powerOffController.prepare();
80 powerOnSelector = new OlyCameraPowerOnSelector(context);
81 powerOnSelector.prepare();
82 logCatViewer = new LogCatViewer(changeScene);
83 logCatViewer.prepare();
91 public void onAttach(Context activity)
93 super.onAttach(activity);
94 Log.v(TAG, "onAttach()");
97 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
98 if (preferenceSynchronizer == null)
100 preferenceSynchronizer = new PreferenceSynchronizer(this.propertyInterface, preferences, this);
103 // Preference を初期設定する
104 initializePreferences();
106 preferences.registerOnSharedPreferenceChangeListener(this);
112 private void initializePreferences()
114 Map<String, ?> items = preferences.getAll();
115 SharedPreferences.Editor editor = preferences.edit();
117 if (!items.containsKey(IPreferencePropertyAccessor.TAKE_MODE))
119 editor.putString(IPreferencePropertyAccessor.TAKE_MODE, IPreferencePropertyAccessor.TAKE_MODE_DEFAULT_VALUE);
121 if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
123 editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
125 if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
127 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
129 if (!items.containsKey(IPreferencePropertyAccessor.RAW))
131 editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
133 if (!items.containsKey(IPreferencePropertyAccessor.BLE_POWER_ON))
135 editor.putBoolean(IPreferencePropertyAccessor.BLE_POWER_ON, false);
137 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
139 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
141 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
143 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
145 if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
147 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
149 if (!items.containsKey(IPreferencePropertyAccessor.MAGNIFYING_LIVE_VIEW_SCALE))
151 editor.putString(IPreferencePropertyAccessor.MAGNIFYING_LIVE_VIEW_SCALE, IPreferencePropertyAccessor.MAGNIFYING_LIVE_VIEW_SCALE_DEFAULT_VALUE);
153 if (!items.containsKey(IPreferencePropertyAccessor.DIGITAL_ZOOM_LEVEL))
155 editor.putString(IPreferencePropertyAccessor.DIGITAL_ZOOM_LEVEL, IPreferencePropertyAccessor.DIGITAL_ZOOM_LEVEL_DEFAULT_VALUE);
157 if (!items.containsKey(IPreferencePropertyAccessor.POWER_ZOOM_LEVEL))
159 editor.putString(IPreferencePropertyAccessor.POWER_ZOOM_LEVEL, IPreferencePropertyAccessor.POWER_ZOOM_LEVEL_DEFAULT_VALUE);
161 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
163 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
173 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
175 Log.v(TAG, "onCreatePreferences()");
177 //super.onCreate(savedInstanceState);
178 addPreferencesFromResource(R.xml.preferences);
181 final HashMap<String, String> sizeTable = new HashMap<>();
182 sizeTable.put("QVGA", "(320x240)");
183 sizeTable.put("VGA", "(640x480)");
184 sizeTable.put("SVGA", "(800x600)");
185 sizeTable.put("XGA", "(1024x768)");
186 sizeTable.put("QUAD_VGA", "(1280x960)");
188 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
189 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
191 public boolean onPreferenceChange(Preference preference, Object newValue) {
192 String key = (String) newValue;
193 preference.setSummary(newValue + " " + sizeTable.get(key));
197 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
199 ListPreference liveViewScale = (ListPreference) findPreference(IPreferencePropertyAccessor.MAGNIFYING_LIVE_VIEW_SCALE);
200 liveViewScale.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
202 public boolean onPreferenceChange(Preference preference, Object newValue) {
203 preference.setSummary(newValue + " ");
207 liveViewScale.setSummary(liveViewScale.getValue() + " ");
209 ListPreference digitalZoom = (ListPreference) findPreference(IPreferencePropertyAccessor.DIGITAL_ZOOM_LEVEL);
210 digitalZoom.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
212 public boolean onPreferenceChange(Preference preference, Object newValue) {
213 preference.setSummary(newValue + " ");
217 digitalZoom.setSummary(digitalZoom.getValue() + " ");
219 ListPreference powerZoom = (ListPreference) findPreference(IPreferencePropertyAccessor.POWER_ZOOM_LEVEL);
220 powerZoom.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
222 public boolean onPreferenceChange(Preference preference, Object newValue) {
223 preference.setSummary(newValue + " ");
227 powerZoom.setSummary(powerZoom.getValue() + " ");
229 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
230 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
232 public boolean onPreferenceChange(Preference preference, Object newValue) {
233 preference.setSummary(newValue + " ");
237 connectionMethod.setSummary(connectionMethod.getValue() + " ");
239 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
240 findPreference("olympus_air_bt").setOnPreferenceClickListener(powerOnSelector);
241 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
245 * ハードウェアのサマリ情報を取得し設定する
247 private void setHardwareSummary()
250 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
253 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
257 float minLength = hardwareStatusInterface.getMinimumFocalLength();
258 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
259 float actualLength = hardwareStatusInterface.getActualFocalLength();
260 if (minLength == maxLength)
262 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
266 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
268 findPreference("focal_length").setSummary(focalLength);
273 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
274 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
277 Log.v(TAG, "- - - - - - - - - -");
278 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
280 String value = (String) entry.getValue();
281 Log.v(TAG, entry.getKey() + " : " + value);
283 Log.v(TAG, "- - - - - - - - - -");
287 findPreference("camera_version").setSummary("Unknown");
296 private void setCameraProperty(String name, String value)
300 String propertyValue = "<" + name + "/" + value + ">";
301 Log.v(TAG, "setCameraProperty() : " + propertyValue);
302 propertyInterface.setCameraPropertyValue(name, propertyValue);
315 public void onResume()
318 Log.v(TAG, "onResume() Start");
320 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
321 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
323 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
324 changeRunModeExecutor.changeRunMode(true);
326 synchronizeCameraProperties(true);
327 Log.v(TAG, "onResume() End");
336 public void onPause()
339 Log.v(TAG, "onPause() Start");
341 // Preference変更のリスナを解除
342 preferences.unregisterOnSharedPreferenceChangeListener(this);
344 Log.v(TAG, "onPause() End");
348 * カメラプロパティとPreferenceとの同期処理を実行
350 private void synchronizeCameraProperties(boolean isPropertyLoad)
353 busyDialog = new ProgressDialog(getActivity());
354 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
355 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
356 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
357 busyDialog.setCancelable(false);
360 // データ読み込み処理(別スレッドで実行)
363 new Thread(preferenceSynchronizer).start();
368 * Preferenceが更新された時に呼び出される処理
370 * @param sharedPreferences sharedPreferences
374 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
376 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
377 String propertyValue;
383 case IPreferencePropertyAccessor.RAW:
384 value = preferences.getBoolean(key, true);
385 setBooleanPreference(key, key, value);
386 propertyValue = (value) ? "ON" : "OFF";
387 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
390 case IPreferencePropertyAccessor.BLE_POWER_ON:
391 value = preferences.getBoolean(key, false);
392 Log.v(TAG, " " + key + " , " + value);
395 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
396 value = preferences.getBoolean(key, true);
397 Log.v(TAG, " " + key + " , " + value);
400 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
401 value = preferences.getBoolean(key, true);
402 Log.v(TAG, " " + key + " , " + value);
406 String strValue = preferences.getString(key, "");
407 setListPreference(key, key, strValue);
408 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
409 if (propertyKey != null)
411 setCameraProperty(propertyKey, strValue);
419 * ListPreference の表示データを設定
421 * @param pref_key Preference(表示)のキー
422 * @param key Preference(データ)のキー
423 * @param defaultValue Preferenceのデフォルト値
425 private void setListPreference(String pref_key, String key, String defaultValue)
428 pref = (ListPreference) findPreference(pref_key);
429 String value = preferences.getString(key, defaultValue);
432 pref.setValue(value);
433 pref.setSummary(value);
438 * BooleanPreference の表示データを設定
440 * @param pref_key Preference(表示)のキー
441 * @param key Preference(データ)のキー
442 * @param defaultValue Preferenceのデフォルト値
444 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
446 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
449 boolean value = preferences.getBoolean(key, defaultValue);
450 pref.setChecked(value);
458 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
460 String target = null;
467 case IPreferencePropertyAccessor.TAKE_MODE:
468 target = IOlyCameraProperty.TAKE_MODE;
471 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
472 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
486 public void synchronizedProperty()
488 FragmentActivity activity = getActivity();
489 if (activity == null)
493 busyDialog.dismiss();
502 activity.runOnUiThread(new Runnable() {
506 // Preferenceの画面に反映させる
507 setListPreference(IPreferencePropertyAccessor.TAKE_MODE, IPreferencePropertyAccessor.TAKE_MODE, IPreferencePropertyAccessor.TAKE_MODE_DEFAULT_VALUE);
508 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
509 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
510 setBooleanPreference(IPreferencePropertyAccessor.BLE_POWER_ON, IPreferencePropertyAccessor.BLE_POWER_ON, false);
511 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
514 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
515 if (hardwareStatusInterface != null)
518 setHardwareSummary();
522 busyDialog.dismiss();
524 } catch (Exception e)