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.util.Log;
8 import net.osdn.gokigen.gr2control.R;
9 import net.osdn.gokigen.gr2control.camera.ricohgr2.operation.CameraPowerOffRicohGr2;
10 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
11 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
12 import net.osdn.gokigen.gr2control.scene.IChangeScene;
16 import androidx.annotation.NonNull;
17 import androidx.appcompat.app.AppCompatActivity;
18 import androidx.fragment.app.FragmentActivity;
19 import androidx.preference.CheckBoxPreference;
20 import androidx.preference.ListPreference;
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceFragmentCompat;
23 import androidx.preference.PreferenceManager;
25 public class RicohGr2PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener
27 private final String TAG = toString();
28 private SharedPreferences preferences = null;
29 private CameraPowerOffRicohGr2 powerOffController = null;
30 private LogCatViewer logCatViewer = null;
36 public static RicohGr2PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
38 RicohGr2PreferenceFragment instance = new RicohGr2PreferenceFragment();
39 instance.prepare(context, changeScene);
41 // パラメータはBundleにまとめておく
42 Bundle arguments = new Bundle();
43 //arguments.putString("title", title);
44 //arguments.putString("message", message);
45 instance.setArguments(arguments);
54 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
58 powerOffController = new CameraPowerOffRicohGr2(context, changeScene);
59 powerOffController.prepare();
61 logCatViewer = new LogCatViewer(changeScene);
62 logCatViewer.prepare();
75 public void onAttach(Context activity)
77 super.onAttach(activity);
78 Log.v(TAG, "onAttach()");
82 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
85 initializePreferences();
87 preferences.registerOnSharedPreferenceChangeListener(this);
99 private void initializePreferences()
103 Map<String, ?> items = preferences.getAll();
104 SharedPreferences.Editor editor = preferences.edit();
106 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
107 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
109 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
110 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
112 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
113 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, false);
115 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
116 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
118 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
119 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
121 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
122 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
124 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
125 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
127 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
128 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
130 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
131 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
146 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
148 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
154 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
155 value = preferences.getBoolean(key, true);
156 Log.v(TAG, " " + key + " , " + value);
159 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
160 value = preferences.getBoolean(key, true);
161 Log.v(TAG, " " + key + " , " + value);
164 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
165 value = preferences.getBoolean(key, false);
166 Log.v(TAG, " " + key + " , " + value);
169 case IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW:
170 value = preferences.getBoolean(key, true);
171 Log.v(TAG, " " + key + " , " + value);
174 case IPreferencePropertyAccessor.GR2_LCD_SLEEP:
175 value = preferences.getBoolean(key, false);
176 Log.v(TAG, " " + key + " , " + value);
179 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
180 value = preferences.getBoolean(key, false);
181 Log.v(TAG, " " + key + " , " + value);
184 case IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND:
185 value = preferences.getBoolean(key, true);
186 Log.v(TAG, " " + key + " , " + value);
189 case IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF:
190 value = preferences.getBoolean(key, false);
191 Log.v(TAG, " " + key + " , " + value);
195 String strValue = preferences.getString(key, "");
196 setListPreference(key, key, strValue);
207 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
209 Log.v(TAG, "onCreatePreferences()");
212 //super.onCreate(savedInstanceState);
213 addPreferencesFromResource(R.xml.preferences_ricoh_gr2);
215 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
216 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
218 public boolean onPreferenceChange(Preference preference, Object newValue) {
219 preference.setSummary(newValue + " ");
223 connectionMethod.setSummary(connectionMethod.getValue() + " ");
225 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
226 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
239 public void onResume()
242 Log.v(TAG, "onResume() Start");
245 synchronizedProperty();
252 Log.v(TAG, "onResume() End");
260 public void onPause()
263 Log.v(TAG, "onPause() Start");
266 // Preference変更のリスナを解除
267 preferences.unregisterOnSharedPreferenceChangeListener(this);
274 Log.v(TAG, "onPause() End");
278 * ListPreference の表示データを設定
280 * @param pref_key Preference(表示)のキー
281 * @param key Preference(データ)のキー
282 * @param defaultValue Preferenceのデフォルト値
284 private void setListPreference(String pref_key, String key, String defaultValue)
289 pref = (ListPreference) findPreference(pref_key);
290 String value = preferences.getString(key, defaultValue);
293 pref.setValue(value);
294 pref.setSummary(value);
304 * BooleanPreference の表示データを設定
306 * @param pref_key Preference(表示)のキー
307 * @param key Preference(データ)のキー
308 * @param defaultValue Preferenceのデフォルト値
310 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
314 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
316 boolean value = preferences.getBoolean(key, defaultValue);
317 pref.setChecked(value);
330 private void synchronizedProperty()
332 final FragmentActivity activity = getActivity();
333 final boolean defaultValue = true;
334 if (activity != null)
336 activity.runOnUiThread(new Runnable() {
341 // Preferenceの画面に反映させる
342 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
343 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
344 setBooleanPreference(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, IPreferencePropertyAccessor.USE_PLAYBACK_MENU, defaultValue);
345 setBooleanPreference(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, defaultValue);
346 setBooleanPreference(IPreferencePropertyAccessor.GR2_LCD_SLEEP, IPreferencePropertyAccessor.GR2_LCD_SLEEP, defaultValue);
347 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, defaultValue);
348 setBooleanPreference(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, defaultValue);
349 setBooleanPreference(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);