1 package net.osdn.gokigen.pkremote.preference.canon;
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 CanonPreferenceFragment 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 CanonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
42 CanonPreferenceFragment instance = new CanonPreferenceFragment();
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(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.CANON_RAW_SUFFIX)) {
121 editor.putString(IPreferencePropertyAccessor.CANON_RAW_SUFFIX, IPreferencePropertyAccessor.CANON_RAW_SUFFIX_DEFAULT_VALUE);
136 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
138 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
144 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
145 value = preferences.getBoolean(key, true);
146 Log.v(TAG, " " + key + " , " + value);
149 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
150 value = preferences.getBoolean(key, true);
151 Log.v(TAG, " " + key + " , " + value);
155 String strValue = preferences.getString(key, "");
156 setListPreference(key, key, strValue);
167 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
169 Log.v(TAG, "onCreatePreferences()");
172 //super.onCreate(savedInstanceState);
173 addPreferencesFromResource(R.xml.preferences_canon);
175 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
176 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
178 public boolean onPreferenceChange(Preference preference, Object newValue) {
179 preference.setSummary(newValue + " ");
183 connectionMethod.setSummary(connectionMethod.getValue() + " ");
185 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
186 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
199 public void onResume()
202 Log.v(TAG, "onResume() Start");
206 synchronizedProperty();
213 Log.v(TAG, "onResume() End");
222 public void onPause()
225 Log.v(TAG, "onPause() Start");
229 // Preference変更のリスナを解除
230 preferences.unregisterOnSharedPreferenceChangeListener(this);
237 Log.v(TAG, "onPause() End");
241 * ListPreference の表示データを設定
243 * @param pref_key Preference(表示)のキー
244 * @param key Preference(データ)のキー
245 * @param defaultValue Preferenceのデフォルト値
247 private void setListPreference(String pref_key, String key, String defaultValue)
252 pref = (ListPreference) findPreference(pref_key);
253 String value = preferences.getString(key, defaultValue);
256 pref.setValue(value);
257 pref.setSummary(value);
267 * BooleanPreference の表示データを設定
269 * @param pref_key Preference(表示)のキー
270 * @param key Preference(データ)のキー
271 * @param defaultValue Preferenceのデフォルト値
273 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
277 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
279 boolean value = preferences.getBoolean(key, defaultValue);
280 pref.setChecked(value);
293 private void synchronizedProperty()
295 final FragmentActivity activity = getActivity();
296 if (activity != null)
298 activity.runOnUiThread(new Runnable() {
303 // Preferenceの画面に反映させる
304 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
305 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);