OSDN Git Service

b75fb92039f18aa6ae7a41be16a5ccb4baf39964
[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.CONNECTION_METHOD)) {
128                 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
129             }
130             editor.apply();
131         }
132         catch (Exception e)
133         {
134             e.printStackTrace();
135         }
136     }
137
138     /**
139      *
140      *
141      */
142     @Override
143     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
144     {
145         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
146         boolean value;
147         if (key != null)
148         {
149             switch (key)
150             {
151                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
152                     value = preferences.getBoolean(key, true);
153                     Log.v(TAG, " " + key + " , " + value);
154                     break;
155
156                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
157                     value = preferences.getBoolean(key, true);
158                     Log.v(TAG, " " + key + " , " + value);
159                     break;
160
161                 default:
162                     String strValue = preferences.getString(key, "");
163                     setListPreference(key, key, strValue);
164                     break;
165             }
166         }
167     }
168
169     /**
170      *
171      *
172      */
173     @Override
174     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
175     {
176         Log.v(TAG, "onCreatePreferences()");
177         try
178         {
179             //super.onCreate(savedInstanceState);
180             addPreferencesFromResource(R.xml.preferences_olympuspen);
181
182             ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
183             connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
184                 @Override
185                 public boolean onPreferenceChange(Preference preference, Object newValue) {
186                     preference.setSummary(newValue + " ");
187                     return (true);
188                 }
189             });
190             connectionMethod.setSummary(connectionMethod.getValue() + " ");
191
192             findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
193             findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
194             //findPreference("panasonic_api_list").setOnPreferenceClickListener(cameraApiListViewer);
195             findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
196         }
197         catch (Exception e)
198         {
199             e.printStackTrace();
200         }
201     }
202
203     /**
204      *
205      *
206      */
207     @Override
208     public void onResume()
209     {
210         super.onResume();
211         Log.v(TAG, "onResume() Start");
212
213         try
214         {
215             synchronizedProperty();
216         }
217         catch (Exception e)
218         {
219             e.printStackTrace();
220         }
221
222         Log.v(TAG, "onResume() End");
223
224     }
225
226     /**
227      *
228      *
229      */
230     @Override
231     public void onPause()
232     {
233         super.onPause();
234         Log.v(TAG, "onPause() Start");
235
236         try
237         {
238             // Preference変更のリスナを解除
239             preferences.unregisterOnSharedPreferenceChangeListener(this);
240         }
241         catch (Exception e)
242         {
243             e.printStackTrace();
244         }
245
246         Log.v(TAG, "onPause() End");
247     }
248
249     /**
250      * ListPreference の表示データを設定
251      *
252      * @param pref_key     Preference(表示)のキー
253      * @param key          Preference(データ)のキー
254      * @param defaultValue Preferenceのデフォルト値
255      */
256     private void setListPreference(String pref_key, String key, String defaultValue)
257     {
258         try
259         {
260             ListPreference pref;
261             pref = (ListPreference) findPreference(pref_key);
262             String value = preferences.getString(key, defaultValue);
263             if (pref != null)
264             {
265                 pref.setValue(value);
266                 pref.setSummary(value);
267             }
268         }
269         catch (Exception e)
270         {
271             e.printStackTrace();
272         }
273     }
274
275     /**
276      * BooleanPreference の表示データを設定
277      *
278      * @param pref_key     Preference(表示)のキー
279      * @param key          Preference(データ)のキー
280      * @param defaultValue Preferenceのデフォルト値
281      */
282     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
283     {
284         try
285         {
286             CheckBoxPreference pref = findPreference(pref_key);
287             if (pref != null) {
288                 boolean value = preferences.getBoolean(key, defaultValue);
289                 pref.setChecked(value);
290             }
291         }
292         catch (Exception e)
293         {
294             e.printStackTrace();
295         }
296     }
297
298     /**
299      *
300      *
301      */
302     private void synchronizedProperty()
303     {
304         final FragmentActivity activity = getActivity();
305         final boolean defaultValue = true;
306         if (activity != null)
307         {
308             activity.runOnUiThread(new Runnable() {
309                 @Override
310                 public void run() {
311                     try
312                     {
313                         // Preferenceの画面に反映させる
314                         setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
315                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
316                     }
317                     catch (Exception e)
318                     {
319                         e.printStackTrace();
320                     }
321                 }
322             });
323         }
324     }
325
326     @Override
327     public boolean onPreferenceClick(Preference preference)
328     {
329         try
330         {
331             String preferenceKey = preference.getKey();
332             if (preferenceKey.contains(WIFI_SETTINGS))
333             {
334                 // Wifi 設定画面を表示する
335                 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
336                 if (context != null)
337                 {
338                     context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
339                 }
340             }
341             return (true);
342         }
343         catch (Exception e)
344         {
345             e.printStackTrace();
346         }
347         return (false);
348     }
349
350 }