1 package net.osdn.gokigen.cameratest.fuji.preference;
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.util.Log;
8 import androidx.annotation.NonNull;
9 import androidx.fragment.app.FragmentActivity;
10 import androidx.preference.CheckBoxPreference;
11 import androidx.preference.EditTextPreference;
12 import androidx.preference.ListPreference;
13 import androidx.preference.Preference;
14 import androidx.preference.PreferenceFragmentCompat;
15 import androidx.preference.PreferenceManager;
17 import net.osdn.gokigen.cameratest.IApplicationControl;
18 import net.osdn.gokigen.cameratest.R;
20 import java.net.Inet4Address;
23 public class FujiPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
25 private final String TAG = toString();
26 private SharedPreferences preferences = null;
27 private PowerOffController powerOffController = null;
33 public static FujiPreferenceFragment newInstance(@NonNull Context context, @NonNull IApplicationControl control)
35 FujiPreferenceFragment instance = new FujiPreferenceFragment();
36 instance.prepare(context, control);
38 // パラメータはBundleにまとめておく
39 Bundle arguments = new Bundle();
40 //arguments.putString("title", title);
41 //arguments.putString("message", message);
42 instance.setArguments(arguments);
47 private void prepare(@NonNull Context context, @NonNull IApplicationControl control)
49 powerOffController = new PowerOffController(context, control);
57 public void onAttach(@NonNull Context activity)
59 super.onAttach(activity);
60 Log.v(TAG, "onAttach()");
64 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
67 initializePreferences();
69 preferences.registerOnSharedPreferenceChangeListener(this);
78 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
82 setPreferencesFromResource(R.xml.preferences_fuji_x, rootKey);
84 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
85 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
87 public boolean onPreferenceChange(Preference preference, Object newValue) {
88 preference.setSummary(newValue + " ");
92 connectionMethod.setSummary(connectionMethod.getValue() + " ");
94 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
102 EditTextPreference focusResolition = (EditTextPreference) findPreference(IPreferencePropertyAccessor.FUJIX_FOCUS_XY);
103 String[] focus = focusResolition.getText().split(",");
104 if (focus.length != 2)
106 focusResolition.setText(IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
110 int x = Integer.parseInt(focus[0]);
111 int y = Integer.parseInt(focus[1]);
112 focusResolition.setText(x + "," + y);
125 private void initializePreferences()
129 Map<String, ?> items = preferences.getAll();
130 SharedPreferences.Editor editor = preferences.edit();
132 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
133 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
135 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
136 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
138 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW)) {
139 editor.putBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
141 if (!items.containsKey(IPreferencePropertyAccessor.FUJIX_FOCUS_XY)) {
142 editor.putString(IPreferencePropertyAccessor.FUJIX_FOCUS_XY, IPreferencePropertyAccessor.FUJIX_FOCUS_XY_DEFAULT_VALUE);
154 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
156 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
162 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
163 value = preferences.getBoolean(key, true);
164 Log.v(TAG, " " + key + " , " + value);
167 case IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW:
168 value = preferences.getBoolean(key, false);
169 Log.v(TAG, " " + key + " , " + value);
173 String strValue = preferences.getString(key, "");
174 setListPreference(key, key, strValue);
185 public void onResume()
188 Log.v(TAG, "onResume() Start");
191 synchronizedProperty();
198 Log.v(TAG, "onResume() End");
206 public void onPause()
209 Log.v(TAG, "onPause() Start");
212 // Preference変更のリスナを解除
213 preferences.unregisterOnSharedPreferenceChangeListener(this);
220 Log.v(TAG, "onPause() End");
224 * ListPreference の表示データを設定
226 * @param pref_key Preference(表示)のキー
227 * @param key Preference(データ)のキー
228 * @param defaultValue Preferenceのデフォルト値
230 private void setListPreference(String pref_key, String key, String defaultValue)
235 pref = (ListPreference) findPreference(pref_key);
236 String value = preferences.getString(key, defaultValue);
239 pref.setValue(value);
240 pref.setSummary(value);
250 * BooleanPreference の表示データを設定
252 * @param pref_key Preference(表示)のキー
253 * @param key Preference(データ)のキー
254 * @param defaultValue Preferenceのデフォルト値
256 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
260 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
262 boolean value = preferences.getBoolean(key, defaultValue);
263 pref.setChecked(value);
276 private void synchronizedProperty()
278 final FragmentActivity activity = getActivity();
279 if (activity != null)
281 activity.runOnUiThread(new Runnable() {
286 // Preferenceの画面に反映させる
287 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
288 setBooleanPreference(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);