OSDN Git Service

f07d7e3dbf8dfbd750c2d813b5f676bdcc15fed3
[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.preference.PreferencePropertyInitializer;
26 import net.osdn.gokigen.pkremote.scene.IChangeScene;
27
28 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
29 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
30 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
31
32 /**
33  *
34  *
35  */
36 public class CanonPreferenceFragment  extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
37 {
38     private final String TAG = toString();
39     private AppCompatActivity context = null;
40     private SharedPreferences preferences = null;
41     private PtpIpCameraPowerOff powerOffController = null;
42     private LogCatViewer logCatViewer = null;
43
44     /**
45      *
46      *
47      */
48     public static CanonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
49     {
50         CanonPreferenceFragment instance = new CanonPreferenceFragment();
51         instance.prepare(context, changeScene);
52
53         // パラメータはBundleにまとめておく
54         Bundle arguments = new Bundle();
55         //arguments.putString("title", title);
56         //arguments.putString("message", message);
57         instance.setArguments(arguments);
58
59         return (instance);
60     }
61
62     /**
63      *
64      *
65      */
66     private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
67     {
68         try
69         {
70             powerOffController = new PtpIpCameraPowerOff(context, changeScene);
71             powerOffController.prepare();
72
73             logCatViewer = new LogCatViewer(changeScene);
74             logCatViewer.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             PreferencePropertyInitializer initializer = new PreferencePropertyInitializer(activity);
101             initializer.initializePreferences();
102
103             preferences.registerOnSharedPreferenceChangeListener(this);
104         }
105         catch (Exception e)
106         {
107             e.printStackTrace();
108         }
109     }
110
111     /**
112      *
113      *
114      */
115     @Override
116     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
117     {
118         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
119         boolean value;
120         if (key != null)
121         {
122             switch (key) {
123                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW -> {
124                     value = preferences.getBoolean(key, true);
125                     Log.v(TAG, " " + key + " , " + value);
126                 }
127                 case IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL -> {
128                     value = preferences.getBoolean(key, false);
129                     Log.v(TAG, " " + key + " , " + value);
130                 }
131                 default -> {
132                     String strValue = preferences.getString(key, "");
133                     setListPreference(key, key, strValue);
134                 }
135             }
136         }
137     }
138
139     /**
140      *
141      *
142      */
143     @Override
144     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
145     {
146         Log.v(TAG, "onCreatePreferences()");
147         try
148         {
149             //super.onCreate(savedInstanceState);
150             addPreferencesFromResource(R.xml.preferences_canon);
151
152             ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
153             if (connectionMethod != null)
154             {
155                 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
156                     @Override
157                     public boolean onPreferenceChange(Preference preference, Object newValue) {
158                         preference.setSummary(newValue + " ");
159                         return (true);
160                     }
161                 });
162                 connectionMethod.setSummary(connectionMethod.getValue() + " ");
163             }
164             findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
165             findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
166             findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
167         }
168         catch (Exception e)
169         {
170             e.printStackTrace();
171         }
172     }
173
174     /**
175      *
176      *
177      */
178     @Override
179     public void onResume()
180     {
181         super.onResume();
182         Log.v(TAG, "onResume() Start");
183         try
184         {
185             synchronizedProperty();
186         }
187         catch (Exception e)
188         {
189             e.printStackTrace();
190         }
191         Log.v(TAG, "onResume() End");
192     }
193
194     /**
195      *
196      *
197      */
198     @Override
199     public void onPause()
200     {
201         super.onPause();
202         Log.v(TAG, "onPause() Start");
203
204         try
205         {
206             // Preference変更のリスナを解除
207             preferences.unregisterOnSharedPreferenceChangeListener(this);
208         }
209         catch (Exception e)
210         {
211             e.printStackTrace();
212         }
213
214         Log.v(TAG, "onPause() End");
215     }
216
217     /**
218      * ListPreference の表示データを設定
219      *
220      * @param pref_key     Preference(表示)のキー
221      * @param key          Preference(データ)のキー
222      * @param defaultValue Preferenceのデフォルト値
223      */
224     private void setListPreference(String pref_key, String key, String defaultValue)
225     {
226         try
227         {
228             ListPreference pref = findPreference(pref_key);
229             String value = preferences.getString(key, defaultValue);
230             if (pref != null)
231             {
232                 pref.setValue(value);
233                 pref.setSummary(value);
234             }
235         }
236         catch (Exception e)
237         {
238             e.printStackTrace();
239         }
240     }
241
242     /**
243      * BooleanPreference の表示データを設定
244      *
245      * @param pref_key     Preference(表示)のキー
246      * @param key          Preference(データ)のキー
247      * @param defaultValue Preferenceのデフォルト値
248      */
249     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
250     {
251         try
252         {
253             CheckBoxPreference pref = findPreference(pref_key);
254             if (pref != null) {
255                 boolean value = preferences.getBoolean(key, defaultValue);
256                 pref.setChecked(value);
257             }
258         }
259         catch (Exception e)
260         {
261             e.printStackTrace();
262         }
263     }
264
265     /**
266      *
267      *
268      */
269     private void synchronizedProperty()
270     {
271         final FragmentActivity activity = getActivity();
272         if (activity != null)
273         {
274             activity.runOnUiThread(new Runnable() {
275                 @Override
276                 public void run() {
277                     try
278                     {
279                         // Preferenceの画面に反映させる
280                         setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
281                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
282                         setBooleanPreference(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, false);
283                     }
284                     catch (Exception e)
285                     {
286                         e.printStackTrace();
287                     }
288                 }
289             });
290         }
291     }
292
293     @Override
294     public boolean onPreferenceClick(Preference preference)
295     {
296         try
297         {
298             String preferenceKey = preference.getKey();
299             if (preferenceKey.contains(WIFI_SETTINGS))
300             {
301                 // Wifi 設定画面を表示する
302                 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
303                 if (context != null)
304                 {
305                     context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
306                 }
307             }
308             return (true);
309         }
310         catch (Exception e)
311         {
312             e.printStackTrace();
313         }
314         return (false);
315     }
316 }