OSDN Git Service

FUJIFILM用画像詳細画面で、タイミングによってはスモールイメージを使用できるようにした。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / preference / Gr2ControlPreferenceFragment.java
1 package net.osdn.gokigen.gr2control.preference;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.os.Bundle;
7 import android.provider.Settings;
8 import android.util.Log;
9
10 import net.osdn.gokigen.gr2control.R;
11 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
12 import net.osdn.gokigen.gr2control.camera.ricohgr2.operation.RicohGr2CameraPowerOff;
13 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
14 import net.osdn.gokigen.gr2control.scene.IChangeScene;
15
16 import java.util.Map;
17
18 import androidx.annotation.NonNull;
19 import androidx.appcompat.app.AppCompatActivity;
20 import androidx.fragment.app.FragmentActivity;
21 import androidx.preference.CheckBoxPreference;
22 import androidx.preference.ListPreference;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceFragmentCompat;
25 import androidx.preference.PreferenceManager;
26
27 public class Gr2ControlPreferenceFragment  extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
28 {
29     private final String TAG = toString();
30     //private RicohGr2CameraPowerOff powerOffController = null;
31     private IChangeScene changeScene = null;
32     private AppCompatActivity context = null;
33     private SharedPreferences preferences = null;
34     private LogCatViewer logCatViewer = null;
35
36     /**
37      *
38      *
39      */
40     public static Gr2ControlPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
41     {
42         Gr2ControlPreferenceFragment instance = new Gr2ControlPreferenceFragment();
43         instance.prepare(context, changeScene);
44
45         // パラメータはBundleにまとめておく
46         Bundle arguments = new Bundle();
47         //arguments.putString("title", title);
48         //arguments.putString("message", message);
49         instance.setArguments(arguments);
50
51         return (instance);
52     }
53
54     /**
55      *
56      *
57      */
58     private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
59     {
60         try
61         {
62             //powerOffController = new RicohGr2CameraPowerOff(context, changeScene);
63             //powerOffController.prepare();
64
65             logCatViewer = new LogCatViewer(changeScene);
66             logCatViewer.prepare();
67             this.changeScene = changeScene;
68             this.context = context;
69         }
70         catch (Exception e)
71         {
72             e.printStackTrace();
73         }
74     }
75
76     /**
77      *
78      *
79      */
80     @Override
81     public void onAttach(@NonNull Context activity)
82     {
83         super.onAttach(activity);
84         Log.v(TAG, " onAttach()");
85         try
86         {
87             // Preference をつかまえる
88             preferences = PreferenceManager.getDefaultSharedPreferences(activity);
89
90             // Preference を初期設定する
91             initializePreferences();
92
93             preferences.registerOnSharedPreferenceChangeListener(this);
94         }
95         catch (Exception e)
96         {
97             e.printStackTrace();
98         }
99     }
100
101     /**
102      * Preferenceの初期化...
103      *
104      */
105     private void initializePreferences()
106     {
107         try
108         {
109             Map<String, ?> items = preferences.getAll();
110             SharedPreferences.Editor editor = preferences.edit();
111
112             if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
113             {
114                 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
115             }
116             if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
117             {
118                 editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
119             }
120             if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
121             {
122                 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
123             }
124             if (!items.containsKey(IPreferencePropertyAccessor.RAW))
125             {
126                 editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
127             }
128             if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
129             {
130                 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
131             }
132             if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
133             {
134                 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, false);
135             }
136             if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
137             {
138                 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
139             }
140             if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
141                 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
142             }
143             if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
144                 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
145             }
146             if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
147                 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
148             }
149             if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
150                 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
151             }
152             if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
153                 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
154             }
155             if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
156                 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
157             }
158             if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW)) {
159                 editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW, false);
160             }
161             if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY)) {
162                 editor.putString(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY, IPreferencePropertyAccessor.FUJI_X_FOCUS_XY_DEFAULT_VALUE);
163             }
164             if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT)) {
165                 editor.putString(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT, IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT_DEFAULT_VALUE);
166             }
167             if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT)) {
168                 editor.putString(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT, IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT_DEFAULT_VALUE);
169             }
170             if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE)) {
171                 editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE, false);
172             }
173             editor.apply();
174         }
175         catch (Exception e)
176         {
177             e.printStackTrace();
178         }
179     }
180
181     /**
182      *
183      *
184      */
185     @Override
186     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
187     {
188         Log.v(TAG, " onSharedPreferenceChanged() : " + key);
189         boolean value;
190         if (key != null)
191         {
192             switch (key)
193             {
194                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
195                     value = preferences.getBoolean(key, true);
196                     Log.v(TAG, " " + key + " , " + value);
197                     break;
198
199                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
200                     value = preferences.getBoolean(key, false);
201                     Log.v(TAG, " BOTH LV : " + key + " , " + value);
202                     break;
203
204                 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
205                     value = preferences.getBoolean(key, true);
206                     Log.v(TAG, " PLAYBACK MENU : " + key + " , " + value);
207                     break;
208
209                 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
210                     value = preferences.getBoolean(key, false);
211                     Log.v(TAG, " SHARE : " + key + " , " + value);
212                     break;
213
214                 default:
215                     String strValue = preferences.getString(key, "");
216                     setListPreference(key, key, strValue);
217                     break;
218             }
219         }
220     }
221
222     /**
223      *
224      *
225      */
226     @Override
227     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
228     {
229         Log.v(TAG, " onCreatePreferences()");
230         try
231         {
232             //super.onCreate(savedInstanceState);
233             addPreferencesFromResource(R.xml.preferences_summary);
234
235             ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
236             if (connectionMethod != null)
237             {
238                 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
239                     @Override
240                     public boolean onPreferenceChange(Preference preference, Object newValue) {
241                         preference.setSummary(newValue + " ");
242                         return (true);
243                     }
244                 });
245                 connectionMethod.setSummary(connectionMethod.getValue() + " ");
246             }
247
248             /*
249             Preference exitApplication = findPreference("exit_application");
250             if (exitApplication != null)
251             {
252                 exitApplication.setOnPreferenceClickListener(powerOffController);
253             }
254             */
255
256             Preference opcPreference = findPreference("opc_settings");
257             if (opcPreference != null)
258             {
259                 opcPreference.setOnPreferenceClickListener(this);
260             }
261             Preference gr2Preference = findPreference("ricoh_settings");
262             if (gr2Preference != null)
263             {
264                 gr2Preference.setOnPreferenceClickListener(this);
265             }
266             Preference fujiXPreference = findPreference("fuji_x_settings");
267             if (fujiXPreference != null)
268             {
269                 fujiXPreference.setOnPreferenceClickListener(this);
270             }
271
272             Preference debug = findPreference("debug_info");
273             if (debug != null)
274             {
275                 debug.setOnPreferenceClickListener(logCatViewer);
276             }
277
278             Preference wifi = findPreference("wifi_settings");
279             if (wifi != null)
280             {
281                 wifi.setOnPreferenceClickListener(this);
282             }
283         }
284         catch (Exception e)
285         {
286             e.printStackTrace();
287         }
288     }
289
290     /**
291      *
292      *
293      */
294     @Override
295     public void onResume()
296     {
297         super.onResume();
298         Log.v(TAG, " onResume() Start");
299         try
300         {
301             synchronizedProperty();
302         }
303         catch (Exception e)
304         {
305             e.printStackTrace();
306         }
307
308         Log.v(TAG, " onResume() End");
309     }
310
311     /**
312      *
313      *
314      */
315     @Override
316     public void onPause()
317     {
318         super.onPause();
319         Log.v(TAG, " onPause() Start");
320         try
321         {
322             // Preference変更のリスナを解除
323             preferences.unregisterOnSharedPreferenceChangeListener(this);
324         }
325         catch (Exception e)
326         {
327             e.printStackTrace();
328         }
329
330         Log.v(TAG, " onPause() End");
331     }
332
333     /**
334      * ListPreference の表示データを設定
335      *
336      * @param pref_key     Preference(表示)のキー
337      * @param key          Preference(データ)のキー
338      * @param defaultValue Preferenceのデフォルト値
339      */
340     private void setListPreference(String pref_key, String key, String defaultValue)
341     {
342         try
343         {
344             ListPreference pref;
345             pref = findPreference(pref_key);
346             String value = preferences.getString(key, defaultValue);
347             if (pref != null)
348             {
349                 pref.setValue(value);
350                 pref.setSummary(value);
351             }
352         }
353         catch (Exception e)
354         {
355             e.printStackTrace();
356         }
357     }
358
359     /**
360      * BooleanPreference の表示データを設定
361      *
362      * @param pref_key     Preference(表示)のキー
363      * @param key          Preference(データ)のキー
364      * @param defaultValue Preferenceのデフォルト値
365      */
366     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
367     {
368         try
369         {
370             CheckBoxPreference pref = findPreference(pref_key);
371             if (pref != null)
372             {
373                 boolean value = preferences.getBoolean(key, defaultValue);
374                 pref.setChecked(value);
375             }
376         }
377         catch (Exception e)
378         {
379             e.printStackTrace();
380         }
381     }
382
383     /**
384      *
385      *
386      */
387     private void synchronizedProperty()
388     {
389         final FragmentActivity activity = getActivity();
390         if (activity != null)
391         {
392             activity.runOnUiThread(new Runnable() {
393                 @Override
394                 public void run() {
395                     try
396                     {
397                         // Preferenceの画面に反映させる
398                         setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
399                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, false);
400                         setBooleanPreference(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
401                         setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
402                     }
403                     catch (Exception e)
404                     {
405                         e.printStackTrace();
406                     }
407                 }
408             });
409         }
410     }
411
412
413     @Override
414     public boolean onPreferenceClick(Preference preference)
415     {
416         try
417         {
418             String preferenceKey = preference.getKey();
419             Log.v(TAG, " onPreferenceClick() : " + preferenceKey);
420             if (preferenceKey.contains("wifi_settings"))
421             {
422                 // Wifi 設定画面を表示する
423                 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
424                 if (context != null)
425                 {
426                     context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
427                 }
428             }
429             else if (preferenceKey.contains("ricoh_settings"))
430             {
431                 changeScene.changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod.RICOH_GR2);
432             }
433             else if (preferenceKey.contains("fuji_x_settings"))
434             {
435                 changeScene.changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod.FUJI_X);
436             }
437             else if (preferenceKey.contains("opc_settings"))
438             {
439                 changeScene.changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod.OPC);
440             }
441             else
442             {
443                 return (false);
444             }
445             return (true);
446         }
447         catch (Exception e)
448         {
449             e.printStackTrace();
450         }
451         return (false);
452     }
453 }