1 package net.osdn.gokigen.pkremote.preference.sony;
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.util.Log;
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;
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;
30 public class SonyPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
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;
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)
45 SonyPreferenceFragment instance = new SonyPreferenceFragment();
46 instance.prepare(context, changeScene);
48 // パラメータはBundleにまとめておく
49 Bundle arguments = new Bundle();
50 //arguments.putString("title", title);
51 //arguments.putString("message", message);
52 instance.setArguments(arguments);
61 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
65 powerOffController = new CameraPowerOffSony(context, changeScene);
66 powerOffController.prepare();
68 logCatViewer = new LogCatViewer(changeScene);
69 logCatViewer.prepare();
71 cameraApiListViewer = new SonyCameraApiListViewer(changeScene);
72 cameraApiListViewer.prepare();
85 public void onAttach(Context activity)
87 super.onAttach(activity);
88 Log.v(TAG, "onAttach()");
93 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
96 initializePreferences();
98 preferences.registerOnSharedPreferenceChangeListener(this);
110 private void initializePreferences()
114 Map<String, ?> items = preferences.getAll();
115 SharedPreferences.Editor editor = preferences.edit();
117 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
118 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
120 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
121 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
123 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
124 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
126 if (!items.containsKey(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA)) {
127 editor.putBoolean(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
142 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
144 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
150 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
151 value = preferences.getBoolean(key, true);
152 Log.v(TAG, " " + key + " , " + value);
155 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
156 value = preferences.getBoolean(key, true);
157 Log.v(TAG, " " + key + " , " + value);
160 case IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA:
161 value = preferences.getBoolean(key, false);
162 Log.v(TAG, " " + key + " , " + value);
166 String strValue = preferences.getString(key, "");
167 setListPreference(key, key, strValue);
178 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
180 Log.v(TAG, "onCreatePreferences()");
183 //super.onCreate(savedInstanceState);
184 addPreferencesFromResource(R.xml.preferences_sony);
186 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
187 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
189 public boolean onPreferenceChange(Preference preference, Object newValue) {
190 preference.setSummary(newValue + " ");
194 connectionMethod.setSummary(connectionMethod.getValue() + " ");
196 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
197 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
198 findPreference("sony_api_list").setOnPreferenceClickListener(cameraApiListViewer);
211 public void onResume()
214 Log.v(TAG, "onResume() Start");
218 synchronizedProperty();
225 Log.v(TAG, "onResume() End");
233 public void onPause()
236 Log.v(TAG, "onPause() Start");
240 // Preference変更のリスナを解除
241 preferences.unregisterOnSharedPreferenceChangeListener(this);
248 Log.v(TAG, "onPause() End");
252 * ListPreference の表示データを設定
254 * @param pref_key Preference(表示)のキー
255 * @param key Preference(データ)のキー
256 * @param defaultValue Preferenceのデフォルト値
258 private void setListPreference(String pref_key, String key, String defaultValue)
263 pref = (ListPreference) findPreference(pref_key);
264 String value = preferences.getString(key, defaultValue);
267 pref.setValue(value);
268 pref.setSummary(value);
278 * BooleanPreference の表示データを設定
280 * @param pref_key Preference(表示)のキー
281 * @param key Preference(データ)のキー
282 * @param defaultValue Preferenceのデフォルト値
284 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
288 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
290 boolean value = preferences.getBoolean(key, defaultValue);
291 pref.setChecked(value);
304 private void synchronizedProperty()
306 final FragmentActivity activity = getActivity();
307 final boolean defaultValue = true;
308 if (activity != null)
310 activity.runOnUiThread(new Runnable() {
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);