OSDN Git Service

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