1 package net.osdn.gokigen.pkremote.preference.nikon;
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.ptpip.operation.PtpIpCameraPowerOff;
21 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
22 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
23 import net.osdn.gokigen.pkremote.scene.IChangeScene;
29 public class NikonPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
31 private final String TAG = toString();
32 private SharedPreferences preferences = null;
33 private PtpIpCameraPowerOff powerOffController = null;
34 private LogCatViewer logCatViewer = null;
40 public static NikonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
42 NikonPreferenceFragment instance = new NikonPreferenceFragment();
43 instance.prepare(context, changeScene);
45 // パラメータはBundleにまとめておく
46 Bundle arguments = new Bundle();
47 //arguments.putString("title", title);
48 //arguments.putString("message", message);
49 instance.setArguments(arguments);
58 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
62 powerOffController = new PtpIpCameraPowerOff(context, changeScene);
63 powerOffController.prepare();
65 logCatViewer = new LogCatViewer(changeScene);
66 logCatViewer.prepare();
79 public void onAttach(@NonNull Context activity)
81 super.onAttach(activity);
82 Log.v(TAG, "onAttach()");
87 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
90 initializePreferences();
92 preferences.registerOnSharedPreferenceChangeListener(this);
104 private void initializePreferences()
108 Map<String, ?> items = preferences.getAll();
109 SharedPreferences.Editor editor = preferences.edit();
111 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
112 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
114 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
115 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
117 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
118 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
120 if (!items.containsKey(IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS)) {
121 editor.putString(IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS, IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS_DEFAULT_VALUE);
123 if (!items.containsKey(IPreferencePropertyAccessor.BLE_WIFI_ON)) {
124 editor.putBoolean(IPreferencePropertyAccessor.BLE_WIFI_ON, false);
139 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
141 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
147 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
148 value = preferences.getBoolean(key, true);
149 Log.v(TAG, " " + key + " , " + value);
152 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
153 value = preferences.getBoolean(key, true);
154 Log.v(TAG, " " + key + " , " + value);
157 case IPreferencePropertyAccessor.BLE_WIFI_ON:
158 value = preferences.getBoolean(key, false);
159 Log.v(TAG, " " + key + " , " + value);
163 String strValue = preferences.getString(key, "");
164 setListPreference(key, key, strValue);
175 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
177 Log.v(TAG, "onCreatePreferences()");
180 //super.onCreate(savedInstanceState);
181 addPreferencesFromResource(R.xml.preferences_nikon);
183 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
184 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
186 public boolean onPreferenceChange(Preference preference, Object newValue) {
187 preference.setSummary(newValue + " ");
191 connectionMethod.setSummary(connectionMethod.getValue() + " ");
193 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
194 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
207 public void onResume()
210 Log.v(TAG, "onResume() Start");
214 synchronizedProperty();
221 Log.v(TAG, "onResume() End");
230 public void onPause()
233 Log.v(TAG, "onPause() Start");
237 // Preference変更のリスナを解除
238 preferences.unregisterOnSharedPreferenceChangeListener(this);
245 Log.v(TAG, "onPause() End");
249 * ListPreference の表示データを設定
251 * @param pref_key Preference(表示)のキー
252 * @param key Preference(データ)のキー
253 * @param defaultValue Preferenceのデフォルト値
255 private void setListPreference(String pref_key, String key, String defaultValue)
260 pref = findPreference(pref_key);
261 String value = preferences.getString(key, defaultValue);
264 pref.setValue(value);
265 pref.setSummary(value);
275 * BooleanPreference の表示データを設定
277 * @param pref_key Preference(表示)のキー
278 * @param key Preference(データ)のキー
279 * @param defaultValue Preferenceのデフォルト値
281 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
285 CheckBoxPreference pref = findPreference(pref_key);
287 boolean value = preferences.getBoolean(key, defaultValue);
288 pref.setChecked(value);
301 private void synchronizedProperty()
303 final FragmentActivity activity = getActivity();
304 if (activity != null)
306 activity.runOnUiThread(new Runnable() {
311 // Preferenceの画面に反映させる
312 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
313 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
315 setBooleanPreference(IPreferencePropertyAccessor.BLE_WIFI_ON, IPreferencePropertyAccessor.BLE_WIFI_ON, false);