OSDN Git Service

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