1 package net.osdn.gokigen.pkremote.preference.ricohgr2;
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.os.Bundle;
8 import android.provider.Settings;
9 import android.util.Log;
11 import net.osdn.gokigen.pkremote.R;
12 import net.osdn.gokigen.pkremote.camera.vendor.ricoh.operation.CameraPowerOffRicohGr2;
13 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
14 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
15 import net.osdn.gokigen.pkremote.scene.IChangeScene;
19 import androidx.annotation.NonNull;
20 import androidx.appcompat.app.AppCompatActivity;
21 import androidx.fragment.app.FragmentActivity;
22 import androidx.preference.CheckBoxPreference;
23 import androidx.preference.ListPreference;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceFragmentCompat;
26 import androidx.preference.PreferenceManager;
28 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
29 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
30 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
32 public class RicohGr2PreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
34 private final String TAG = toString();
35 private AppCompatActivity context = null;
36 private SharedPreferences preferences = null;
37 private CameraPowerOffRicohGr2 powerOffController = null;
38 private LogCatViewer logCatViewer = null;
44 public static RicohGr2PreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
46 RicohGr2PreferenceFragment instance = new RicohGr2PreferenceFragment();
47 instance.prepare(context, changeScene);
49 // パラメータはBundleにまとめておく
50 Bundle arguments = new Bundle();
51 //arguments.putString("title", title);
52 //arguments.putString("message", message);
53 instance.setArguments(arguments);
62 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
66 powerOffController = new CameraPowerOffRicohGr2(context, changeScene);
67 powerOffController.prepare();
69 logCatViewer = new LogCatViewer(changeScene);
70 logCatViewer.prepare();
72 this.context = context;
85 public void onAttach(Context activity)
87 super.onAttach(activity);
88 Log.v(TAG, "onAttach()");
92 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
95 initializePreferences();
97 preferences.registerOnSharedPreferenceChangeListener(this);
109 private void initializePreferences()
113 Map<String, ?> items = preferences.getAll();
114 SharedPreferences.Editor editor = preferences.edit();
116 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
118 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
120 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
122 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
124 if (!items.containsKey(IPreferencePropertyAccessor.USE_PLAYBACK_MENU))
126 editor.putBoolean(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, true);
128 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
130 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
132 if (!items.containsKey(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW))
134 editor.putBoolean(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, true);
136 if (!items.containsKey(IPreferencePropertyAccessor.GR2_LCD_SLEEP))
138 editor.putBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false);
140 if (!items.containsKey(IPreferencePropertyAccessor.SHARE_AFTER_SAVE))
142 editor.putBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false);
144 if (!items.containsKey(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND))
146 editor.putBoolean(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, true);
148 if (!items.containsKey(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF))
150 editor.putBoolean(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
152 if (!items.containsKey(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE))
154 editor.putString(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE, IPreferencePropertyAccessor.SMALL_PICTURE_SIZE_DEFAULT_VALUE);
156 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
158 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
160 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_MAX_COUNT))
162 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_MAX_COUNT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_MAX_COUNT_DEFAULT_VALUE);
164 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_HOST_IP))
166 editor.putString(IPreferencePropertyAccessor.PIXPRO_HOST_IP, IPreferencePropertyAccessor.PIXPRO_HOST_IP_DEFAULT_VALUE);
168 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT))
170 editor.putString(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT, IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT_DEFAULT_VALUE);
172 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT))
174 editor.putString(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
176 if (!items.containsKey(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE))
178 editor.putString(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE, IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE_DEFAULT_VALUE);
180 if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
181 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
183 if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
184 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
199 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
201 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
207 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
208 value = preferences.getBoolean(key, true);
209 Log.v(TAG, " " + key + " , " + value);
212 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
213 value = preferences.getBoolean(key, true);
214 Log.v(TAG, " " + key + " , " + value);
217 case IPreferencePropertyAccessor.USE_PLAYBACK_MENU:
218 value = preferences.getBoolean(key, true);
219 Log.v(TAG, " " + key + " , " + value);
222 case IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW:
223 value = preferences.getBoolean(key, true);
224 Log.v(TAG, " " + key + " , " + value);
227 case IPreferencePropertyAccessor.GR2_LCD_SLEEP:
228 value = preferences.getBoolean(key, false);
229 Log.v(TAG, " " + key + " , " + value);
232 case IPreferencePropertyAccessor.SHARE_AFTER_SAVE:
233 value = preferences.getBoolean(key, false);
234 Log.v(TAG, " " + key + " , " + value);
237 case IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND:
238 value = preferences.getBoolean(key, true);
239 Log.v(TAG, " " + key + " , " + value);
242 case IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF:
243 value = preferences.getBoolean(key, false);
244 Log.v(TAG, " " + key + " , " + value);
248 String strValue = preferences.getString(key, "");
249 setListPreference(key, key, strValue);
260 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
262 Log.v(TAG, "onCreatePreferences()");
265 //super.onCreate(savedInstanceState);
266 addPreferencesFromResource(R.xml.preferences_ricoh_gr2);
268 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
269 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
271 public boolean onPreferenceChange(Preference preference, Object newValue) {
272 preference.setSummary(newValue + " ");
276 connectionMethod.setSummary(connectionMethod.getValue() + " ");
278 ListPreference smallPictureSize = (ListPreference) findPreference(IPreferencePropertyAccessor.SMALL_PICTURE_SIZE);
279 smallPictureSize.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
281 public boolean onPreferenceChange(Preference preference, Object newValue) {
282 preference.setSummary(newValue + " ");
286 smallPictureSize.setSummary(smallPictureSize.getValue() + " ");
288 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
289 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
290 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
303 public void onResume()
306 Log.v(TAG, "onResume() Start");
309 synchronizedProperty();
316 Log.v(TAG, "onResume() End");
324 public void onPause()
327 Log.v(TAG, "onPause() Start");
330 // Preference変更のリスナを解除
331 preferences.unregisterOnSharedPreferenceChangeListener(this);
338 Log.v(TAG, "onPause() End");
342 * ListPreference の表示データを設定
344 * @param pref_key Preference(表示)のキー
345 * @param key Preference(データ)のキー
346 * @param defaultValue Preferenceのデフォルト値
348 private void setListPreference(String pref_key, String key, String defaultValue)
353 pref = (ListPreference) findPreference(pref_key);
354 String value = preferences.getString(key, defaultValue);
357 pref.setValue(value);
358 pref.setSummary(value);
368 * BooleanPreference の表示データを設定
370 * @param pref_key Preference(表示)のキー
371 * @param key Preference(データ)のキー
372 * @param defaultValue Preferenceのデフォルト値
374 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
378 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
380 boolean value = preferences.getBoolean(key, defaultValue);
381 pref.setChecked(value);
394 private void synchronizedProperty()
396 final FragmentActivity activity = getActivity();
397 final boolean defaultValue = true;
398 if (activity != null)
400 activity.runOnUiThread(new Runnable() {
405 // Preferenceの画面に反映させる
406 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
407 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
408 setBooleanPreference(IPreferencePropertyAccessor.USE_PLAYBACK_MENU, IPreferencePropertyAccessor.USE_PLAYBACK_MENU, defaultValue);
409 setBooleanPreference(IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, IPreferencePropertyAccessor.GR2_DISPLAY_CAMERA_VIEW, defaultValue);
410 setBooleanPreference(IPreferencePropertyAccessor.GR2_LCD_SLEEP, IPreferencePropertyAccessor.GR2_LCD_SLEEP, defaultValue);
411 setBooleanPreference(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, IPreferencePropertyAccessor.SHARE_AFTER_SAVE, defaultValue);
412 setBooleanPreference(IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, IPreferencePropertyAccessor.USE_GR2_SPECIAL_COMMAND, defaultValue);
413 setBooleanPreference(IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, IPreferencePropertyAccessor.PENTAX_CAPTURE_AFTER_AF, false);
425 public boolean onPreferenceClick(Preference preference)
429 String preferenceKey = preference.getKey();
430 if (preferenceKey.contains(WIFI_SETTINGS))
433 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
436 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));