OSDN Git Service

FUJIFILM用画像詳細画面で、タイミングによってはスモールイメージを使用できるようにした。
[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.app.ProgressDialog;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.os.Bundle;
8 import android.provider.Settings;
9 import android.util.Log;
10
11 import net.osdn.gokigen.gr2control.R;
12 import net.osdn.gokigen.gr2control.camera.ICameraHardwareStatus;
13 import net.osdn.gokigen.gr2control.camera.ICameraRunMode;
14 import net.osdn.gokigen.gr2control.camera.ICameraRunModeCallback;
15 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
16 import net.osdn.gokigen.gr2control.camera.olympus.operation.CameraPowerOff;
17 import net.osdn.gokigen.gr2control.camera.olympus.wrapper.property.IOlyCameraProperty;
18 import net.osdn.gokigen.gr2control.camera.olympus.wrapper.property.IOlyCameraPropertyProvider;
19 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
20 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
21 import net.osdn.gokigen.gr2control.scene.IChangeScene;
22
23 import java.util.HashMap;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import androidx.annotation.NonNull;
28 import androidx.appcompat.app.AppCompatActivity;
29 import androidx.fragment.app.FragmentActivity;
30 import androidx.preference.CheckBoxPreference;
31 import androidx.preference.ListPreference;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceFragmentCompat;
34 import androidx.preference.PreferenceManager;
35 import jp.co.olympus.camerakit.OLYCamera;
36
37
38 /**
39  *   SettingFragment
40  *
41  */
42 public class PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, PreferenceSynchronizer.IPropertySynchronizeCallback, Preference.OnPreferenceClickListener, ICameraRunModeCallback
43 {
44     private final String TAG = toString();
45     private AppCompatActivity context = null;
46     private IOlyCameraPropertyProvider propertyInterface = null;
47     private ICameraHardwareStatus hardwareStatusInterface = null;
48     private ICameraRunMode changeRunModeExecutor = null;
49     private CameraPowerOff powerOffController = null;
50     private LogCatViewer logCatViewer = null;
51     private SharedPreferences preferences = null;
52     private ProgressDialog busyDialog = null;
53     private PreferenceSynchronizer preferenceSynchronizer = null;
54
55
56     public static PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
57     {
58         PreferenceFragment instance = new PreferenceFragment();
59         instance.setInterface(context, factory, changeScene);
60
61         // パラメータはBundleにまとめておく
62         Bundle arguments = new Bundle();
63         //arguments.putString("title", title);
64         //arguments.putString("message", message);
65         instance.setArguments(arguments);
66
67         return (instance);
68     }
69
70     /**
71      *
72      *
73      */
74     private void setInterface(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
75     {
76         Log.v(TAG, "setInterface()");
77         try
78         {
79             this.propertyInterface = factory.getOlympusInterfaceProvider().getCameraPropertyProvider();
80             this.changeRunModeExecutor = factory.getCameraRunMode();
81             hardwareStatusInterface = factory.getHardwareStatus();
82             powerOffController = new CameraPowerOff(context, changeScene);
83             powerOffController.prepare();
84             logCatViewer = new LogCatViewer(changeScene);
85             logCatViewer.prepare();
86
87             this.context = context;
88         }
89         catch (Exception e)
90         {
91             e.printStackTrace();
92         }
93     }
94
95     /**
96      *
97      *
98      */
99     @Override
100     public void onAttach(@NonNull Context activity)
101     {
102         super.onAttach(activity);
103         Log.v(TAG, "onAttach()");
104         try
105         {
106             // Preference をつかまえる
107             preferences = PreferenceManager.getDefaultSharedPreferences(activity);
108             if (preferenceSynchronizer == null)
109             {
110                 preferenceSynchronizer = new PreferenceSynchronizer(this.propertyInterface, preferences, this);
111             }
112
113             // Preference を初期設定する
114             initializePreferences();
115
116             preferences.registerOnSharedPreferenceChangeListener(this);
117         }
118         catch (Exception e)
119         {
120             e.printStackTrace();
121         }
122     }
123
124     /**
125      * Preferenceの初期化...
126      */
127     private void initializePreferences()
128     {
129         Map<String, ?> items = preferences.getAll();
130         SharedPreferences.Editor editor = preferences.edit();
131         if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
132         {
133             editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
134         }
135         if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
136         {
137             editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
138         }
139         if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
140         {
141             editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
142         }
143         if (!items.containsKey(IPreferencePropertyAccessor.RAW))
144         {
145             editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
146         }
147         if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
148         {
149             editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
150         }
151         if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
152         {
153             editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
154         }
155         if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
156         {
157             editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
158         }
159         if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
160             editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
161         }
162         if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
163             editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
164         }
165         if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
166             editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
167         }
168         if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
169             editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
170         }
171         if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
172             editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
173         }
174         if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
175             editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
176         }
177         if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW)) {
178             editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW, false);
179         }
180         if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY)) {
181             editor.putString(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY, IPreferencePropertyAccessor.FUJI_X_FOCUS_XY_DEFAULT_VALUE);
182         }
183         if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT)) {
184             editor.putString(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT, IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT_DEFAULT_VALUE);
185         }
186         if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT)) {
187             editor.putString(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT, IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT_DEFAULT_VALUE);
188         }
189         if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE)) {
190             editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE, false);
191         }
192         editor.apply();
193     }
194
195     /**
196      *
197      *
198      */
199     @Override
200     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
201     {
202         Log.v(TAG, "onCreatePreferences()");
203
204         //super.onCreate(savedInstanceState);
205         addPreferencesFromResource(R.xml.preferences_opc);
206
207         try
208         {
209             final HashMap<String, String> sizeTable = new HashMap<>();
210             sizeTable.put("QVGA", "(320x240)");
211             sizeTable.put("VGA", "(640x480)");
212             sizeTable.put("SVGA", "(800x600)");
213             sizeTable.put("XGA", "(1024x768)");
214             sizeTable.put("QUAD_VGA", "(1280x960)");
215
216             ListPreference liveViewQuality = (ListPreference) findPreference(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY);
217             liveViewQuality.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
218                 @Override
219                 public boolean onPreferenceChange(Preference preference, Object newValue) {
220                     String key = (String) newValue;
221                     preference.setSummary(newValue + " " + sizeTable.get(key));
222                     return (true);
223                 }
224             });
225             liveViewQuality.setSummary(liveViewQuality.getValue() + " " + sizeTable.get(liveViewQuality.getValue()));
226
227             ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
228             connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
229                 @Override
230                 public boolean onPreferenceChange(Preference preference, Object newValue) {
231                     preference.setSummary(newValue + " ");
232                     return (true);
233                 }
234             });
235             connectionMethod.setSummary(connectionMethod.getValue() + " ");
236
237             findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
238             findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
239             findPreference("wifi_settings").setOnPreferenceClickListener(this);
240         }
241         catch (Exception e)
242         {
243             e.printStackTrace();
244         }
245     }
246
247     /**
248      * ハードウェアのサマリ情報を取得し設定する
249      */
250     private void setHardwareSummary()
251     {
252         try
253         {
254             // レンズ状態
255             findPreference("lens_status").setSummary(hardwareStatusInterface.getLensMountStatus());
256
257             // メディア状態
258             findPreference("media_status").setSummary(hardwareStatusInterface.getMediaMountStatus());
259
260             // 焦点距離
261             String focalLength;
262             float minLength = hardwareStatusInterface.getMinimumFocalLength();
263             float maxLength = hardwareStatusInterface.getMaximumFocalLength();
264             float actualLength = hardwareStatusInterface.getActualFocalLength();
265             if (minLength == maxLength)
266             {
267                 focalLength = String.format(Locale.ENGLISH, "%3.0fmm", actualLength);
268             }
269             else
270             {
271                 focalLength = String.format(Locale.ENGLISH, "%3.0fmm - %3.0fmm (%3.0fmm)", minLength, maxLength, actualLength);
272             }
273             findPreference("focal_length").setSummary(focalLength);
274         }
275         catch (Exception e)
276         {
277             e.printStackTrace();
278         }
279
280         // カメラのバージョン
281         try
282         {
283             Map<String, Object> hardwareInformation = hardwareStatusInterface.inquireHardwareInformation();
284             findPreference("camera_version").setSummary((String) hardwareInformation.get(OLYCamera.HARDWARE_INFORMATION_CAMERA_FIRMWARE_VERSION_KEY));
285
286             // 取得した一覧はログに出力する。)
287             Log.v(TAG, "- - - - - - - - - -");
288             for (Map.Entry<String, Object> entry : hardwareInformation.entrySet())
289             {
290                 String value = (String) entry.getValue();
291                 Log.v(TAG, entry.getKey() + " : " + value);
292             }
293             Log.v(TAG, "- - - - - - - - - -");
294         }
295         catch (Exception e)
296         {
297             findPreference("camera_version").setSummary("Unknown");
298             e.printStackTrace();
299         }
300     }
301
302     /**
303      *
304      *
305      */
306     private void setCameraProperty(String name, String value)
307     {
308         try
309         {
310             String propertyValue = "<" + name + "/" + value + ">";
311             Log.v(TAG, "setCameraProperty() : " + propertyValue);
312             propertyInterface.setCameraPropertyValue(name, propertyValue);
313         }
314         catch (Exception e)
315         {
316             e.printStackTrace();
317         }
318     }
319
320     /**
321      *
322      *
323      */
324     @Override
325     public void onResume()
326     {
327         super.onResume();
328         Log.v(TAG, "onResume() Start");
329         try
330         {
331             // 撮影モードかどうかを確認して、撮影モードではなかったら撮影モードに切り替える
332             if ((changeRunModeExecutor != null) && (!changeRunModeExecutor.isRecordingMode()))
333             {
334                 // Runモードを切り替える。(でも切り替えると、設定がクリアされてしまう...。
335                 changeRunModeExecutor.changeRunMode(true, this);
336                 return;
337             }
338             synchronizeCameraProperties(true);
339         }
340         catch (Exception e)
341         {
342             e.printStackTrace();
343         }
344         Log.v(TAG, "onResume() End");
345     }
346
347     @Override
348     public void onCompleted(boolean isRecording)
349     {
350         synchronizeCameraProperties(true);
351     }
352
353     @Override
354     public void onErrorOccurred(boolean isRecording)
355     {
356         Log.v(TAG, " onErrorOccurred()");
357         synchronizeCameraProperties(true);
358     }
359
360     /**
361      *
362      *
363      */
364     @Override
365     public void onPause()
366     {
367         super.onPause();
368         Log.v(TAG, "onPause() Start");
369
370         // Preference変更のリスナを解除
371         preferences.unregisterOnSharedPreferenceChangeListener(this);
372
373         Log.v(TAG, "onPause() End");
374     }
375
376     /**
377      * カメラプロパティとPreferenceとの同期処理を実行
378      */
379     private void synchronizeCameraProperties(boolean isPropertyLoad)
380     {
381         // 実行中ダイアログを取得する
382         busyDialog = new ProgressDialog(getActivity());
383         busyDialog.setTitle(getString(R.string.dialog_title_loading_properties));
384         busyDialog.setMessage(getString(R.string.dialog_message_loading_properties));
385         busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
386         busyDialog.setCancelable(false);
387         busyDialog.show();
388
389         // データ読み込み処理(別スレッドで実行)
390         if (isPropertyLoad)
391         {
392             new Thread(preferenceSynchronizer).start();
393         }
394     }
395
396     /**
397      * Preferenceが更新された時に呼び出される処理
398      *
399      * @param sharedPreferences sharedPreferences
400      * @param key               変更されたキー
401      */
402     @Override
403     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
404     {
405         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
406         String propertyValue;
407         boolean value;
408         if (key != null)
409         {
410             switch (key)
411             {
412                 case IPreferencePropertyAccessor.RAW:
413                     value = preferences.getBoolean(key, true);
414                     setBooleanPreference(key, key, value);
415                     propertyValue = (value) ? "ON" : "OFF";
416                     setCameraProperty(IOlyCameraProperty.RAW, propertyValue);
417                     break;
418
419                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
420                     value = preferences.getBoolean(key, true);
421                     Log.v(TAG, " " + key + " , " + value);
422                     break;
423
424                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
425                     value = preferences.getBoolean(key, true);
426                     Log.v(TAG, " " + key + " , " + value);
427                     break;
428
429                 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
430                     value = preferences.getBoolean(key, false);
431                     Log.v(TAG, " " + key + " , " + value);
432                     break;
433
434                 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
435                     value = preferences.getBoolean(key, false);
436                     Log.v(TAG, " " + key + " , " + value);
437                     break;
438
439                 default:
440                     String strValue = preferences.getString(key, "");
441                     setListPreference(key, key, strValue);
442                     String propertyKey = convertKeyFromPreferenceToCameraPropertyKey(key);
443                     if (propertyKey != null)
444                     {
445                         setCameraProperty(propertyKey, strValue);
446                     }
447                     break;
448             }
449         }
450     }
451
452     /**
453      * ListPreference の表示データを設定
454      *
455      * @param pref_key     Preference(表示)のキー
456      * @param key          Preference(データ)のキー
457      * @param defaultValue Preferenceのデフォルト値
458      */
459     private void setListPreference(String pref_key, String key, String defaultValue)
460     {
461         ListPreference pref;
462         pref = (ListPreference) findPreference(pref_key);
463         String value = preferences.getString(key, defaultValue);
464         if (pref != null)
465         {
466             pref.setValue(value);
467             pref.setSummary(value);
468         }
469     }
470
471     /**
472      * BooleanPreference の表示データを設定
473      *
474      * @param pref_key     Preference(表示)のキー
475      * @param key          Preference(データ)のキー
476      * @param defaultValue Preferenceのデフォルト値
477      */
478     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
479     {
480         CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
481         if (pref != null)
482         {
483             boolean value = preferences.getBoolean(key, defaultValue);
484             pref.setChecked(value);
485         }
486     }
487
488     /**
489      *
490      *
491      */
492     private String convertKeyFromPreferenceToCameraPropertyKey(String key)
493     {
494         String target = null;
495         if (key == null)
496         {
497             return (null);
498         }
499         switch (key)
500         {
501             case IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL:
502                 target = IOlyCameraProperty.SOUND_VOLUME_LEVEL;
503                 break;
504
505             default:
506                 // target == null
507                 break;
508         }
509         return (target);
510     }
511
512     /**
513      * カメラプロパティの同期処理終了通知
514      */
515     @Override
516     public void synchronizedProperty()
517     {
518         FragmentActivity activity = getActivity();
519         if (activity == null)
520         {
521             try
522             {
523                 busyDialog.dismiss();
524                 busyDialog = null;
525             }
526             catch (Exception e)
527             {
528                 e.printStackTrace();
529             }
530             return;
531         }
532         activity.runOnUiThread(new Runnable() {
533             @Override
534             public void run() {
535                 try {
536                     // Preferenceの画面に反映させる
537                     setListPreference(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
538                     setBooleanPreference(IPreferencePropertyAccessor.RAW, IPreferencePropertyAccessor.RAW, true);
539                     setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
540                     setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
541
542                     // カメラキットのバージョン
543                     findPreference(IPreferencePropertyAccessor.CAMERAKIT_VERSION).setSummary(OLYCamera.getVersion());
544                     if (hardwareStatusInterface != null)
545                     {
546                         // その他のハードウェア情報の情報設定
547                         setHardwareSummary();
548                     }
549
550                     // 実行中ダイアログを消す
551                     busyDialog.dismiss();
552                     busyDialog = null;
553                 }
554                 catch (Exception e)
555                 {
556                     e.printStackTrace();
557                 }
558             }
559         });
560     }
561
562
563     @Override
564     public boolean onPreferenceClick(Preference preference)
565     {
566         try
567         {
568             String preferenceKey = preference.getKey();
569             if (preferenceKey.contains("wifi_settings"))
570             {
571                 // Wifi 設定画面を表示する
572                 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
573                 if (context != null)
574                 {
575                     context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
576                 }
577             }
578             return (true);
579         }
580         catch (Exception e)
581         {
582             e.printStackTrace();
583         }
584         return (false);
585     }
586 }