1 package net.osdn.gokigen.gr2control.preference;
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.os.Bundle;
7 import android.provider.Settings;
8 import android.util.Log;
10 import net.osdn.gokigen.gr2control.R;
11 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
12 import net.osdn.gokigen.gr2control.camera.ricohgr2.operation.RicohGr2CameraPowerOff;
13 import net.osdn.gokigen.gr2control.logcat.LogCatViewer;
14 import net.osdn.gokigen.gr2control.scene.IChangeScene;
18 import androidx.annotation.NonNull;
19 import androidx.appcompat.app.AppCompatActivity;
20 import androidx.fragment.app.FragmentActivity;
21 import androidx.preference.CheckBoxPreference;
22 import androidx.preference.ListPreference;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceFragmentCompat;
25 import androidx.preference.PreferenceManager;
27 public class Gr2ControlPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
29 private final String TAG = toString();
30 //private RicohGr2CameraPowerOff powerOffController = null;
31 private IChangeScene changeScene = null;
32 private AppCompatActivity context = null;
33 private SharedPreferences preferences = null;
34 private LogCatViewer logCatViewer = null;
40 public static Gr2ControlPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
42 Gr2ControlPreferenceFragment instance = new Gr2ControlPreferenceFragment();
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 RicohGr2CameraPowerOff(context, changeScene);
63 //powerOffController.prepare();
65 logCatViewer = new LogCatViewer(changeScene);
66 logCatViewer.prepare();
67 this.changeScene = changeScene;
68 this.context = context;
81 public void onAttach(@NonNull Context activity)
83 super.onAttach(activity);
84 Log.v(TAG, " onAttach()");
88 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
91 initializePreferences();
93 preferences.registerOnSharedPreferenceChangeListener(this);
105 private void initializePreferences()
109 Map<String, ?> items = preferences.getAll();
110 SharedPreferences.Editor editor = preferences.edit();
112 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
114 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
116 if (!items.containsKey(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY))
118 editor.putString(IPreferencePropertyAccessor.LIVE_VIEW_QUALITY, IPreferencePropertyAccessor.LIVE_VIEW_QUALITY_DEFAULT_VALUE);
120 if (!items.containsKey(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL))
122 editor.putString(IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL, IPreferencePropertyAccessor.SOUND_VOLUME_LEVEL_DEFAULT_VALUE);
124 if (!items.containsKey(IPreferencePropertyAccessor.RAW))
126 editor.putBoolean(IPreferencePropertyAccessor.RAW, true);
128 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
130 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
132 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
134 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, false);
136 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
138 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
140 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE)) {
141 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
143 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU)) {
144 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
146 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW)) {
147 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
149 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP)) {
150 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
152 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND)) {
153 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
155 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF)) {
156 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
158 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW)) {
159 editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_DISPLAY_CAMERA_VIEW, false);
161 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY)) {
162 editor.putString(IPreferencePropertyAccessor.FUJI_X_FOCUS_XY, IPreferencePropertyAccessor.FUJI_X_FOCUS_XY_DEFAULT_VALUE);
164 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT)) {
165 editor.putString(IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT, IPreferencePropertyAccessor.FUJI_X_LIVEVIEW_WAIT_DEFAULT_VALUE);
167 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT)) {
168 editor.putString(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT, IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT_DEFAULT_VALUE);
170 if (!items.containsKey(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE)) {
171 editor.putBoolean(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE, false);
186 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
188 Log.v(TAG, " onSharedPreferenceChanged() : " + key);
194 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
195 value = preferences.getBoolean(key, true);
196 Log.v(TAG, " " + key + " , " + value);
199 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
200 value = preferences.getBoolean(key, false);
201 Log.v(TAG, " BOTH LV : " + key + " , " + value);
204 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
205 value = preferences.getBoolean(key, true);
206 Log.v(TAG, " PLAYBACK MENU : " + key + " , " + value);
209 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
210 value = preferences.getBoolean(key, false);
211 Log.v(TAG, " SHARE : " + key + " , " + value);
215 String strValue = preferences.getString(key, "");
216 setListPreference(key, key, strValue);
227 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
229 Log.v(TAG, " onCreatePreferences()");
232 //super.onCreate(savedInstanceState);
233 addPreferencesFromResource(R.xml.preferences_summary);
235 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
236 if (connectionMethod != null)
238 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
240 public boolean onPreferenceChange(Preference preference, Object newValue) {
241 preference.setSummary(newValue + " ");
245 connectionMethod.setSummary(connectionMethod.getValue() + " ");
249 Preference exitApplication = findPreference("exit_application");
250 if (exitApplication != null)
252 exitApplication.setOnPreferenceClickListener(powerOffController);
256 Preference opcPreference = findPreference("opc_settings");
257 if (opcPreference != null)
259 opcPreference.setOnPreferenceClickListener(this);
261 Preference gr2Preference = findPreference("ricoh_settings");
262 if (gr2Preference != null)
264 gr2Preference.setOnPreferenceClickListener(this);
266 Preference fujiXPreference = findPreference("fuji_x_settings");
267 if (fujiXPreference != null)
269 fujiXPreference.setOnPreferenceClickListener(this);
272 Preference debug = findPreference("debug_info");
275 debug.setOnPreferenceClickListener(logCatViewer);
278 Preference wifi = findPreference("wifi_settings");
281 wifi.setOnPreferenceClickListener(this);
295 public void onResume()
298 Log.v(TAG, " onResume() Start");
301 synchronizedProperty();
308 Log.v(TAG, " onResume() End");
316 public void onPause()
319 Log.v(TAG, " onPause() Start");
322 // Preference変更のリスナを解除
323 preferences.unregisterOnSharedPreferenceChangeListener(this);
330 Log.v(TAG, " onPause() End");
334 * ListPreference の表示データを設定
336 * @param pref_key Preference(表示)のキー
337 * @param key Preference(データ)のキー
338 * @param defaultValue Preferenceのデフォルト値
340 private void setListPreference(String pref_key, String key, String defaultValue)
345 pref = findPreference(pref_key);
346 String value = preferences.getString(key, defaultValue);
349 pref.setValue(value);
350 pref.setSummary(value);
360 * BooleanPreference の表示データを設定
362 * @param pref_key Preference(表示)のキー
363 * @param key Preference(データ)のキー
364 * @param defaultValue Preferenceのデフォルト値
366 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
370 CheckBoxPreference pref = findPreference(pref_key);
373 boolean value = preferences.getBoolean(key, defaultValue);
374 pref.setChecked(value);
387 private void synchronizedProperty()
389 final FragmentActivity activity = getActivity();
390 if (activity != null)
392 activity.runOnUiThread(new Runnable() {
397 // Preferenceの画面に反映させる
398 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
399 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, false);
400 setBooleanPreference(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
401 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
414 public boolean onPreferenceClick(Preference preference)
418 String preferenceKey = preference.getKey();
419 Log.v(TAG, " onPreferenceClick() : " + preferenceKey);
420 if (preferenceKey.contains("wifi_settings"))
423 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
426 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
429 else if (preferenceKey.contains("ricoh_settings"))
431 changeScene.changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod.RICOH_GR2);
433 else if (preferenceKey.contains("fuji_x_settings"))
435 changeScene.changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod.FUJI_X);
437 else if (preferenceKey.contains("opc_settings"))
439 changeScene.changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod.OPC);