OSDN Git Service

SONYカメラのダウンロード機能搭載。
[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.SharedPreferences;
5 import android.os.Bundle;
6 import android.util.Log;
7
8 import java.util.Map;
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.pkremote.R;
20 import net.osdn.gokigen.pkremote.camera.vendor.sony.cameraproperty.SonyCameraApiListViewer;
21 import net.osdn.gokigen.pkremote.camera.vendor.sony.operation.CameraPowerOffSony;
22 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
23 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
24 import net.osdn.gokigen.pkremote.scene.IChangeScene;
25
26 /**
27  *
28  *
29  */
30 public class SonyPreferenceFragment  extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
31 {
32     private final String TAG = toString();
33     private SharedPreferences preferences = null;
34     private CameraPowerOffSony powerOffController = null;
35     private LogCatViewer logCatViewer = null;
36     private SonyCameraApiListViewer cameraApiListViewer = null;
37
38     /**
39      *
40      *
41      */
42     //public static SonyPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
43     public static SonyPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
44     {
45         SonyPreferenceFragment instance = new SonyPreferenceFragment();
46         instance.prepare(context, changeScene);
47
48         // パラメータはBundleにまとめておく
49         Bundle arguments = new Bundle();
50         //arguments.putString("title", title);
51         //arguments.putString("message", message);
52         instance.setArguments(arguments);
53
54         return (instance);
55     }
56
57     /**
58      *
59      *
60      */
61     private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
62     {
63         try
64         {
65             powerOffController = new CameraPowerOffSony(context, changeScene);
66             powerOffController.prepare();
67
68             logCatViewer = new LogCatViewer(changeScene);
69             logCatViewer.prepare();
70
71             cameraApiListViewer = new SonyCameraApiListViewer(changeScene);
72             cameraApiListViewer.prepare();
73         }
74         catch (Exception e)
75         {
76             e.printStackTrace();
77         }
78     }
79
80     /**
81      *
82      *
83      */
84     @Override
85     public void onAttach(Context activity)
86     {
87         super.onAttach(activity);
88         Log.v(TAG, "onAttach()");
89
90         try
91         {
92             // Preference をつかまえる
93             preferences = PreferenceManager.getDefaultSharedPreferences(activity);
94
95             // Preference を初期設定する
96             initializePreferences();
97
98             preferences.registerOnSharedPreferenceChangeListener(this);
99         }
100         catch (Exception e)
101         {
102             e.printStackTrace();
103         }
104     }
105
106     /**
107      * Preferenceの初期化...
108      *
109      */
110     private void initializePreferences()
111     {
112         try
113         {
114             Map<String, ?> items = preferences.getAll();
115             SharedPreferences.Editor editor = preferences.edit();
116
117             if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
118                 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
119             }
120             if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
121                 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
122             }
123             if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
124                 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
125             }
126             if (!items.containsKey(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA)) {
127                 editor.putBoolean(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
128             }
129             editor.apply();
130         }
131         catch (Exception e)
132         {
133             e.printStackTrace();
134         }
135     }
136
137     /**
138      *
139      *
140      */
141     @Override
142     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
143     {
144         Log.v(TAG, "onSharedPreferenceChanged() : " + key);
145         boolean value;
146         if (key != null)
147         {
148             switch (key)
149             {
150                 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
151                     value = preferences.getBoolean(key, true);
152                     Log.v(TAG, " " + key + " , " + value);
153                     break;
154
155                 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
156                     value = preferences.getBoolean(key, true);
157                     Log.v(TAG, " " + key + " , " + value);
158                     break;
159
160                 case IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA:
161                     value = preferences.getBoolean(key, false);
162                     Log.v(TAG, " " + key + " , " + value);
163                     break;
164
165                 default:
166                     String strValue = preferences.getString(key, "");
167                     setListPreference(key, key, strValue);
168                     break;
169             }
170         }
171     }
172
173     /**
174      *
175      *
176      */
177     @Override
178     public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
179     {
180         Log.v(TAG, "onCreatePreferences()");
181         try
182         {
183             //super.onCreate(savedInstanceState);
184             addPreferencesFromResource(R.xml.preferences_sony);
185
186             ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
187             connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
188                 @Override
189                 public boolean onPreferenceChange(Preference preference, Object newValue) {
190                     preference.setSummary(newValue + " ");
191                     return (true);
192                 }
193             });
194             connectionMethod.setSummary(connectionMethod.getValue() + " ");
195
196             findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
197             findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
198             findPreference("sony_api_list").setOnPreferenceClickListener(cameraApiListViewer);
199         }
200         catch (Exception e)
201         {
202             e.printStackTrace();
203         }
204     }
205
206     /**
207      *
208      *
209      */
210     @Override
211     public void onResume()
212     {
213         super.onResume();
214         Log.v(TAG, "onResume() Start");
215
216         try
217         {
218             synchronizedProperty();
219         }
220         catch (Exception e)
221         {
222             e.printStackTrace();
223         }
224
225         Log.v(TAG, "onResume() End");
226     }
227
228     /**
229      *
230      *
231      */
232     @Override
233     public void onPause()
234     {
235         super.onPause();
236         Log.v(TAG, "onPause() Start");
237
238         try
239         {
240             // Preference変更のリスナを解除
241             preferences.unregisterOnSharedPreferenceChangeListener(this);
242         }
243         catch (Exception e)
244         {
245             e.printStackTrace();
246         }
247
248         Log.v(TAG, "onPause() End");
249     }
250
251     /**
252      * ListPreference の表示データを設定
253      *
254      * @param pref_key     Preference(表示)のキー
255      * @param key          Preference(データ)のキー
256      * @param defaultValue Preferenceのデフォルト値
257      */
258     private void setListPreference(String pref_key, String key, String defaultValue)
259     {
260         try
261         {
262             ListPreference pref;
263             pref = (ListPreference) findPreference(pref_key);
264             String value = preferences.getString(key, defaultValue);
265             if (pref != null)
266             {
267                 pref.setValue(value);
268                 pref.setSummary(value);
269             }
270         }
271         catch (Exception e)
272         {
273             e.printStackTrace();
274         }
275     }
276
277     /**
278      * BooleanPreference の表示データを設定
279      *
280      * @param pref_key     Preference(表示)のキー
281      * @param key          Preference(データ)のキー
282      * @param defaultValue Preferenceのデフォルト値
283      */
284     private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
285     {
286         try
287         {
288             CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
289             if (pref != null) {
290                 boolean value = preferences.getBoolean(key, defaultValue);
291                 pref.setChecked(value);
292             }
293         }
294         catch (Exception e)
295         {
296             e.printStackTrace();
297         }
298     }
299
300     /**
301      *
302      *
303      */
304     private void synchronizedProperty()
305     {
306         final FragmentActivity activity = getActivity();
307         final boolean defaultValue = true;
308         if (activity != null)
309         {
310             activity.runOnUiThread(new Runnable() {
311                 @Override
312                 public void run() {
313                     try
314                     {
315                         // Preferenceの画面に反映させる
316                         setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
317                         setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
318                         setBooleanPreference(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
319                     }
320                     catch (Exception e)
321                     {
322                         e.printStackTrace();
323                     }
324                 }
325             });
326         }
327     }
328
329 }