OSDN Git Service

フォーカスの設定・解除をできるようにした。(EOS M6)
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / preference / canon / CanonPreferenceFragment.java
1 package net.osdn.gokigen.a01d.preference.canon;
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 androidx.annotation.NonNull;
11 import androidx.appcompat.app.AppCompatActivity;
12 import androidx.fragment.app.FragmentActivity;
13 import androidx.preference.CheckBoxPreference;
14 import androidx.preference.ListPreference;
15 import androidx.preference.Preference;
16 import androidx.preference.PreferenceFragmentCompat;
17 import androidx.preference.PreferenceManager;
18
19 import net.osdn.gokigen.a01d.IChangeScene;
20 import net.osdn.gokigen.a01d.R;
21 import net.osdn.gokigen.a01d.camera.ptpip.operation.PtpIpCameraPowerOff;
22 import net.osdn.gokigen.a01d.logcat.LogCatViewer;
23 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
24
25 import java.util.Map;
26
27 import static net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
28
29 /**
30  *
31  *
32  */
33 public class CanonPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
34 {
35     private final String TAG = toString();
36     private AppCompatActivity context = null;
37     private SharedPreferences preferences = null;
38     private PtpIpCameraPowerOff powerOffController = null;
39     private LogCatViewer logCatViewer = null;
40
41     /**
42      *
43      *
44      */
45     public static CanonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
46     {
47         CanonPreferenceFragment instance = new CanonPreferenceFragment();
48         instance.prepare(context, changeScene);
49
50         // パラメータはBundleにまとめておく
51         Bundle arguments = new Bundle();
52         //arguments.putString("title", title);
53         //arguments.putString("message", message);
54         instance.setArguments(arguments);
55
56         return (instance);
57     }
58
59     /**
60      *
61      *
62      */
63     private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
64     {
65         try
66         {
67             powerOffController = new PtpIpCameraPowerOff(context, changeScene);
68             powerOffController.prepare();
69
70             logCatViewer = new LogCatViewer(changeScene);
71             logCatViewer.prepare();
72
73             //cameraApiListViewer = new PanasonicCameraApiListViewer(changeScene);
74             //cameraApiListViewer.prepare();
75
76             this.context = context;
77         }
78         catch (Exception e)
79         {
80             e.printStackTrace();
81         }
82     }
83
84     /**
85      *
86      *
87      */
88     @Override
89     public void onAttach(@NonNull Context activity)
90     {
91         super.onAttach(activity);
92         Log.v(TAG, "onAttach()");
93
94         try
95         {
96             // Preference をつかまえる
97             preferences = PreferenceManager.getDefaultSharedPreferences(activity);
98
99             // Preference を初期設定する
100             initializePreferences();
101
102             preferences.registerOnSharedPreferenceChangeListener(this);
103         }
104         catch (Exception e)
105         {
106             e.printStackTrace();
107         }
108     }
109
110     /**
111      * Preferenceの初期化...
112      *
113      */
114     private void initializePreferences()
115     {
116         try
117         {
118             Map<String, ?> items = preferences.getAll();
119             SharedPreferences.Editor editor = preferences.edit();
120
121             if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
122                 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
123             }
124             if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
125                 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
126             }
127             if (!items.containsKey(IPreferencePropertyAccessor.CANON_FOCUS_XY)) {
128                 editor.putString(IPreferencePropertyAccessor.CANON_FOCUS_XY, IPreferencePropertyAccessor.CANON_FOCUS_XY_DEFAULT_VALUE);
129             }
130             if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
131                 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
132             }
133             editor.apply();
134         }
135         catch (Exception e)
136         {
137             e.printStackTrace();
138         }
139     }
140
141     /**
142      *
143      *
144      */
145     @Override
146     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
147     {
148         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
149         boolean value;
150         if (key != null)
151         {
152             switch (key)
153             {
154                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
155                     value = preferences.getBoolean(key, true);
156                     Log.v(TAG, " " + key + " , " + value);
157                     break;
158
159                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
160                     value = preferences.getBoolean(key, true);
161                     Log.v(TAG, " " + key + " , " + value);
162                     break;
163
164                 default:
165                     String strValue = preferences.getString(key, "");
166                     setListPreference(key, key, strValue);
167                     break;
168             }
169         }
170     }
171
172     /**
173      *
174      *
175      */
176     @Override
177     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
178     {
179         Log.v(TAG, "onCreatePreferences()");
180         try
181         {
182             //super.onCreate(savedInstanceState);
183             addPreferencesFromResource(R.xml.preferences_olympuspen);
184
185             ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
186             connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
187                 @Override
188                 public boolean onPreferenceChange(Preference preference, Object newValue) {
189                     preference.setSummary(newValue + " ");
190                     return (true);
191                 }
192             });
193             connectionMethod.setSummary(connectionMethod.getValue() + " ");
194
195             findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
196             findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
197             //findPreference("panasonic_api_list").setOnPreferenceClickListener(cameraApiListViewer);
198             findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
199         }
200         catch (Exception e)
201         {
202             e.printStackTrace();
203         }
204     }
205
206     /**
207      *
208      *
209      */
210     @Override
211     public void onResume()
212     {
213         super.onResume();
214         Log.v(TAG, "onResume() Start");
215
216         try
217         {
218             synchronizedProperty();
219         }
220         catch (Exception e)
221         {
222             e.printStackTrace();
223         }
224
225         Log.v(TAG, "onResume() End");
226
227     }
228
229     /**
230      *
231      *
232      */
233     @Override
234     public void onPause()
235     {
236         super.onPause();
237         Log.v(TAG, "onPause() Start");
238
239         try
240         {
241             // Preference変更のリスナを解除
242             preferences.unregisterOnSharedPreferenceChangeListener(this);
243         }
244         catch (Exception e)
245         {
246             e.printStackTrace();
247         }
248
249         Log.v(TAG, "onPause() End");
250     }
251
252     /**
253      * ListPreference の表示データを設定
254      *
255      * @param pref_key     Preference(表示)のキー
256      * @param key          Preference(データ)のキー
257      * @param defaultValue Preferenceのデフォルト値
258      */
259     private void setListPreference(String pref_key, String key, String defaultValue)
260     {
261         try
262         {
263             ListPreference pref = findPreference(pref_key);
264             String value = preferences.getString(key, defaultValue);
265             if (pref != null)
266             {
267                 pref.setValue(value);
268                 pref.setSummary(value);
269             }
270         }
271         catch (Exception e)
272         {
273             e.printStackTrace();
274         }
275     }
276
277     /**
278      * BooleanPreference の表示データを設定
279      *
280      * @param pref_key     Preference(表示)のキー
281      * @param key          Preference(データ)のキー
282      * @param defaultValue Preferenceのデフォルト値
283      */
284     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
285     {
286         try
287         {
288             CheckBoxPreference pref = findPreference(pref_key);
289             if (pref != null) {
290                 boolean value = preferences.getBoolean(key, defaultValue);
291                 pref.setChecked(value);
292             }
293         }
294         catch (Exception e)
295         {
296             e.printStackTrace();
297         }
298     }
299
300     /**
301      *
302      *
303      */
304     private void synchronizedProperty()
305     {
306         final FragmentActivity activity = getActivity();
307         final boolean defaultValue = true;
308         if (activity != null)
309         {
310             activity.runOnUiThread(new Runnable() {
311                 @Override
312                 public void run() {
313                     try
314                     {
315                         // Preferenceの画面に反映させる
316                         setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
317                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
318                     }
319                     catch (Exception e)
320                     {
321                         e.printStackTrace();
322                     }
323                 }
324             });
325         }
326     }
327
328     @Override
329     public boolean onPreferenceClick(Preference preference)
330     {
331         try
332         {
333             String preferenceKey = preference.getKey();
334             if (preferenceKey.contains(WIFI_SETTINGS))
335             {
336                 // Wifi 設定画面を表示する
337                 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
338                 if (context != null)
339                 {
340                     context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
341                 }
342             }
343             return (true);
344         }
345         catch (Exception e)
346         {
347             e.printStackTrace();
348         }
349         return (false);
350     }
351
352 }