OSDN Git Service

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