1 package net.osdn.gokigen.gr2control.preference.ricohgr2;
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.support.annotation.NonNull;
7 import android.support.v4.app.FragmentActivity;
8 import android.support.v7.app.AppCompatActivity;
9 import android.support.v7.preference.CheckBoxPreference;
10 import android.support.v7.preference.ListPreference;
11 import android.support.v7.preference.Preference;
12 import android.support.v7.preference.PreferenceFragmentCompat;
13 import android.support.v7.preference.PreferenceManager;
14 import android.util.Log;
16 import net.osdn.gokigen.gr2control.R;
17 import net.osdn.gokigen.gr2control.camera.ricohgr2.operation.CameraPowerOffRicohGr2;
18 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
19 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
20 import net.osdn.gokigen.gr2control.scene.IChangeScene;
24 public class RicohGr2PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
26 private final String TAG = toString();
27 private SharedPreferences preferences = null;
28 private CameraPowerOffRicohGr2 powerOffController = null;
29 private LogCatViewer logCatViewer = null;
35 public static RicohGr2PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
37 RicohGr2PreferenceFragment instance = new RicohGr2PreferenceFragment();
38 instance.prepare(context, changeScene);
40 // パラメータはBundleにまとめておく
41 Bundle arguments = new Bundle();
42 //arguments.putString("title", title);
43 //arguments.putString("message", message);
44 instance.setArguments(arguments);
53 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
57 powerOffController = new CameraPowerOffRicohGr2(context, changeScene);
58 powerOffController.prepare();
60 logCatViewer = new LogCatViewer(changeScene);
61 logCatViewer.prepare();
74 public void onAttach(Context activity)
76 super.onAttach(activity);
77 Log.v(TAG, "onAttach()");
81 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
84 initializePreferences();
86 preferences.registerOnSharedPreferenceChangeListener(this);
98 private void initializePreferences()
102 Map<String, ?> items = preferences.getAll();
103 SharedPreferences.Editor editor = preferences.edit();
105 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
106 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
108 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
109 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
111 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
112 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, false);
114 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
115 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
117 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
118 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
120 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
121 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
123 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
124 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
126 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
127 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
129 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
130 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
145 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
147 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
153 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
154 value = preferences.getBoolean(key, true);
155 Log.v(TAG, " " + key + " , " + value);
158 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
159 value = preferences.getBoolean(key, true);
160 Log.v(TAG, " " + key + " , " + value);
163 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
164 value = preferences.getBoolean(key, false);
165 Log.v(TAG, " " + key + " , " + value);
168 case IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW:
169 value = preferences.getBoolean(key, true);
170 Log.v(TAG, " " + key + " , " + value);
173 case IPreferencePropertyAccessor.GR2_LCD_SLEEP:
174 value = preferences.getBoolean(key, false);
175 Log.v(TAG, " " + key + " , " + value);
178 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
179 value = preferences.getBoolean(key, false);
180 Log.v(TAG, " " + key + " , " + value);
183 case IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND:
184 value = preferences.getBoolean(key, true);
185 Log.v(TAG, " " + key + " , " + value);
188 case IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF:
189 value = preferences.getBoolean(key, false);
190 Log.v(TAG, " " + key + " , " + value);
194 String strValue = preferences.getString(key, "");
195 setListPreference(key, key, strValue);
206 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
208 Log.v(TAG, "onCreatePreferences()");
211 //super.onCreate(savedInstanceState);
212 addPreferencesFromResource(R.xml.preferences_ricoh_gr2);
214 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
215 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
217 public boolean onPreferenceChange(Preference preference, Object newValue) {
218 preference.setSummary(newValue + " ");
222 connectionMethod.setSummary(connectionMethod.getValue() + " ");
224 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
225 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
238 public void onResume()
241 Log.v(TAG, "onResume() Start");
244 synchronizedProperty();
251 Log.v(TAG, "onResume() End");
259 public void onPause()
262 Log.v(TAG, "onPause() Start");
265 // Preference変更のリスナを解除
266 preferences.unregisterOnSharedPreferenceChangeListener(this);
273 Log.v(TAG, "onPause() End");
277 * ListPreference の表示データを設定
279 * @param pref_key Preference(表示)のキー
280 * @param key Preference(データ)のキー
281 * @param defaultValue Preferenceのデフォルト値
283 private void setListPreference(String pref_key, String key, String defaultValue)
288 pref = (ListPreference) findPreference(pref_key);
289 String value = preferences.getString(key, defaultValue);
292 pref.setValue(value);
293 pref.setSummary(value);
303 * BooleanPreference の表示データを設定
305 * @param pref_key Preference(表示)のキー
306 * @param key Preference(データ)のキー
307 * @param defaultValue Preferenceのデフォルト値
309 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
313 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
315 boolean value = preferences.getBoolean(key, defaultValue);
316 pref.setChecked(value);
329 private void synchronizedProperty()
331 final FragmentActivity activity = getActivity();
332 final boolean defaultValue = true;
333 if (activity != null)
335 activity.runOnUiThread(new Runnable() {
340 // Preferenceの画面に反映させる
341 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
342 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
343 setBooleanPreference(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, IPreferencePropertyAccessor.USE_PLAYBACK_MENU, defaultValue);
344 setBooleanPreference(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, defaultValue);
345 setBooleanPreference(IPreferencePropertyAccessor.GR2_LCD_SLEEP, IPreferencePropertyAccessor.GR2_LCD_SLEEP, defaultValue);
346 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, defaultValue);
347 setBooleanPreference(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, defaultValue);
348 setBooleanPreference(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);