OSDN Git Service

フォーカスのエリアサイズを変更できるようにした。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / fuji / preference / FujiPreferenceFragment.java
1 package net.osdn.gokigen.cameratest.fuji.preference;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.util.Log;
7
8 import androidx.annotation.NonNull;
9 import androidx.fragment.app.FragmentActivity;
10 import androidx.preference.CheckBoxPreference;
11 import androidx.preference.EditTextPreference;
12 import androidx.preference.ListPreference;
13 import androidx.preference.Preference;
14 import androidx.preference.PreferenceFragmentCompat;
15 import androidx.preference.PreferenceManager;
16
17 import net.osdn.gokigen.cameratest.IApplicationControl;
18 import net.osdn.gokigen.cameratest.R;
19
20 import java.net.Inet4Address;
21 import java.util.Map;
22
23 public class FujiPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
24 {
25     private final String TAG = toString();
26     private SharedPreferences preferences = null;
27     private PowerOffController powerOffController = null;
28
29     /**
30      *
31      *
32      */
33     public static FujiPreferenceFragment newInstance(@NonNull Context context, @NonNull IApplicationControl control)
34     {
35         FujiPreferenceFragment instance = new FujiPreferenceFragment();
36         instance.prepare(context, control);
37
38         // パラメータはBundleにまとめておく
39         Bundle arguments = new Bundle();
40         //arguments.putString("title", title);
41         //arguments.putString("message", message);
42         instance.setArguments(arguments);
43
44         return (instance);
45     }
46
47     private void prepare(@NonNull Context context, @NonNull IApplicationControl control)
48     {
49         powerOffController = new PowerOffController(context, control);
50     }
51
52     /**
53      *
54      *
55      */
56     @Override
57     public void onAttach(@NonNull Context activity)
58     {
59         super.onAttach(activity);
60         Log.v(TAG, "onAttach()");
61         try
62         {
63             // Preference をつかまえる
64             preferences = PreferenceManager.getDefaultSharedPreferences(activity);
65
66             // Preference を初期設定する
67             initializePreferences();
68
69             preferences.registerOnSharedPreferenceChangeListener(this);
70         }
71         catch (Exception e)
72         {
73             e.printStackTrace();
74         }
75     }
76
77     @Override
78     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
79     {
80         try
81         {
82             setPreferencesFromResource(R.xml.preferences_fuji_x, rootKey);
83
84             ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
85             connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
86                 @Override
87                 public boolean onPreferenceChange(Preference preference, Object newValue) {
88                     preference.setSummary(newValue + " ");
89                     return (true);
90                 }
91             });
92             connectionMethod.setSummary(connectionMethod.getValue() + " ");
93
94             findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
95         }
96         catch (Exception e)
97         {
98             e.printStackTrace();
99         }
100         try
101         {
102             EditTextPreference focusResolition = (EditTextPreference) findPreference(IPreferencePropertyAccessor.FUJIX_FOCUS_XY);
103             String[] focus = focusResolition.getText().split(",");
104             if (focus.length != 2)
105             {
106                 focusResolition.setText(IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
107             }
108             else
109             {
110                 int x = Integer.parseInt(focus[0]);
111                 int y = Integer.parseInt(focus[1]);
112                 focusResolition.setText(x + "," + y);
113             }
114         }
115         catch (Exception e)
116         {
117             e.printStackTrace();
118         }
119     }
120
121     /**
122      * Preferenceの初期化...
123      *
124      */
125     private void initializePreferences()
126     {
127         try
128         {
129             Map<String, ?> items = preferences.getAll();
130             SharedPreferences.Editor editor = preferences.edit();
131
132             if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
133                 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
134             }
135             if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
136                 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
137             }
138             if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW)) {
139                 editor.putBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
140             }
141             if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_FOCUS_XY)) {
142                 editor.putString(IPreferencePropertyAccessor.FUJIX_FOCUS_XY, IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
143             }
144             editor.apply();
145         }
146         catch (Exception e)
147         {
148             e.printStackTrace();
149         }
150     }
151
152
153     @Override
154     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
155     {
156         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
157         boolean value;
158         if (key != null)
159         {
160             switch (key)
161             {
162                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
163                     value = preferences.getBoolean(key, true);
164                     Log.v(TAG, " " + key + " , " + value);
165                     break;
166
167                 case IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW:
168                     value = preferences.getBoolean(key, false);
169                     Log.v(TAG, " " + key + " , " + value);
170                     break;
171
172                 default:
173                     String strValue = preferences.getString(key, "");
174                     setListPreference(key, key, strValue);
175                     break;
176             }
177         }
178     }
179
180     /**
181      *
182      *
183      */
184     @Override
185     public void onResume()
186     {
187         super.onResume();
188         Log.v(TAG, "onResume() Start");
189         try
190         {
191             synchronizedProperty();
192         }
193         catch (Exception e)
194         {
195             e.printStackTrace();
196         }
197
198         Log.v(TAG, "onResume() End");
199     }
200
201     /**
202      *
203      *
204      */
205     @Override
206     public void onPause()
207     {
208         super.onPause();
209         Log.v(TAG, "onPause() Start");
210         try
211         {
212             // Preference変更のリスナを解除
213             preferences.unregisterOnSharedPreferenceChangeListener(this);
214         }
215         catch (Exception e)
216         {
217             e.printStackTrace();
218         }
219
220         Log.v(TAG, "onPause() End");
221     }
222
223     /**
224      * ListPreference の表示データを設定
225      *
226      * @param pref_key     Preference(表示)のキー
227      * @param key          Preference(データ)のキー
228      * @param defaultValue Preferenceのデフォルト値
229      */
230     private void setListPreference(String pref_key, String key, String defaultValue)
231     {
232         try
233         {
234             ListPreference pref;
235             pref = (ListPreference) findPreference(pref_key);
236             String value = preferences.getString(key, defaultValue);
237             if (pref != null)
238             {
239                 pref.setValue(value);
240                 pref.setSummary(value);
241             }
242         }
243         catch (Exception e)
244         {
245             e.printStackTrace();
246         }
247     }
248
249     /**
250      * BooleanPreference の表示データを設定
251      *
252      * @param pref_key     Preference(表示)のキー
253      * @param key          Preference(データ)のキー
254      * @param defaultValue Preferenceのデフォルト値
255      */
256     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
257     {
258         try
259         {
260             CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
261             if (pref != null) {
262                 boolean value = preferences.getBoolean(key, defaultValue);
263                 pref.setChecked(value);
264             }
265         }
266         catch (Exception e)
267         {
268             e.printStackTrace();
269         }
270     }
271
272     /**
273      *
274      *
275      */
276     private void synchronizedProperty()
277     {
278         final FragmentActivity activity = getActivity();
279         if (activity != null)
280         {
281             activity.runOnUiThread(new Runnable() {
282                 @Override
283                 public void run() {
284                     try
285                     {
286                         // Preferenceの画面に反映させる
287                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
288                         setBooleanPreference(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
289                     }
290                     catch (Exception e)
291                     {
292                         e.printStackTrace();
293                     }
294                 }
295             });
296         }
297     }
298 }