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.preference.PreferencePropertyInitializer;
22 import net.osdn.gokigen.pkremote.scene.IChangeScene;
24 import java.util.HashMap;
25 import java.util.Locale;
28 import androidx.annotation.NonNull;
29 import androidx.appcompat.app.AppCompatActivity;
30 import androidx.fragment.app.FragmentActivity;
31 import androidx.preference.CheckBoxPreference;
32 import androidx.preference.ListPreference;
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceFragmentCompat;
35 import androidx.preference.PreferenceManager;
36 import jp.co.olympus.camerakit.OLYCamera;
38 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
39 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
40 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
46 public class OpcPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, OpcPreferenceSynchronizer.IPropertySynchronizeCallback, Preference.OnPreferenceClickListener
48 private final String TAG = toString();
49 private AppCompatActivity context = null;
50 private IOlyCameraPropertyProvider propertyInterface = null;
51 private ICameraHardwareStatus hardwareStatusInterface = null;
52 private ICameraRunMode changeRunModeExecutor = null;
53 private CameraPowerOff powerOffController = null;
54 private LogCatViewer logCatViewer = null;
55 private SharedPreferences preferences = null;
56 private ProgressDialog busyDialog = null;
57 private OpcPreferenceSynchronizer opcPreferenceSynchronizer = null;
60 public static OpcPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
62 OpcPreferenceFragment instance = new OpcPreferenceFragment();
63 instance.setInterface(context, factory, changeScene);
65 // パラメータはBundleにまとめておく
66 Bundle arguments = new Bundle();
67 //arguments.putString("title", title);
68 //arguments.putString("message", message);
69 instance.setArguments(arguments);
78 private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
80 Log.v(TAG, "setInterface()");
81 this.propertyInterface = factory.getOlympusInterfaceProvider().getCameraPropertyProvider();
82 this.changeRunModeExecutor = factory.getCameraRunMode();
83 hardwareStatusInterface = factory.getHardwareStatus();
84 powerOffController = new CameraPowerOff(context, changeScene);
85 powerOffController.prepare();
86 logCatViewer = new LogCatViewer(changeScene);
87 logCatViewer.prepare();
89 this.context = context;
97 public void onAttach(Context activity)
99 super.onAttach(activity);
100 Log.v(TAG, "onAttach()");
103 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
104 if (opcPreferenceSynchronizer == null)
106 opcPreferenceSynchronizer = new OpcPreferenceSynchronizer(this.propertyInterface, preferences, this);
109 // Preference を初期設定する
110 PreferencePropertyInitializer initializer = new PreferencePropertyInitializer(activity);
111 initializer.initializePreferences();
113 preferences.registerOnSharedPreferenceChangeListener(this);
121 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
123 Log.v(TAG, "onCreatePreferences()");
125 //super.onCreate(savedInstanceState);
126 addPreferencesFromResource(R.xml.preferences_opc);
129 final HashMap<String, String> sizeTable = new HashMap<>();
130 sizeTable.put("QVGA", "(320x240)");
131 sizeTable.put("VGA", "(640x480)");
132 sizeTable.put("SVGA", "(800x600)");
133 sizeTable.put("XGA", "(1024x768)");
134 sizeTable.put("QUAD_VGA", "(1280x960)");
136 ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
137 liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
139 public boolean onPreferenceChange(Preference preference, Object newValue) {
140 String key = (String) newValue;
141 preference.setSummary(newValue + " " + sizeTable.get(key));
145 liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
147 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
148 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
150 public boolean onPreferenceChange(Preference preference, Object newValue) {
151 preference.setSummary(newValue + " ");
155 connectionMethod.setSummary(connectionMethod.getValue() + " ");
157 ListPreference smallPictureSize = (ListPreference) findPreference(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE);
158 smallPictureSize.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
160 public boolean onPreferenceChange(Preference preference, Object newValue) {
161 preference.setSummary(newValue + " ");
165 smallPictureSize.setSummary(smallPictureSize.getValue() + " ");
167 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
168 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
169 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
173 * ハードウェアのサマリ情報を取得し設定する
175 private void setHardwareSummary()
178 findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
181 findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
185 float minLength = hardwareStatusInterface.getMinimumFocalLength();
186 float maxLength = hardwareStatusInterface.getMaximumFocalLength();
187 float actualLength = hardwareStatusInterface.getActualFocalLength();
188 if (minLength == maxLength)
190 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
194 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
196 findPreference("focal_length").setSummary(focalLength);
201 Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
202 findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
205 Log.v(TAG, "- - - - - - - - - -");
206 for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
208 String value = (String) entry.getValue();
209 Log.v(TAG, entry.getKey() + " : " + value);
211 Log.v(TAG, "- - - - - - - - - -");
215 findPreference("camera_version").setSummary("Unknown");
224 private void setCameraProperty(String name, String value)
228 String propertyValue = "<" + name + "/" + value + ">";
229 Log.v(TAG, "setCameraProperty() : " + propertyValue);
230 propertyInterface.setCameraPropertyValue(name, propertyValue);
243 public void onResume()
246 Log.v(TAG, "onResume() Start");
248 // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
249 if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
251 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
252 changeRunModeExecutor.changeRunMode(true);
254 synchronizeCameraProperties(true);
255 Log.v(TAG, "onResume() End");
264 public void onPause()
267 Log.v(TAG, "onPause() Start");
269 // Preference変更のリスナを解除
270 preferences.unregisterOnSharedPreferenceChangeListener(this);
272 Log.v(TAG, "onPause() End");
276 * カメラプロパティとPreferenceとの同期処理を実行
278 private void synchronizeCameraProperties(boolean isPropertyLoad)
281 busyDialog = new ProgressDialog(getActivity());
282 busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
283 busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
284 busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
285 busyDialog.setCancelable(false);
288 // データ読み込み処理(別スレッドで実行)
291 new Thread(opcPreferenceSynchronizer).start();
296 * Preferenceが更新された時に呼び出される処理
298 * @param sharedPreferences sharedPreferences
302 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
304 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
305 String propertyValue;
311 case IPreferencePropertyAccessor.RAW:
312 value = preferences.getBoolean(key, true);
313 setBooleanPreference(key, key, value);
314 propertyValue = (value) ? "ON" : "OFF";
315 setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
318 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
319 value = preferences.getBoolean(key, true);
320 Log.v(TAG, " " + key + " , " + value);
323 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
324 value = preferences.getBoolean(key, true);
325 Log.v(TAG, " " + key + " , " + value);
328 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
329 value = preferences.getBoolean(key, false);
330 Log.v(TAG, " " + key + " , " + value);
333 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
334 value = preferences.getBoolean(key, true);
335 Log.v(TAG, " " + key + " , " + value);
339 String strValue = preferences.getString(key, "");
340 setListPreference(key, key, strValue);
341 String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
342 if (propertyKey != null)
344 setCameraProperty(propertyKey, strValue);
352 * ListPreference の表示データを設定
354 * @param pref_key Preference(表示)のキー
355 * @param key Preference(データ)のキー
356 * @param defaultValue Preferenceのデフォルト値
358 private void setListPreference(String pref_key, String key, String defaultValue)
361 pref = (ListPreference) findPreference(pref_key);
362 String value = preferences.getString(key, defaultValue);
365 pref.setValue(value);
366 pref.setSummary(value);
371 * BooleanPreference の表示データを設定
373 * @param pref_key Preference(表示)のキー
374 * @param key Preference(データ)のキー
375 * @param defaultValue Preferenceのデフォルト値
377 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
379 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
382 boolean value = preferences.getBoolean(key, defaultValue);
383 pref.setChecked(value);
391 private String convertKeyFromPreferenceToCameraPropertyKey(String key)
393 String target = null;
400 case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
401 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
415 public void synchronizedProperty()
417 FragmentActivity activity = getActivity();
418 if (activity == null)
422 busyDialog.dismiss();
431 activity.runOnUiThread(new Runnable() {
435 // Preferenceの画面に反映させる
436 setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
437 setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
438 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
439 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
442 findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
443 if (hardwareStatusInterface != null)
446 setHardwareSummary();
450 busyDialog.dismiss();
462 public boolean onPreferenceClick(Preference preference)
466 String preferenceKey = preference.getKey();
467 if (preferenceKey.contains(WIFI_SETTINGS))
470 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
473 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));