1 package net.osdn.gokigen.pkremote.preference.nikon;
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;
12 import androidx.annotation.NonNull;
13 import androidx.appcompat.app.AppCompatActivity;
14 import androidx.fragment.app.FragmentActivity;
15 import androidx.preference.CheckBoxPreference;
16 import androidx.preference.ListPreference;
17 import androidx.preference.Preference;
18 import androidx.preference.PreferenceFragmentCompat;
19 import androidx.preference.PreferenceManager;
21 import net.osdn.gokigen.pkremote.R;
22 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.operation.PtpIpCameraPowerOff;
23 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
24 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
25 import net.osdn.gokigen.pkremote.scene.IChangeScene;
27 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
28 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
29 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
35 public class NikonPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
37 private final String TAG = toString();
38 private AppCompatActivity context = null;
39 private SharedPreferences preferences = null;
40 private PtpIpCameraPowerOff powerOffController = null;
41 private LogCatViewer logCatViewer = null;
47 public static NikonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
49 NikonPreferenceFragment instance = new NikonPreferenceFragment();
50 instance.prepare(context, changeScene);
52 // パラメータはBundleにまとめておく
53 Bundle arguments = new Bundle();
54 //arguments.putString("title", title);
55 //arguments.putString("message", message);
56 instance.setArguments(arguments);
65 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
69 powerOffController = new PtpIpCameraPowerOff(context, changeScene);
70 powerOffController.prepare();
72 logCatViewer = new LogCatViewer(changeScene);
73 logCatViewer.prepare();
75 this.context = context;
88 public void onAttach(@NonNull Context activity)
90 super.onAttach(activity);
91 Log.v(TAG, "onAttach()");
96 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
99 initializePreferences();
101 preferences.registerOnSharedPreferenceChangeListener(this);
113 private void initializePreferences()
117 Map<String, ?> items = preferences.getAll();
118 SharedPreferences.Editor editor = preferences.edit();
120 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
121 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
123 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
124 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
126 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
127 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
129 if (!items.containsKey(IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS)) {
130 editor.putString(IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS, IPreferencePropertyAccessor.NIKON_CAMERA_IP_ADDRESS_DEFAULT_VALUE);
132 if (!items.containsKey(IPreferencePropertyAccessor.NIKON_RECEIVE_WAIT)) {
133 editor.putString(IPreferencePropertyAccessor.NIKON_RECEIVE_WAIT, IPreferencePropertyAccessor.NIKON_RECEIVE_WAIT_DEFAULT_VALUE);
135 if (!items.containsKey(IPreferencePropertyAccessor.NIKON_USE_SCREENNAIL_AS_SMALL)) {
136 editor.putBoolean(IPreferencePropertyAccessor.NIKON_USE_SCREENNAIL_AS_SMALL, false);
138 if (!items.containsKey(IPreferencePropertyAccessor.BLE_WIFI_ON)) {
139 editor.putBoolean(IPreferencePropertyAccessor.BLE_WIFI_ON, false);
141 if (!items.containsKey(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA))
143 editor.putBoolean(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
145 if (!items.containsKey(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE))
147 editor.putBoolean(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, false);
149 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
151 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
153 if (!items.containsKey(IPreferencePropertyAccessor.USE_OSC_THETA_V21))
155 editor.putBoolean(IPreferencePropertyAccessor.USE_OSC_THETA_V21, false);
157 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_HOST_IP))
159 editor.putString(IPreferencePropertyAccessor.PIXPRO_HOST_IP, IPreferencePropertyAccessor.PIXPRO_HOST_IP_DEFAULT_VALUE);
161 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT))
163 editor.putString(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT, IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT_DEFAULT_VALUE);
165 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT))
167 editor.putString(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
169 if (!items.containsKey(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE))
171 editor.putString(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE, IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE_DEFAULT_VALUE);
173 if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
174 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
176 if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
177 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
192 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
194 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
200 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
201 value = preferences.getBoolean(key, true);
202 Log.v(TAG, " " + key + " , " + value);
205 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
206 value = preferences.getBoolean(key, true);
207 Log.v(TAG, " " + key + " , " + value);
210 case IPreferencePropertyAccessor.NIKON_USE_SCREENNAIL_AS_SMALL:
211 value = preferences.getBoolean(key, false);
212 Log.v(TAG, " " + key + " , " + value);
215 case IPreferencePropertyAccessor.BLE_WIFI_ON:
216 value = preferences.getBoolean(key, false);
217 Log.v(TAG, " " + key + " , " + value);
221 String strValue = preferences.getString(key, "");
222 setListPreference(key, key, strValue);
233 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
235 Log.v(TAG, "onCreatePreferences()");
238 //super.onCreate(savedInstanceState);
239 addPreferencesFromResource(R.xml.preferences_nikon);
241 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
242 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
244 public boolean onPreferenceChange(Preference preference, Object newValue) {
245 preference.setSummary(newValue + " ");
249 connectionMethod.setSummary(connectionMethod.getValue() + " ");
251 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
252 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
253 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
266 public void onResume()
269 Log.v(TAG, "onResume() Start");
273 synchronizedProperty();
280 Log.v(TAG, "onResume() End");
289 public void onPause()
292 Log.v(TAG, "onPause() Start");
296 // Preference変更のリスナを解除
297 preferences.unregisterOnSharedPreferenceChangeListener(this);
304 Log.v(TAG, "onPause() End");
308 * ListPreference の表示データを設定
310 * @param pref_key Preference(表示)のキー
311 * @param key Preference(データ)のキー
312 * @param defaultValue Preferenceのデフォルト値
314 private void setListPreference(String pref_key, String key, String defaultValue)
319 pref = findPreference(pref_key);
320 String value = preferences.getString(key, defaultValue);
323 pref.setValue(value);
324 pref.setSummary(value);
334 * BooleanPreference の表示データを設定
336 * @param pref_key Preference(表示)のキー
337 * @param key Preference(データ)のキー
338 * @param defaultValue Preferenceのデフォルト値
340 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
344 CheckBoxPreference pref = findPreference(pref_key);
346 boolean value = preferences.getBoolean(key, defaultValue);
347 pref.setChecked(value);
360 private void synchronizedProperty()
362 final FragmentActivity activity = getActivity();
363 if (activity != null)
365 activity.runOnUiThread(new Runnable() {
370 // Preferenceの画面に反映させる
371 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
372 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
373 setBooleanPreference(IPreferencePropertyAccessor.NIKON_USE_SCREENNAIL_AS_SMALL, IPreferencePropertyAccessor.NIKON_USE_SCREENNAIL_AS_SMALL, false);
374 setBooleanPreference(IPreferencePropertyAccessor.BLE_WIFI_ON, IPreferencePropertyAccessor.BLE_WIFI_ON, false);
386 public boolean onPreferenceClick(Preference preference)
390 String preferenceKey = preference.getKey();
391 if (preferenceKey.contains(WIFI_SETTINGS))
394 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
397 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));