OSDN Git Service

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