OSDN Git Service

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