OSDN Git Service

一部フィルムシミュレーションの設定ができなかったのを修正する。
[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             editor.apply();
169         }
170         catch (Exception e)
171         {
172             e.printStackTrace();
173         }
174     }
175
176     /**
177      *
178      *
179      */
180     @Override
181     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
182     {
183         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
184         boolean value;
185         if (key != null)
186         {
187             switch (key)
188             {
189                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
190                     value = preferences.getBoolean(key, true);
191                     Log.v(TAG, " " + key + " , " + value);
192                     break;
193
194                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
195                     value = preferences.getBoolean(key, true);
196                     Log.v(TAG, " CAPTURE BOTH CAMERA AND LIVE VIEW : " + key + " , " + value);
197                     break;
198
199                 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
200                     value = preferences.getBoolean(key, false);
201                     Log.v(TAG, " " + key + " , " + value);
202                     break;
203
204                 case IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW:
205                     value = preferences.getBoolean(key, false);
206                     Log.v(TAG, " DISPLAY CAMERA VIEW : " + key + " , " + value);
207                     break;
208
209                 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
210                     value = preferences.getBoolean(key, false);
211                     Log.v(TAG, " SHARE AFTER SAVE : " + 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_fuji_x);
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             Preference exitPreference = findPreference("exit_application");
248             if (exitPreference != null)
249             {
250                 exitPreference.setOnPreferenceClickListener(powerOffController);
251             }
252             Preference debugPreference = findPreference("debug_info");
253             if (debugPreference != null)
254             {
255                 debugPreference.setOnPreferenceClickListener(logCatViewer);
256             }
257             Preference wifiPreference = findPreference("wifi_settings");
258             if (wifiPreference != null)
259             {
260                 wifiPreference.setOnPreferenceClickListener(this);
261             }
262         }
263         catch (Exception e)
264         {
265             e.printStackTrace();
266         }
267     }
268
269     /**
270      *
271      *
272      */
273     @Override
274     public void onResume()
275     {
276         super.onResume();
277         Log.v(TAG, "onResume() Start");
278         try
279         {
280             synchronizedProperty();
281         }
282         catch (Exception e)
283         {
284             e.printStackTrace();
285         }
286
287         Log.v(TAG, "onResume() End");
288     }
289
290     /**
291      *
292      *
293      */
294     @Override
295     public void onPause()
296     {
297         super.onPause();
298         Log.v(TAG, "onPause() Start");
299         try
300         {
301             // Preference変更のリスナを解除
302             preferences.unregisterOnSharedPreferenceChangeListener(this);
303         }
304         catch (Exception e)
305         {
306             e.printStackTrace();
307         }
308
309         Log.v(TAG, "onPause() End");
310     }
311
312     /**
313      * ListPreference の表示データを設定
314      *
315      * @param pref_key     Preference(表示)のキー
316      * @param key          Preference(データ)のキー
317      * @param defaultValue Preferenceのデフォルト値
318      */
319     private void setListPreference(String pref_key, String key, String defaultValue)
320     {
321         try
322         {
323             ListPreference pref;
324             pref = findPreference(pref_key);
325             String value = preferences.getString(key, defaultValue);
326             if (pref != null)
327             {
328                 pref.setValue(value);
329                 pref.setSummary(value);
330             }
331         }
332         catch (Exception e)
333         {
334             e.printStackTrace();
335         }
336     }
337
338     /**
339      * BooleanPreference の表示データを設定
340      *
341      * @param pref_key     Preference(表示)のキー
342      * @param key          Preference(データ)のキー
343      * @param defaultValue Preferenceのデフォルト値
344      */
345     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
346     {
347         try
348         {
349             CheckBoxPreference pref = findPreference(pref_key);
350             if (pref != null) {
351                 boolean value = preferences.getBoolean(key, defaultValue);
352                 pref.setChecked(value);
353             }
354         }
355         catch (Exception e)
356         {
357             e.printStackTrace();
358         }
359     }
360
361     /**
362      *
363      *
364      */
365     private void synchronizedProperty()
366     {
367         final FragmentActivity activity = getActivity();
368         final boolean defaultValue = true;
369         if (activity != null)
370         {
371             activity.runOnUiThread(new Runnable() {
372                 @Override
373                 public void run() {
374                     try
375                     {
376                         // Preferenceの画面に反映させる
377                         setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
378                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
379                         setBooleanPreference(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, IPreferencePropertyAccessor.USE_PLAYBACK_MENU, defaultValue);
380                         setBooleanPreference(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW, false);
381                         setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, defaultValue);
382                     }
383                     catch (Exception e)
384                     {
385                         e.printStackTrace();
386                     }
387                 }
388             });
389         }
390     }
391
392     @Override
393     public boolean onPreferenceClick(Preference preference)
394     {
395         try
396         {
397             String preferenceKey = preference.getKey();
398             if (preferenceKey.contains("wifi_settings"))
399             {
400                 // Wifi 設定画面を表示する
401                 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
402                 if (context != null)
403                 {
404                     context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
405                 }
406             }
407             return (true);
408         }
409         catch (Exception e)
410         {
411             e.printStackTrace();
412         }
413         return (false);
414     }
415 }