OSDN Git Service

Pentax DSLR機で、AFを動作後シャッターを切る設定ができるよう変更する。(でも、再起動必要...)
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / preference / olympus / PreferenceFragment.java
1 package net.osdn.gokigen.gr2control.preference.olympus;
2
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;
16
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;
27
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import jp.co.olympus.camerakit.OLYCamera;
33
34
35 /**
36  *   SettingFragment
37  *
38  */
39 public class PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, PreferenceSynchronizer.IPropertySynchronizeCallback
40 {
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;
50
51
52     public static PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
53     {
54         PreferenceFragment instance = new PreferenceFragment();
55         instance.setInterface(context, factory, changeScene);
56
57         // パラメータはBundleにまとめておく
58         Bundle arguments = new Bundle();
59         //arguments.putString("title", title);
60         //arguments.putString("message", message);
61         instance.setArguments(arguments);
62
63         return (instance);
64     }
65
66     /**
67      *
68      *
69      */
70     private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
71     {
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();
80     }
81
82     /**
83      *
84      *
85      */
86     @Override
87     public void onAttach(Context activity)
88     {
89         super.onAttach(activity);
90         Log.v(TAG, "onAttach()");
91
92         // Preference をつかまえる
93         preferences = PreferenceManager.getDefaultSharedPreferences(activity);
94         if (preferenceSynchronizer == null)
95         {
96             preferenceSynchronizer = new PreferenceSynchronizer(this.propertyInterface, preferences, this);
97         }
98
99         // Preference を初期設定する
100         initializePreferences();
101
102         preferences.registerOnSharedPreferenceChangeListener(this);
103     }
104
105     /**
106      * Preferenceの初期化...
107      */
108     private void initializePreferences()
109     {
110         Map<String, ?> items = preferences.getAll();
111         SharedPreferences.Editor editor = preferences.edit();
112
113         if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
114         {
115             editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
116         }
117         if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
118         {
119             editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
120         }
121         if (!items.containsKey(IPreferencePropertyAccessor.RAW))
122         {
123             editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
124         }
125         if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
126         {
127             editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
128         }
129         if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
130         {
131             editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
132         }
133         if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
134         {
135             editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
136         }
137         if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
138             editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
139         }
140         if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
141             editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, false);
142         }
143         if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
144             editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
145         }
146         if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
147             editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
148         }
149         if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
150             editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
151         }
152         if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
153             editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
154         }
155         editor.apply();
156     }
157
158     /**
159      *
160      *
161      */
162     @Override
163     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
164     {
165         Log.v(TAG, "onCreatePreferences()");
166
167         //super.onCreate(savedInstanceState);
168         addPreferencesFromResource(R.xml.preferences_opc);
169
170         {
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)");
177
178             ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
179             liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
180                 @Override
181                 public boolean onPreferenceChange(Preference preference, Object newValue) {
182                     String key = (String) newValue;
183                     preference.setSummary(newValue + " " + sizeTable.get(key));
184                     return (true);
185                 }
186             });
187             liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
188
189             ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
190             connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
191                 @Override
192                 public boolean onPreferenceChange(Preference preference, Object newValue) {
193                     preference.setSummary(newValue + " ");
194                     return (true);
195                 }
196             });
197             connectionMethod.setSummary(connectionMethod.getValue() + " ");
198         }
199         findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
200         findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
201     }
202
203     /**
204      * ハードウェアのサマリ情報を取得し設定する
205      */
206     private void setHardwareSummary()
207     {
208         // レンズ状態
209         findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
210
211         // メディア状態
212         findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
213
214         // 焦点距離
215         String focalLength;
216         float minLength = hardwareStatusInterface.getMinimumFocalLength();
217         float maxLength = hardwareStatusInterface.getMaximumFocalLength();
218         float actualLength = hardwareStatusInterface.getActualFocalLength();
219         if (minLength == maxLength)
220         {
221             focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
222         }
223         else
224         {
225             focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
226         }
227         findPreference("focal_length").setSummary(focalLength);
228
229         // カメラのバージョン
230         try
231         {
232             Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
233             findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
234
235             // 取得した一覧はログに出力する。)
236             Log.v(TAG, "- - - - - - - - - -");
237             for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
238             {
239                 String value = (String) entry.getValue();
240                 Log.v(TAG, entry.getKey() + " : " + value);
241             }
242             Log.v(TAG, "- - - - - - - - - -");
243         }
244         catch (Exception e)
245         {
246             findPreference("camera_version").setSummary("Unknown");
247             e.printStackTrace();
248         }
249     }
250
251     /**
252      *
253      *
254      */
255     private void setCameraProperty(String name, String value)
256     {
257         try
258         {
259             String propertyValue = "<" + name + "/" + value + ">";
260             Log.v(TAG, "setCameraProperty() : " + propertyValue);
261             propertyInterface.setCameraPropertyValue(name, propertyValue);
262         }
263         catch (Exception e)
264         {
265             e.printStackTrace();
266         }
267     }
268
269     /**
270      *
271      *
272      */
273     @Override
274     public void onResume()
275     {
276         super.onResume();
277         Log.v(TAG, "onResume() Start");
278
279         // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
280         if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
281         {
282             // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
283             changeRunModeExecutor.changeRunMode(true);
284         }
285         synchronizeCameraProperties(true);
286         Log.v(TAG, "onResume() End");
287
288     }
289
290     /**
291      *
292      *
293      */
294     @Override
295     public void onPause()
296     {
297         super.onPause();
298         Log.v(TAG, "onPause() Start");
299
300         // Preference変更のリスナを解除
301         preferences.unregisterOnSharedPreferenceChangeListener(this);
302
303         Log.v(TAG, "onPause() End");
304     }
305
306     /**
307      * カメラプロパティとPreferenceとの同期処理を実行
308      */
309     private void synchronizeCameraProperties(boolean isPropertyLoad)
310     {
311         // 実行中ダイアログを取得する
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);
317         busyDialog.show();
318
319         // データ読み込み処理(別スレッドで実行)
320         if (isPropertyLoad)
321         {
322             new Thread(preferenceSynchronizer).start();
323         }
324     }
325
326     /**
327      * Preferenceが更新された時に呼び出される処理
328      *
329      * @param sharedPreferences sharedPreferences
330      * @param key               変更されたキー
331      */
332     @Override
333     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
334     {
335         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
336         String propertyValue;
337         boolean value;
338         if (key != null)
339         {
340             switch (key)
341             {
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);
347                     break;
348
349                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
350                     value = preferences.getBoolean(key, true);
351                     Log.v(TAG, " " + key + " , " + value);
352                     break;
353
354                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
355                     value = preferences.getBoolean(key, true);
356                     Log.v(TAG, " " + key + " , " + value);
357                     break;
358
359                 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
360                     value = preferences.getBoolean(key, false);
361                     Log.v(TAG, " " + key + " , " + value);
362                     break;
363
364                 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
365                     value = preferences.getBoolean(key, false);
366                     Log.v(TAG, " " + key + " , " + value);
367                     break;
368
369                 default:
370                     String strValue = preferences.getString(key, "");
371                     setListPreference(key, key, strValue);
372                     String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
373                     if (propertyKey != null)
374                     {
375                         setCameraProperty(propertyKey, strValue);
376                     }
377                     break;
378             }
379         }
380     }
381
382     /**
383      * ListPreference の表示データを設定
384      *
385      * @param pref_key     Preference(表示)のキー
386      * @param key          Preference(データ)のキー
387      * @param defaultValue Preferenceのデフォルト値
388      */
389     private void setListPreference(String pref_key, String key, String defaultValue)
390     {
391         ListPreference pref;
392         pref = (ListPreference) findPreference(pref_key);
393         String value = preferences.getString(key, defaultValue);
394         if (pref != null)
395         {
396             pref.setValue(value);
397             pref.setSummary(value);
398         }
399     }
400
401     /**
402      * BooleanPreference の表示データを設定
403      *
404      * @param pref_key     Preference(表示)のキー
405      * @param key          Preference(データ)のキー
406      * @param defaultValue Preferenceのデフォルト値
407      */
408     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
409     {
410         CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
411         if (pref != null)
412         {
413             boolean value = preferences.getBoolean(key, defaultValue);
414             pref.setChecked(value);
415         }
416     }
417
418     /**
419      *
420      *
421      */
422     private String convertKeyFromPreferenceToCameraPropertyKey(String key)
423     {
424         String target = null;
425         if (key == null)
426         {
427             return (null);
428         }
429         switch (key)
430         {
431             case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
432                 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
433                 break;
434
435             default:
436                 // target == null
437                 break;
438         }
439         return (target);
440     }
441
442     /**
443      * カメラプロパティの同期処理終了通知
444      */
445     @Override
446     public void synchronizedProperty()
447     {
448         FragmentActivity activity = getActivity();
449         if (activity == null)
450         {
451             try
452             {
453                 busyDialog.dismiss();
454                 busyDialog = null;
455             }
456             catch (Exception e)
457             {
458                 e.printStackTrace();
459             }
460             return;
461         }
462         activity.runOnUiThread(new Runnable() {
463             @Override
464             public void run() {
465                 try {
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);
471
472                     // カメラキットのバージョン
473                     findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
474                     if (hardwareStatusInterface != null)
475                     {
476                         // その他のハードウェア情報の情報設定
477                         setHardwareSummary();
478                     }
479
480                     // 実行中ダイアログを消す
481                     busyDialog.dismiss();
482                     busyDialog = null;
483                 }
484                 catch (Exception e)
485                 {
486                     e.printStackTrace();
487                 }
488             }
489         });
490     }
491 }