OSDN Git Service

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