1 package net.osdn.gokigen.a01d.preference.fujix;
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;
10 import net.osdn.gokigen.a01d.IChangeScene;
11 import net.osdn.gokigen.a01d.R;
12 import net.osdn.gokigen.a01d.camera.ICameraConnection;
13 import net.osdn.gokigen.a01d.camera.fujix.operation.CameraPowerOffFujiX;
14 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
18 import androidx.annotation.NonNull;
19 import androidx.appcompat.app.AppCompatActivity;
20 import androidx.fragment.app.FragmentActivity;
21 import androidx.preference.CheckBoxPreference;
22 import androidx.preference.ListPreference;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceFragmentCompat;
25 import androidx.preference.PreferenceManager;
27 import static net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
28 import static net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor.SEND_MESSAGE_DIALOG;
29 import static net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
35 public class FujiXPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
37 private final String TAG = toString();
38 private AppCompatActivity context = null;
39 private IChangeScene changeScene = null;
40 private SharedPreferences preferences = null;
41 private CameraPowerOffFujiX powerOffController = null;
47 public static FujiXPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
49 FujiXPreferenceFragment instance = new FujiXPreferenceFragment();
50 instance.prepare(context, changeScene);
52 // パラメータはBundleにまとめておく
53 Bundle arguments = new Bundle();
54 //arguments.putString("title", title);
55 //arguments.putString("message", message);
56 instance.setArguments(arguments);
65 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
69 powerOffController = new CameraPowerOffFujiX(context, changeScene);
70 powerOffController.prepare();
72 this.context = context;
73 this.changeScene = changeScene;
86 public void onAttach(@NonNull Context activity)
88 super.onAttach(activity);
89 Log.v(TAG, "onAttach()");
94 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
97 initializePreferences();
99 preferences.registerOnSharedPreferenceChangeListener(this);
111 private void initializePreferences()
115 Map<String, ?> items = preferences.getAll();
116 SharedPreferences.Editor editor = preferences.edit();
118 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
119 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
121 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
122 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
124 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW)) {
125 editor.putBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
127 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_FOCUS_XY)) {
128 editor.putString(IPreferencePropertyAccessor.FUJIX_FOCUS_XY, IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
130 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_LIVEVIEW_WAIT)) {
131 editor.putString(IPreferencePropertyAccessor.FUJIX_LIVEVIEW_WAIT, IPreferencePropertyAccessor.FUJIX_LIVEVIEW_WAIT_DEFAULT_VALUE);
133 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_COMMAND_POLLING_WAIT)) {
134 editor.putString(IPreferencePropertyAccessor.FUJIX_COMMAND_POLLING_WAIT, IPreferencePropertyAccessor.FUJIX_COMMAND_POLLING_WAIT_DEFAULT_VALUE);
136 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
137 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
139 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_CONNECTION_FOR_READ)) {
140 editor.putBoolean(IPreferencePropertyAccessor.FUJIX_CONNECTION_FOR_READ, false);
155 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
157 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
165 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
166 value = preferences.getBoolean(key, true);
167 Log.v(TAG, " " + key + " , " + value);
170 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
171 value = preferences.getBoolean(key, true);
172 Log.v(TAG, " " + key + " , " + value);
175 case IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW:
176 value = preferences.getBoolean(key, false);
177 Log.v(TAG, " " + key + " , " + value);
180 case IPreferencePropertyAccessor.FUJIX_CONNECTION_FOR_READ:
181 value = preferences.getBoolean(key, false);
182 Log.v(TAG, " " + key + " , " + value);
186 String strValue = preferences.getString(key, "");
187 setListPreference(key, key, strValue);
203 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
205 Log.v(TAG, "onCreatePreferences()");
208 //super.onCreate(savedInstanceState);
209 addPreferencesFromResource(R.xml.preferences_fuji_x);
211 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
212 if (connectionMethod != null)
214 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
216 public boolean onPreferenceChange(Preference preference, Object newValue) {
217 preference.setSummary(newValue + " ");
221 connectionMethod.setSummary(connectionMethod.getValue() + " ");
224 Preference exitApplication = findPreference(EXIT_APPLICATION);
225 if (exitApplication != null)
227 exitApplication.setOnPreferenceClickListener(powerOffController);
230 Preference httpDialog = findPreference(SEND_MESSAGE_DIALOG);
231 if (httpDialog != null)
233 httpDialog.setOnPreferenceClickListener(this);
247 public void onResume()
250 Log.v(TAG, "onResume() Start");
254 synchronizedProperty();
261 Log.v(TAG, "onResume() End");
270 public void onPause()
273 Log.v(TAG, "onPause() Start");
277 // Preference変更のリスナを解除
278 preferences.unregisterOnSharedPreferenceChangeListener(this);
285 Log.v(TAG, "onPause() End");
289 * ListPreference の表示データを設定
291 * @param pref_key Preference(表示)のキー
292 * @param key Preference(データ)のキー
293 * @param defaultValue Preferenceのデフォルト値
295 private void setListPreference(String pref_key, String key, String defaultValue)
299 String value = preferences.getString(key, defaultValue);
300 ListPreference pref = findPreference(pref_key);
303 pref.setValue(value);
304 pref.setSummary(value);
314 * BooleanPreference の表示データを設定
316 * @param pref_key Preference(表示)のキー
317 * @param key Preference(データ)のキー
318 * @param defaultValue Preferenceのデフォルト値
320 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
324 CheckBoxPreference pref = findPreference(pref_key);
326 boolean value = preferences.getBoolean(key, defaultValue);
327 pref.setChecked(value);
340 private void synchronizedProperty()
342 final FragmentActivity activity = getActivity();
343 if (activity != null)
345 activity.runOnUiThread(new Runnable() {
350 // Preferenceの画面に反映させる
351 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
352 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
353 setBooleanPreference(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
354 setBooleanPreference(IPreferencePropertyAccessor.FUJIX_CONNECTION_FOR_READ, IPreferencePropertyAccessor.FUJIX_CONNECTION_FOR_READ, false);
366 public boolean onPreferenceClick(Preference preference)
370 String preferenceKey = preference.getKey();
371 if (preferenceKey.contains(WIFI_SETTINGS))
374 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
377 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
380 else if (preferenceKey.contains(SEND_MESSAGE_DIALOG))
383 if (changeScene != null)
385 changeScene.changeSceneToCameraPropertyList(ICameraConnection.CameraConnectionMethod.FUJI_X);