OSDN Git Service

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